Skip to content

Commit 62bf965

Browse files
committed
WIP
1 parent eab55d7 commit 62bf965

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

halo2_gadgets/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ plotters = { version = "0.3.0", default-features = false, optional = true }
4040

4141
[dev-dependencies]
4242
criterion = "0.3"
43+
halo2_poseidon = { version = "0.0", path = "../halo2_poseidon", default-features = false, features = ["test-dependencies"] }
4344
proptest = "1.0.0"
4445

4546
[target.'cfg(unix)'.dev-dependencies]

halo2_gadgets/src/poseidon.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,7 @@ impl<
148148
pub fn new(chip: PoseidonChip, mut layouter: impl Layouter<F>) -> Result<Self, Error> {
149149
chip.initial_state(&mut layouter).map(|state| Sponge {
150150
chip,
151-
mode: Absorbing(
152-
(0..RATE)
153-
.map(|_| None)
154-
.collect::<Vec<_>>()
155-
.try_into()
156-
.unwrap(),
157-
),
151+
mode: Absorbing::init_empty(),
158152
state,
159153
_marker: PhantomData::default(),
160154
})

halo2_gadgets/src/poseidon/pow5.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<
341341

342342
// Load the input into this region.
343343
let load_input_word = |i: usize| {
344-
let (cell, value) = match input.0[i].clone() {
344+
let (cell, value) = match input.get(i).expect("i in range").clone() {
345345
Some(PaddedWord::Message(word)) => (word.cell(), word.value().copied()),
346346
Some(PaddedWord::Padding(padding_value)) => {
347347
let cell = region
@@ -394,14 +394,7 @@ impl<
394394
}
395395

396396
fn get_output(state: &State<Self::Word, WIDTH>) -> Squeezing<Self::Word, RATE> {
397-
Squeezing(
398-
state[..RATE]
399-
.iter()
400-
.map(|word| Some(word.clone()))
401-
.collect::<Vec<_>>()
402-
.try_into()
403-
.unwrap(),
404-
)
397+
Squeezing::init_with(state[..RATE].try_into().unwrap())
405398
}
406399
}
407400

halo2_poseidon/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ bitvec = "1"
1919
ff = "0.13"
2020
group = "0.13"
2121
pasta_curves = "0.5"
22+
23+
[features]
24+
test-dependencies = []

halo2_poseidon/src/lib.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ pub(crate) mod fq;
1212
pub(crate) mod grain;
1313
pub(crate) mod mds;
1414

15-
#[cfg(test)]
16-
pub(crate) mod test_vectors;
15+
#[cfg(any(test, feature = "test-dependencies"))]
16+
pub mod test_vectors;
1717

1818
mod p128pow5t3;
1919
pub use p128pow5t3::P128Pow5T3;
2020

2121
use grain::SboxType;
2222

2323
/// The type used to hold permutation state.
24-
pub(crate) type State<F, const T: usize> = [F; T];
24+
pub type State<F, const T: usize> = [F; T];
2525

2626
/// The type used to hold sponge rate.
2727
pub(crate) type SpongeRate<F, const RATE: usize> = [Option<F>; RATE];
@@ -175,7 +175,17 @@ impl<F, const RATE: usize> SpongeMode for Absorbing<F, RATE> {}
175175
impl<F, const RATE: usize> SpongeMode for Squeezing<F, RATE> {}
176176

177177
impl<F: fmt::Debug, const RATE: usize> Absorbing<F, RATE> {
178-
pub(crate) fn init_with(val: F) -> Self {
178+
pub fn init_empty() -> Self {
179+
Self(
180+
(0..RATE)
181+
.map(|_| None)
182+
.collect::<Vec<_>>()
183+
.try_into()
184+
.unwrap(),
185+
)
186+
}
187+
188+
pub fn init_with(val: F) -> Self {
179189
Self(
180190
iter::once(Some(val))
181191
.chain((1..RATE).map(|_| None))
@@ -184,6 +194,22 @@ impl<F: fmt::Debug, const RATE: usize> Absorbing<F, RATE> {
184194
.unwrap(),
185195
)
186196
}
197+
198+
pub fn get(&self, n: usize) -> Option<Option<&F>> {
199+
self.0.iter().nth(n).map(|x| x.as_ref())
200+
}
201+
}
202+
203+
impl<F: fmt::Debug, const RATE: usize> Squeezing<F, RATE> {
204+
pub fn init_with(vals: [F; RATE]) -> Self {
205+
Self(
206+
vals.into_iter()
207+
.map(Some)
208+
.collect::<Vec<_>>()
209+
.try_into()
210+
.unwrap(),
211+
)
212+
}
187213
}
188214

189215
/// A Poseidon sponge.

0 commit comments

Comments
 (0)