diff --git a/math/benches/criterion_field.rs b/math/benches/criterion_field.rs index f48eb5abe..e7733c1ad 100644 --- a/math/benches/criterion_field.rs +++ b/math/benches/criterion_field.rs @@ -17,6 +17,8 @@ use fields::{ criterion_group!( name = field_benches; config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); - targets = babybear_ops_benchmarks,babybear_extension_ops_benchmarks,babybear_p3_ops_benchmarks,babybear_extension_ops_benchmarks_p3 + targets = babybear_ops_benchmarks,babybear_extension_ops_benchmarks,babybear_p3_ops_benchmarks,babybear_extension_ops_benchmarks_p3, + mersenne31_extension_ops_benchmarks,mersenne31_ops_benchmarks,mersenne31_mont_ops_benchmarks,starkfield_ops_benchmarks,u64_goldilocks_ops_benchmarks + ,u64_goldilocks_montgomery_ops_benchmarks ); criterion_main!(field_benches); diff --git a/math/benches/fields/babybear.rs b/math/benches/fields/babybear.rs index f8aaf3cc2..a7a7afd02 100644 --- a/math/benches/fields/babybear.rs +++ b/math/benches/fields/babybear.rs @@ -1,13 +1,9 @@ use criterion::Criterion; use std::hint::black_box; -use lambdaworks_math::field::fields::fft_friendly::quadratic_babybear::QuadraticBabybearField; use lambdaworks_math::field::fields::fft_friendly::quartic_babybear::Degree4BabyBearExtensionField; use lambdaworks_math::field::{ - element::FieldElement, - errors::FieldError, - fields::fft_friendly::babybear::Babybear31PrimeField, - traits::{IsFFTField, IsField, IsSubFieldOf}, + element::FieldElement, fields::fft_friendly::babybear::Babybear31PrimeField, }; use p3_baby_bear::BabyBear; @@ -15,18 +11,13 @@ use p3_field::extension::BinomialExtensionField; use p3_field::{Field, FieldAlgebra}; use rand::random; - use rand::Rng; pub type F = FieldElement; -pub type Fp2E = FieldElement; pub type Fp4E = FieldElement; type EF4 = BinomialExtensionField; -// Create a vector of random field elements for the elements using LambdaWorks - pub fn rand_field_elements(num: usize) -> Vec<(F, F)> { - //let mut result = Vec::with_capacity(num); let mut result = Vec::with_capacity(num); for _ in 0..result.capacity() { result.push((F::from(random::()), F::from(random::()))); @@ -55,12 +46,6 @@ pub fn rand_babybear_fp4_elements(num: usize) -> Vec<(Fp4E, Fp4E)> { result } -// Create a vector of random field elements for the elements using Plonky3 -// use u64? - -//to do create u32 for montgomery in lambdaworks? -// use a more idiomatic way to do the benches - fn rand_babybear_elements_p3(num: usize) -> Vec<(BabyBear, BabyBear)> { let mut rng = rand::thread_rng(); (0..num) @@ -75,17 +60,15 @@ fn rand_babybear_fp4_elements_p3(num: usize) -> Vec<(EF4, EF4)> { .collect() } -// Operations for BabyBear extension field 4 using Lambdaworks -pub fn babybear_extension_ops_benchmarks(c: &mut Criterion) { - let input: Vec> = [1, 10, 100, 1000, 10000, 100000, 1000000] +pub fn babybear_ops_benchmarks(c: &mut Criterion) { + let input: Vec> = [1, 10, 100, 1000, 10000, 100000, 1000000] .into_iter() - .map(rand_babybear_fp4_elements) + .map(rand_field_elements) .collect::>(); - - let mut group = c.benchmark_group("BabyBear Fp4 operations"); + let mut group = c.benchmark_group("BabyBear operations using Lambdaworks"); for i in input.clone().into_iter() { - group.bench_with_input(format!("Add of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Addition {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(x) + black_box(y)); @@ -95,7 +78,7 @@ pub fn babybear_extension_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("Mul of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Multiplication {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(x) * black_box(y)); @@ -105,7 +88,7 @@ pub fn babybear_extension_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("Square of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Square {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, _) in i { black_box(black_box(x).square()); @@ -115,35 +98,35 @@ pub fn babybear_extension_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("Inv of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Inverse {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { - for (x, y) in i { - black_box(black_box(x) / black_box(y)); + for (x, _) in i { + black_box(black_box(x).inv().unwrap()); } }); }); } for i in input.clone().into_iter() { - group.bench_with_input(format!("Div of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Division {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { - for (x, _) in i { - black_box(black_box(x).inv().unwrap()); + for (x, y) in i { + black_box(black_box(x) / black_box(y)); } }); }); } } - -pub fn babybear_ops_benchmarks(c: &mut Criterion) { - let input: Vec> = [1, 10, 100, 1000, 10000, 100000, 1000000] +pub fn babybear_extension_ops_benchmarks(c: &mut Criterion) { + let input: Vec> = [1, 10, 100, 1000, 10000, 100000, 1000000] .into_iter() - .map(rand_field_elements) + .map(rand_babybear_fp4_elements) .collect::>(); - let mut group = c.benchmark_group("BabyBear operations using Lambdaworks"); + + let mut group = c.benchmark_group("BabyBear Fp4 operations"); for i in input.clone().into_iter() { - group.bench_with_input(format!("add {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Addition of Fp4 {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(x) + black_box(y)); @@ -153,17 +136,21 @@ pub fn babybear_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("mul {:?}", &i.len()), &i, |bench, i| { - bench.iter(|| { - for (x, y) in i { - black_box(black_box(x) * black_box(y)); - } - }); - }); + group.bench_with_input( + format!("Multiplication of Fp4 {:?}", &i.len()), + &i, + |bench, i| { + bench.iter(|| { + for (x, y) in i { + black_box(black_box(x) * black_box(y)); + } + }); + }, + ); } for i in input.clone().into_iter() { - group.bench_with_input(format!("square {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Square of Fp4 {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, _) in i { black_box(black_box(x).square()); @@ -173,17 +160,17 @@ pub fn babybear_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("sub {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Inverse of Fp4 {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { - black_box(black_box(x) - black_box(y)); + black_box(black_box(x) / black_box(y)); } }); }); } for i in input.clone().into_iter() { - group.bench_with_input(format!("inv {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Division of Fp4 {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, _) in i { black_box(black_box(x).inv().unwrap()); @@ -191,18 +178,8 @@ pub fn babybear_ops_benchmarks(c: &mut Criterion) { }); }); } - - for i in input.clone().into_iter() { - group.bench_with_input(format!("div {:?}", &i.len()), &i, |bench, i| { - bench.iter(|| { - for (x, y) in i { - black_box(black_box(x) / black_box(y)); - } - }); - }); - } } -// Operations benchmarks for BabyBear field using Plonky3 + pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { let input: Vec> = [1, 10, 100, 1000, 10000, 100000, 1000000] .into_iter() @@ -212,7 +189,7 @@ pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { let mut group = c.benchmark_group("BabyBear operations using Plonky3"); for i in input.clone().into_iter() { - group.bench_with_input(format!("add {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Addition {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(*x) + black_box(*y)); @@ -222,16 +199,7 @@ pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("sub {:?}", &i.len()), &i, |bench, i| { - bench.iter(|| { - for (x, y) in i { - black_box(black_box(*x) - black_box(*y)); - } - }); - }); - } - for i in input.clone().into_iter() { - group.bench_with_input(format!("mul {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Multiplication {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(*x) * black_box(*y)); @@ -241,7 +209,7 @@ pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("square {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Square {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, _) in i { black_box(black_box(x).square()); @@ -250,7 +218,7 @@ pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { }); } for i in input.clone().into_iter() { - group.bench_with_input(format!("inv {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Inverse {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, _) in i { black_box(black_box(x).inverse()); @@ -260,7 +228,7 @@ pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("div {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Division {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(*x) / black_box(*y)); @@ -270,8 +238,6 @@ pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { } } -// Operations benchmarks for BabyBear extension 4 field using Plonky3 - pub fn babybear_extension_ops_benchmarks_p3(c: &mut Criterion) { let input_sizes = [1, 10, 100, 1000, 10000, 100000, 1000000]; let input: Vec> = input_sizes @@ -282,7 +248,7 @@ pub fn babybear_extension_ops_benchmarks_p3(c: &mut Criterion) { let mut group = c.benchmark_group("BabyBear Fp4 operations using Plonky3"); for i in input.clone().into_iter() { - group.bench_with_input(format!("Add of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Addition of Fp4 {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(*x) + black_box(*y)); @@ -291,13 +257,17 @@ pub fn babybear_extension_ops_benchmarks_p3(c: &mut Criterion) { }); } for i in input.clone().into_iter() { - group.bench_with_input(format!("Mul of Fp4 {:?}", &i.len()), &i, |bench, i| { - bench.iter(|| { - for (x, y) in i { - black_box(black_box(*x) * black_box(*y)); - } - }); - }); + group.bench_with_input( + format!("Multiplication of Fp4 {:?}", &i.len()), + &i, + |bench, i| { + bench.iter(|| { + for (x, y) in i { + black_box(black_box(*x) * black_box(*y)); + } + }); + }, + ); } for i in input.clone().into_iter() { group.bench_with_input(format!("Square of Fp4 {:?}", &i.len()), &i, |bench, i| { @@ -310,7 +280,7 @@ pub fn babybear_extension_ops_benchmarks_p3(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("Inv of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Inverse of Fp4 {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, _) in i { black_box(black_box(x).inverse()); @@ -320,7 +290,7 @@ pub fn babybear_extension_ops_benchmarks_p3(c: &mut Criterion) { } for i in input.clone().into_iter() { - group.bench_with_input(format!("Div of Fp4 {:?}", &i.len()), &i, |bench, i| { + group.bench_with_input(format!("Division of Fp4 {:?}", &i.len()), &i, |bench, i| { bench.iter(|| { for (x, y) in i { black_box(black_box(*x) / black_box(*y));