Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Poseidon primitive into halo2_poseidon #831

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions halo2_gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ arrayvec = "0.7.0"
bitvec = "1"
ff = "0.13"
group = "0.13"
halo2_poseidon = { version = "0.0", path = "../halo2_poseidon", default-features = false }
halo2_proofs = { version = "0.3", path = "../halo2_proofs", default-features = false }
lazy_static = "1"
pasta_curves = "0.5"
Expand All @@ -40,6 +41,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"
sinsemilla = { version = "0.1", features = ["test-dependencies"] }

Expand Down
30 changes: 10 additions & 20 deletions halo2_gadgets/src/poseidon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! The Poseidon algebraic hash function.

use std::convert::TryInto;
use std::fmt;
use std::marker::PhantomData;

Expand All @@ -13,7 +12,7 @@
mod pow5;
pub use pow5::{Pow5Chip, Pow5Config, StateWord};

pub mod primitives;
pub use ::halo2_poseidon as primitives;
use primitives::{Absorbing, ConstantLength, Domain, Spec, SpongeMode, Squeezing, State};

/// A word from the padded input to a Poseidon sponge.
Expand Down Expand Up @@ -148,15 +147,9 @@
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(),

Check warning on line 152 in halo2_gadgets/src/poseidon.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

use of `default` to create a unit struct

warning: use of `default` to create a unit struct --> halo2_gadgets/src/poseidon.rs:152:33 | 152 | _marker: PhantomData::default(), | ^^^^^^^^^^^ help: remove this call to `default` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_constructed_unit_structs = note: `-W clippy::default-constructed-unit-structs` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::default_constructed_unit_structs)]`

Check warning on line 152 in halo2_gadgets/src/poseidon.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

use of `default` to create a unit struct

warning: use of `default` to create a unit struct --> halo2_gadgets/src/poseidon.rs:152:33 | 152 | _marker: PhantomData::default(), | ^^^^^^^^^^^ help: remove this call to `default` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_constructed_unit_structs = note: `-W clippy::default-constructed-unit-structs` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::default_constructed_unit_structs)]`
})
}

Expand All @@ -166,12 +159,10 @@
mut layouter: impl Layouter<F>,
value: PaddedWord<F>,
) -> Result<(), Error> {
for entry in self.mode.0.iter_mut() {
if entry.is_none() {
*entry = Some(value);
return Ok(());
}
}
let value = match self.mode.absorb(value) {
Ok(()) => return Ok(()),
Err(value) => value,
};

// We've already absorbed as many elements as we can
let _ = poseidon_sponge(
Expand All @@ -180,7 +171,8 @@
&mut self.state,
Some(&self.mode),
)?;
self.mode = Absorbing::init_with(value);
self.mode = Absorbing::init_empty();
self.mode.absorb(value).expect("state is not full");
Comment on lines +174 to +175
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Ok(())
}
Expand All @@ -203,7 +195,7 @@
chip: self.chip,
mode,
state: self.state,
_marker: PhantomData::default(),

Check warning on line 198 in halo2_gadgets/src/poseidon.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

use of `default` to create a unit struct

warning: use of `default` to create a unit struct --> halo2_gadgets/src/poseidon.rs:198:33 | 198 | _marker: PhantomData::default(), | ^^^^^^^^^^^ help: remove this call to `default` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_constructed_unit_structs

Check warning on line 198 in halo2_gadgets/src/poseidon.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

use of `default` to create a unit struct

warning: use of `default` to create a unit struct --> halo2_gadgets/src/poseidon.rs:198:33 | 198 | _marker: PhantomData::default(), | ^^^^^^^^^^^ help: remove this call to `default` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_constructed_unit_structs
})
}
}
Expand All @@ -220,10 +212,8 @@
/// Squeezes an element from the sponge.
pub fn squeeze(&mut self, mut layouter: impl Layouter<F>) -> Result<AssignedCell<F, F>, Error> {
loop {
for entry in self.mode.0.iter_mut() {
if let Some(inner) = entry.take() {
return Ok(inner.into());
}
if let Some(value) = self.mode.squeeze() {
return Ok(value.into());
}

// We've already squeezed out all available elements
Expand Down
28 changes: 14 additions & 14 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,31 +238,31 @@
// Load the initial state into this region.
let state = Pow5State::load(&mut region, config, initial_state)?;

let state = (0..config.half_full_rounds).fold(Ok(state), |res, r| {
res.and_then(|state| state.full_round(&mut region, config, r, r))
})?;

Check warning on line 243 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

usage of `Iterator::fold` on a type that implements `Try`

warning: usage of `Iterator::fold` on a type that implements `Try` --> halo2_gadgets/src/poseidon/pow5.rs:241:58 | 241 | let state = (0..config.half_full_rounds).fold(Ok(state), |res, r| { | __________________________________________________________^ 242 | | res.and_then(|state| state.full_round(&mut region, config, r, r)) 243 | | })?; | |__________________^ help: use `try_fold` instead: `try_fold(state, |res, r| ...)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold = note: `-W clippy::manual-try-fold` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::manual_try_fold)]`

Check warning on line 243 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

usage of `Iterator::fold` on a type that implements `Try`

warning: usage of `Iterator::fold` on a type that implements `Try` --> halo2_gadgets/src/poseidon/pow5.rs:241:58 | 241 | let state = (0..config.half_full_rounds).fold(Ok(state), |res, r| { | __________________________________________________________^ 242 | | res.and_then(|state| state.full_round(&mut region, config, r, r)) 243 | | })?; | |__________________^ help: use `try_fold` instead: `try_fold(state, |res, r| ...)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold = note: `-W clippy::manual-try-fold` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::manual_try_fold)]`

let state = (0..config.half_partial_rounds).fold(Ok(state), |res, r| {
res.and_then(|state| {
state.partial_round(
&mut region,
config,
config.half_full_rounds + 2 * r,
config.half_full_rounds + r,
)
})
})?;

Check warning on line 254 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

usage of `Iterator::fold` on a type that implements `Try`

warning: usage of `Iterator::fold` on a type that implements `Try` --> halo2_gadgets/src/poseidon/pow5.rs:245:61 | 245 | let state = (0..config.half_partial_rounds).fold(Ok(state), |res, r| { | _____________________________________________________________^ 246 | | res.and_then(|state| { 247 | | state.partial_round( 248 | | &mut region, ... | 253 | | }) 254 | | })?; | |__________________^ help: use `try_fold` instead: `try_fold(state, |res, r| ...)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold

Check warning on line 254 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

usage of `Iterator::fold` on a type that implements `Try`

warning: usage of `Iterator::fold` on a type that implements `Try` --> halo2_gadgets/src/poseidon/pow5.rs:245:61 | 245 | let state = (0..config.half_partial_rounds).fold(Ok(state), |res, r| { | _____________________________________________________________^ 246 | | res.and_then(|state| { 247 | | state.partial_round( 248 | | &mut region, ... | 253 | | }) 254 | | })?; | |__________________^ help: use `try_fold` instead: `try_fold(state, |res, r| ...)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold

let state = (0..config.half_full_rounds).fold(Ok(state), |res, r| {
res.and_then(|state| {
state.full_round(
&mut region,
config,
config.half_full_rounds + 2 * config.half_partial_rounds + r,
config.half_full_rounds + config.half_partial_rounds + r,
)
})
})?;

Check warning on line 265 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

usage of `Iterator::fold` on a type that implements `Try`

warning: usage of `Iterator::fold` on a type that implements `Try` --> halo2_gadgets/src/poseidon/pow5.rs:256:58 | 256 | let state = (0..config.half_full_rounds).fold(Ok(state), |res, r| { | __________________________________________________________^ 257 | | res.and_then(|state| { 258 | | state.full_round( 259 | | &mut region, ... | 264 | | }) 265 | | })?; | |__________________^ help: use `try_fold` instead: `try_fold(state, |res, r| ...)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold

Check warning on line 265 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

usage of `Iterator::fold` on a type that implements `Try`

warning: usage of `Iterator::fold` on a type that implements `Try` --> halo2_gadgets/src/poseidon/pow5.rs:256:58 | 256 | let state = (0..config.half_full_rounds).fold(Ok(state), |res, r| { | __________________________________________________________^ 257 | | res.and_then(|state| { 258 | | state.full_round( 259 | | &mut region, ... | 264 | | }) 265 | | })?; | |__________________^ help: use `try_fold` instead: `try_fold(state, |res, r| ...)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold

Ok(state.0)
},
Expand Down Expand Up @@ -340,19 +340,20 @@
let initial_state = initial_state?;

// Load the input into this region.
let load_input_word = |i: usize| {
let (cell, value) = match input.0[i].clone() {
let load_input_word = |(i, input_word): (usize, &Option<PaddedWord<F>>)| {
let (cell, value) = match input_word {
Some(PaddedWord::Message(word)) => (word.cell(), word.value().copied()),
Some(PaddedWord::Padding(padding_value)) => {
let value = Value::known(*padding_value);
let cell = region
.assign_fixed(
|| format!("load pad_{}", i),
config.rc_b[i],
1,
|| Value::known(padding_value),
|| value,
)?
.cell();
(cell, Value::known(padding_value))
(cell, value)
}
_ => panic!("Input is not padded"),
};
Expand All @@ -366,7 +367,12 @@

Ok(StateWord(var))
};
let input: Result<Vec<_>, Error> = (0..RATE).map(load_input_word).collect();
let input: Result<Vec<_>, Error> = input
.expose_inner()
.iter()
.enumerate()
.map(load_input_word)
.collect();
let input = input?;

// Constrain the output.
Expand Down Expand Up @@ -394,14 +400,8 @@
}

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(),
)
let vals = state[..RATE].to_vec();
Squeezing::init_full(vals.try_into().expect("correct length"))
}
}

Expand Down Expand Up @@ -448,7 +448,7 @@
.value()
.map(|v| *v + config.round_constants[round][idx])
});
let r: Value<Vec<F>> = q.map(|q| q.map(|q| q.pow(&config.alpha))).collect();

Check warning on line 451 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> halo2_gadgets/src/poseidon/pow5.rs:451:62 | 451 | let r: Value<Vec<F>> = q.map(|q| q.map(|q| q.pow(&config.alpha))).collect(); | ^^^^^^^^^^^^^ help: change this to: `config.alpha` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args

Check warning on line 451 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> halo2_gadgets/src/poseidon/pow5.rs:451:62 | 451 | let r: Value<Vec<F>> = q.map(|q| q.map(|q| q.pow(&config.alpha))).collect(); | ^^^^^^^^^^^^^ help: change this to: `config.alpha` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
let m = &config.m_reg;
let state = m.iter().map(|m_i| {
r.as_ref().map(|r| {
Expand All @@ -474,7 +474,7 @@
let p: Value<Vec<_>> = self.0.iter().map(|word| word.0.value().cloned()).collect();

let r: Value<Vec<_>> = p.map(|p| {
let r_0 = (p[0] + config.round_constants[round][0]).pow(&config.alpha);

Check warning on line 477 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> halo2_gadgets/src/poseidon/pow5.rs:477:73 | 477 | let r_0 = (p[0] + config.round_constants[round][0]).pow(&config.alpha); | ^^^^^^^^^^^^^ help: change this to: `config.alpha` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args

Check warning on line 477 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> halo2_gadgets/src/poseidon/pow5.rs:477:73 | 477 | let r_0 = (p[0] + config.round_constants[round][0]).pow(&config.alpha); | ^^^^^^^^^^^^^ help: change this to: `config.alpha` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
let r_i = p[1..]
.iter()
.enumerate()
Expand Down Expand Up @@ -514,7 +514,7 @@
}

let r_mid: Value<Vec<_>> = p_mid.map(|p| {
let r_0 = (p[0] + config.round_constants[round + 1][0]).pow(&config.alpha);

Check warning on line 517 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> halo2_gadgets/src/poseidon/pow5.rs:517:77 | 517 | let r_0 = (p[0] + config.round_constants[round + 1][0]).pow(&config.alpha); | ^^^^^^^^^^^^^ help: change this to: `config.alpha` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args

Check warning on line 517 in halo2_gadgets/src/poseidon/pow5.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> halo2_gadgets/src/poseidon/pow5.rs:517:77 | 517 | let r_0 = (p[0] + config.round_constants[round + 1][0]).pow(&config.alpha); | ^^^^^^^^^^^^^ help: change this to: `config.alpha` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
let r_i = p[1..]
.iter()
.enumerate()
Expand Down Expand Up @@ -687,7 +687,7 @@
.try_into()
.unwrap();
let (round_constants, mds, _) = S::constants();
poseidon::permute::<_, S, WIDTH, RATE>(
poseidon::test_only_permute::<_, S, WIDTH, RATE>(
&mut expected_final_state,
&mds,
&round_constants,
Expand Down
Loading
Loading