-
Notifications
You must be signed in to change notification settings - Fork 119
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
feat: Poseidon2 implementation and availabe as Merkle Hash #708
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
246b0ea
generic poseidon2 impl
alxiong 33d0c4a
add bls12 s=2 param and tests
alxiong 4f3f784
use macro to derive instance
alxiong 8fa8817
add more parameters
alxiong a698a0f
impl HasherAlgorithm for Poseidon2
alxiong 6f1678a
add Poseidon2 benchmark
alxiong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# CHANGELOG | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
|
||
## 0.1.0 | ||
|
||
- Initial release with Poseidon2 Permutation (w/ BLS12-381, BN254 instances) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "jf-poseidon2" | ||
version = "0.1.0" | ||
description = "Poseidon2 algebraic hash functions implementation." | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
rust-version = { workspace = true } | ||
homepage = { workspace = true } | ||
documentation = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[dependencies] | ||
ark-bls12-381 = { workspace = true, optional = true } | ||
ark-bn254 = { workspace = true, optional = true } | ||
ark-ff = { workspace = true } | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! Poseidon2 Constants copied from <https://github.com/HorizenLabs/poseidon2/blob/main/plain_implementations/src/poseidon2/> | ||
|
||
use ark_ff::PrimeField; | ||
use hex::FromHex; | ||
|
||
#[cfg(feature = "bls12-381")] | ||
pub mod bls12_381; | ||
#[cfg(feature = "bn254")] | ||
pub mod bn254; | ||
|
||
#[inline] | ||
pub(crate) fn from_hex<F: PrimeField>(s: &str) -> F { | ||
F::from_be_bytes_mod_order(&<[u8; 32]>::from_hex(s).expect("Invalid HexStr")) | ||
} | ||
|
||
/// macros to derive instances that implements `trait Poseidon2Params` | ||
#[macro_export] | ||
macro_rules! define_poseidon2_params { | ||
( | ||
$struct_name:ident, | ||
$state_size:expr, | ||
$sbox_size:expr, | ||
$ext_rounds:expr, | ||
$int_rounds:expr, | ||
$rc_ext:ident, | ||
$rc_int:ident, | ||
$mat_diag_m_1:ident | ||
) => { | ||
/// Poseidon parameters for Bls12-381 scalar field, with | ||
/// - state size = $state_size | ||
/// - sbox size = $sbox_size | ||
/// - external rounds = $ext_rounds | ||
/// - internal rounds = $int_rounds | ||
pub struct $struct_name; | ||
|
||
impl Poseidon2Params<Fr, $state_size> for $struct_name { | ||
const T: usize = $state_size; | ||
const D: usize = $sbox_size; | ||
const EXT_ROUNDS: usize = $ext_rounds; | ||
const INT_ROUNDS: usize = $int_rounds; | ||
|
||
fn external_rc() -> &'static [[Fr; $state_size]] { | ||
&*$rc_ext | ||
} | ||
|
||
fn internal_rc() -> &'static [Fr] { | ||
&*$rc_int | ||
} | ||
|
||
fn internal_mat_diag_m_1() -> &'static [Fr; $state_size] { | ||
&$mat_diag_m_1 | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that we should have a CRHF impl (and maybe sponge as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's add in a separate PR. This is already getting big