Skip to content

Commit

Permalink
add benches
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Feb 21, 2024
1 parent 27abd27 commit 5b53639
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
9 changes: 7 additions & 2 deletions math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = [
Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions math/benches/criterion_icicle.rs
Original file line number Diff line number Diff line change
@@ -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<C: IsEllipticCurve>(
msm_size: usize,
) -> (Vec<FieldElement<C::BaseField>>, Vec<C::PointRepresentation>)
where
<C::BaseField as IsField>::BaseType: From<u64>,
{
// 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::<C::BaseField>::new(rng.gen::<u64>().into()))
.collect();

let points: Vec<_> = (0..msm_size)
.map(|_| g.operate_with_self(rng.gen::<u64>()))
.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::<BLS12381Curve>(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::<BLS12377Curve>(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::<BN254Curve>(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);

0 comments on commit 5b53639

Please sign in to comment.