Skip to content

Commit

Permalink
add Poseidon2 benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
alxiong committed Dec 12, 2024
1 parent a698a0f commit 6f1678a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.*.sw*
.DS_Store
.idea
/target
**/target
cargo-system-config.toml
Cargo.lock
*.org
Expand Down
2 changes: 1 addition & 1 deletion poseidon2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 0.1.0

- Initial release.
- Initial release with Poseidon2 Permutation (w/ BLS12-381, BN254 instances)
7 changes: 7 additions & 0 deletions poseidon2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ ark-std = { workspace = true }
hex = "0.4.3"
lazy_static = "1.5.0"

[dev-dependencies]
criterion = "0.5.1"

[features]
default = ["bls12-381", "bn254"]
# curve-named features contains constants for scalar fields of that curve
bls12-381 = ["dep:ark-bls12-381"]
bn254 = ["dep:ark-bn254"]

[[bench]]
name = "p2_native"
harness = false
98 changes: 98 additions & 0 deletions poseidon2/benches/p2_native.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//! Benchmark for native speed of Poseidon2
//! `cargo bench --bench p2_native`
#[macro_use]
extern crate criterion;
use std::time::Duration;

use ark_std::{test_rng, UniformRand};
use criterion::Criterion;
use jf_poseidon2::{
constants::{
bls12_381::{Poseidon2ParamsBls2, Poseidon2ParamsBls3},
bn254::Poseidon2ParamsBn3,
},
Poseidon2,
};

// BLS12-381 scalar field, state size = 2
fn bls2(c: &mut Criterion) {
let mut group = c.benchmark_group("Poseidon2 over (Bls12_381::Fr, t=2)");
group.sample_size(10).measurement_time(Duration::new(20, 0));
type Fr = ark_bls12_381::Fr;
let rng = &mut test_rng();

group.bench_function("1k iter", |b| {
b.iter(|| {
let mut input = [Fr::rand(rng), Fr::rand(rng)];
for _ in 0..1000 {
Poseidon2::permute_mut::<Poseidon2ParamsBls2, 2>(&mut input);
}
})
});
group.bench_function("100k iter", |b| {
b.iter(|| {
let mut input = [Fr::rand(rng), Fr::rand(rng)];
for _ in 0..100_000 {
Poseidon2::permute_mut::<Poseidon2ParamsBls2, 2>(&mut input);
}
})
});
group.finish();
}

// BLS12-381 scalar field, state size = 3
fn bls3(c: &mut Criterion) {
let mut group = c.benchmark_group("Poseidon2 over (Bls12_381::Fr, t=3)");
group.sample_size(10).measurement_time(Duration::new(20, 0));
type Fr = ark_bls12_381::Fr;
let rng = &mut test_rng();

group.bench_function("1k iter", |b| {
b.iter(|| {
let mut input = [Fr::rand(rng), Fr::rand(rng), Fr::rand(rng)];
for _ in 0..1000 {
Poseidon2::permute_mut::<Poseidon2ParamsBls3, 3>(&mut input);
}
})
});
group.bench_function("100k iter", |b| {
b.iter(|| {
let mut input = [Fr::rand(rng), Fr::rand(rng), Fr::rand(rng)];
for _ in 0..100_000 {
Poseidon2::permute_mut::<Poseidon2ParamsBls3, 3>(&mut input);
}
})
});
group.finish();
}

// BN254 scalar field, state size = 3
fn bn3(c: &mut Criterion) {
let mut group = c.benchmark_group("Poseidon2 over (Bn254::Fr, t=3)");
group.sample_size(10).measurement_time(Duration::new(20, 0));
type Fr = ark_bn254::Fr;
let rng = &mut test_rng();

group.bench_function("1k iter", |b| {
b.iter(|| {
let mut input = [Fr::rand(rng), Fr::rand(rng), Fr::rand(rng)];
for _ in 0..1000 {
Poseidon2::permute_mut::<Poseidon2ParamsBn3, 3>(&mut input);
}
})
});
group.bench_function("100k iter", |b| {
b.iter(|| {
let mut input = [Fr::rand(rng), Fr::rand(rng), Fr::rand(rng)];
for _ in 0..100_000 {
Poseidon2::permute_mut::<Poseidon2ParamsBn3, 3>(&mut input);
}
})
});

group.finish();
}

criterion_group!(benches, bls2, bls3, bn3);

criterion_main!(benches);
5 changes: 5 additions & 0 deletions poseidon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

//! The Poseidon2 permutation.
//!
//! # Available instances
//! We have implemented Poseidon2 instances: (field, state_size)
//! - (Bls12_381::Fr, 2), (Bls12_381::Fr, 3)
//! - (Bn254::Fr, 3)
//!
//! This implementation was based upon the following resources:
//! - https://github.com/HorizenLabs/poseidon2/blob/main/plain_implementations/src/poseidon2/poseidon2.rs
//! - https://eprint.iacr.org/2023/323.pdf
Expand Down

0 comments on commit 6f1678a

Please sign in to comment.