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

feat: poseidon and pedersen extended benchmarks #57

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.

Loading