From 7ff1a05d693954a81f2a9d7d209a75c8393af0a7 Mon Sep 17 00:00:00 2001 From: "dustin.ray" Date: Thu, 22 Aug 2024 21:20:09 -0700 Subject: [PATCH] add benches --- Cargo.toml | 6 ++++++ benches/hash_chain_benches.rs | 39 +++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 46 insertions(+) create mode 100644 benches/hash_chain_benches.rs diff --git a/Cargo.toml b/Cargo.toml index f73353a..6d1e149 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ sha3 = "0.10.6" structopt = { version = "0.3.26", default-features = false } env_logger = "0.11.5" thiserror = "1.0.63" +criterion = "0.3" [dev-dependencies] debug_print = { version = "1.0.0" } @@ -34,3 +35,8 @@ plonky2_crypto = { git = "https://github.com/Lagrange-Labs/plonky2-crypto", bran [profile.dev] opt-level = 3 + +[[bench]] +name = "hash_chain_bench" +path = "benches/hash_chain_benches.rs" +harness = false # Disable the default test harness for benchmarks \ No newline at end of file diff --git a/benches/hash_chain_benches.rs b/benches/hash_chain_benches.rs new file mode 100644 index 0000000..274ef1f --- /dev/null +++ b/benches/hash_chain_benches.rs @@ -0,0 +1,39 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use hash_chain::HashChain; +use plonky2::{ + field::goldilocks_field::GoldilocksField, + plonk::{ + circuit_builder::CircuitBuilder, + circuit_data::CircuitConfig, + config::{GenericConfig, PoseidonGoldilocksConfig}, + }, +}; + +fn hash_chain_benchmark(c: &mut Criterion) { + const D: usize = 2; + type C = PoseidonGoldilocksConfig; + type F = >::F; + + let config = CircuitConfig::standard_recursion_config(); + + let step_sizes = [2, 4, 8, 16, 32, 64]; // Example step sizes + + for &steps in &step_sizes { + c.bench_function(&format!("hash_chain_{}_steps", steps), |b| { + b.iter(|| { + // Create the circuit inside the black_box to ensure it is evaluated + let mut circuit = black_box(CircuitBuilder::::new(config.clone())); + + let (_, _) = as HashChain< + GoldilocksField, + D, + C, + >>::build_hash_chain_circuit(&mut circuit, steps) + .unwrap(); + }); + }); + } +} + +criterion_group!(benches, hash_chain_benchmark); +criterion_main!(benches); diff --git a/src/lib.rs b/src/lib.rs index a491fb7..91ea911 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -476,6 +476,7 @@ mod tests { >>::build_hash_chain_circuit(&mut circuit, 2) .unwrap(); + let num_bytes = proof.to_bytes().len(); let result = as HashChain>::verify( proof,