-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #708 from EspressoSystems/ax/poseidon2
feat: Poseidon2 implementation and availabe as Merkle Hash
- Loading branch information
Showing
14 changed files
with
970 additions
and
11 deletions.
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.