Skip to content

Add Bench for Babybear #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
1 change: 1 addition & 0 deletions math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ proptest = "1.1.0"
pprof = { version = "0.13.0", features = ["criterion", "flamegraph"] }
p3-baby-bear = { git = "https://github.com/Plonky3/Plonky3" }
p3-field = { git = "https://github.com/Plonky3/Plonky3" }

[features]
default = ["parallel", "std"]
std = ["alloc", "serde?/std", "serde_json?/std"]
Expand Down
9 changes: 7 additions & 2 deletions math/benches/criterion_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ mod fields;
use fields::mersenne31::{mersenne31_extension_ops_benchmarks, mersenne31_ops_benchmarks};
use fields::mersenne31_montgomery::mersenne31_mont_ops_benchmarks;
use fields::{
baby_bear::{babybear_ops_benchmarks, babybear_ops_benchmarks_f64, babybear_p3_ops_benchmarks},
baby_bear::{
babybear_extension_ops_benchmarks_p3, babybear_p3_ops_benchmarks,
babybear_u32_ops_benchmarks, babybear_u64_extension_ops_benchmarks,
babybear_u64_ops_benchmarks,
},
stark252::starkfield_ops_benchmarks,
u64_goldilocks::u64_goldilocks_ops_benchmarks,
u64_goldilocks_montgomery::u64_goldilocks_montgomery_ops_benchmarks,
Expand All @@ -14,7 +18,8 @@ use fields::{
criterion_group!(
name = field_benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets =babybear_ops_benchmarks,babybear_ops_benchmarks_f64, babybear_p3_ops_benchmarks,mersenne31_extension_ops_benchmarks,mersenne31_ops_benchmarks,

targets =babybear_u32_ops_benchmarks,babybear_u64_ops_benchmarks,babybear_u64_extension_ops_benchmarks, babybear_p3_ops_benchmarks,babybear_extension_ops_benchmarks_p3,mersenne31_extension_ops_benchmarks,mersenne31_ops_benchmarks,
starkfield_ops_benchmarks,u64_goldilocks_ops_benchmarks,u64_goldilocks_montgomery_ops_benchmarks,mersenne31_mont_ops_benchmarks
);
criterion_main!(field_benches);
180 changes: 169 additions & 11 deletions math/benches/fields/baby_bear.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use criterion::Criterion;
use std::hint::black_box;

use lambdaworks_math::field::fields::fft_friendly::quartic_babybear::Degree4BabyBearExtensionField;
use lambdaworks_math::field::{
element::FieldElement,
fields::{
Expand All @@ -10,6 +11,7 @@ use lambdaworks_math::field::{
};

use p3_baby_bear::BabyBear;
use p3_field::extension::BinomialExtensionField;
use p3_field::{Field, FieldAlgebra};

use rand::random;
Expand All @@ -18,26 +20,64 @@ use rand::Rng;
pub type U32Babybear31PrimeField = U32MontgomeryBackendPrimeField<2013265921>;
pub type F = FieldElement<U32Babybear31PrimeField>;
pub type F64 = FieldElement<Babybear31PrimeField>;
pub type Fp4E = FieldElement<Degree4BabyBearExtensionField>;
type EF4 = BinomialExtensionField<BabyBear, 4>;

pub fn rand_field_elements(num: usize) -> Vec<(F, F)> {
pub fn rand_field_elements_u32(num: usize) -> Vec<(F, F)> {
let mut result = Vec::with_capacity(num);
for _ in 0..result.capacity() {
result.push((F::from(random::<u64>()), F::from(random::<u64>())));
}
result
}

pub fn rand_field_elements_u64(num: usize) -> Vec<(F64, F64)> {
let mut result = Vec::with_capacity(num);
for _ in 0..result.capacity() {
result.push((F64::from(random::<u64>()), F64::from(random::<u64>())));
}
result
}

pub fn rand_babybear_fp4_elements(num: usize) -> Vec<(Fp4E, Fp4E)> {
let mut result = Vec::with_capacity(num);
for _ in 0..num {
result.push((
Fp4E::new([
F64::from(random::<u64>()),
F64::from(random::<u64>()),
F64::from(random::<u64>()),
F64::from(random::<u64>()),
]),
Fp4E::new([
F64::from(random::<u64>()),
F64::from(random::<u64>()),
F64::from(random::<u64>()),
F64::from(random::<u64>()),
]),
));
}
result
}

fn rand_babybear_elements_p3(num: usize) -> Vec<(BabyBear, BabyBear)> {
let mut rng = rand::thread_rng();
(0..num)
.map(|_| (rng.gen::<BabyBear>(), rng.gen::<BabyBear>()))
.collect()
}

pub fn babybear_ops_benchmarks(c: &mut Criterion) {
fn rand_babybear_fp4_elements_p3(num: usize) -> Vec<(EF4, EF4)> {
let mut rng = rand::thread_rng();
(0..num)
.map(|_| (rng.gen::<EF4>(), rng.gen::<EF4>()))
.collect()
}

pub fn babybear_u32_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(F, F)>> = [1000000]
.into_iter()
.map(rand_field_elements)
.map(rand_field_elements_u32)
.collect::<Vec<_>>();
let mut group = c.benchmark_group("BabyBear operations using Lambdaworks u32");

Expand Down Expand Up @@ -92,14 +132,7 @@ pub fn babybear_ops_benchmarks(c: &mut Criterion) {
}
}

pub fn rand_field_elements_u64(num: usize) -> Vec<(F64, F64)> {
let mut result = Vec::with_capacity(num);
for _ in 0..result.capacity() {
result.push((F64::from(random::<u64>()), F64::from(random::<u64>())));
}
result
}
pub fn babybear_ops_benchmarks_f64(c: &mut Criterion) {
pub fn babybear_u64_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(F64, F64)>> = [1000000]
.into_iter()
.map(rand_field_elements_u64)
Expand Down Expand Up @@ -157,6 +190,69 @@ pub fn babybear_ops_benchmarks_f64(c: &mut Criterion) {
}
}

pub fn babybear_u64_extension_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(Fp4E, Fp4E)>> = [1000000]
.into_iter()
.map(rand_babybear_fp4_elements)
.collect::<Vec<_>>();

let mut group = c.benchmark_group("BabyBear Fp4 operations");

for i in input.clone().into_iter() {
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));
}
});
});
}

for i in input.clone().into_iter() {
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| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).square());
}
});
});
}

for i in input.clone().into_iter() {
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));
}
});
});
}

for i in input.clone().into_iter() {
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());
}
});
});
}
}

pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) {
let input: Vec<Vec<(BabyBear, BabyBear)>> = [1000000]
.into_iter()
Expand Down Expand Up @@ -214,3 +310,65 @@ pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) {
});
}
}

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
.into_iter()
.map(rand_babybear_fp4_elements_p3)
.collect::<Vec<_>>();

let mut group = c.benchmark_group("BabyBear Fp4 operations using Plonky3");

for i in input.clone().into_iter() {
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));
}
});
});
}
for i in input.clone().into_iter() {
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| {
bench.iter(|| {
for (x, _) in i {
black_box(black_box(x).square());
}
});
});
}

for i in input.clone().into_iter() {
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());
}
});
});
}

for i in input.clone().into_iter() {
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));
}
});
});
}
}
Loading
Loading