Skip to content

Commit

Permalink
feat: poseidon and pedersen extended benchmarks (starknet-io#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejka authored Apr 11, 2024
1 parent cad00f7 commit 3921cb9
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

.idea/*

# Added by cargo

Expand Down
3 changes: 2 additions & 1 deletion crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion crates/starknet-types-core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ test:
cargo test

bench:
cargo bench --bench criterion_pedersen --features hash
cargo bench --bench criterion_hashes --features hash
91 changes: 91 additions & 0 deletions crates/starknet-types-core/benches/criterion_hashes.rs
Original file line number Diff line number Diff line change
@@ -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<Felt> {
(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);
24 changes: 0 additions & 24 deletions crates/starknet-types-core/benches/criterion_pedersen.rs

This file was deleted.

0 comments on commit 3921cb9

Please sign in to comment.