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/lib.rs b/ssz-rs/src/lib.rs index d779806a..6c7075eb 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -82,7 +82,6 @@ //! ``` //! //! [ssz]: https://github.com/ethereum/consensus-specs/blob/dev/ssz/simple-serialize.md -#![cfg_attr(not(feature = "std"), no_std)] #[cfg(not(feature = "std"))] extern crate alloc; diff --git a/ssz-rs/src/merkleization/proofs.rs b/ssz-rs/src/merkleization/proofs.rs index 7a8e73df..abbc7244 100644 --- a/ssz-rs/src/merkleization/proofs.rs +++ b/ssz-rs/src/merkleization/proofs.rs @@ -295,34 +295,6 @@ pub(crate) mod tests { assert!(result.is_ok()); } - #[test] - fn test_list_proving() { - 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 root = outer.hash_tree_root().unwrap(); - - let index = PathElement::from(1); - - let start_proof = std::time::Instant::now(); - 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()); - } - #[test] fn test_proving_primitives_fails_with_bad_path() { let data = 8u8;