diff --git a/Cargo.lock b/Cargo.lock index 86e8dc0650..5c4c0d7c5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2179,6 +2179,7 @@ dependencies = [ "ark-serialize", "blake2", "colored", + "criterion", "groupmap", "itertools 0.12.1", "mina-curves", diff --git a/poly-commitment/Cargo.toml b/poly-commitment/Cargo.toml index 680c50cc99..5496e686ae 100644 --- a/poly-commitment/Cargo.toml +++ b/poly-commitment/Cargo.toml @@ -35,9 +35,14 @@ ocaml = { workspace = true, optional = true } ocaml-gen = { workspace = true, optional = true } [dev-dependencies] +criterion.workspace = true colored.workspace = true rand_chacha.workspace = true ark-bn254.workspace = true [features] ocaml_types = ["ocaml", "ocaml-gen"] + +[[bench]] +name = "poly_comm" +harness = false \ No newline at end of file diff --git a/poly-commitment/benches/poly_comm.rs b/poly-commitment/benches/poly_comm.rs new file mode 100644 index 0000000000..5a961d6c7c --- /dev/null +++ b/poly-commitment/benches/poly_comm.rs @@ -0,0 +1,112 @@ +use ark_ec::{ + short_weierstrass::{Affine, SWCurveConfig}, + AffineRepr, +}; +use ark_ff::{PrimeField, UniformRand}; +use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; +use mina_curves::pasta::{Pallas, Vesta}; +use poly_commitment::PolyComm; +use rand_core::{CryptoRng, RngCore}; + +fn helper_generate_random_elliptic_curve_point(rng: &mut RNG) -> Affine

+where + P::BaseField: PrimeField, + RNG: RngCore + CryptoRng, + P: SWCurveConfig, +{ + let p1_x = P::BaseField::rand(rng); + let mut p1: Option> = Affine::

::get_point_from_x_unchecked(p1_x, false); + while p1.is_none() { + let p1_x = P::BaseField::rand(rng); + p1 = Affine::

::get_point_from_x_unchecked(p1_x, false); + } + let p1: Affine

= p1.unwrap().mul_by_cofactor_to_group().into(); + p1 +} + +fn generate_poly_comm_pallas(rng: &mut RNG, n: usize) -> PolyComm +where + RNG: RngCore + CryptoRng, +{ + let elems: Vec = (0..n) + .map(|_| helper_generate_random_elliptic_curve_point(rng)) + .collect(); + PolyComm::new(elems) +} + +fn generate_poly_comm_vesta(rng: &mut RNG, n: usize) -> PolyComm +where + RNG: RngCore + CryptoRng, +{ + let elems: Vec = (0..n) + .map(|_| helper_generate_random_elliptic_curve_point(rng)) + .collect(); + PolyComm::new(elems) +} + +fn benchmark_polycomm_add(c: &mut Criterion) { + let mut group = c.benchmark_group("PolyComm Add"); + let mut rng = o1_utils::tests::make_test_rng(None); + + for n in [16, 32, 64, 128, 256, 1 << 10, 1 << 14, 1 << 15].into_iter() { + let poly1: PolyComm = generate_poly_comm_pallas(&mut rng, n); + let poly2: PolyComm = generate_poly_comm_pallas(&mut rng, n); + group.bench_with_input(BenchmarkId::new("PolyComm Add Pallas", n), &n, |b, _| { + b.iter_batched( + || (poly1.clone(), poly2.clone()), + |(poly1, poly2)| { + black_box(&poly1 + &poly2); + }, + BatchSize::SmallInput, + ) + }); + + let poly1: PolyComm = generate_poly_comm_vesta(&mut rng, n); + let poly2: PolyComm = generate_poly_comm_vesta(&mut rng, n); + group.bench_with_input(BenchmarkId::new("PolyComm Add Vesta", n), &n, |b, _| { + b.iter_batched( + || (poly1.clone(), poly2.clone()), + |(poly1, poly2)| { + black_box(&poly1 + &poly2); + }, + BatchSize::SmallInput, + ) + }); + } + group.finish(); +} + +fn benchmark_polycomm_sub(c: &mut Criterion) { + let mut group = c.benchmark_group("PolyComm Sub"); + let mut rng = o1_utils::tests::make_test_rng(None); + + for n in [16, 32, 64, 128, 256, 1 << 10, 1 << 14, 1 << 15].into_iter() { + let poly1: PolyComm = generate_poly_comm_pallas(&mut rng, n); + let poly2: PolyComm = generate_poly_comm_pallas(&mut rng, n); + group.bench_with_input(BenchmarkId::new("PolyComm Sub Pallas", n), &n, |b, _| { + b.iter_batched( + || (poly1.clone(), poly2.clone()), + |(poly1, poly2)| { + black_box(&poly1 - &poly2); + }, + BatchSize::SmallInput, + ) + }); + + let poly1: PolyComm = generate_poly_comm_vesta(&mut rng, n); + let poly2: PolyComm = generate_poly_comm_vesta(&mut rng, n); + group.bench_with_input(BenchmarkId::new("PolyComm Sub Vesta", n), &n, |b, _| { + b.iter_batched( + || (poly1.clone(), poly2.clone()), + |(poly1, poly2)| { + black_box(&poly1 - &poly2); + }, + BatchSize::SmallInput, + ) + }); + } + group.finish(); +} + +criterion_group!(benches, benchmark_polycomm_add, benchmark_polycomm_sub); +criterion_main!(benches);