diff --git a/math/Cargo.toml b/math/Cargo.toml index 07e5a9f02..194f92c15 100644 --- a/math/Cargo.toml +++ b/math/Cargo.toml @@ -34,7 +34,7 @@ icicle-bn254 = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.4 lambdaworks-gpu = { workspace = true, optional = true } [dev-dependencies] -rand = { version = "0.8.5", default-features = false } +rand = { version = "0.8.5" } rand_chacha = "0.3.1" criterion = "0.5.1" const-random = "0.1.15" @@ -51,7 +51,7 @@ lambdaworks-serde-binary = ["dep:serde", "alloc"] lambdaworks-serde-string = ["dep:serde", "dep:serde_json", "alloc"] proptest = ["dep:proptest"] winter_compatibility = ["winter-math", "miden-core"] -icicle = ["dep:icicle-cuda-runtime", "icicle-core", "icicle-bls12-377", "icicle-bls12-381", "icicle-bn254"] +icicle = ["dep:icicle-cuda-runtime", "dep:icicle-core", "dep:icicle-bls12-377", "dep:icicle-bls12-381", "dep:icicle-bn254"] # gpu metal = [ @@ -87,6 +87,11 @@ name = "criterion_msm" harness = false required-features = ["parallel"] +[[bench]] +name = "criterion_icicle" +harness = false +required-features = ["icicle"] + [[bench]] name = "criterion_fft" harness = false diff --git a/math/benches/criterion_icicle.rs b/math/benches/criterion_icicle.rs new file mode 100644 index 000000000..07a271220 --- /dev/null +++ b/math/benches/criterion_icicle.rs @@ -0,0 +1,71 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use lambdaworks_math::{ + cyclic_group::IsGroup, + elliptic_curve::{ + short_weierstrass::curves::{ + bls12_377::curve::BLS12377Curve, bls12_381::curve::BLS12381Curve, + bn_254::curve::BN254Curve, + }, + traits::IsEllipticCurve, + }, + field::{element::FieldElement, traits::IsField}, +}; + +use lambdaworks_math::gpu::icicle::{ + bls12_377::bls12_377_g1_msm, bls12_381::bls12_381_g1_msm, bn254::bn254_g1_msm, +}; +use rand::{rngs::StdRng, Rng, SeedableRng}; + +pub fn generate_cs_and_points( + msm_size: usize, +) -> (Vec>, Vec) +where + ::BaseType: From, +{ + // We use a seeded rng so the benchmarks are reproducible. + let mut rng = StdRng::seed_from_u64(42); + + let g = C::generator(); + + let cs: Vec<_> = (0..msm_size) + .map(|_| FieldElement::::new(rng.gen::().into())) + .collect(); + + let points: Vec<_> = (0..msm_size) + .map(|_| g.operate_with_self(rng.gen::())) + .collect(); + + (cs, points) +} + +pub fn msm_benchmarks_with_size(c: &mut Criterion, msm_size: usize) { + let mut group = c.benchmark_group(format!("MSM benchmarks with size {msm_size}")); + + let (cs, points) = generate_cs_and_points::(msm_size); + group.bench_function("BLS12_381", |bench| { + bench.iter(|| black_box(bls12_381_g1_msm(&cs, &points, None))); + }); + + let (cs, points) = generate_cs_and_points::(msm_size); + group.bench_function("BLS12_377", |bench| { + bench.iter(|| black_box(bls12_377_g1_msm(&cs, &points, None))); + }); + + let (cs, points) = generate_cs_and_points::(msm_size); + group.bench_function("BN_254", |bench| { + bench.iter(|| black_box(bn254_g1_msm(&cs, &points, None))); + }); +} + +pub fn run_benchmarks(c: &mut Criterion) { + let exponents = 1..=18; + + for exp in exponents { + let msm_size = 1 << exp; + + msm_benchmarks_with_size(c, msm_size); + } +} + +criterion_group!(icicle_msm, run_benchmarks); +criterion_main!(icicle_msm);