diff --git a/.gitignore b/.gitignore index 2d01884..b77ac50 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb +.idea/* # Added by cargo diff --git a/crates/starknet-types-core/Cargo.toml b/crates/starknet-types-core/Cargo.toml index 4406dd0..34a3b67 100644 --- a/crates/starknet-types-core/Cargo.toml +++ b/crates/starknet-types-core/Cargo.toml @@ -58,5 +58,6 @@ rand = "0.8.5" lazy_static = { version = "1.4.0", default-features = false } [[bench]] -name = "criterion_pedersen" +name = "criterion_hashes" harness = false + diff --git a/crates/starknet-types-core/Makefile b/crates/starknet-types-core/Makefile index 5f8628a..1fdb0dc 100644 --- a/crates/starknet-types-core/Makefile +++ b/crates/starknet-types-core/Makefile @@ -7,4 +7,4 @@ test: cargo test bench: - cargo bench --bench criterion_pedersen --features hash + cargo bench --bench criterion_hashes --features hash diff --git a/crates/starknet-types-core/benches/criterion_hashes.rs b/crates/starknet-types-core/benches/criterion_hashes.rs new file mode 100644 index 0000000..3fd5c22 --- /dev/null +++ b/crates/starknet-types-core/benches/criterion_hashes.rs @@ -0,0 +1,91 @@ +use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; +use rand::{Rng, RngCore, SeedableRng}; +use rand_chacha::ChaCha8Rng; + +use starknet_types_core::felt::Felt; +use starknet_types_core::hash::StarkHash; +use starknet_types_core::hash::{Pedersen, Poseidon}; + +const SEED: u64 = 3; + +fn rnd_felt(rng: &mut ChaCha8Rng) -> Felt { + let mut felt: [u8; 32] = Default::default(); + let first_non_zero = rng.gen_range(0..32); + rng.fill_bytes(&mut felt[first_non_zero..32]); + Felt::from_bytes_be(&felt) +} + +fn rnd_felts(rng: &mut ChaCha8Rng, n: usize) -> Vec { + (0..n).map(|_| rnd_felt(rng)).collect() +} + +fn rnd_pair_of_felts(rng: &mut ChaCha8Rng) -> (Felt, Felt) { + (rnd_felt(rng), rnd_felt(rng)) +} + +fn poseidon_hash(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + let mut group = c.benchmark_group("Poseidon"); + + group.bench_function("hash", |bench| { + bench.iter_batched( + || rnd_pair_of_felts(&mut rng), + |(x, y)| black_box(Poseidon::hash(&x, &y)), + BatchSize::SmallInput, + ); + }); +} + +fn poseidon_hash_array(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + + let mut group = c.benchmark_group("Poseidon"); + + for n in [10, 100, 1000, 10000].iter() { + group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { + bench.iter_batched( + || rnd_felts(&mut rng, n), + |felts| black_box(Poseidon::hash_array(&felts)), + BatchSize::SmallInput, + ); + }); + } +} + +fn pedersen_hash(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + let mut group = c.benchmark_group("Pedersen"); + + group.bench_function("hash", |bench| { + bench.iter_batched( + || rnd_pair_of_felts(&mut rng), + |(x, y)| black_box(Pedersen::hash(&x, &y)), + BatchSize::SmallInput, + ); + }); +} + +fn pedersen_hash_array(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + + let mut group = c.benchmark_group("Pedersen"); + + for n in [10, 100, 1000, 10000].iter() { + group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { + bench.iter_batched( + || rnd_felts(&mut rng, n), + |felts| black_box(Pedersen::hash_array(&felts)), + BatchSize::SmallInput, + ); + }); + } +} + +criterion_group!( + hashes, + pedersen_hash, + pedersen_hash_array, + poseidon_hash, + poseidon_hash_array +); +criterion_main!(hashes); diff --git a/crates/starknet-types-core/benches/criterion_pedersen.rs b/crates/starknet-types-core/benches/criterion_pedersen.rs deleted file mode 100644 index 5821d7f..0000000 --- a/crates/starknet-types-core/benches/criterion_pedersen.rs +++ /dev/null @@ -1,24 +0,0 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use rand::{RngCore, SeedableRng}; -use rand_chacha::ChaCha8Rng; -use starknet_types_core::felt::Felt; -use starknet_types_core::hash::Pedersen; -use starknet_types_core::hash::StarkHash; - -fn pedersen_benchmarks(c: &mut Criterion) { - let mut rng = ChaCha8Rng::seed_from_u64(2); - let mut felt1: [u8; 32] = Default::default(); - rng.fill_bytes(&mut felt1); - let mut felt2: [u8; 32] = Default::default(); - rng.fill_bytes(&mut felt2); - - let x = Felt::from_bytes_be(&felt1); - let y = Felt::from_bytes_be(&felt2); - let mut group = c.benchmark_group("Pedersen Benchmark"); - - group.bench_function("Hashing with black_box", |bench| { - bench.iter(|| black_box(Pedersen::hash(&x, &y))) - }); -} -criterion_group!(pedersen, pedersen_benchmarks); -criterion_main!(pedersen);