diff --git a/Cargo.toml b/Cargo.toml index 26178092bae5fea858235f64d7194a843aae2e74..31e8a7330f2dd2fc325730376d532fc8f9e818b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ ark-poly = "0.4.2" ark-serialize = "0.4.2" ark-std = "0.4.0" clap = { version = "4.5.4", features = ["derive"] } -plnk = { git = "https://gitlab.isae-supaero.fr/a.stevan/plnk", tag = "0.3.0", version = "0.3.0" } +plnk = { git = "https://gitlab.isae-supaero.fr/a.stevan/plnk", tag = "0.6.0", version = "0.6.0" } rand = "0.8.5" rs_merkle = "1.4.1" thiserror = "1.0.50" diff --git a/benches/README.md b/benches/README.md index 2c1f21f1c68c8926cee5de1cb2887e5772f6fd6d..b9df21b961262ff72631674c32e345c33eb0ab71 100644 --- a/benches/README.md +++ b/benches/README.md @@ -87,6 +87,8 @@ let res = cargo run --example bench_commit -- --nb-measurements 10 ...$degrees | update times { into duration } | insert mean {|it| $it.times | math avg} | insert stddev {|it| $it.times | into int | into float | math stddev | into int | into duration} + | update label { parse "degree {d}" | into record | get d | into int } + | rename --column { label: "degree", name: "curve" } python scripts/plot/bench_commit.py ( $res | group-by curve --to-table | update items { reject curve } | to json diff --git a/examples/benches/commit.rs b/examples/benches/commit.rs index f0dccf0bf4cd45511c6cbc3fa475103e8fed8bff..dd60d9b509c83efa2df23b6d36b2cdc8be519ec1 100644 --- a/examples/benches/commit.rs +++ b/examples/benches/commit.rs @@ -1,6 +1,4 @@ // see `benches/README.md` -use std::time::Instant; - use ark_ec::{pairing::Pairing, CurveGroup}; use ark_ff::PrimeField; use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial}; @@ -19,6 +17,7 @@ where { eprintln!("curve: {}", curve); let rng = &mut rand::thread_rng(); + let b = plnk::Bencher::new(nb_measurements).with_name(curve); let max_degree = *degrees.iter().max().unwrap_or(&0); @@ -26,32 +25,12 @@ where let setup = zk::setup::<F, G>(max_degree, rng).unwrap(); eprintln!("done"); - for (i, degree) in degrees.iter().enumerate() { - let mut times = vec![]; - for j in 0..nb_measurements { - eprint!( - " d: {} [{}/{}:{:>5}/{}] \r", - degree, - i + 1, - degrees.len(), - j + 1, - nb_measurements - ); + for degree in degrees { + plnk::bench(&b, &format!("degree {}", degree), || { let polynomial = P::rand(*degree, rng); - - let start_time = Instant::now(); - let _ = zk::commit(&setup, &polynomial); - let end_time = Instant::now(); - - times.push(end_time.duration_since(start_time).as_nanos()); - } - - println!( - "{{curve: {}, degree: {}, times: {:?}}}", - curve, degree, times, - ); + plnk::timeit(|| zk::commit(&setup, &polynomial)) + }); } - eprintln!(); } fn ark_run<E, P>(degrees: &Vec<usize>, curve: &str, nb_measurements: usize) @@ -62,6 +41,7 @@ where { eprintln!("curve: {}", curve); let rng = &mut rand::thread_rng(); + let b = plnk::Bencher::new(nb_measurements).with_name(curve); let max_degree = *degrees.iter().max().unwrap_or(&0); @@ -79,32 +59,12 @@ where }; eprintln!("done"); - for (i, degree) in degrees.iter().enumerate() { - let mut times = vec![]; - for j in 0..nb_measurements { - eprint!( - " d: {} [{}/{}:{:>5}/{}] \r", - degree, - i + 1, - degrees.len(), - j + 1, - nb_measurements - ); + for degree in degrees { + plnk::bench(&b, &format!("degree {}", degree), || { let polynomial = P::rand(*degree, rng); - - let start_time = Instant::now(); - let _ = KZG10::commit(&setup, &polynomial, None, None); - let end_time = Instant::now(); - - times.push(end_time.duration_since(start_time).as_nanos()); - } - - println!( - "{{curve: {}, degree: {}, times: {:?}}}", - curve, degree, times, - ); + plnk::timeit(|| KZG10::commit(&setup, &polynomial, None, None)) + }) } - eprintln!(); } /// ## example diff --git a/examples/benches/operations/curve_group.rs b/examples/benches/operations/curve_group.rs index 978c7e00fc2eb5b3042357a335caa5760dcec4ff..ee2732a8dc0dfaf00d9b856099d676a6bd750e67 100644 --- a/examples/benches/operations/curve_group.rs +++ b/examples/benches/operations/curve_group.rs @@ -1,64 +1,64 @@ // see `benches/README.md` -use std::time::Instant; - use ark_ec::CurveGroup; use ark_ff::PrimeField; use clap::{command, Parser}; -fn bench_template<F: PrimeField, G: CurveGroup<ScalarField = F>>(b: &mut plnk::Bencher) { - plnk::bench(b, "random sampling", |rng| plnk::timeit!((|| G::rand(rng)))); +fn bench_template<F: PrimeField, G: CurveGroup<ScalarField = F>>(b: &plnk::Bencher) { + let rng = &mut ark_std::rand::thread_rng(); + + plnk::bench(b, "random sampling", || plnk::timeit(|| G::rand(rng))); - plnk::bench(b, "addition", |rng| { + plnk::bench(b, "addition", || { let g1 = G::rand(rng); let g2 = G::rand(rng); - plnk::timeit!((|| g1 + g2)) + plnk::timeit(|| g1 + g2) }); - plnk::bench(b, "substraction", |rng| { + plnk::bench(b, "substraction", || { let g1 = G::rand(rng); let g2 = G::rand(rng); - plnk::timeit!((|| g1 - g2)) + plnk::timeit(|| g1 - g2) }); - plnk::bench(b, "double", |rng| { + plnk::bench(b, "double", || { let g1 = G::rand(rng); - plnk::timeit!((|| g1.double())) + plnk::timeit(|| g1.double()) }); - plnk::bench(b, "scalar multiplication", |rng| { + plnk::bench(b, "scalar multiplication", || { let g1 = G::rand(rng); let f1 = F::rand(rng); - plnk::timeit!((|| g1.mul(f1))) + plnk::timeit(|| g1.mul(f1)) }); - plnk::bench(b, "into affine", |rng| { + plnk::bench(b, "into affine", || { let g1 = G::rand(rng); - plnk::timeit!((|| g1.into_affine())) + plnk::timeit(|| g1.into_affine()) }); - plnk::bench(b, "from affine", |rng| { + plnk::bench(b, "from affine", || { let g1_affine = G::rand(rng).into_affine(); - plnk::timeit!((|| Into::<G>::into(g1_affine))) + plnk::timeit(|| Into::<G>::into(g1_affine)) }); - plnk::bench(b, "affine addition", |rng| { + plnk::bench(b, "affine addition", || { let g1_affine = G::rand(rng).into_affine(); let g2_affine = G::rand(rng).into_affine(); - plnk::timeit!((|| g1_affine + g2_affine)) + plnk::timeit(|| g1_affine + g2_affine) }); - plnk::bench(b, "affine scalar multiplication", |rng| { + plnk::bench(b, "affine scalar multiplication", || { let g1_affine = G::rand(rng).into_affine(); let f1 = F::rand(rng); - plnk::timeit!((|| g1_affine * f1)) + plnk::timeit(|| g1_affine * f1) }); } @@ -74,11 +74,11 @@ struct Cli { fn main() { let cli = Cli::parse(); - let bencher = plnk::Bencher::new(cli.nb_measurements, ark_std::rand::thread_rng()); + let bencher = plnk::Bencher::new(cli.nb_measurements); bench_template::<ark_bls12_381::Fr, ark_bls12_381::G1Projective>( - &mut bencher.with_name("BLS12-381"), + &bencher.with_name("BLS12-381"), ); - bench_template::<ark_bn254::Fr, ark_bn254::G1Projective>(&mut bencher.with_name("BN-254")); - bench_template::<ark_pallas::Fr, ark_pallas::Projective>(&mut bencher.with_name("PALLAS")); + bench_template::<ark_bn254::Fr, ark_bn254::G1Projective>(&bencher.with_name("BN-254")); + bench_template::<ark_pallas::Fr, ark_pallas::Projective>(&bencher.with_name("PALLAS")); } diff --git a/examples/benches/operations/field.rs b/examples/benches/operations/field.rs index b37b1ec75b7175587128dd7d13783e49834a00f7..283f1ac449ae6c4e08cb4bf427e7b87364e37f6e 100644 --- a/examples/benches/operations/field.rs +++ b/examples/benches/operations/field.rs @@ -1,76 +1,78 @@ // see `benches/README.md` -use std::time::{Duration, Instant}; +use std::time::Duration; use ark_ff::PrimeField; use clap::{arg, command, Parser}; -fn bench_template<F: PrimeField>(b: &mut plnk::Bencher) { - plnk::bench(b, "random sampling", |rng| plnk::timeit!((|| F::rand(rng)))); +fn bench_template<F: PrimeField>(b: &plnk::Bencher) { + let rng = &mut ark_std::rand::thread_rng(); - plnk::bench(b, "addition", |rng| { + plnk::bench(b, "random sampling", || plnk::timeit(|| F::rand(rng))); + + plnk::bench(b, "addition", || { let f1 = F::rand(rng); let f2 = F::rand(rng); - plnk::timeit!((|| f1 + f2)) + plnk::timeit(|| f1 + f2) }); - plnk::bench(b, "substraction", |rng| { + plnk::bench(b, "substraction", || { let f1 = F::rand(rng); let f2 = F::rand(rng); - plnk::timeit!((|| f1 - f2)) + plnk::timeit(|| f1 - f2) }); - plnk::bench(b, "double", |rng| { + plnk::bench(b, "double", || { let f1 = F::rand(rng); - plnk::timeit!((|| f1.double())) + plnk::timeit(|| f1.double()) }); - plnk::bench(b, "multiplication", |rng| { + plnk::bench(b, "multiplication", || { let f1 = F::rand(rng); let f2 = F::rand(rng); - plnk::timeit!((|| f1 * f2)) + plnk::timeit(|| f1 * f2) }); - plnk::bench(b, "square", |rng| { + plnk::bench(b, "square", || { let f1 = F::rand(rng); - plnk::timeit!((|| f1.square())) + plnk::timeit(|| f1.square()) }); - plnk::bench(b, "inverse", |rng| { + plnk::bench(b, "inverse", || { let f1 = F::rand(rng); - plnk::timeit!((|| f1.inverse())) + plnk::timeit(|| f1.inverse()) }); - plnk::bench(b, "legendre", |rng| { + plnk::bench(b, "legendre", || { let f1 = F::rand(rng); - plnk::timeit!((|| f1.legendre())) + plnk::timeit(|| f1.legendre()) }); - plnk::bench(b, "sqrt", |rng| { + plnk::bench(b, "sqrt", || { let f1 = F::rand(rng); if f1.legendre().is_qr() { - plnk::timeit!((|| f1.sqrt())) + plnk::timeit(|| f1.sqrt()) } else { Duration::default() } }); - plnk::bench(b, "exponentiation", |rng| { + plnk::bench(b, "exponentiation", || { let f1 = F::rand(rng); - plnk::timeit!((|| f1.pow(F::MODULUS))) + plnk::timeit(|| f1.pow(F::MODULUS)) }); - plnk::bench(b, "into bigint", |rng| { + plnk::bench(b, "into bigint", || { let f1 = F::rand(rng); - plnk::timeit!((|| f1.into_bigint())) + plnk::timeit(|| f1.into_bigint()) }); } @@ -86,9 +88,9 @@ struct Cli { fn main() { let cli = Cli::parse(); - let bencher = plnk::Bencher::new(cli.nb_measurements, ark_std::rand::thread_rng()); + let bencher = plnk::Bencher::new(cli.nb_measurements); - bench_template::<ark_bls12_381::Fr>(&mut bencher.with_name("BLS12-381")); - bench_template::<ark_bn254::Fr>(&mut bencher.with_name("BN-254")); - bench_template::<ark_pallas::Fr>(&mut bencher.with_name("PALLAS")); + bench_template::<ark_bls12_381::Fr>(&bencher.with_name("BLS12-381")); + bench_template::<ark_bn254::Fr>(&bencher.with_name("BN-254")); + bench_template::<ark_pallas::Fr>(&bencher.with_name("PALLAS")); } diff --git a/scripts/plot/bench_commit.py b/scripts/plot/bench_commit.py index 09df517aee7c1bdd47e3787c5ed399e0a27f14b9..ba9f5033b29251f8e7ed5c2cd1798a59d95527b2 100644 --- a/scripts/plot/bench_commit.py +++ b/scripts/plot/bench_commit.py @@ -22,7 +22,7 @@ if __name__ == "__main__": plt.fill_between(xs, down, up, alpha=0.3) plt.xlabel("degree") - plt.ylabel("time (in ns)") + plt.ylabel("time (in ms)") plt.title("time to commit polynomials for certain curves")