Skip to content

Commit

Permalink
chore: add poseidon benches comparing other implementations (#856)
Browse files Browse the repository at this point in the history
* Add poseidon comparation benches with pathfinder and starknet-rs

* Minor syntax change

* Remove plonky3 dependency

* Add comments in bench

* remove unused module
  • Loading branch information
entropidelic authored Apr 4, 2024
1 parent 52042bf commit 699c12d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
9 changes: 9 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ rand = "0.8.5"
rand_chacha = "0.3.1"
starknet-curve = { git = "https://github.com/xJonathanLEI/starknet-rs" }
starknet-ff = { git = "https://github.com/xJonathanLEI/starknet-rs" }
starknet-crypto = { git = "https://github.com/xJonathanLEI/starknet-rs" }
pathfinder-crypto = { git = "https://github.com/eqlabs/pathfinder.git" }

[dependencies.lambdaworks-math]
path = "../math"

[dependencies.lambdaworks-crypto]
path = "../crypto"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false }
rand_chacha = "0.3.1"
Expand Down Expand Up @@ -46,3 +51,7 @@ harness = false
[[bench]]
name = "point"
harness = false

[[bench]]
name = "poseidon"
harness = false
47 changes: 47 additions & 0 deletions benches/benches/poseidon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_crypto::hash::poseidon::starknet::PoseidonCairoStark252;
use lambdaworks_crypto::hash::poseidon::Poseidon;
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_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 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)))
});

let mut mont_x = lw_x.value().limbs;
let mut mont_y = lw_y.value().limbs;

// 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)))
});

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)))
});
}
criterion_group!(poseidon, poseidon_benchmarks);
criterion_main!(poseidon);
14 changes: 11 additions & 3 deletions crypto/benches/criterion_poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ use lambdaworks_crypto::hash::poseidon::starknet::PoseidonCairoStark252;
use lambdaworks_crypto::hash::poseidon::Poseidon;
use lambdaworks_math::field::element::FieldElement;
use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField;
use lambdaworks_math::traits::ByteConversion;
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;

fn poseidon_benchmarks(c: &mut Criterion) {
let x = FieldElement::<Stark252PrimeField>::from_hex("0x123456").unwrap();
let y = FieldElement::<Stark252PrimeField>::from_hex("0x789101").unwrap();
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 = FieldElement::<Stark252PrimeField>::from_bytes_be(&felt1).unwrap();
let y = FieldElement::<Stark252PrimeField>::from_bytes_be(&felt2).unwrap();
let mut group = c.benchmark_group("Poseidon Benchmark");

// Benchmark with black_box is 0.41% faster
group.bench_function("Hashing with black_box", |bench| {
bench.iter(|| black_box(PoseidonCairoStark252::hash(&x, &y)))
});
Expand Down

0 comments on commit 699c12d

Please sign in to comment.