diff --git a/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/lib.rs b/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/lib.rs index 952c4498d84..43ee6a9ddd2 100644 --- a/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/lib.rs +++ b/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/lib.rs @@ -13,10 +13,7 @@ mod schnorr; use ark_ec::AffineRepr; pub use embedded_curve_ops::{embedded_curve_add, multi_scalar_mul}; pub use generator::generators::derive_generators; -pub use poseidon2::{ - field_from_hex, poseidon2_permutation, poseidon_hash, Poseidon2Config, Poseidon2Sponge, - POSEIDON2_CONFIG, -}; +pub use poseidon2::{field_from_hex, poseidon2_permutation, Poseidon2Config, POSEIDON2_CONFIG}; // Temporary hack, this ensure that we always use a bn254 field here // without polluting the feature flags of the `acir_field` crate. diff --git a/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/poseidon2.rs b/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/poseidon2.rs index 64823e37029..dd3e8b725c2 100644 --- a/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/poseidon2.rs +++ b/noir/noir-repo/acvm-repo/bn254_blackbox_solver/src/poseidon2.rs @@ -543,75 +543,6 @@ impl<'a> Poseidon2<'a> { } } -/// Performs a poseidon hash with a sponge construction equivalent to the one in poseidon2.nr -pub fn poseidon_hash(inputs: &[FieldElement]) -> Result { - let two_pow_64 = 18446744073709551616_u128.into(); - let iv = FieldElement::from(inputs.len()) * two_pow_64; - let mut sponge = Poseidon2Sponge::new(iv, 3); - for input in inputs.iter() { - sponge.absorb(*input)?; - } - sponge.squeeze() -} - -pub struct Poseidon2Sponge<'a> { - rate: usize, - poseidon: Poseidon2<'a>, - squeezed: bool, - cache: Vec, - state: Vec, -} - -impl<'a> Poseidon2Sponge<'a> { - pub fn new(iv: FieldElement, rate: usize) -> Poseidon2Sponge<'a> { - let mut result = Poseidon2Sponge { - cache: Vec::with_capacity(rate), - state: vec![FieldElement::zero(); rate + 1], - squeezed: false, - rate, - poseidon: Poseidon2::new(), - }; - result.state[rate] = iv; - result - } - - fn perform_duplex(&mut self) -> Result<(), BlackBoxResolutionError> { - // zero-pad the cache - for _ in self.cache.len()..self.rate { - self.cache.push(FieldElement::zero()); - } - // add the cache into sponge state - for i in 0..self.rate { - self.state[i] += self.cache[i]; - } - self.state = self.poseidon.permutation(&self.state, 4)?; - Ok(()) - } - - pub fn absorb(&mut self, input: FieldElement) -> Result<(), BlackBoxResolutionError> { - assert!(!self.squeezed); - if self.cache.len() == self.rate { - // If we're absorbing, and the cache is full, apply the sponge permutation to compress the cache - self.perform_duplex()?; - self.cache = vec![input]; - } else { - // If we're absorbing, and the cache is not full, add the input into the cache - self.cache.push(input); - } - Ok(()) - } - - pub fn squeeze(&mut self) -> Result { - assert!(!self.squeezed); - // If we're in absorb mode, apply sponge permutation to compress the cache. - self.perform_duplex()?; - self.squeezed = true; - - // Pop one item off the top of the permutation and return it. - Ok(self.state[0]) - } -} - #[cfg(test)] mod test { use acir::AcirField; @@ -631,19 +562,4 @@ mod test { ]; assert_eq!(result, expected_result); } - - #[test] - fn hash_smoke_test() { - let fields = [ - FieldElement::from(1u128), - FieldElement::from(2u128), - FieldElement::from(3u128), - FieldElement::from(4u128), - ]; - let result = super::poseidon_hash(&fields).expect("should hash successfully"); - assert_eq!( - result, - field_from_hex("130bf204a32cac1f0ace56c78b731aa3809f06df2731ebcf6b3464a15788b1b9"), - ); - } }