Skip to content

Commit

Permalink
feat: poseidon_circomlib
Browse files Browse the repository at this point in the history
  • Loading branch information
themighty1 committed Nov 19, 2024
1 parent c10c915 commit 4797a20
Show file tree
Hide file tree
Showing 5 changed files with 693 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"crates/components/hmac-sha256",
"crates/components/hmac-sha256-circuits",
"crates/components/key-exchange",
"crates/components/poseidon-circomlib",
"crates/components/stream-cipher",
"crates/components/universal-hash",
"crates/core",
Expand Down Expand Up @@ -43,6 +44,7 @@ opt-level = 1
[workspace.dependencies]
notary-client = { path = "crates/notary/client" }
notary-server = { path = "crates/notary/server" }
poseidon-circomlib = { path = "crates/components/poseidon-circomlib" }
tls-server-fixture = { path = "crates/tls/server-fixture" }
tlsn-aead = { path = "crates/components/aead" }
tlsn-benches-browser-core = { path = "crates/benches/browser/core" }
Expand Down
20 changes: 20 additions & 0 deletions crates/components/poseidon-circomlib/Cargo.toml
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" }
183 changes: 183 additions & 0 deletions crates/components/poseidon-circomlib/src/lib.rs
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!(),
}
}
Loading

0 comments on commit 4797a20

Please sign in to comment.