Skip to content

Commit 7a427aa

Browse files
nicole-grausNicoleColoCarlettidiegokingston
authored
Optimize Mersenne31 Field (lambdaclass#921)
* optimize add * save changes. Add, sub and mul checked * fix tests * add new inv * add mult by powers of two * replace inverse * test new inv * modify old algorithm for inv * fix tests extension * add mul for degree 4 extension * add fp4 isField and isSubField operations and benchmarks * new version for fp4 mul based on the paper * add mul of a fp2e by non-residue * change inv using mul_fp2_by_non_resiude * save work * wip fp2 test * add fp2 tests * add 2 * a^2 - 1 function * use karatsuba in fp4 mul version 1 * clean up * fix Fp as subfield of Fp2. Tests Fp plus Fp4 is now correct * fix inv * fix comments * fix comments * fixes * fix clippy * fix cargo check no-std * fix clippy * change zero function of isField to rust default * fix two_square_minus_one function and optimize inv function * fix clippy --------- Co-authored-by: Nicole <nicole@Nicoles-MacBook-Air.local> Co-authored-by: Joaquin Carletti <joaquin.carletti@lambdaclass.com> Co-authored-by: diegokingston <dkingston@fi.uba.ar> Co-authored-by: Diego K <43053772+diegokingston@users.noreply.github.com> Co-authored-by: Joaquin Carletti <56092489+ColoCarletti@users.noreply.github.com>
1 parent 9617e52 commit 7a427aa

File tree

9 files changed

+863
-428
lines changed

9 files changed

+863
-428
lines changed

math/benches/criterion_field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
22
use pprof::criterion::{Output, PProfProfiler};
33

44
mod fields;
5-
use fields::mersenne31::mersenne31_ops_benchmarks;
5+
use fields::mersenne31::{mersenne31_extension_ops_benchmarks, mersenne31_ops_benchmarks};
66
use fields::mersenne31_montgomery::mersenne31_mont_ops_benchmarks;
77
use fields::{
88
stark252::starkfield_ops_benchmarks, u64_goldilocks::u64_goldilocks_ops_benchmarks,
@@ -12,6 +12,6 @@ use fields::{
1212
criterion_group!(
1313
name = field_benches;
1414
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
15-
targets = starkfield_ops_benchmarks, mersenne31_ops_benchmarks, mersenne31_mont_ops_benchmarks, u64_goldilocks_ops_benchmarks, u64_goldilocks_montgomery_ops_benchmarks
15+
targets = mersenne31_ops_benchmarks, mersenne31_extension_ops_benchmarks, mersenne31_mont_ops_benchmarks, starkfield_ops_benchmarks, u64_goldilocks_ops_benchmarks, u64_goldilocks_montgomery_ops_benchmarks
1616
);
1717
criterion_main!(field_benches);

math/benches/fields/mersenne31.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
use std::hint::black_box;
22

33
use criterion::Criterion;
4-
use lambdaworks_math::field::{element::FieldElement, fields::mersenne31::field::Mersenne31Field};
4+
use lambdaworks_math::field::{
5+
element::FieldElement,
6+
fields::mersenne31::{
7+
extensions::{Degree2ExtensionField, Degree4ExtensionField},
8+
field::Mersenne31Field,
9+
},
10+
};
511
use rand::random;
612

713
pub type F = FieldElement<Mersenne31Field>;
14+
pub type Fp2E = FieldElement<Degree2ExtensionField>;
15+
pub type Fp4E = FieldElement<Degree4ExtensionField>;
816

917
#[inline(never)]
1018
#[no_mangle]
@@ -17,6 +25,60 @@ pub fn rand_field_elements(num: usize) -> Vec<(F, F)> {
1725
result
1826
}
1927

28+
//TODO: Check if this is the correct way to bench.
29+
pub fn rand_fp4e(num: usize) -> Vec<(Fp4E, Fp4E)> {
30+
let mut result = Vec::with_capacity(num);
31+
for _ in 0..result.capacity() {
32+
result.push((
33+
Fp4E::new([
34+
Fp2E::new([F::new(random()), F::new(random())]),
35+
Fp2E::new([F::new(random()), F::new(random())]),
36+
]),
37+
Fp4E::new([
38+
Fp2E::new([F::new(random()), F::new(random())]),
39+
Fp2E::new([F::new(random()), F::new(random())]),
40+
]),
41+
));
42+
}
43+
result
44+
}
45+
46+
pub fn mersenne31_extension_ops_benchmarks(c: &mut Criterion) {
47+
let input: Vec<Vec<(Fp4E, Fp4E)>> = [1000000].into_iter().map(rand_fp4e).collect::<Vec<_>>();
48+
49+
let mut group = c.benchmark_group("Mersenne31 Fp4 operations");
50+
51+
for i in input.clone().into_iter() {
52+
group.bench_with_input(format!("Mul of Fp4 {:?}", &i.len()), &i, |bench, i| {
53+
bench.iter(|| {
54+
for (x, y) in i {
55+
black_box(black_box(x) * black_box(y));
56+
}
57+
});
58+
});
59+
}
60+
61+
for i in input.clone().into_iter() {
62+
group.bench_with_input(format!("Square of Fp4 {:?}", &i.len()), &i, |bench, i| {
63+
bench.iter(|| {
64+
for (x, _) in i {
65+
black_box(black_box(x).square());
66+
}
67+
});
68+
});
69+
}
70+
71+
for i in input.clone().into_iter() {
72+
group.bench_with_input(format!("Inv of Fp4 {:?}", &i.len()), &i, |bench, i| {
73+
bench.iter(|| {
74+
for (x, _) in i {
75+
black_box(black_box(x).inv().unwrap());
76+
}
77+
});
78+
});
79+
}
80+
}
81+
2082
pub fn mersenne31_ops_benchmarks(c: &mut Criterion) {
2183
let input: Vec<Vec<(F, F)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
2284
.into_iter()

0 commit comments

Comments
 (0)