From 309c436dc669cb1ba35207ebcc0fe306dcf7fbce Mon Sep 17 00:00:00 2001 From: Theprotocolwhisperer Date: Wed, 29 Nov 2023 17:56:39 -0500 Subject: [PATCH 1/8] (feature): Benchmark Poseidon --- crypto/Cargo.toml | 4 ++++ crypto/benches/criterion_poseidon.rs | 30 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 crypto/benches/criterion_poseidon.rs diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index 8261ee196..9df642ff2 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -29,3 +29,7 @@ harness = false [[bench]] name = "iai_merkle" harness = false + +[[bench]] +name= "criterion_poseidon" +harness=false \ No newline at end of file diff --git a/crypto/benches/criterion_poseidon.rs b/crypto/benches/criterion_poseidon.rs new file mode 100644 index 000000000..c6e930144 --- /dev/null +++ b/crypto/benches/criterion_poseidon.rs @@ -0,0 +1,30 @@ +use core::time::Duration; +use criterion::{criterion_group, criterion_main, Criterion}; +use lambdaworks_crypto::hash::poseidon::starknet::{parameters::DefaultPoseidonParams, Poseidon}; +use lambdaworks_math::elliptic_curve::short_weierstrass::curves::bls12_381::field_extension::BLS12381PrimeField; +use lambdaworks_math::field::element::FieldElement; + +fn poseidon_benchmarks(c: &mut Criterion) { + let mut group = c.benchmark_group("Poseidon hash"); + type FE = FieldElement; + group.sample_size(10); + group.measurement_time(Duration::from_secs(30)); + + let points_x: Vec<_> = core::iter::successors(Some(FE::zero()), |s| Some(s + FE::one())) + // `(1 << 20) + 1` exploits worst cases in terms of rounding up to powers of 2. + .take((1 << 20) + 1) + .collect(); + let points_y: Vec<_> = core::iter::successors(Some(FE::zero()), |s| Some(s + FE::one())) + .take((1 << 20) + 1) + .collect(); + let poseidon: Poseidon = Poseidon::new_with_params(DefaultPoseidonParams); + group.bench_with_input( + "build", + unhashed_fields.as_slice(), + |bench, unhashed_fields| { + bench.iter_with_large_drop(|| Poseidon::hash(&poseidon, points_x, points_y)); + }, + ); +} +criterion_group!(poseidon, poseidon_benchmarks); +criterion_main!(poseidon); From 0932dcbc141710717f18640d4162904b5c335a68 Mon Sep 17 00:00:00 2001 From: Theprotocolwhisperer Date: Wed, 29 Nov 2023 23:19:21 -0500 Subject: [PATCH 2/8] (fix): Instantiation Poseidon --- crypto/benches/criterion_poseidon.rs | 31 +++++++++++++--------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/crypto/benches/criterion_poseidon.rs b/crypto/benches/criterion_poseidon.rs index c6e930144..8afbc9047 100644 --- a/crypto/benches/criterion_poseidon.rs +++ b/crypto/benches/criterion_poseidon.rs @@ -1,30 +1,27 @@ -use core::time::Duration; use criterion::{criterion_group, criterion_main, Criterion}; -use lambdaworks_crypto::hash::poseidon::starknet::{parameters::DefaultPoseidonParams, Poseidon}; -use lambdaworks_math::elliptic_curve::short_weierstrass::curves::bls12_381::field_extension::BLS12381PrimeField; +use lambdaworks_crypto::hash::poseidon::starknet::{ + parameters::{DefaultPoseidonParams, PermutationParameters}, + Poseidon, +}; use lambdaworks_math::field::element::FieldElement; +use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField; +use std::time::Duration; +type F = Stark252PrimeField; +type FE = FieldElement; fn poseidon_benchmarks(c: &mut Criterion) { - let mut group = c.benchmark_group("Poseidon hash"); - type FE = FieldElement; + let mut group = c.benchmark_group("Poseidon batch"); group.sample_size(10); group.measurement_time(Duration::from_secs(30)); - let points_x: Vec<_> = core::iter::successors(Some(FE::zero()), |s| Some(s + FE::one())) // `(1 << 20) + 1` exploits worst cases in terms of rounding up to powers of 2. .take((1 << 20) + 1) .collect(); - let points_y: Vec<_> = core::iter::successors(Some(FE::zero()), |s| Some(s + FE::one())) - .take((1 << 20) + 1) - .collect(); - let poseidon: Poseidon = Poseidon::new_with_params(DefaultPoseidonParams); - group.bench_with_input( - "build", - unhashed_fields.as_slice(), - |bench, unhashed_fields| { - bench.iter_with_large_drop(|| Poseidon::hash(&poseidon, points_x, points_y)); - }, - ); + let params = PermutationParameters::new_with(DefaultPoseidonParams::CairoStark252); + let poseidon = Poseidon::new_with_params(params); + group.bench_with_input("build", points_x.as_slice(), |bench, points_x| { + bench.iter_with_large_drop(|| Poseidon::hash_many(&poseidon, points_x)); + }); } criterion_group!(poseidon, poseidon_benchmarks); criterion_main!(poseidon); From 9c84e6a571ebd1af471cd2dba4f69bfeedf8c2b1 Mon Sep 17 00:00:00 2001 From: Theprotocolwhisperer Date: Wed, 6 Dec 2023 13:13:30 -0500 Subject: [PATCH 3/8] (fix): Updating imports --- crypto/benches/criterion_poseidon.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crypto/benches/criterion_poseidon.rs b/crypto/benches/criterion_poseidon.rs index 8afbc9047..fd5f7cdce 100644 --- a/crypto/benches/criterion_poseidon.rs +++ b/crypto/benches/criterion_poseidon.rs @@ -1,8 +1,6 @@ use criterion::{criterion_group, criterion_main, Criterion}; -use lambdaworks_crypto::hash::poseidon::starknet::{ - parameters::{DefaultPoseidonParams, PermutationParameters}, - Poseidon, -}; +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 std::time::Duration; @@ -17,10 +15,8 @@ fn poseidon_benchmarks(c: &mut Criterion) { // `(1 << 20) + 1` exploits worst cases in terms of rounding up to powers of 2. .take((1 << 20) + 1) .collect(); - let params = PermutationParameters::new_with(DefaultPoseidonParams::CairoStark252); - let poseidon = Poseidon::new_with_params(params); group.bench_with_input("build", points_x.as_slice(), |bench, points_x| { - bench.iter_with_large_drop(|| Poseidon::hash_many(&poseidon, points_x)); + bench.iter_with_large_drop(|| PoseidonCairoStark252::hash_many(points_x)); }); } criterion_group!(poseidon, poseidon_benchmarks); From 05d3a8400e608a7dbc031e33ff8348dc0c3e2419 Mon Sep 17 00:00:00 2001 From: Theprotocolwhisperer Date: Fri, 15 Dec 2023 14:16:22 -0300 Subject: [PATCH 4/8] (rebase): Poseidon benchmark --- benches/src/lib.rs | 2 ++ crypto/Cargo.toml | 2 +- crypto/benches/criterion_poseidon.rs | 27 +++++++++++++-------------- 3 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 benches/src/lib.rs diff --git a/benches/src/lib.rs b/benches/src/lib.rs new file mode 100644 index 000000000..3dc7626f3 --- /dev/null +++ b/benches/src/lib.rs @@ -0,0 +1,2 @@ +#[path = "../benches/utils.rs"] +pub mod utils; diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index fea3f1798..f3ff823c5 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -14,7 +14,7 @@ sha2 = "0.10" thiserror = "1.0.38" serde = { version = "1.0", features = ["derive"] } rayon = { version = "1.8.0", optional = true } - +lambdaworks-benches = { path = "../benches" } [dev-dependencies] criterion = "0.4" iai-callgrind.workspace = true diff --git a/crypto/benches/criterion_poseidon.rs b/crypto/benches/criterion_poseidon.rs index fd5f7cdce..72d194594 100644 --- a/crypto/benches/criterion_poseidon.rs +++ b/crypto/benches/criterion_poseidon.rs @@ -1,22 +1,21 @@ -use criterion::{criterion_group, criterion_main, Criterion}; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use lambdaworks_benches::utils::{generate_random_elements, to_lambdaworks_vec}; 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 std::time::Duration; +use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::MontgomeryConfigStark252PrimeField; +use lambdaworks_math::field::fields::montgomery_backed_prime_fields::MontgomeryBackendPrimeField; -type F = Stark252PrimeField; -type FE = FieldElement; +type F = MontgomeryBackendPrimeField; +fn generate_points() -> Vec> { + let random_points = generate_random_elements(2); + to_lambdaworks_vec(&random_points) +} fn poseidon_benchmarks(c: &mut Criterion) { - let mut group = c.benchmark_group("Poseidon batch"); - group.sample_size(10); - group.measurement_time(Duration::from_secs(30)); - let points_x: Vec<_> = core::iter::successors(Some(FE::zero()), |s| Some(s + FE::one())) - // `(1 << 20) + 1` exploits worst cases in terms of rounding up to powers of 2. - .take((1 << 20) + 1) - .collect(); - group.bench_with_input("build", points_x.as_slice(), |bench, points_x| { - bench.iter_with_large_drop(|| PoseidonCairoStark252::hash_many(points_x)); + let mut group = c.benchmark_group("Poseidon Benchmark".to_string()); + let points = generate_points(); + group.bench_function("Merkle hashing ", |bench| { + bench.iter(|| black_box(PoseidonCairoStark252::hash(&points[0], &points[1]))) }); } criterion_group!(poseidon, poseidon_benchmarks); From ff3cd59fa1e4808a91b7ce7fd5ae3e9bd5e4eb08 Mon Sep 17 00:00:00 2001 From: protocolwhisper Date: Wed, 3 Jan 2024 20:01:38 -0500 Subject: [PATCH 5/8] (fix): bench parameters --- benches/src/lib.rs | 2 -- crypto/Cargo.toml | 1 - crypto/benches/criterion_poseidon.rs | 21 +++++++++------------ 3 files changed, 9 insertions(+), 15 deletions(-) delete mode 100644 benches/src/lib.rs diff --git a/benches/src/lib.rs b/benches/src/lib.rs deleted file mode 100644 index 3dc7626f3..000000000 --- a/benches/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[path = "../benches/utils.rs"] -pub mod utils; diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index f3ff823c5..ff0637bb2 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -14,7 +14,6 @@ sha2 = "0.10" thiserror = "1.0.38" serde = { version = "1.0", features = ["derive"] } rayon = { version = "1.8.0", optional = true } -lambdaworks-benches = { path = "../benches" } [dev-dependencies] criterion = "0.4" iai-callgrind.workspace = true diff --git a/crypto/benches/criterion_poseidon.rs b/crypto/benches/criterion_poseidon.rs index 72d194594..52b584ff1 100644 --- a/crypto/benches/criterion_poseidon.rs +++ b/crypto/benches/criterion_poseidon.rs @@ -1,22 +1,19 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use lambdaworks_benches::utils::{generate_random_elements, to_lambdaworks_vec}; 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::MontgomeryConfigStark252PrimeField; -use lambdaworks_math::field::fields::montgomery_backed_prime_fields::MontgomeryBackendPrimeField; +use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField; -type F = MontgomeryBackendPrimeField; -fn generate_points() -> Vec> { - let random_points = generate_random_elements(2); - to_lambdaworks_vec(&random_points) -} fn poseidon_benchmarks(c: &mut Criterion) { - let mut group = c.benchmark_group("Poseidon Benchmark".to_string()); - let points = generate_points(); - group.bench_function("Merkle hashing ", |bench| { - bench.iter(|| black_box(PoseidonCairoStark252::hash(&points[0], &points[1]))) + let x = FieldElement::::from_hex("0x123456").unwrap(); + let y = FieldElement::::from_hex("0x789101").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))) }); + } criterion_group!(poseidon, poseidon_benchmarks); criterion_main!(poseidon); From a488de1c9873b3252243f69fa143cd4194222fac Mon Sep 17 00:00:00 2001 From: Theprotocolwhisperer Date: Mon, 8 Jan 2024 13:15:25 -0300 Subject: [PATCH 6/8] (rebase): Fixing fmt error --- crypto/benches/criterion_poseidon.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crypto/benches/criterion_poseidon.rs b/crypto/benches/criterion_poseidon.rs index 52b584ff1..79574e7f7 100644 --- a/crypto/benches/criterion_poseidon.rs +++ b/crypto/benches/criterion_poseidon.rs @@ -9,11 +9,10 @@ fn poseidon_benchmarks(c: &mut Criterion) { let y = FieldElement::::from_hex("0x789101").unwrap(); let mut group = c.benchmark_group("Poseidon Benchmark"); - // Benchmark with black_box is 0.41% faster + // Benchmark with black_box is 0.41% faster group.bench_function("Hashing with black_box", |bench| { bench.iter(|| black_box(PoseidonCairoStark252::hash(&x, &y))) }); - } criterion_group!(poseidon, poseidon_benchmarks); criterion_main!(poseidon); From f29542bf2cc8e4af4c8310e2659916bfbed4b6ed Mon Sep 17 00:00:00 2001 From: Mauro Toscano <12560266+MauroToscano@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:50:43 -0300 Subject: [PATCH 7/8] Add missing newline --- crypto/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index cd1255c48..e3e886325 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -37,4 +37,4 @@ harness = false [[bench]] name= "criterion_poseidon" -harness=false \ No newline at end of file +harness=false From dc5583ebc670d9b4145d5ad54f42a6ee0566cc02 Mon Sep 17 00:00:00 2001 From: Mauro Toscano <12560266+MauroToscano@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:52:05 -0300 Subject: [PATCH 8/8] Undo removing of a newline --- crypto/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index e3e886325..78ccd9263 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -15,6 +15,7 @@ sha2 = { version = "0.10", default-features = false } # Optional serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true } rayon = { version = "1.8.0", optional = true } + [dev-dependencies] criterion = "0.4" iai-callgrind.workspace = true