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

upstream update #37

Merged
merged 16 commits into from
Jan 27, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
name: Test on ${{ matrix.os }}${{ matrix.name_suffix }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
stage: [stable, beta, nightly]
os: [ubuntu-latest, windows-latest, macOS-latest]
Expand Down
29 changes: 26 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
members = [
"halo2",
"halo2_gadgets",
"halo2_poseidon",
"halo2_proofs",
]
6 changes: 6 additions & 0 deletions halo2_gadgets/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to Rust's notion of

## [Unreleased]

## [0.3.1] - 2024-12-16
- `halo2_gadgets::poseidon::primitives` is now a re-export of the new `halo2_poseidon`
crate.
- `halo2_gadgets::sinsemilla::primitives` is now a re-export of the new `sinsemilla`
crate.

## [0.3.0] - 2023-03-21
### Added
- `halo2_gadgets::poseidon::primitives::{Mds, generate_constants}`
Expand Down
11 changes: 9 additions & 2 deletions halo2_gadgets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "halo2_gadgets"
version = "0.3.0"
version = "0.3.1"
authors = [
"Sean Bowe <[email protected]>",
"Jack Grigg <[email protected]>",
Expand All @@ -26,11 +26,13 @@ arrayvec = "0.7.0"
bitvec = "1"
ff = "0.13"
group = "0.13"
halo2_poseidon = { version = "0.1", path = "../halo2_poseidon", default-features = false }
halo2_proofs = { version = "0.3", path = "../halo2_proofs", default-features = false }
lazy_static = "1"
pasta_curves = "0.5"
proptest = { version = "1.0.0", optional = true }
rand = "0.8"
sinsemilla = "0.1"
subtle = "2.3"
uint = "0.9.2" # MSRV 1.56.1

Expand All @@ -39,7 +41,9 @@ plotters = { version = "0.3.0", default-features = false, optional = true }

[dev-dependencies]
criterion = "0.3"
halo2_poseidon = { version = "0.1", path = "../halo2_poseidon", default-features = false, features = ["test-dependencies"] }
proptest = "1.0.0"
sinsemilla = { version = "0.1", features = ["test-dependencies"] }

[target.'cfg(unix)'.dev-dependencies]
inferno = ">=0.11, <0.11.5" # MSRV 1.59
Expand All @@ -56,7 +60,10 @@ test-dev-graph = [
"plotters/bitmap_encoder",
"plotters/ttf",
]
test-dependencies = ["proptest"]
test-dependencies = [
"proptest",
"sinsemilla/test-dependencies",
]

# In-development features
# See https://zcash.github.io/halo2/dev/features.html
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");

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
})
}
}
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)]`

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
3 changes: 2 additions & 1 deletion halo2_gadgets/src/sinsemilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
pub mod chip;
pub mod merkle;
mod message;
pub mod primitives;

pub use ::sinsemilla as primitives;

/// The set of circuit instructions required to use the [`Sinsemilla`](https://zcash.github.io/halo2/design/gadgets/sinsemilla.html) gadget.
/// This trait is bounded on two constant parameters: `K`, the number of bits
Expand Down Expand Up @@ -655,8 +656,8 @@
let point = merkle_crh
.hash_to_point(
l.into_iter()
.chain(left.into_iter())

Check warning on line 659 in halo2_gadgets/src/sinsemilla.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> halo2_gadgets/src/sinsemilla.rs:659:48 | 659 | ... .chain(left.into_iter()) | ^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `left` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/14445aaf35d45f62fddda8cb5027f44ba4316e7f/library/core/src/iter/traits/iterator.rs:471:12 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `-W clippy::useless-conversion` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::useless_conversion)]`
.chain(right.into_iter()),

Check warning on line 660 in halo2_gadgets/src/sinsemilla.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> halo2_gadgets/src/sinsemilla.rs:660:48 | 660 | ... .chain(right.into_iter()), | ^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `right` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/14445aaf35d45f62fddda8cb5027f44ba4316e7f/library/core/src/iter/traits/iterator.rs:471:12 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
)
.unwrap();
point.to_affine()
Expand Down
7 changes: 6 additions & 1 deletion halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ where
.collect();

let hasher_S = pallas::Point::hash_to_curve(S_PERSONALIZATION);
let S = |chunk: &[bool]| hasher_S(&lebs2ip_k(chunk).to_le_bytes());
let S = |chunk: &[bool]| {
hasher_S(
&lebs2ip_k(chunk.try_into().expect("correct length")).to_le_bytes(),
)
};

// We can use complete addition here because it differs from
// incomplete addition with negligible probability.
Expand Down Expand Up @@ -249,6 +253,7 @@ where
let words: Value<Vec<u32>> = bitstring.map(|bitstring| {
bitstring
.chunks_exact(sinsemilla::K)
.map(|chunk| chunk.try_into().expect("correct length"))
.map(lebs2ip_k)
.collect()
});
Expand Down
1 change: 0 additions & 1 deletion halo2_gadgets/src/sinsemilla/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
}

#[cfg(test)]
pub mod tests {

Check failure on line 174 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Code coverage

missing documentation for a module

Check failure on line 174 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

missing documentation for a module

error: missing documentation for a module --> halo2_gadgets/src/sinsemilla/merkle.rs:174:1 | 174 | pub mod tests { | ^^^^^^^^^^^^^ | note: the lint level is defined here --> halo2_gadgets/src/lib.rs:21:9 | 21 | #![deny(missing_docs)] | ^^^^^^^^^^^^

Check failure on line 174 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Code coverage

missing documentation for a module
use super::{
chip::{MerkleChip, MerkleConfig},
MerklePath,
Expand Down Expand Up @@ -322,7 +322,6 @@
(sibling, &node)
};

use crate::sinsemilla::primitives as sinsemilla;
let merkle_crh =
sinsemilla::HashDomain::from_Q(TestHashDomain.Q().into());

Expand Down
Loading
Loading