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

chore: tune poseidon benches #857

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
63 changes: 38 additions & 25 deletions benches/benches/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,55 @@ use lambdaworks_math::field::element::FieldElement;
use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField;
use lambdaworks_math::traits::ByteConversion;
use pathfinder_crypto::MontFelt;
use rand::{RngCore, SeedableRng};
use rand::{Rng, RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;

fn poseidon_benchmarks(c: &mut Criterion) {
let mut group = c.benchmark_group("poseidon");

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 mut rng = ChaCha8Rng::seed_from_u64(1);

let lw_x = FieldElement::<Stark252PrimeField>::from_bytes_be(&felt1).unwrap();
let lw_y = FieldElement::<Stark252PrimeField>::from_bytes_be(&felt2).unwrap();
group.bench_function("lambdaworks", |bench| {
bench.iter(|| black_box(PoseidonCairoStark252::hash(&lw_x, &lw_y)))
group.bench_function("lambdaworks", |b| {
b.iter_batched(
|| {
let mut lw_felt_x_bytes: [u8; 32] = Default::default();
rng.fill_bytes(&mut lw_felt_x_bytes);
let mut lw_felt_y_bytes: [u8; 32] = Default::default();
rng.fill_bytes(&mut lw_felt_y_bytes);
let lw_x =
FieldElement::<Stark252PrimeField>::from_bytes_be(&lw_felt_x_bytes).unwrap();
let lw_y =
FieldElement::<Stark252PrimeField>::from_bytes_be(&lw_felt_y_bytes).unwrap();
(lw_x, lw_y)
},
|(a, b)| black_box(PoseidonCairoStark252::hash(&a, &b)),
criterion::BatchSize::SmallInput,
)
});

let mut mont_x = lw_x.value().limbs;
let mut mont_y = lw_y.value().limbs;
group.bench_function("starknet-rs", |b| {
b.iter_batched(
|| {
let mut mont_x: [u64; 4] = Default::default();
rng.fill(&mut mont_x);
let mut mont_y: [u64; 4] = Default::default();
rng.fill(&mut mont_y);
let sn_ff_x = starknet_ff::FieldElement::from_mont(mont_x);
let sn_ff_y = starknet_ff::FieldElement::from_mont(mont_y);

// In order use the same field elements for starknet-rs and pathfinder, we have to reverse
// the limbs order respect to the lambdaworks implementation.
mont_x.reverse();
mont_y.reverse();

let sn_ff_x = starknet_ff::FieldElement::from_mont(mont_x);
let sn_ff_y = starknet_ff::FieldElement::from_mont(mont_y);
group.bench_function("starknet-rs", |bench| {
bench.iter(|| black_box(starknet_crypto::poseidon_hash(sn_ff_x, sn_ff_y)))
(sn_ff_x, sn_ff_y)
},
|(a, b)| black_box(starknet_crypto::poseidon_hash(a, b)),
criterion::BatchSize::SmallInput,
)
});

let pf_x = MontFelt(mont_x);
let pf_y = MontFelt(mont_y);
group.bench_function("pathfinder", |bench| {
bench.iter(|| black_box(pathfinder_crypto::hash::poseidon_hash(pf_x, pf_y)))
group.bench_function("pathfinder", |b| {
b.iter_batched(
|| (MontFelt::random(&mut rng), MontFelt::random(&mut rng)),
|(a, b)| black_box(pathfinder_crypto::hash::poseidon_hash(a, b)),
criterion::BatchSize::SmallInput,
)
});
}
criterion_group!(poseidon, poseidon_benchmarks);
Expand Down
Loading