From 82b7fabd9d66f3c9581b0271365d8255f05c511c Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Fri, 13 May 2022 11:48:53 +0530 Subject: [PATCH] absorb running instance separately (for clarity) (#52) --- src/circuit.rs | 4 ++-- src/gadgets/r1cs.rs | 5 +++-- src/poseidon.rs | 22 ++++++++++++---------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/circuit.rs b/src/circuit.rs index 619a943..6c1f474 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -203,7 +203,7 @@ where ) -> Result<(AllocatedRelaxedR1CSInstance, AllocatedBit), SynthesisError> { // Check that u.x[0] = Hash(params, U, i, z0, zi) let mut ro: PoseidonROGadget = PoseidonROGadget::new(self.poseidon_constants.clone()); - ro.absorb(params); + ro.absorb(params.clone()); ro.absorb(i); ro.absorb(z_0); ro.absorb(z_i); @@ -220,7 +220,7 @@ where // Run NIFS Verifier let U_fold = U.fold_with_r1cs( cs.namespace(|| "compute fold of U and u"), - hash, // hash of the (params, U, i, z0, zi) + params, u, T, self.poseidon_constants.clone(), diff --git a/src/gadgets/r1cs.rs b/src/gadgets/r1cs.rs index 2b4ce43..7cab720 100644 --- a/src/gadgets/r1cs.rs +++ b/src/gadgets/r1cs.rs @@ -216,7 +216,7 @@ where pub fn fold_with_r1cs::Base>>( &self, mut cs: CS, - hash: AllocatedNum, // hash of (params, running_instance, i, z0, zi) + params: AllocatedNum, // hash of R1CSShape of F' u: AllocatedR1CSInstance, T: AllocatedPoint, poseidon_constants: NovaPoseidonConstants, @@ -225,7 +225,8 @@ where ) -> Result, SynthesisError> { // Compute r: let mut ro: PoseidonROGadget = PoseidonROGadget::new(poseidon_constants); - ro.absorb(hash); + ro.absorb(params); + self.absorb_in_ro(cs.namespace(|| "absorb running instance"), &mut ro)?; u.absorb_in_ro(&mut ro); ro.absorb(T.x.clone()); ro.absorb(T.y.clone()); diff --git a/src/poseidon.rs b/src/poseidon.rs index 0725e52..92aa8f4 100644 --- a/src/poseidon.rs +++ b/src/poseidon.rs @@ -12,7 +12,7 @@ use bellperson::{ }; use core::marker::PhantomData; use ff::{PrimeField, PrimeFieldBits}; -use generic_array::typenum::{U27, U9}; +use generic_array::typenum::{U27, U32}; use neptune::{ circuit::poseidon_hash, poseidon::{Poseidon, PoseidonConstants}, @@ -25,8 +25,8 @@ pub struct NovaPoseidonConstants where Scalar: PrimeField, { - constants9: PoseidonConstants, constants27: PoseidonConstants, + constants32: PoseidonConstants, } impl HashFuncConstantsTrait for NovaPoseidonConstants @@ -36,11 +36,11 @@ where /// Generate Poseidon constants for the arities that Nova uses #[allow(clippy::new_without_default)] fn new() -> Self { - let constants9 = PoseidonConstants::::new_with_strength(Strength::Strengthened); let constants27 = PoseidonConstants::::new_with_strength(Strength::Strengthened); + let constants32 = PoseidonConstants::::new_with_strength(Strength::Strengthened); Self { - constants9, constants27, + constants32, } } } @@ -65,10 +65,12 @@ where { fn hash_inner(&self) -> Base { match self.state.len() { - 9 => Poseidon::::new_with_preimage(&self.state, &self.constants.constants9).hash(), 27 => { Poseidon::::new_with_preimage(&self.state, &self.constants.constants27).hash() } + 32 => { + Poseidon::::new_with_preimage(&self.state, &self.constants.constants32).hash() + } _ => { panic!( "Number of elements in the RO state does not match any of the arities used in Nova: {:?}", @@ -169,16 +171,16 @@ where CS: ConstraintSystem, { let out = match self.state.len() { - 9 => poseidon_hash( - cs.namespace(|| "Posideon hash"), - self.state.clone(), - &self.constants.constants9, - )?, 27 => poseidon_hash( cs.namespace(|| "Poseidon hash"), self.state.clone(), &self.constants.constants27, )?, + 32 => poseidon_hash( + cs.namespace(|| "Posideon hash"), + self.state.clone(), + &self.constants.constants32, + )?, _ => { panic!( "Number of elements in the RO state does not match any of the arities used in Nova: {}",