Skip to content

Commit

Permalink
restore commented benches
Browse files Browse the repository at this point in the history
  • Loading branch information
jotabulacios committed Nov 29, 2024
1 parent a9a22fc commit 8ad2490
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 86 deletions.
4 changes: 3 additions & 1 deletion math/benches/criterion_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
140 changes: 55 additions & 85 deletions math/benches/fields/babybear.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
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;
use p3_field::extension::BinomialExtensionField;
use p3_field::{Field, FieldAlgebra};

use rand::random;

use rand::Rng;

pub type F = FieldElement<Babybear31PrimeField>;
pub type Fp2E = FieldElement<QuadraticBabybearField>;
pub type Fp4E = FieldElement<Degree4BabyBearExtensionField>;
type EF4 = BinomialExtensionField<BabyBear, 4>;

// 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::<u64>()), F::from(random::<u64>())));
Expand Down Expand Up @@ -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)
Expand All @@ -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<Vec<(Fp4E, Fp4E)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
pub fn babybear_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(F, F)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
.into_iter()
.map(rand_babybear_fp4_elements)
.map(rand_field_elements)
.collect::<Vec<_>>();

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));
Expand All @@ -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));
Expand All @@ -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());
Expand All @@ -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<Vec<(F, F)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
pub fn babybear_extension_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(Fp4E, Fp4E)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
.into_iter()
.map(rand_field_elements)
.map(rand_babybear_fp4_elements)
.collect::<Vec<_>>();
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));
Expand All @@ -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());
Expand All @@ -173,36 +160,26 @@ 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());
}
});
});
}

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<Vec<(BabyBear, BabyBear)>> = [1, 10, 100, 1000, 10000, 100000, 1000000]
.into_iter()
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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));
Expand All @@ -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<Vec<(EF4, EF4)>> = input_sizes
Expand All @@ -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));
Expand All @@ -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| {
Expand All @@ -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());
Expand All @@ -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));
Expand Down

0 comments on commit 8ad2490

Please sign in to comment.