From 9a6ac4cf0a701252843fa17b93121d40ca39bd44 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 29 Nov 2023 17:58:06 +0100 Subject: [PATCH 1/6] Get path for precomputed srs directly from Cargo. Getting them from the environment variable seems to not work on every system (in particular, it doesn't on my Mac :) ) --- kimchi/src/precomputed_srs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kimchi/src/precomputed_srs.rs b/kimchi/src/precomputed_srs.rs index 116554e262..8c55d25377 100644 --- a/kimchi/src/precomputed_srs.rs +++ b/kimchi/src/precomputed_srs.rs @@ -17,7 +17,7 @@ pub const SERIALIZED_SRS_SIZE: u32 = 16; /// The path of the serialized SRS. fn get_srs_path() -> PathBuf { - let base_path = std::env::var("CARGO_MANIFEST_DIR").expect("failed to get manifest path"); + let base_path = env!("CARGO_MANIFEST_DIR"); PathBuf::from(base_path) .join("../srs") .join(format!("{}.srs", G::NAME)) From e991fd101ded6cc0afa04903e63bf70df689a719 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 29 Nov 2023 18:16:18 +0100 Subject: [PATCH 2/6] Ensure benchmark IDs are unique. Using the `srs_size` was not unique, and so criterion couldn't run the benchmarks. --- kimchi/benches/proof_criterion.rs | 6 +++--- kimchi/src/bench.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kimchi/benches/proof_criterion.rs b/kimchi/benches/proof_criterion.rs index 8a1923e48f..243a6ccaac 100644 --- a/kimchi/benches/proof_criterion.rs +++ b/kimchi/benches/proof_criterion.rs @@ -7,13 +7,13 @@ pub fn bench_proof_creation(c: &mut Criterion) { let ctx = BenchmarkCtx::new(10); group.bench_function( - format!("proof creation (SRS size 2^{})", ctx.srs_size()), + format!("proof creation ({} gates)", ctx.num_gates), |b| b.iter(|| black_box(ctx.create_proof())), ); let ctx = BenchmarkCtx::new(14); group.bench_function( - format!("proof creation (SRS size 2^{})", ctx.srs_size()), + format!("proof creation ({} gates)", ctx.num_gates), |b| b.iter(|| black_box(ctx.create_proof())), ); @@ -21,7 +21,7 @@ pub fn bench_proof_creation(c: &mut Criterion) { group.sample_size(100).sampling_mode(SamplingMode::Auto); group.bench_function( - format!("proof verification (SRS size 2^{})", ctx.srs_size()), + format!("proof verification ({} gates)", ctx.num_gates), |b| b.iter(|| ctx.batch_verification(black_box(&vec![proof_and_public.clone()]))), ); } diff --git a/kimchi/src/bench.rs b/kimchi/src/bench.rs index ccf4808c84..e8fdbe0c47 100644 --- a/kimchi/src/bench.rs +++ b/kimchi/src/bench.rs @@ -26,7 +26,7 @@ type BaseSponge = DefaultFqSponge; type ScalarSponge = DefaultFrSponge; pub struct BenchmarkCtx { - num_gates: usize, + pub num_gates: usize, group_map: BWParameters, index: ProverIndex>, verifier_index: VerifierIndex>, From f317dc10be4aaa125a7f1a25be75eb27f8fe6520 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 29 Nov 2023 18:45:24 +0100 Subject: [PATCH 3/6] Split proof creation and proof verification benchmarks into groups. Also, adding a benchmark for verifying a proof with a different size, and removing some code duplication. --- kimchi/benches/proof_criterion.rs | 38 +++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/kimchi/benches/proof_criterion.rs b/kimchi/benches/proof_criterion.rs index 243a6ccaac..a37aafabfe 100644 --- a/kimchi/benches/proof_criterion.rs +++ b/kimchi/benches/proof_criterion.rs @@ -5,26 +5,30 @@ pub fn bench_proof_creation(c: &mut Criterion) { let mut group = c.benchmark_group("Proof creation"); group.sample_size(10).sampling_mode(SamplingMode::Flat); // for slow benchmarks - let ctx = BenchmarkCtx::new(10); - group.bench_function( - format!("proof creation ({} gates)", ctx.num_gates), - |b| b.iter(|| black_box(ctx.create_proof())), - ); + for size in [10, 14] { + let ctx = BenchmarkCtx::new(size); - let ctx = BenchmarkCtx::new(14); - group.bench_function( - format!("proof creation ({} gates)", ctx.num_gates), - |b| b.iter(|| black_box(ctx.create_proof())), - ); - - let proof_and_public = ctx.create_proof(); + group.bench_function( + format!("proof creation ({} gates)", ctx.num_gates), + |b| b.iter(|| black_box(ctx.create_proof())), + ); + } +} +pub fn bench_proof_verification(c: &mut Criterion) { + let mut group = c.benchmark_group("Proof verification"); group.sample_size(100).sampling_mode(SamplingMode::Auto); - group.bench_function( - format!("proof verification ({} gates)", ctx.num_gates), - |b| b.iter(|| ctx.batch_verification(black_box(&vec![proof_and_public.clone()]))), - ); + + for size in [10, 14] { + let ctx = BenchmarkCtx::new(size); + let proof_and_public = ctx.create_proof(); + + group.bench_function( + format!("proof verification ({} gates)", ctx.num_gates), + |b| b.iter(|| ctx.batch_verification(black_box(&vec![proof_and_public.clone()]))), + ); + } } -criterion_group!(benches, bench_proof_creation); +criterion_group!(benches, bench_proof_creation, bench_proof_verification); criterion_main!(benches); From 745984ccd1670127a5d440d69ec4a84483b4632e Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 29 Nov 2023 21:41:28 +0100 Subject: [PATCH 4/6] Amend kimchi/README.md To explicitly mention dependencies that need to be installed in order to run the criterion benchmarks. --- kimchi/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kimchi/README.md b/kimchi/README.md index 76a71006cc..65befdb938 100644 --- a/kimchi/README.md +++ b/kimchi/README.md @@ -6,7 +6,7 @@ Kimchi is based on [plonk](https://eprint.iacr.org/2019/953.pdf), a zk-SNARK pro To bench kimchi, we have two types of benchmark engines. -[Criterion](https://bheisler.github.io/criterion.rs/) is used to benchmark time: +[Criterion](https://bheisler.github.io/criterion.rs/) is used to benchmark time. If you have installed [cargo-criterion](https://github.com/bheisler/cargo-criterion) and [gnuplot](http://www.gnuplot.info), you can run the benchmarks via ```console $ cargo criterion -p kimchi --bench proof_criterion From 5e9ecdc85ac04d2953028ce73f548b7dccb16008 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 29 Nov 2023 22:16:47 +0100 Subject: [PATCH 5/6] Adding the SRS size back in. --- kimchi/benches/proof_criterion.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kimchi/benches/proof_criterion.rs b/kimchi/benches/proof_criterion.rs index a37aafabfe..a3110a36d2 100644 --- a/kimchi/benches/proof_criterion.rs +++ b/kimchi/benches/proof_criterion.rs @@ -9,7 +9,7 @@ pub fn bench_proof_creation(c: &mut Criterion) { let ctx = BenchmarkCtx::new(size); group.bench_function( - format!("proof creation ({} gates)", ctx.num_gates), + format!("proof creation (SRS size 2^{{{}}}, {} gates)", ctx.srs_size(), ctx.num_gates), |b| b.iter(|| black_box(ctx.create_proof())), ); } @@ -24,7 +24,7 @@ pub fn bench_proof_verification(c: &mut Criterion) { let proof_and_public = ctx.create_proof(); group.bench_function( - format!("proof verification ({} gates)", ctx.num_gates), + format!("proof verification (SRS size 2^{{{}}}, {} gates)", ctx.srs_size(), ctx.num_gates), |b| b.iter(|| ctx.batch_verification(black_box(&vec![proof_and_public.clone()]))), ); } From e87baa8b084ae10afacc5bec44dd24d3e1a0a143 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Wed, 29 Nov 2023 23:17:50 +0100 Subject: [PATCH 6/6] Fix formatting. --- kimchi/benches/proof_criterion.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kimchi/benches/proof_criterion.rs b/kimchi/benches/proof_criterion.rs index a3110a36d2..80d68564b1 100644 --- a/kimchi/benches/proof_criterion.rs +++ b/kimchi/benches/proof_criterion.rs @@ -9,7 +9,11 @@ pub fn bench_proof_creation(c: &mut Criterion) { let ctx = BenchmarkCtx::new(size); group.bench_function( - format!("proof creation (SRS size 2^{{{}}}, {} gates)", ctx.srs_size(), ctx.num_gates), + format!( + "proof creation (SRS size 2^{{{}}}, {} gates)", + ctx.srs_size(), + ctx.num_gates + ), |b| b.iter(|| black_box(ctx.create_proof())), ); } @@ -24,7 +28,11 @@ pub fn bench_proof_verification(c: &mut Criterion) { let proof_and_public = ctx.create_proof(); group.bench_function( - format!("proof verification (SRS size 2^{{{}}}, {} gates)", ctx.srs_size(), ctx.num_gates), + format!( + "proof verification (SRS size 2^{{{}}}, {} gates)", + ctx.srs_size(), + ctx.num_gates + ), |b| b.iter(|| ctx.batch_verification(black_box(&vec![proof_and_public.clone()]))), ); }