Skip to content

Commit

Permalink
use newest hasher from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
John Petterson committed Jan 12, 2024
1 parent 713da17 commit e9ae2b5
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,46 @@ pub const PREFIX_CHALLENGE: u64 = 0u64;
pub const PREFIX_POINT: u64 = 1u64;
pub const PREFIX_SCALAR: u64 = 2u64;

/// There is two variants of haser used in upstream.
/// This is the POSEIDON_HASHER
/// https://github.com/DelphinusLab/zkWasm-host-circuits/blob/f0bae8b70c33941d6969635e4b1bba012441ea4d/src/host/poseidon.rs#L9-L17
/// There are three variants of haser used in upstream.
/// https://github.com/DelphinusLab/zkWasm-host-circuits/blob/e3a2eff4583b2fd8be7fc3e54f2789cbfbfd72d4/src/host/poseidon.rs#L9-L20
/// This function creates a hasher equivalent to the MERKLE_LEAF_HASHER.
/// ```text
/// We have two hasher here
/// 1. MERKLE_HASHER that is used for non sponge hash for hash two merkle siblings
/// 2. POSEIDON_HASHER thas is use for poseidon hash of data
/// // We have two hasher here
/// // 1. MERKLE_HASHER that is used for non sponge hash for hash two merkle siblings
/// // 2. POSEIDON_HASHER thas is use for poseidon hash of data
/// ```
///
/// ```rust,ignore
/// lazy_static::lazy_static! {
/// pub static ref POSEIDON_HASHER: poseidon::Poseidon<Fr, 9, 8> = Poseidon::<Fr, 9, 8>::new(8, 63);
/// pub static ref MERKLE_HASHER: poseidon::Poseidon<Fr, 3, 2> = Poseidon::<Fr, 3, 2>::new(8, 57);
/// pub static ref MERKLE_LEAF_HASHER: poseidon::Poseidon<Fr, 3, 2> = Poseidon::<Fr, 3, 2>::new(8, 57);
/// pub static ref POSEIDON_HASHER_SPEC: poseidon::Spec<Fr, 9, 8> = Spec::new(8, 63);
/// pub static ref MERKLE_HASHER_SPEC: poseidon::Spec<Fr, 3, 2> = Spec::new(8, 57);
/// pub static ref MERKLE_LEAF_HASHER_SPEC: poseidon::Spec<Fr, 3, 2> = Spec::new(8, 57);
/// }
/// ```
pub fn gen_poseidon_hasher() -> Poseidon<Fr, 9, 8> {
Poseidon::<Fr, 9, 8>::new(8, 63)
pub fn gen_poseidon_hasher() -> Poseidon<Fr, 3, 2> {
Poseidon::<Fr, 3, 2>::new(8, 57)
}

/// There is two variants of haser used in upstream.
/// This is the MERKLE_HASHER
/// https://github.com/DelphinusLab/zkWasm-host-circuits/blob/f0bae8b70c33941d6969635e4b1bba012441ea4d/src/host/poseidon.rs#L9-L17
/// There are three variants of haser used in upstream.
/// https://github.com/DelphinusLab/zkWasm-host-circuits/blob/e3a2eff4583b2fd8be7fc3e54f2789cbfbfd72d4/src/host/poseidon.rs#L9-L20
/// This function creates a hasher equivalent to the MERKLE_HASHER.
/// ```text
/// We have two hasher here
/// 1. MERKLE_HASHER that is used for non sponge hash for hash two merkle siblings
/// 2. POSEIDON_HASHER thas is use for poseidon hash of data
/// // We have two hasher here
/// // 1. MERKLE_HASHER that is used for non sponge hash for hash two merkle siblings
/// // 2. POSEIDON_HASHER thas is use for poseidon hash of data
/// ```
///
/// ```rust,ignore
/// lazy_static::lazy_static! {
/// pub static ref POSEIDON_HASHER: poseidon::Poseidon<Fr, 9, 8> = Poseidon::<Fr, 9, 8>::new(8, 63);
/// pub static ref MERKLE_HASHER: poseidon::Poseidon<Fr, 3, 2> = Poseidon::<Fr, 3, 2>::new(8, 57);
/// pub static ref MERKLE_LEAF_HASHER: poseidon::Poseidon<Fr, 3, 2> = Poseidon::<Fr, 3, 2>::new(8, 57);
/// pub static ref POSEIDON_HASHER_SPEC: poseidon::Spec<Fr, 9, 8> = Spec::new(8, 63);
/// pub static ref MERKLE_HASHER_SPEC: poseidon::Spec<Fr, 3, 2> = Spec::new(8, 57);
/// pub static ref MERKLE_LEAF_HASHER_SPEC: poseidon::Spec<Fr, 3, 2> = Spec::new(8, 57);
/// }
/// ```
pub fn gen_merkle_hasher() -> Poseidon<Fr, 3, 2> {
Expand Down Expand Up @@ -78,15 +82,39 @@ pub fn hash(data_to_hash: &[u8]) -> Result<<Fr as PrimeField>::Repr, Error> {

#[cfg(test)]
mod tests {
use ff::PrimeField;
use halo2_proofs::pairing::bn256::Fr;

#[test]
fn test_merkle_hash_zero() {
const ZERO_HASHER_SQUEEZE: &str =
"0x0ac6c5f29f5187473a70dfde3329ef18f01a4d84edb01e6c21813f629a6b5f50";
let mut hasher = super::gen_poseidon_hasher();
hasher.update(&[Fr::zero()]);
let result = hasher.squeeze();
println!("hash result is {:?}", result);
assert_eq!(result.to_string(), ZERO_HASHER_SQUEEZE);
}

#[test]
fn test_poseidon() {
fn test_poseidon_hash_zero() {
const ZERO_HASHER_SQUEEZE: &str =
"0x03f943aabd67cd7b72a539f3de686c3280c36c572be09f2b9193f5ef78761c6b"; //force the hasher is for fr field result.
"0x0ac6c5f29f5187473a70dfde3329ef18f01a4d84edb01e6c21813f629a6b5f50";
let mut hasher = super::gen_poseidon_hasher();
hasher.update(&[Fr::zero()]);
let result = hasher.squeeze();
println!("hash result is {:?}", result);
assert_eq!(result.to_string(), ZERO_HASHER_SQUEEZE);
}

#[test]
fn test_poseidon_hash_equivalent() {
let mut hasher = super::gen_poseidon_hasher();
hasher.update(&[Fr::zero()]);
let result = hasher.squeeze().to_repr();
println!("hash result is {:?}", result);

let result2 = super::hash(&[0; 32]).expect("Hash succeeded");
assert_eq!(result, result2);
}
}

0 comments on commit e9ae2b5

Please sign in to comment.