Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create benchmarks for proof generation and verification #162

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ssz-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ 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"
hex = "0.4.3"

[build-dependencies]
sha2 = "0.9.8"

[[bench]]
name = "proofs"
path = "benches/proofs.rs"
harness = false
36 changes: 36 additions & 0 deletions ssz-rs/benches/proofs.rs
Original file line number Diff line number Diff line change
@@ -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<List<u8, 1073741824>> = 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<List<u8, 1073741824>, 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<List<u8, 1073741824>> = 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<List<u8, 1073741824>, 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);
4 changes: 3 additions & 1 deletion ssz-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
EchoAlice marked this conversation as resolved.
Show resolved Hide resolved

mod array;
mod bitlist;
Expand Down
28 changes: 0 additions & 28 deletions ssz-rs/src/merkleization/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,34 +295,6 @@ pub(crate) mod tests {
assert!(result.is_ok());
}

#[test]
fn test_list_proving() {
let inner: Vec<List<u8, 1073741824>> = 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<List<u8, 1073741824>, 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;
Expand Down
Loading