-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c10c915
commit 4797a20
Showing
5 changed files
with
693 additions
and
0 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
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,20 @@ | ||
[package] | ||
name = "poseidon-circomlib" | ||
authors = ["TLSNotary Team"] | ||
description = "Poseidon permutation over the bn256 curve compatible with iden3's circomlib" | ||
categories = ["cryptography"] | ||
license = "MIT OR Apache-2.0" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "poseidon_circomlib" | ||
|
||
[dependencies] | ||
ff = { version = "0.13" } | ||
poseidon-base = { git = "https://github.com/scroll-tech/poseidon-circuit", rev = "b978cee" } | ||
|
||
[dev-dependencies] | ||
lazy_static = { version = "1.4" } | ||
num-bigint = { version = "0.4" } | ||
num-traits = { version = "0.2" } |
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,183 @@ | ||
//! Poseidon permutation over the bn256 curve compatible with iden3's circomlib. | ||
use poseidon_base::primitives::{permute, Spec}; | ||
|
||
use crate::spec::{ | ||
Spec1, Spec10, Spec11, Spec12, Spec13, Spec14, Spec15, Spec16, Spec2, Spec3, Spec4, Spec5, | ||
Spec6, Spec7, Spec8, Spec9, | ||
}; | ||
|
||
mod spec; | ||
|
||
pub use poseidon_base::primitives::bn256::Fp as F; | ||
|
||
/// Hashes the provided `input` field elements returning the digest. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the provided `input` length is larger than 16. | ||
pub fn hash(input: &[F]) -> F { | ||
match input.len() { | ||
1 => { | ||
let mut state = [F::zero(); 2]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec1::constants(); | ||
permute::<F, Spec1<F>, 2, 1>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
2 => { | ||
let mut state = [F::zero(); 3]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec2::constants(); | ||
permute::<F, Spec2<F>, 3, 2>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
3 => { | ||
let mut state = [F::zero(); 4]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec3::constants(); | ||
permute::<F, Spec3<F>, 4, 3>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
4 => { | ||
let mut state = [F::zero(); 5]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec4::constants(); | ||
permute::<F, Spec4<F>, 5, 4>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
5 => { | ||
let mut state = [F::zero(); 6]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec5::constants(); | ||
permute::<F, Spec5<F>, 6, 5>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
6 => { | ||
let mut state = [F::zero(); 7]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec6::constants(); | ||
permute::<F, Spec6<F>, 7, 6>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
7 => { | ||
let mut state = [F::zero(); 8]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec7::constants(); | ||
permute::<F, Spec7<F>, 8, 7>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
8 => { | ||
let mut state = [F::zero(); 9]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec8::constants(); | ||
permute::<F, Spec8<F>, 9, 8>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
9 => { | ||
let mut state = [F::zero(); 10]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec9::constants(); | ||
permute::<F, Spec9<F>, 10, 9>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
10 => { | ||
let mut state = [F::zero(); 11]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec10::constants(); | ||
permute::<F, Spec10<F>, 11, 10>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
11 => { | ||
let mut state = [F::zero(); 12]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec11::constants(); | ||
permute::<F, Spec11<F>, 12, 11>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
12 => { | ||
let mut state = [F::zero(); 13]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec12::constants(); | ||
permute::<F, Spec12<F>, 13, 12>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
13 => { | ||
let mut state = [F::zero(); 14]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec13::constants(); | ||
permute::<F, Spec13<F>, 14, 13>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
14 => { | ||
let mut state = [F::zero(); 15]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec14::constants(); | ||
permute::<F, Spec14<F>, 15, 14>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
15 => { | ||
let mut state = [F::zero(); 16]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec15::constants(); | ||
permute::<F, Spec15<F>, 16, 15>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
16 => { | ||
let mut state = [F::zero(); 17]; | ||
// The first element of the state is initialized to 0. | ||
state[1..].copy_from_slice(input); | ||
|
||
let (round_constants, mds, _) = Spec16::constants(); | ||
permute::<F, Spec16<F>, 17, 16>(&mut state, &mds, &round_constants); | ||
|
||
state[0] | ||
} | ||
_ => unimplemented!(), | ||
} | ||
} |
Oops, something went wrong.