From 5cb838f1a254ba1499af5edaa8100fe6c92e3e3c Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 1 Dec 2021 23:52:25 +0000 Subject: [PATCH] circuit: Remove `Copy` impl from `poseidon::pow5::StateWord` We will be making it a newtype around `halo2::circuit::AssignedCell`, which does not impl `Copy`. --- src/circuit/gadget/poseidon.rs | 21 +++++++++++++++++---- src/circuit/gadget/poseidon/pow5.rs | 6 +++--- src/primitives/poseidon.rs | 9 +++++++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/circuit/gadget/poseidon.rs b/src/circuit/gadget/poseidon.rs index 96e5917d..dba40b5d 100644 --- a/src/circuit/gadget/poseidon.rs +++ b/src/circuit/gadget/poseidon.rs @@ -1,6 +1,7 @@ //! Gadget and chips for the Poseidon algebraic hash function. use std::array; +use std::convert::TryInto; use std::fmt; use halo2::{ @@ -20,7 +21,7 @@ pub trait PoseidonInstructions, const T: usize, Chip { /// Variable representing the word over which the Poseidon permutation operates. - type Word: Copy + fmt::Debug + From> + Into>; + type Word: Clone + fmt::Debug + From> + Into>; /// Applies the Poseidon permutation to the given state. fn permute( @@ -82,7 +83,7 @@ impl< { /// The word contained in this gadget. pub fn inner(&self) -> PoseidonChip::Word { - self.inner + self.inner.clone() } /// Construct a [`Word`] gadget from the inner word. @@ -144,7 +145,13 @@ impl< chip.initial_state(&mut layouter, &domain) .map(|state| Duplex { chip, - sponge: Sponge::Absorbing([None; RATE]), + sponge: Sponge::Absorbing( + (0..RATE) + .map(|_| None) + .collect::>() + .try_into() + .unwrap(), + ), state, domain, }) @@ -205,7 +212,13 @@ impl< } // We've already squeezed out all available elements - self.sponge = Sponge::Absorbing([None; RATE]); + self.sponge = Sponge::Absorbing( + (0..RATE) + .map(|_| None) + .collect::>() + .try_into() + .unwrap(), + ); } } } diff --git a/src/circuit/gadget/poseidon/pow5.rs b/src/circuit/gadget/poseidon/pow5.rs index f6829791..ba314fe3 100644 --- a/src/circuit/gadget/poseidon/pow5.rs +++ b/src/circuit/gadget/poseidon/pow5.rs @@ -341,7 +341,7 @@ impl, const WIDTH: usize, const RATE: usize // Load the input and padding into this region. let load_input_word = |i: usize| { - let (constraint_var, value) = match (input[i], padding_values[i]) { + let (constraint_var, value) = match (input[i].clone(), padding_values[i]) { (Some(word), None) => (word.var, word.value), (None, Some(padding_value)) => { let padding_var = region.assign_fixed( @@ -395,7 +395,7 @@ impl, const WIDTH: usize, const RATE: usize fn get_output(state: &State) -> SpongeState { state[..RATE] .iter() - .map(|word| Some(*word)) + .map(|word| Some(word.clone())) .collect::>() .try_into() .unwrap() @@ -403,7 +403,7 @@ impl, const WIDTH: usize, const RATE: usize } /// A word in the Poseidon state. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Debug)] pub struct StateWord { var: Cell, value: Option, diff --git a/src/primitives/poseidon.rs b/src/primitives/poseidon.rs index 8078fac2..1afe5cbe 100644 --- a/src/primitives/poseidon.rs +++ b/src/primitives/poseidon.rs @@ -1,6 +1,7 @@ //! The Poseidon algebraic hash function. use std::array; +use std::convert::TryInto; use std::fmt; use std::iter; use std::marker::PhantomData; @@ -147,9 +148,13 @@ pub(crate) enum Sponge { Squeezing(SpongeState), } -impl Sponge { +impl Sponge { pub(crate) fn absorb(val: F) -> Self { - let mut input = [None; RATE]; + let mut input: [Option; RATE] = (0..RATE) + .map(|_| None) + .collect::>() + .try_into() + .unwrap(); input[0] = Some(val); Sponge::Absorbing(input) }