Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Dec 13, 2024
1 parent eab55d7 commit 62bf965
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions halo2_gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 1 addition & 7 deletions halo2_gadgets/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,7 @@ impl<
pub fn new(chip: PoseidonChip, mut layouter: impl Layouter<F>) -> Result<Self, Error> {
chip.initial_state(&mut layouter).map(|state| Sponge {
chip,
mode: Absorbing(
(0..RATE)
.map(|_| None)
.collect::<Vec<_>>()
.try_into()
.unwrap(),
),
mode: Absorbing::init_empty(),
state,
_marker: PhantomData::default(),
})
Expand Down
11 changes: 2 additions & 9 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -394,14 +394,7 @@ impl<
}

fn get_output(state: &State<Self::Word, WIDTH>) -> Squeezing<Self::Word, RATE> {
Squeezing(
state[..RATE]
.iter()
.map(|word| Some(word.clone()))
.collect::<Vec<_>>()
.try_into()
.unwrap(),
)
Squeezing::init_with(state[..RATE].try_into().unwrap())

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the trait bound `[poseidon::pow5::StateWord<F>; RATE]: std::convert::TryFrom<&[poseidon::pow5::StateWord<F>]>` is not satisfied

error[E0277]: the trait bound `[poseidon::pow5::StateWord<F>; RATE]: std::convert::TryFrom<&[poseidon::pow5::StateWord<F>]>` is not satisfied --> halo2_gadgets/src/poseidon/pow5.rs:397:44 | 397 | Squeezing::init_with(state[..RATE].try_into().unwrap()) | ^^^^^^^^ the trait `std::convert::TryFrom<&[poseidon::pow5::StateWord<F>]>` is not implemented for `[poseidon::pow5::StateWord<F>; RATE]` | = help: the following other types implement trait `std::convert::TryFrom<T>`: `&[T; N]` implements `std::convert::TryFrom<&[T]>` `&mut [T; N]` implements `std::convert::TryFrom<&mut [T]>` `[T; N]` implements `std::convert::TryFrom<&[T]>` `[T; N]` implements `std::convert::TryFrom<&mut [T]>` `[T; N]` implements `std::convert::TryFrom<std::vec::Vec<T, A>>` = note: required for `&[poseidon::pow5::StateWord<F>]` to implement `std::convert::TryInto<[poseidon::pow5::StateWord<F>; RATE]>`

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the trait bound `[poseidon::pow5::StateWord<F>; RATE]: std::convert::TryFrom<&[poseidon::pow5::StateWord<F>]>` is not satisfied

error[E0277]: the trait bound `[poseidon::pow5::StateWord<F>; RATE]: std::convert::TryFrom<&[poseidon::pow5::StateWord<F>]>` is not satisfied --> halo2_gadgets/src/poseidon/pow5.rs:397:44 | 397 | Squeezing::init_with(state[..RATE].try_into().unwrap()) | ^^^^^^^^ the trait `std::convert::TryFrom<&[poseidon::pow5::StateWord<F>]>` is not implemented for `[poseidon::pow5::StateWord<F>; RATE]` | = help: the following other types implement trait `std::convert::TryFrom<T>`: `&[T; N]` implements `std::convert::TryFrom<&[T]>` `&mut [T; N]` implements `std::convert::TryFrom<&mut [T]>` `[T; N]` implements `std::convert::TryFrom<&[T]>` `[T; N]` implements `std::convert::TryFrom<&mut [T]>` `[T; N]` implements `std::convert::TryFrom<std::vec::Vec<T, A>>` = note: required for `&[poseidon::pow5::StateWord<F>]` to implement `std::convert::TryInto<[poseidon::pow5::StateWord<F>; RATE]>`

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Bitrot check

TryFrom<&[pow5::StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Book tests

TryFrom<&[StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Book tests

TryFrom<&[StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Bitrot check

TryFrom<&[pow5::StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Book tests

TryFrom<&[StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Book tests

TryFrom<&[StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-wasi

TryFrom<&[pow5::StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-wasi

TryFrom<&[pow5::StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-wasi

TryFrom<&[pow5::StateWord<F>]>` is not satisfied

Check failure on line 397 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-wasi

TryFrom<&[pow5::StateWord<F>]>` is not satisfied
}
}

Expand Down
3 changes: 3 additions & 0 deletions halo2_poseidon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ bitvec = "1"
ff = "0.13"
group = "0.13"
pasta_curves = "0.5"

[features]
test-dependencies = []
34 changes: 30 additions & 4 deletions halo2_poseidon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ 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;

use grain::SboxType;

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

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

impl<F: fmt::Debug, const RATE: usize> Absorbing<F, RATE> {
pub(crate) fn init_with(val: F) -> Self {
pub fn init_empty() -> Self {
Self(
(0..RATE)
.map(|_| None)
.collect::<Vec<_>>()
.try_into()
.unwrap(),
)
}

pub fn init_with(val: F) -> Self {
Self(
iter::once(Some(val))
.chain((1..RATE).map(|_| None))
Expand All @@ -184,6 +194,22 @@ impl<F: fmt::Debug, const RATE: usize> Absorbing<F, RATE> {
.unwrap(),
)
}

pub fn get(&self, n: usize) -> Option<Option<&F>> {
self.0.iter().nth(n).map(|x| x.as_ref())

Check warning on line 199 in halo2_poseidon/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

called `.iter().nth()` on a slice

warning: called `.iter().nth()` on a slice --> halo2_poseidon/src/lib.rs:199:9 | 199 | self.0.iter().nth(n).map(|x| x.as_ref()) | ^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth = note: `-W clippy::iter-nth` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::iter_nth)]` help: `get` is equivalent but more concise | 199 | self.0.get(n).map(|x| x.as_ref()) | ~~~

Check warning on line 199 in halo2_poseidon/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

called `.iter().nth()` on a slice

warning: called `.iter().nth()` on a slice --> halo2_poseidon/src/lib.rs:199:9 | 199 | self.0.iter().nth(n).map(|x| x.as_ref()) | ^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth = note: `-W clippy::iter-nth` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::iter_nth)]` help: `get` is equivalent but more concise | 199 | self.0.get(n).map(|x| x.as_ref()) | ~~~
}
}

impl<F: fmt::Debug, const RATE: usize> Squeezing<F, RATE> {
pub fn init_with(vals: [F; RATE]) -> Self {
Self(
vals.into_iter()
.map(Some)
.collect::<Vec<_>>()
.try_into()
.unwrap(),
)
}
}

/// A Poseidon sponge.
Expand Down

0 comments on commit 62bf965

Please sign in to comment.