diff --git a/halo2_gadgets/Cargo.toml b/halo2_gadgets/Cargo.toml index ebf8dec62..565f02dba 100644 --- a/halo2_gadgets/Cargo.toml +++ b/halo2_gadgets/Cargo.toml @@ -40,6 +40,7 @@ plotters = { version = "0.3.0", default-features = false, optional = true } [dev-dependencies] criterion = "0.3" +halo2_poseidon = { version = "0.0", path = "../halo2_poseidon", default-features = false, features = ["test-dependencies"] } proptest = "1.0.0" [target.'cfg(unix)'.dev-dependencies] diff --git a/halo2_gadgets/src/poseidon.rs b/halo2_gadgets/src/poseidon.rs index 07ba0df0a..6538f330c 100644 --- a/halo2_gadgets/src/poseidon.rs +++ b/halo2_gadgets/src/poseidon.rs @@ -148,13 +148,7 @@ impl< pub fn new(chip: PoseidonChip, mut layouter: impl Layouter) -> Result { chip.initial_state(&mut layouter).map(|state| Sponge { chip, - mode: Absorbing( - (0..RATE) - .map(|_| None) - .collect::>() - .try_into() - .unwrap(), - ), + mode: Absorbing::init_empty(), state, _marker: PhantomData::default(), }) diff --git a/halo2_gadgets/src/poseidon/pow5.rs b/halo2_gadgets/src/poseidon/pow5.rs index 704bda1da..61ccd2415 100644 --- a/halo2_gadgets/src/poseidon/pow5.rs +++ b/halo2_gadgets/src/poseidon/pow5.rs @@ -341,7 +341,7 @@ impl< // Load the input into this region. let load_input_word = |i: usize| { - let (cell, value) = match input.0[i].clone() { + let (cell, value) = match input.get(i).expect("i in range").clone() { Some(PaddedWord::Message(word)) => (word.cell(), word.value().copied()), Some(PaddedWord::Padding(padding_value)) => { let cell = region @@ -394,14 +394,7 @@ impl< } fn get_output(state: &State) -> Squeezing { - Squeezing( - state[..RATE] - .iter() - .map(|word| Some(word.clone())) - .collect::>() - .try_into() - .unwrap(), - ) + Squeezing::init_with(state[..RATE].try_into().unwrap()) } } diff --git a/halo2_poseidon/Cargo.toml b/halo2_poseidon/Cargo.toml index 25b7784ea..2831342e1 100644 --- a/halo2_poseidon/Cargo.toml +++ b/halo2_poseidon/Cargo.toml @@ -19,3 +19,6 @@ bitvec = "1" ff = "0.13" group = "0.13" pasta_curves = "0.5" + +[features] +test-dependencies = [] diff --git a/halo2_poseidon/src/lib.rs b/halo2_poseidon/src/lib.rs index 6c8d133dc..be887170d 100644 --- a/halo2_poseidon/src/lib.rs +++ b/halo2_poseidon/src/lib.rs @@ -12,8 +12,8 @@ pub(crate) mod fq; pub(crate) mod grain; pub(crate) mod mds; -#[cfg(test)] -pub(crate) mod test_vectors; +#[cfg(any(test, feature = "test-dependencies"))] +pub mod test_vectors; mod p128pow5t3; pub use p128pow5t3::P128Pow5T3; @@ -21,7 +21,7 @@ pub use p128pow5t3::P128Pow5T3; use grain::SboxType; /// The type used to hold permutation state. -pub(crate) type State = [F; T]; +pub type State = [F; T]; /// The type used to hold sponge rate. pub(crate) type SpongeRate = [Option; RATE]; @@ -175,7 +175,17 @@ impl SpongeMode for Absorbing {} impl SpongeMode for Squeezing {} impl Absorbing { - pub(crate) fn init_with(val: F) -> Self { + pub fn init_empty() -> Self { + Self( + (0..RATE) + .map(|_| None) + .collect::>() + .try_into() + .unwrap(), + ) + } + + pub fn init_with(val: F) -> Self { Self( iter::once(Some(val)) .chain((1..RATE).map(|_| None)) @@ -184,6 +194,22 @@ impl Absorbing { .unwrap(), ) } + + pub fn get(&self, n: usize) -> Option> { + self.0.iter().nth(n).map(|x| x.as_ref()) + } +} + +impl Squeezing { + pub fn init_with(vals: [F; RATE]) -> Self { + Self( + vals.into_iter() + .map(Some) + .collect::>() + .try_into() + .unwrap(), + ) + } } /// A Poseidon sponge.