use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::field::types::Sample; use plonky2::hash::hash_types::{BytesHash, RichField}; use plonky2::hash::keccak::KeccakHash; use plonky2::hash::poseidon::{Poseidon, SPONGE_WIDTH}; use plonky2::plonk::config::Hasher; use plonky2_monolith::monolith_hash::Monolith; use rand_chacha::rand_core::SeedableRng; use rand_chacha::ChaCha12Rng; use tynm::type_name; mod allocator; pub(crate) fn bench_keccak(c: &mut Criterion) { let mut rng = ChaCha12Rng::seed_from_u64(38u64); c.bench_function("keccak256", |b| { b.iter_batched( || { ( BytesHash::<32>::sample(&mut rng), BytesHash::<32>::sample(&mut rng), ) }, |(left, right)| as Hasher>::two_to_one(left, right), BatchSize::SmallInput, ) }); } pub(crate) fn bench_poseidon(c: &mut Criterion) { c.bench_function( &format!("poseidon<{}, {}>", type_name::(), SPONGE_WIDTH,), |b| { b.iter_batched( || F::rand_array::(), |state| F::poseidon(state), BatchSize::SmallInput, ) }, ); } pub(crate) fn bench_monolith(c: &mut Criterion) { c.bench_function( &format!("monolith<{}, {}>", type_name::(), SPONGE_WIDTH,), |b| { b.iter_batched( || F::rand_array::(), |state| F::monolith(state), BatchSize::SmallInput, ) }, ); } fn criterion_benchmark(c: &mut Criterion) { bench_poseidon::(c); bench_monolith::(c); bench_keccak::(c); } criterion_group!(name = benches; config = Criterion::default().sample_size(500); targets = criterion_benchmark); criterion_main!(benches);