From 258706623326b119149872a0f9f70b7dbb079c3b Mon Sep 17 00:00:00 2001 From: EchoAlice Date: Fri, 6 Sep 2024 10:26:23 -0600 Subject: [PATCH 1/3] Create benchmarks for proof generation and verification --- ssz-rs/src/lib.rs | 4 +++- ssz-rs/src/merkleization/proofs.rs | 31 ++++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index d779806a..a18903e9 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -82,10 +82,12 @@ //! ``` //! //! [ssz]: https://github.com/ethereum/consensus-specs/blob/dev/ssz/simple-serialize.md -#![cfg_attr(not(feature = "std"), no_std)] +#![feature(test)] +#![cfg_attr(not(feature = "std"), no_std)] #[cfg(not(feature = "std"))] extern crate alloc; +extern crate test; mod array; mod bitlist; diff --git a/ssz-rs/src/merkleization/proofs.rs b/ssz-rs/src/merkleization/proofs.rs index 7a8e73df..36434e26 100644 --- a/ssz-rs/src/merkleization/proofs.rs +++ b/ssz-rs/src/merkleization/proofs.rs @@ -295,8 +295,8 @@ pub(crate) mod tests { assert!(result.is_ok()); } - #[test] - fn test_list_proving() { + #[bench] + fn bench_proof_generation(b: &mut test::Bencher) { let inner: Vec> = vec![ vec![0u8, 1u8, 2u8].try_into().unwrap(), vec![3u8, 4u8, 5u8].try_into().unwrap(), @@ -307,20 +307,27 @@ pub(crate) mod tests { // Emulate a transactions tree let outer: List, 1048576> = List::try_from(inner).unwrap(); - let root = outer.hash_tree_root().unwrap(); + b.iter(|| { + let index = PathElement::from(1); + outer.prove(&[index]).unwrap() + }) + } - let index = PathElement::from(1); + #[bench] + fn bench_proof_verification(b: &mut test::Bencher) { + let inner: Vec> = vec![ + vec![0u8, 1u8, 2u8].try_into().unwrap(), + vec![3u8, 4u8, 5u8].try_into().unwrap(), + vec![6u8, 7u8, 8u8].try_into().unwrap(), + vec![9u8, 10u8, 11u8].try_into().unwrap(), + ]; - let start_proof = std::time::Instant::now(); + // Emulate a transactions tree + let outer: List, 1048576> = List::try_from(inner).unwrap(); + let index = PathElement::from(1); let (proof, witness) = outer.prove(&[index]).unwrap(); - println!("Generated proof in {:?}", start_proof.elapsed()); - - // Root and witness must be the same - assert_eq!(root, witness); - let start_verify = std::time::Instant::now(); - assert!(proof.verify(witness).is_ok()); - println!("Verified proof in {:?}", start_verify.elapsed()); + b.iter(|| proof.verify(witness)) } #[test] From 5b7f40df04d9fcd4a15c2265c48b68b3ac73536d Mon Sep 17 00:00:00 2001 From: EchoAlice Date: Mon, 9 Sep 2024 10:22:02 -0600 Subject: [PATCH 2/3] Move benchmarks to top level dir in `ssz-rs`. Migrate to Criterion benchmarking framework --- ssz-rs/Cargo.toml | 6 +++++ ssz-rs/benches/proofs.rs | 36 ++++++++++++++++++++++++++++++ ssz-rs/src/merkleization/proofs.rs | 35 ----------------------------- 3 files changed, 42 insertions(+), 35 deletions(-) create mode 100644 ssz-rs/benches/proofs.rs diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index ae5f3dbd..8d4b6e42 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -28,6 +28,7 @@ serde = { version = "1.0", default-features = false, features = [ alloy-primitives = { version = "~0.7", default-features = false } [dev-dependencies] +criterion = "0.5" snap = "1.0" project-root = "0.2.2" serde_json = "1.0.81" @@ -35,3 +36,8 @@ hex = "0.4.3" [build-dependencies] sha2 = "0.9.8" + +[[bench]] +name = "proofs" +path = "benches/proofs.rs" +harness = false \ No newline at end of file diff --git a/ssz-rs/benches/proofs.rs b/ssz-rs/benches/proofs.rs new file mode 100644 index 00000000..fc0560ed --- /dev/null +++ b/ssz-rs/benches/proofs.rs @@ -0,0 +1,36 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use ssz_rs::{List, PathElement, Prove}; + +fn bench_proof_generation(c: &mut Criterion) { + let inner: Vec> = vec![ + vec![0u8, 1u8, 2u8].try_into().unwrap(), + vec![3u8, 4u8, 5u8].try_into().unwrap(), + vec![6u8, 7u8, 8u8].try_into().unwrap(), + vec![9u8, 10u8, 11u8].try_into().unwrap(), + ]; + + // Emulate a transactions tree + let index = PathElement::from(1); + let outer: List, 1048576> = List::try_from(inner).unwrap(); + + c.bench_function("proof generation", |b| b.iter(|| outer.prove(&[index.clone()]).unwrap())); +} + +fn bench_proof_verification(c: &mut Criterion) { + let inner: Vec> = vec![ + vec![0u8, 1u8, 2u8].try_into().unwrap(), + vec![3u8, 4u8, 5u8].try_into().unwrap(), + vec![6u8, 7u8, 8u8].try_into().unwrap(), + vec![9u8, 10u8, 11u8].try_into().unwrap(), + ]; + + // Emulate a transactions tree + let outer: List, 1048576> = List::try_from(inner).unwrap(); + let index = PathElement::from(1); + let (proof, witness) = outer.prove(&[index]).unwrap(); + + c.bench_function("proof verification", |b| b.iter(|| proof.verify(witness))); +} + +criterion_group!(benches, bench_proof_generation, bench_proof_verification); +criterion_main!(benches); diff --git a/ssz-rs/src/merkleization/proofs.rs b/ssz-rs/src/merkleization/proofs.rs index 36434e26..abbc7244 100644 --- a/ssz-rs/src/merkleization/proofs.rs +++ b/ssz-rs/src/merkleization/proofs.rs @@ -295,41 +295,6 @@ pub(crate) mod tests { assert!(result.is_ok()); } - #[bench] - fn bench_proof_generation(b: &mut test::Bencher) { - let inner: Vec> = vec![ - vec![0u8, 1u8, 2u8].try_into().unwrap(), - vec![3u8, 4u8, 5u8].try_into().unwrap(), - vec![6u8, 7u8, 8u8].try_into().unwrap(), - vec![9u8, 10u8, 11u8].try_into().unwrap(), - ]; - - // Emulate a transactions tree - let outer: List, 1048576> = List::try_from(inner).unwrap(); - - b.iter(|| { - let index = PathElement::from(1); - outer.prove(&[index]).unwrap() - }) - } - - #[bench] - fn bench_proof_verification(b: &mut test::Bencher) { - let inner: Vec> = vec![ - vec![0u8, 1u8, 2u8].try_into().unwrap(), - vec![3u8, 4u8, 5u8].try_into().unwrap(), - vec![6u8, 7u8, 8u8].try_into().unwrap(), - vec![9u8, 10u8, 11u8].try_into().unwrap(), - ]; - - // Emulate a transactions tree - let outer: List, 1048576> = List::try_from(inner).unwrap(); - let index = PathElement::from(1); - let (proof, witness) = outer.prove(&[index]).unwrap(); - - b.iter(|| proof.verify(witness)) - } - #[test] fn test_proving_primitives_fails_with_bad_path() { let data = 8u8; From c5a46eed290f12c395e82a75c9f21187f30cba5a Mon Sep 17 00:00:00 2001 From: EchoAlice Date: Wed, 11 Sep 2024 09:34:43 -0600 Subject: [PATCH 3/3] Remove unused dependency --- ssz-rs/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index a18903e9..6c7075eb 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -83,11 +83,8 @@ //! //! [ssz]: https://github.com/ethereum/consensus-specs/blob/dev/ssz/simple-serialize.md -#![feature(test)] -#![cfg_attr(not(feature = "std"), no_std)] #[cfg(not(feature = "std"))] extern crate alloc; -extern crate test; mod array; mod bitlist;