diff --git a/halo2_gadgets/src/ecc.rs b/halo2_gadgets/src/ecc.rs index b5cc37406a..c6130a7574 100644 --- a/halo2_gadgets/src/ecc.rs +++ b/halo2_gadgets/src/ecc.rs @@ -14,7 +14,7 @@ pub mod chip; /// The set of circuit instructions required to use the ECC gadgets. pub trait EccInstructions: -Chip + UtilitiesInstructions + Clone + Debug + Eq + Chip + UtilitiesInstructions + Clone + Debug + Eq { /// Variable representing a scalar used in variable-base scalar mul. /// @@ -380,7 +380,7 @@ impl> NonIdentityPoint { } impl + Clone + Debug + Eq> -From> for Point + From> for Point { fn from(non_id_point: NonIdentityPoint) -> Self { Self { @@ -638,7 +638,7 @@ pub(crate) mod tests { }, FixedPoints, }; - use crate::utilities::lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheck}; + use crate::utilities::lookup_range_check::{LookupRangeCheck, LookupRangeCheckConfig}; #[derive(Debug, Eq, PartialEq, Clone)] pub(crate) struct TestFixedBases; diff --git a/halo2_gadgets/src/ecc/chip.rs b/halo2_gadgets/src/ecc/chip.rs index 8036b08338..402020384b 100644 --- a/halo2_gadgets/src/ecc/chip.rs +++ b/halo2_gadgets/src/ecc/chip.rs @@ -245,7 +245,7 @@ impl> Chip for Ecc } impl> UtilitiesInstructions -for EccChip + for EccChip { type Var = AssignedCell; } @@ -360,7 +360,7 @@ pub struct EccScalarFixedShort { /// The circuit-assigned running sum constraining this signed short scalar, or `None` /// if the scalar has not been used yet. running_sum: - Option, { NUM_WINDOWS_SHORT + 1 }>>, + Option, { NUM_WINDOWS_SHORT + 1 }>>, } /// A base field element used for fixed-base scalar multiplication. @@ -408,12 +408,12 @@ pub enum ScalarVar { } impl> EccInstructions for EccChip - where - >::Base: +where + >::Base: FixedPoint, - >::FullScalar: + >::FullScalar: FixedPoint, - >::ShortScalar: + >::ShortScalar: FixedPoint, { type ScalarFixed = EccScalarFixed; @@ -625,13 +625,13 @@ impl> EccInstructions for Ecc } impl> BaseFitsInScalarInstructions -for EccChip - where - >::Base: + for EccChip +where + >::Base: FixedPoint, - >::FullScalar: + >::FullScalar: FixedPoint, - >::ShortScalar: + >::ShortScalar: FixedPoint, { fn scalar_var_from_base( @@ -641,4 +641,4 @@ for EccChip ) -> Result { Ok(ScalarVar::BaseFieldElem(base.clone())) } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/ecc/chip/mul/overflow.rs b/halo2_gadgets/src/ecc/chip/mul/overflow.rs index ba2d876e3c..89852693a7 100644 --- a/halo2_gadgets/src/ecc/chip/mul/overflow.rs +++ b/halo2_gadgets/src/ecc/chip/mul/overflow.rs @@ -12,8 +12,8 @@ use halo2_proofs::{ }; use pasta_curves::pallas; -use std::iter; use crate::utilities::lookup_range_check::LookupRangeCheck; +use std::iter; #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct Config { diff --git a/halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs b/halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs index 758a35107e..d939b0e00e 100644 --- a/halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs +++ b/halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs @@ -15,8 +15,8 @@ use halo2_proofs::{ }; use pasta_curves::pallas; -use std::convert::TryInto; use crate::utilities::lookup_range_check::LookupRangeCheck; +use std::convert::TryInto; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Config> { diff --git a/halo2_gadgets/src/ecc/chip/mul_fixed/short.rs b/halo2_gadgets/src/ecc/chip/mul_fixed/short.rs index 98c4805f1d..056e716e1e 100644 --- a/halo2_gadgets/src/ecc/chip/mul_fixed/short.rs +++ b/halo2_gadgets/src/ecc/chip/mul_fixed/short.rs @@ -3,13 +3,13 @@ use std::convert::TryInto; use super::super::{EccPoint, EccScalarFixedShort, FixedPoints, L_SCALAR_SHORT, NUM_WINDOWS_SHORT}; use crate::{ecc::chip::MagnitudeSign, utilities::bool_check}; +use halo2_proofs::circuit::AssignedCell; use halo2_proofs::{ circuit::{Layouter, Region}, plonk::{ConstraintSystem, Constraints, Error, Expression, Selector}, poly::Rotation, }; use pasta_curves::pallas; -use halo2_proofs::circuit::AssignedCell; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Config> { @@ -316,6 +316,7 @@ pub mod tests { }; use pasta_curves::pallas; + use crate::utilities::lookup_range_check::LookupRangeCheck; use crate::{ ecc::{ chip::{EccChip, FixedPoint, MagnitudeSign}, @@ -324,7 +325,6 @@ pub mod tests { }, utilities::{lookup_range_check::LookupRangeCheckConfig, UtilitiesInstructions}, }; - use crate::utilities::lookup_range_check::LookupRangeCheck; #[allow(clippy::op_ref)] pub(crate) fn test_mul_fixed_short( diff --git a/halo2_gadgets/src/ecc/chip/witness_point.rs b/halo2_gadgets/src/ecc/chip/witness_point.rs index bb21bbb511..98f865a6dc 100644 --- a/halo2_gadgets/src/ecc/chip/witness_point.rs +++ b/halo2_gadgets/src/ecc/chip/witness_point.rs @@ -206,6 +206,6 @@ pub mod tests { layouter.namespace(|| "witness identity"), Value::known(pallas::Affine::identity()), ) - .expect_err("witnessing 𝒪 should return an error"); + .expect_err("witnessing 𝒪 should return an error"); } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/lib.rs b/halo2_gadgets/src/lib.rs index b4564dc84f..eadea1bb65 100644 --- a/halo2_gadgets/src/lib.rs +++ b/halo2_gadgets/src/lib.rs @@ -27,6 +27,6 @@ pub mod poseidon; #[cfg_attr(docsrs, doc(cfg(feature = "unstable-sha256-gadget")))] pub mod sha256; pub mod sinsemilla; +pub mod sinsemilla_opt; pub mod utilities; pub mod utilities_opt; -pub mod sinsemilla_opt; diff --git a/halo2_gadgets/src/sinsemilla.rs b/halo2_gadgets/src/sinsemilla.rs index f626252ad2..a7028052bf 100644 --- a/halo2_gadgets/src/sinsemilla.rs +++ b/halo2_gadgets/src/sinsemilla.rs @@ -88,7 +88,6 @@ pub trait SinsemillaInstructions Result<(Self::NonIdentityPoint, Vec), Error>; - /// Hashes a message to an ECC curve point. /// This returns both the resulting point, as well as the message /// decomposition in the form of intermediate values in a cumulative @@ -103,7 +102,6 @@ pub trait SinsemillaInstructions Result<(Self::NonIdentityPoint, Vec), Error>; - /// Extracts the x-coordinate of the output of a Sinsemilla hash. fn extract(point: &Self::NonIdentityPoint) -> Self::X; } @@ -115,17 +113,17 @@ pub trait SinsemillaInstructions - where - SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, +where + SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, { chip: SinsemillaChip, inner: SinsemillaChip::Message, } impl -Message - where - SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, + Message +where + SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, { #![allow(dead_code)] fn from_bitstring( @@ -179,16 +177,16 @@ Message /// A message piece with a bitlength of some multiple of `K`. #[derive(Copy, Clone, Debug)] pub struct MessagePiece - where - SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, +where + SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, { inner: SinsemillaChip::MessagePiece, } impl -MessagePiece - where - SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, + MessagePiece +where + SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, { /// Returns the inner MessagePiece contained in this gadget. pub fn inner(&self) -> SinsemillaChip::MessagePiece { @@ -197,9 +195,9 @@ MessagePiece } impl -MessagePiece - where - SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, + MessagePiece +where + SinsemillaChip: SinsemillaInstructions + Clone + Debug + Eq, { #![allow(dead_code)] fn from_bitstring( @@ -381,7 +379,7 @@ HashDomain /// Trait allowing circuit's Sinsemilla CommitDomains to be enumerated. pub trait CommitDomains, H: HashDomains>: -Clone + Debug + Clone + Debug { /// Returns the fixed point corresponding to the R constant used for /// randomization in this CommitDomain. @@ -578,9 +576,9 @@ pub(crate) mod tests { use lazy_static::lazy_static; use pasta_curves::pallas; - use std::convert::TryInto; use crate::sinsemilla::chip::SinsemillaChipProps; use crate::utilities::lookup_range_check::LookupRangeCheck; + use std::convert::TryInto; pub(crate) const PERSONALIZATION: &str = "MerkleCRH"; @@ -650,7 +648,7 @@ pub(crate) mod tests { let table_idx = meta.lookup_table_column(); #[cfg(feature = "zsa")] - let table_range_check_tag = meta.lookup_table_column(); + let table_range_check_tag = meta.lookup_table_column(); let lagrange_coeffs = [ meta.fixed_column(), meta.fixed_column(), diff --git a/halo2_gadgets/src/sinsemilla/chip.rs b/halo2_gadgets/src/sinsemilla/chip.rs index 5ef8eab48f..7ab26bec82 100644 --- a/halo2_gadgets/src/sinsemilla/chip.rs +++ b/halo2_gadgets/src/sinsemilla/chip.rs @@ -11,9 +11,9 @@ use crate::{ }, utilities::lookup_range_check::LookupRangeCheckConfig, }; -use std::marker::PhantomData; use ff::PrimeField; use pasta_curves::arithmetic::CurveAffine; +use std::marker::PhantomData; use halo2_proofs::{ circuit::{AssignedCell, Chip, Layouter, Value}, @@ -28,20 +28,20 @@ use pasta_curves::pallas::Base; use proptest::test_runner::Config; mod generator_table; +use crate::sinsemilla::primitives::{lebs2ip_k, INV_TWO_POW_K, SINSEMILLA_S}; use generator_table::GeneratorTableConfig; use halo2_proofs::circuit::Region; use halo2_proofs::plonk::Assigned; -use crate::sinsemilla::primitives::{INV_TWO_POW_K, lebs2ip_k, SINSEMILLA_S}; mod hash_to_point; /// Configuration for the Sinsemilla hash chip #[derive(Eq, PartialEq, Clone, Debug)] pub struct SinsemillaConfigCommon - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { /// Binary selector used in lookup argument and in the body of the Sinsemilla hash. pub(crate) q_sinsemilla1: Selector, @@ -66,10 +66,10 @@ pub struct SinsemillaConfigCommon /// Configuration for the Sinsemilla hash chip #[derive(Eq, PartialEq, Clone, Debug)] pub struct SinsemillaConfig - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { base: SinsemillaConfigCommon, /// The lookup table where $(\mathsf{idx}, x_p, y_p)$ are loaded for the $2^K$ @@ -80,10 +80,10 @@ pub struct SinsemillaConfig } // TODO: add doc, rename it to SinsemillaConfigProps pub trait SinsemillaConfigProps - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type LookupConfigType; fn base(&self) -> &SinsemillaConfigCommon; @@ -111,11 +111,7 @@ pub trait SinsemillaConfigProps fn get_y_q(&self, meta: &mut VirtualCells) -> Expression; - fn configure_from_y_q( - &self, - meta: &mut ConstraintSystem, - ) where - { + fn configure_from_y_q(&self, meta: &mut ConstraintSystem) { let two = pallas::Base::from(2); // Closures for expressions that are derived multiple times @@ -149,8 +145,10 @@ pub trait SinsemillaConfigProps let q_s1 = meta.query_selector(self.base().q_sinsemilla1); let q_s3 = self.q_s3(meta); - let lambda_1_next = meta.query_advice(self.base().double_and_add.lambda_1, Rotation::next()); - let lambda_2_cur = meta.query_advice(self.base().double_and_add.lambda_2, Rotation::cur()); + let lambda_1_next = + meta.query_advice(self.base().double_and_add.lambda_1, Rotation::next()); + let lambda_2_cur = + meta.query_advice(self.base().double_and_add.lambda_2, Rotation::cur()); let x_a_cur = meta.query_advice(self.base().double_and_add.x_a, Rotation::cur()); let x_a_next = meta.query_advice(self.base().double_and_add.x_a, Rotation::next()); @@ -189,15 +187,13 @@ pub trait SinsemillaConfigProps Constraints::with_selector(q_s1, [("Secant line", secant_line), ("y check", y_check)]) }); } - - } // TODO: add doc impl SinsemillaConfigProps for SinsemillaConfig - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type LookupConfigType = LookupRangeCheckConfig; fn base(&self) -> &SinsemillaConfigCommon { @@ -214,26 +210,25 @@ impl SinsemillaConfigProps for SinsemillaConfi } } - /// A chip that implements 10-bit Sinsemilla using a lookup table and 5 advice columns. /// /// [Chip description](https://zcash.github.io/halo2/design/gadgets/sinsemilla.html#plonk--halo-2-constraints). #[derive(Eq, PartialEq, Clone, Debug)] pub struct SinsemillaChip - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { config: SinsemillaConfig, } // TODO: add doc,rename it to SinsemillaChipProps pub trait SinsemillaChipProps - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type Loaded; @@ -259,14 +254,13 @@ pub trait SinsemillaChipProps lookup: Self::LookupType, range_check: Self::RangeCheckConfigType, ) -> Self::SinsemillaConfigType; - } impl SinsemillaChipProps for SinsemillaChip - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type Loaded = >::Loaded; @@ -338,10 +332,10 @@ pub fn create_common_config( witness_pieces: Column, fixed_y_q: Column, ) -> SinsemillaConfigCommon - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { SinsemillaConfigCommon { q_sinsemilla1: meta.complex_selector(), @@ -360,13 +354,12 @@ pub fn create_common_config( } } - // TODO: remove duplicate? impl Chip for SinsemillaChip - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { type Config = SinsemillaConfig; type Loaded = (); @@ -380,17 +373,15 @@ impl Chip for SinsemillaChip SinsemillaInstructions -for SinsemillaChip - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, + for SinsemillaChip +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type CellValue = AssignedCell; @@ -459,4 +450,4 @@ for SinsemillaChip fn extract(point: &Self::NonIdentityPoint) -> Self::X { point.x() } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/sinsemilla/chip/generator_table.rs b/halo2_gadgets/src/sinsemilla/chip/generator_table.rs index 9193f85722..16194ffc8e 100644 --- a/halo2_gadgets/src/sinsemilla/chip/generator_table.rs +++ b/halo2_gadgets/src/sinsemilla/chip/generator_table.rs @@ -57,7 +57,8 @@ impl GeneratorTableConfig { // y_{p,i} = (Y_{A,i} / 2) - lambda1 * (x_{A,i} - x_{P,i}) let y_p = { - let lambda1 = meta.query_advice(config.base.double_and_add.lambda_1, Rotation::cur()); + let lambda1 = + meta.query_advice(config.base.double_and_add.lambda_1, Rotation::cur()); let x_a = meta.query_advice(config.base.double_and_add.x_a, Rotation::cur()); let Y_A = config.base.double_and_add.Y_A(meta, Rotation::cur()); @@ -95,4 +96,4 @@ impl GeneratorTableConfig { }, ) } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs b/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs index 8f2e34c441..9ca6f448a0 100644 --- a/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs +++ b/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs @@ -17,16 +17,12 @@ use pasta_curves::{arithmetic::CurveAffine, pallas}; use std::ops::Deref; // TODO: SinsemillaChip to SinsemillaChipTraits - - - -impl SinsemillaChip - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +impl SinsemillaChip +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { - // TODO: simplify three hash_message functions /// [Specification](https://p.z.cash/halo2-0.1:sinsemilla-constraints?partial). #[allow(non_snake_case)] @@ -60,10 +56,8 @@ impl SinsemillaChip NonIdentityEccPoint::from_coordinates_unchecked(x_a.0, y_a), zs_sum, )) - } - /// [Specification](https://p.z.cash/halo2-0.1:sinsemilla-constraints?partial). #[allow(non_snake_case)] #[allow(clippy::type_complexity)] @@ -98,7 +92,6 @@ impl SinsemillaChip )) } - /// [Specification](https://p.z.cash/halo2-0.1:sinsemilla-constraints?partial). #[allow(non_snake_case)] #[allow(clippy::type_complexity)] @@ -171,7 +164,6 @@ impl SinsemillaChip )) } - #[allow(non_snake_case)] fn public_initialization_vanilla( &self, @@ -188,7 +180,7 @@ impl SinsemillaChip // Constrain the initial x_a, lambda_1, lambda_2, x_p using the q_sinsemilla4 // selector. let mut y_a: Y = { -// Enable `q_sinsemilla4` on the first row. + // Enable `q_sinsemilla4` on the first row. config.base.q_sinsemilla4.enable(region, offset)?; region.assign_fixed( || "fixed y_q", @@ -285,8 +277,12 @@ impl SinsemillaChip // Enable `q_sinsemilla4` on the second row. config.base.q_sinsemilla4.enable(region, offset + 1)?; let q_y: AssignedCell, pallas::Base> = Q.y().into(); - let y_a: AssignedCell, pallas::Base> = - q_y.copy_advice(|| "fixed y_q", region, config.base.double_and_add.x_p, offset)?; + let y_a: AssignedCell, pallas::Base> = q_y.copy_advice( + || "fixed y_q", + region, + config.base.double_and_add.x_p, + offset, + )?; y_a.value_field().into() }; @@ -294,7 +290,12 @@ impl SinsemillaChip let x_a: X = { let q_x: AssignedCell, pallas::Base> = Q.x().into(); - let x_a = q_x.copy_advice(|| "fixed x_q", region, config.base.double_and_add.x_a, offset)?; + let x_a = q_x.copy_advice( + || "fixed x_q", + region, + config.base.double_and_add.x_a, + offset, + )?; x_a.into() }; @@ -347,8 +348,12 @@ impl SinsemillaChip // Assign the final y_a. let y_a = { // Assign the final y_a. - let y_a_cell = - region.assign_advice(|| "y_a", config.base.double_and_add.lambda_1, offset, || y_a.0)?; + let y_a_cell = region.assign_advice( + || "y_a", + config.base.double_and_add.lambda_1, + offset, + || y_a.0, + )?; // Assign lambda_2 and x_p zero values since they are queried // in the gate. (The actual values do not matter since they are @@ -519,7 +524,12 @@ impl SinsemillaChip let y_p = gen.map(|gen| gen.1); // Assign `x_p` - region.assign_advice(|| "x_p", config.base.double_and_add.x_p, offset + row, || x_p)?; + region.assign_advice( + || "x_p", + config.base.double_and_add.x_p, + offset + row, + || x_p, + )?; // Compute and assign `lambda_1` let lambda_1 = { diff --git a/halo2_gadgets/src/sinsemilla/merkle.rs b/halo2_gadgets/src/sinsemilla/merkle.rs index 91461639d2..58f7851f45 100644 --- a/halo2_gadgets/src/sinsemilla/merkle.rs +++ b/halo2_gadgets/src/sinsemilla/merkle.rs @@ -195,10 +195,10 @@ pub mod tests { plonk::{Circuit, ConstraintSystem, Error}, }; - use rand::{rngs::OsRng, RngCore}; - use std::{convert::TryInto, iter}; use crate::sinsemilla::chip::SinsemillaChipProps; use crate::utilities::lookup_range_check::LookupRangeCheck; + use rand::{rngs::OsRng, RngCore}; + use std::{convert::TryInto, iter}; const MERKLE_DEPTH: usize = 32; diff --git a/halo2_gadgets/src/sinsemilla/merkle/chip.rs b/halo2_gadgets/src/sinsemilla/merkle/chip.rs index b132b03e4b..0d57fd3249 100644 --- a/halo2_gadgets/src/sinsemilla/merkle/chip.rs +++ b/halo2_gadgets/src/sinsemilla/merkle/chip.rs @@ -9,6 +9,8 @@ use pasta_curves::pallas; use super::MerkleInstructions; +use crate::sinsemilla::chip::{SinsemillaChipProps, SinsemillaConfigProps}; +use crate::utilities::lookup_range_check::witness_short_generic; use crate::{ sinsemilla::{primitives as sinsemilla, MessagePiece}, utilities::RangeConstrained, @@ -25,16 +27,14 @@ use crate::{ }, }; use group::ff::PrimeField; -use crate::sinsemilla::chip::{SinsemillaChipProps, SinsemillaConfigProps}; -use crate::utilities::lookup_range_check::witness_short_generic; /// Configuration for the `MerkleChip` implementation. #[derive(Clone, Debug, PartialEq, Eq)] pub struct MerkleConfig - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { advices: [Column; 5], q_decompose: Selector, @@ -54,19 +54,19 @@ pub struct MerkleConfig /// `left` and `right`. #[derive(Clone, Debug, PartialEq, Eq)] pub struct MerkleChip - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { config: MerkleConfig, } impl Chip for MerkleChip - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { type Config = MerkleConfig; type Loaded = (); @@ -81,10 +81,10 @@ impl Chip for MerkleChip } impl MerkleChip - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { /// Configures the [`MerkleChip`]. pub fn configure( @@ -199,12 +199,12 @@ impl MerkleChip } impl -MerkleInstructions -for MerkleChip - where - Hash: HashDomains + Eq, - F: FixedPoints, - Commit: CommitDomains + Eq, + MerkleInstructions + for MerkleChip +where + Hash: HashDomains + Eq, + F: FixedPoints, + Commit: CommitDomains + Eq, { #[allow(non_snake_case)] fn hash_layer( @@ -418,19 +418,19 @@ for MerkleChip } impl UtilitiesInstructions for MerkleChip - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type Var = AssignedCell; } impl CondSwapInstructions for MerkleChip - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { #[allow(clippy::type_complexity)] fn swap( @@ -458,11 +458,11 @@ impl CondSwapInstructions for MerkleChip SinsemillaInstructions -for MerkleChip - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, + for MerkleChip +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type CellValue = as SinsemillaInstructions< pallas::Affine, @@ -537,7 +537,6 @@ for MerkleChip chip.hash_to_point(layouter, Q, message) } - #[allow(non_snake_case)] #[allow(clippy::type_complexity)] fn hash_to_point_with_private_init( @@ -554,4 +553,4 @@ for MerkleChip fn extract(point: &Self::NonIdentityPoint) -> Self::X { SinsemillaChip::::extract(point) } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/sinsemilla/primitives.rs b/halo2_gadgets/src/sinsemilla/primitives.rs index 668558fed9..ad9e397b5e 100644 --- a/halo2_gadgets/src/sinsemilla/primitives.rs +++ b/halo2_gadgets/src/sinsemilla/primitives.rs @@ -303,7 +303,7 @@ mod tests { .iter() .cloned() ) - .collect::>(), + .collect::>(), vec![true, true, false, true, false, true, false, true, false, true] ); assert_eq!( @@ -312,7 +312,7 @@ mod tests { .iter() .cloned() ) - .collect::>(), + .collect::>(), vec![ true, true, false, true, false, true, false, true, false, true, true, false, false, false, false, false, false, false, false, false @@ -365,4 +365,4 @@ mod tests { // Test equality assert_eq!(commit1.unwrap(), commit2.unwrap()); } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/sinsemilla_opt/chip.rs b/halo2_gadgets/src/sinsemilla_opt/chip.rs index 073e2141d6..73b29ba77a 100644 --- a/halo2_gadgets/src/sinsemilla_opt/chip.rs +++ b/halo2_gadgets/src/sinsemilla_opt/chip.rs @@ -1,16 +1,14 @@ mod generator_table; mod hash_to_point; -use crate::sinsemilla::{ - primitives as sinsemilla, -}; -use crate::{ecc::{ - chip::{DoubleAndAdd, NonIdentityEccPoint}, - FixedPoints, -}, utilities::lookup_range_check::LookupRangeCheckConfig}; -use std::marker::PhantomData; -use crate::sinsemilla::{ - message::{Message, MessagePiece}, +use crate::sinsemilla::message::{Message, MessagePiece}; +use crate::sinsemilla::primitives as sinsemilla; +use crate::{ + ecc::{ + chip::{DoubleAndAdd, NonIdentityEccPoint}, + FixedPoints, + }, + utilities::lookup_range_check::LookupRangeCheckConfig, }; use halo2_proofs::{ circuit::{AssignedCell, Chip, Layouter, Value}, @@ -22,19 +20,23 @@ use halo2_proofs::{ }; use pasta_curves::pallas; use pasta_curves::pallas::Base; +use std::marker::PhantomData; -use generator_table::GeneratorTableConfigOptimized; +use crate::sinsemilla::chip::{ + create_common_config, SinsemillaChip, SinsemillaChipProps, SinsemillaConfigCommon, + SinsemillaConfigProps, +}; use crate::sinsemilla::{CommitDomains, HashDomains, SinsemillaInstructions}; -use crate::sinsemilla::chip::{create_common_config, SinsemillaChip, SinsemillaChipProps, SinsemillaConfigCommon, SinsemillaConfigProps}; use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; +use generator_table::GeneratorTableConfigOptimized; /// Configuration for the SinsemillaConfigOptimized hash chip #[derive(Eq, PartialEq, Clone, Debug)] pub struct SinsemillaConfigOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { base: SinsemillaConfigCommon, @@ -44,11 +46,12 @@ pub struct SinsemillaConfigOptimized /// An advice column configured to perform lookup range checks. lookup_config: LookupRangeCheckConfigOptimized, } -impl SinsemillaConfigProps for SinsemillaConfigOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +impl SinsemillaConfigProps + for SinsemillaConfigOptimized +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type LookupConfigType = LookupRangeCheckConfigOptimized; fn base(&self) -> &SinsemillaConfigCommon { @@ -70,20 +73,20 @@ impl SinsemillaConfigProps for SinsemillaConfi /// [Chip description](https://zcash.github.io/halo2/design/gadgets/sinsemilla.html#plonk--halo-2-constraints). #[derive(Eq, PartialEq, Clone, Debug)] pub struct SinsemillaChipOptimized - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { config: SinsemillaConfigOptimized, } - -impl SinsemillaChipProps for SinsemillaChipOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +impl SinsemillaChipProps + for SinsemillaChipOptimized +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type Loaded = >::Loaded; @@ -151,10 +154,10 @@ impl SinsemillaChipProps for SinsemillaChipOpt // TODO: remove duplicate? impl Chip for SinsemillaChipOptimized - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { type Config = SinsemillaConfigOptimized; type Loaded = (); @@ -172,11 +175,11 @@ impl Chip for SinsemillaChipOptimized SinsemillaInstructions -for SinsemillaChipOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, + for SinsemillaChipOptimized +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type CellValue = AssignedCell; @@ -245,4 +248,3 @@ for SinsemillaChipOptimized point.x() } } - diff --git a/halo2_gadgets/src/sinsemilla_opt/chip/generator_table.rs b/halo2_gadgets/src/sinsemilla_opt/chip/generator_table.rs index 12616a3b0a..1c6f1293d2 100644 --- a/halo2_gadgets/src/sinsemilla_opt/chip/generator_table.rs +++ b/halo2_gadgets/src/sinsemilla_opt/chip/generator_table.rs @@ -6,9 +6,9 @@ use halo2_proofs::{ }; use super::{CommitDomains, FixedPoints, HashDomains}; +use crate::sinsemilla::chip::SinsemillaConfigProps; use crate::sinsemilla::primitives::{self as sinsemilla, K, SINSEMILLA_S}; use pasta_curves::pallas; -use crate::sinsemilla::chip::SinsemillaConfigProps; /// Table containing independent generators S[0..2^k] #[derive(Eq, PartialEq, Copy, Clone, Debug)] @@ -59,7 +59,8 @@ impl GeneratorTableConfigOptimized { // y_{p,i} = (Y_{A,i} / 2) - lambda1 * (x_{A,i} - x_{P,i}) let y_p = { - let lambda1 = meta.query_advice(config.base.double_and_add.lambda_1, Rotation::cur()); + let lambda1 = + meta.query_advice(config.base.double_and_add.lambda_1, Rotation::cur()); let x_a = meta.query_advice(config.base.double_and_add.x_a, Rotation::cur()); let Y_A = config.base.double_and_add.Y_A(meta, Rotation::cur()); @@ -173,4 +174,4 @@ impl GeneratorTableConfigOptimized { }, ) } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/sinsemilla_opt/chip/hash_to_point.rs b/halo2_gadgets/src/sinsemilla_opt/chip/hash_to_point.rs index d4c1167445..e4f1558e6e 100644 --- a/halo2_gadgets/src/sinsemilla_opt/chip/hash_to_point.rs +++ b/halo2_gadgets/src/sinsemilla_opt/chip/hash_to_point.rs @@ -13,20 +13,16 @@ use halo2_proofs::{ use group::ff::{PrimeField, PrimeFieldBits}; use pasta_curves::{arithmetic::CurveAffine, pallas}; -use std::ops::Deref; use crate::sinsemilla::{CommitDomains, HashDomains, SinsemillaInstructions}; +use std::ops::Deref; // TODO: SinsemillaChip to SinsemillaChipTraits - - - -impl SinsemillaChipOptimized - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +impl SinsemillaChipOptimized +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { - // TODO: simplify three hash_message functions /// [Specification](https://p.z.cash/halo2-0.1:sinsemilla-constraints?partial). #[allow(non_snake_case)] @@ -60,10 +56,8 @@ impl SinsemillaChipOptimized NonIdentityEccPoint::from_coordinates_unchecked(x_a.0, y_a), zs_sum, )) - } - /// [Specification](https://p.z.cash/halo2-0.1:sinsemilla-constraints?partial). #[allow(non_snake_case)] #[allow(clippy::type_complexity)] @@ -98,7 +92,6 @@ impl SinsemillaChipOptimized )) } - /// [Specification](https://p.z.cash/halo2-0.1:sinsemilla-constraints?partial). #[allow(non_snake_case)] #[allow(clippy::type_complexity)] @@ -171,7 +164,6 @@ impl SinsemillaChipOptimized )) } - #[allow(non_snake_case)] fn public_initialization_vanilla( &self, @@ -188,7 +180,7 @@ impl SinsemillaChipOptimized // Constrain the initial x_a, lambda_1, lambda_2, x_p using the q_sinsemilla4 // selector. let mut y_a: Y = { -// Enable `q_sinsemilla4` on the first row. + // Enable `q_sinsemilla4` on the first row. config.base.q_sinsemilla4.enable(region, offset)?; region.assign_fixed( || "fixed y_q", @@ -285,8 +277,12 @@ impl SinsemillaChipOptimized // Enable `q_sinsemilla4` on the second row. config.base.q_sinsemilla4.enable(region, offset + 1)?; let q_y: AssignedCell, pallas::Base> = Q.y().into(); - let y_a: AssignedCell, pallas::Base> = - q_y.copy_advice(|| "fixed y_q", region, config.base.double_and_add.x_p, offset)?; + let y_a: AssignedCell, pallas::Base> = q_y.copy_advice( + || "fixed y_q", + region, + config.base.double_and_add.x_p, + offset, + )?; y_a.value_field().into() }; @@ -294,7 +290,12 @@ impl SinsemillaChipOptimized let x_a: X = { let q_x: AssignedCell, pallas::Base> = Q.x().into(); - let x_a = q_x.copy_advice(|| "fixed x_q", region, config.base.double_and_add.x_a, offset)?; + let x_a = q_x.copy_advice( + || "fixed x_q", + region, + config.base.double_and_add.x_a, + offset, + )?; x_a.into() }; @@ -347,8 +348,12 @@ impl SinsemillaChipOptimized // Assign the final y_a. let y_a = { // Assign the final y_a. - let y_a_cell = - region.assign_advice(|| "y_a", config.base.double_and_add.lambda_1, offset, || y_a.0)?; + let y_a_cell = region.assign_advice( + || "y_a", + config.base.double_and_add.lambda_1, + offset, + || y_a.0, + )?; // Assign lambda_2 and x_p zero values since they are queried // in the gate. (The actual values do not matter since they are @@ -519,7 +524,12 @@ impl SinsemillaChipOptimized let y_p = gen.map(|gen| gen.1); // Assign `x_p` - region.assign_advice(|| "x_p", config.base.double_and_add.x_p, offset + row, || x_p)?; + region.assign_advice( + || "x_p", + config.base.double_and_add.x_p, + offset + row, + || x_p, + )?; // Compute and assign `lambda_1` let lambda_1 = { diff --git a/halo2_gadgets/src/sinsemilla_opt/merkle.rs b/halo2_gadgets/src/sinsemilla_opt/merkle.rs index a6d35d7f2a..8dd8967ed6 100644 --- a/halo2_gadgets/src/sinsemilla_opt/merkle.rs +++ b/halo2_gadgets/src/sinsemilla_opt/merkle.rs @@ -11,7 +11,6 @@ use crate::sinsemilla::{HashDomains, SinsemillaInstructions}; use crate::utilities::{cond_swap::CondSwapInstructions, i2lebsp, UtilitiesInstructions}; pub mod chip; - /// SWU hash-to-curve personalization for the Merkle CRH generator pub const MERKLE_CRH_PERSONALIZATION: &str = "z.cash:Orchard-MerkleCRH"; @@ -24,10 +23,10 @@ pub trait MerkleInstructions< const K: usize, const MAX_WORDS: usize, >: -SinsemillaInstructions -+ CondSwapInstructions -+ UtilitiesInstructions -+ Chip + SinsemillaInstructions + + CondSwapInstructions + + UtilitiesInstructions + + Chip { /// Compute MerkleCRH for a given `layer`. The hash that computes the root /// is at layer 0, and the hashes that are applied to two leaves are at @@ -64,15 +63,15 @@ pub struct MerklePath< } impl< - C: CurveAffine, - MerkleChipOptimized, - const PATH_LENGTH: usize, - const K: usize, - const MAX_WORDS: usize, - const PAR: usize, -> MerklePath - where - MerkleChipOptimized: MerkleInstructions + Clone, + C: CurveAffine, + MerkleChipOptimized, + const PATH_LENGTH: usize, + const K: usize, + const MAX_WORDS: usize, + const PAR: usize, + > MerklePath +where + MerkleChipOptimized: MerkleInstructions + Clone, { /// Constructs a [`MerklePath`]. /// @@ -99,15 +98,15 @@ impl< #[allow(non_snake_case)] impl< - C: CurveAffine, - MerkleChipOptimized, - const PATH_LENGTH: usize, - const K: usize, - const MAX_WORDS: usize, - const PAR: usize, -> MerklePath - where - MerkleChipOptimized: MerkleInstructions + Clone, + C: CurveAffine, + MerkleChipOptimized, + const PATH_LENGTH: usize, + const K: usize, + const MAX_WORDS: usize, + const PAR: usize, + > MerklePath +where + MerkleChipOptimized: MerkleInstructions + Clone, { /// Calculates the root of the tree containing the given leaf at this Merkle path. /// @@ -177,6 +176,7 @@ pub mod tests { MerklePath, }; + use crate::sinsemilla_opt::chip::SinsemillaChipOptimized; use crate::{ ecc::tests::TestFixedBases, sinsemilla::{ @@ -185,11 +185,6 @@ pub mod tests { }, utilities::{i2lebsp, lookup_range_check::LookupRangeCheckConfig, UtilitiesInstructions}, }; - use crate::{ - sinsemilla_opt::{ - chip::SinsemillaChipOptimized, - }, - }; use group::ff::{Field, PrimeField, PrimeFieldBits}; use halo2_proofs::{ @@ -199,11 +194,11 @@ pub mod tests { plonk::{Circuit, ConstraintSystem, Error}, }; - use rand::{rngs::OsRng, RngCore}; - use std::{convert::TryInto, iter}; use crate::sinsemilla::chip::SinsemillaChipProps; use crate::utilities::lookup_range_check::LookupRangeCheck; use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; + use rand::{rngs::OsRng, RngCore}; + use std::{convert::TryInto, iter}; const MERKLE_DEPTH: usize = 32; @@ -404,4 +399,4 @@ pub mod tests { .render(11, &circuit, &root) .unwrap(); } -} \ No newline at end of file +} diff --git a/halo2_gadgets/src/sinsemilla_opt/merkle/chip.rs b/halo2_gadgets/src/sinsemilla_opt/merkle/chip.rs index ef88473cb9..474de3044d 100644 --- a/halo2_gadgets/src/sinsemilla_opt/merkle/chip.rs +++ b/halo2_gadgets/src/sinsemilla_opt/merkle/chip.rs @@ -9,14 +9,15 @@ use pasta_curves::pallas; use super::MerkleInstructions; +use crate::sinsemilla::chip::{SinsemillaChipProps, SinsemillaConfigProps}; +use crate::sinsemilla_opt::chip::{SinsemillaChipOptimized, SinsemillaConfigOptimized}; +use crate::utilities::lookup_range_check::witness_short_generic; use crate::{ sinsemilla::{primitives as sinsemilla, MessagePiece}, utilities::RangeConstrained, { ecc::FixedPoints, - sinsemilla::{ - CommitDomains, HashDomains, SinsemillaInstructions, - }, + sinsemilla::{CommitDomains, HashDomains, SinsemillaInstructions}, utilities::{ cond_swap::{CondSwapChip, CondSwapConfig, CondSwapInstructions}, UtilitiesInstructions, @@ -24,17 +25,14 @@ use crate::{ }, }; use group::ff::PrimeField; -use crate::sinsemilla::chip::{SinsemillaChipProps, SinsemillaConfigProps}; -use crate::sinsemilla_opt::chip::{SinsemillaChipOptimized, SinsemillaConfigOptimized}; -use crate::utilities::lookup_range_check::witness_short_generic; /// Configuration for the `MerkleChipOptimized` implementation. #[derive(Clone, Debug, PartialEq, Eq)] pub struct MerkleConfigOptimized - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { advices: [Column; 5], q_decompose: Selector, @@ -54,19 +52,19 @@ pub struct MerkleConfigOptimized /// `left` and `right`. #[derive(Clone, Debug, PartialEq, Eq)] pub struct MerkleChipOptimized - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { config: MerkleConfigOptimized, } impl Chip for MerkleChipOptimized - where - Hash: HashDomains, - Fixed: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + Fixed: FixedPoints, + Commit: CommitDomains, { type Config = MerkleConfigOptimized; type Loaded = (); @@ -81,10 +79,10 @@ impl Chip for MerkleChipOptimized MerkleChipOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { /// Configures the [`MerkleChipOptimized`]. pub fn configure( @@ -199,12 +197,12 @@ impl MerkleChipOptimized } impl -MerkleInstructions -for MerkleChipOptimized - where - Hash: HashDomains + Eq, - F: FixedPoints, - Commit: CommitDomains + Eq, + MerkleInstructions + for MerkleChipOptimized +where + Hash: HashDomains + Eq, + F: FixedPoints, + Commit: CommitDomains + Eq, { #[allow(non_snake_case)] fn hash_layer( @@ -418,19 +416,19 @@ for MerkleChipOptimized } impl UtilitiesInstructions for MerkleChipOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type Var = AssignedCell; } impl CondSwapInstructions for MerkleChipOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { #[allow(clippy::type_complexity)] fn swap( @@ -458,11 +456,11 @@ impl CondSwapInstructions for MerkleChipOptimized } impl SinsemillaInstructions -for MerkleChipOptimized - where - Hash: HashDomains, - F: FixedPoints, - Commit: CommitDomains, + for MerkleChipOptimized +where + Hash: HashDomains, + F: FixedPoints, + Commit: CommitDomains, { type CellValue = as SinsemillaInstructions< pallas::Affine, @@ -554,4 +552,3 @@ for MerkleChipOptimized SinsemillaChipOptimized::::extract(point) } } - diff --git a/halo2_gadgets/src/utilities/cond_swap.rs b/halo2_gadgets/src/utilities/cond_swap.rs index 2e292f3195..504685800e 100644 --- a/halo2_gadgets/src/utilities/cond_swap.rs +++ b/halo2_gadgets/src/utilities/cond_swap.rs @@ -2,11 +2,16 @@ use super::{bool_check, ternary, UtilitiesInstructions}; +use crate::ecc::chip::{EccPoint, NonIdentityEccPoint}; use group::ff::{Field, PrimeField}; -use halo2_proofs::{circuit::{AssignedCell, Chip, Layouter, Value}, plonk::{Advice, Column, ConstraintSystem, Constraints, Error, Selector}, plonk, poly::Rotation}; -use std::marker::PhantomData; +use halo2_proofs::{ + circuit::{AssignedCell, Chip, Layouter, Value}, + plonk, + plonk::{Advice, Column, ConstraintSystem, Constraints, Error, Selector}, + poly::Rotation, +}; use pasta_curves::pallas; -use crate::ecc::chip::{EccPoint, NonIdentityEccPoint}; +use std::marker::PhantomData; /// Instructions for a conditional swap gadget. pub trait CondSwapInstructions: UtilitiesInstructions { @@ -30,7 +35,6 @@ pub trait CondSwapInstructions: UtilitiesInstructions { left: Self::Var, right: Self::Var, ) -> Result; - } /// A chip implementing a conditional swap. diff --git a/halo2_gadgets/src/utilities/lookup_range_check.rs b/halo2_gadgets/src/utilities/lookup_range_check.rs index 047c52afe3..de9ddaa1df 100644 --- a/halo2_gadgets/src/utilities/lookup_range_check.rs +++ b/halo2_gadgets/src/utilities/lookup_range_check.rs @@ -8,9 +8,9 @@ use halo2_proofs::{ }; use std::{convert::TryInto, marker::PhantomData}; -use ff::PrimeFieldBits; use crate::sinsemilla::chip::{SinsemillaConfig, SinsemillaConfigProps}; use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; +use ff::PrimeFieldBits; use super::*; @@ -33,22 +33,24 @@ pub fn witness_short_generic( value: Value<&F>, bitrange: Range, ) -> Result>, Error> - where - F: PrimeFieldBits, - C: LookupRangeCheck, +where + F: PrimeFieldBits, + C: LookupRangeCheck, { let num_bits = bitrange.len(); assert!(num_bits < K); - lookup_config.witness_short_check( - layouter, - value.map(|value| bitrange_subset(value, bitrange)), - num_bits, - ).map(|inner| RangeConstrained { - inner, - num_bits, - _phantom: PhantomData::default(), - }) + lookup_config + .witness_short_check( + layouter, + value.map(|value| bitrange_subset(value, bitrange)), + num_bits, + ) + .map(|inner| RangeConstrained { + inner, + num_bits, + _phantom: PhantomData::default(), + }) } /// Configuration that provides methods for a lookup range check. @@ -73,8 +75,8 @@ pub trait LookupRangeCheck { running_sum: Column, table_idx: TableColumn, ) -> Self - where - Self: Sized; + where + Self: Sized; #[cfg(test)] fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error>; @@ -164,7 +166,7 @@ pub trait LookupRangeCheck { .map(|word| F::from(lebs2ip::(&(word.try_into().unwrap())))) .collect::>() }) - .transpose_vec(num_words) + .transpose_vec(num_words) }; let mut zs = vec![element.clone()]; @@ -262,12 +264,9 @@ pub trait LookupRangeCheck { }, ) } - } -impl LookupRangeCheck -for LookupRangeCheckConfig -{ +impl LookupRangeCheck for LookupRangeCheckConfig { fn base(&self) -> &LookupRangeCheckConfig { self } @@ -415,7 +414,7 @@ for LookupRangeCheckConfig } #[cfg(test)] mod tests { - use super::{LookupRangeCheckConfig, LookupRangeCheck}; + use super::{LookupRangeCheck, LookupRangeCheckConfig}; use super::super::lebs2ip; use crate::sinsemilla::primitives::K; diff --git a/halo2_gadgets/src/utilities_opt/cond_swap.rs b/halo2_gadgets/src/utilities_opt/cond_swap.rs index e69de29bb2..8b13789179 100644 --- a/halo2_gadgets/src/utilities_opt/cond_swap.rs +++ b/halo2_gadgets/src/utilities_opt/cond_swap.rs @@ -0,0 +1 @@ + diff --git a/halo2_gadgets/src/utilities_opt/lookup_range_check.rs b/halo2_gadgets/src/utilities_opt/lookup_range_check.rs index b7e1583557..d3e17320f7 100644 --- a/halo2_gadgets/src/utilities_opt/lookup_range_check.rs +++ b/halo2_gadgets/src/utilities_opt/lookup_range_check.rs @@ -17,11 +17,9 @@ use halo2_proofs::circuit::{Layouter, Value}; use ff::PrimeFieldBits; -use crate::utilities::lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheck}; +use crate::utilities::lookup_range_check::{LookupRangeCheck, LookupRangeCheckConfig}; use crate::utilities::{bitrange_subset, RangeConstrained}; - - /// Configuration that provides methods for a lookup range check. #[derive(Eq, PartialEq, Debug, Clone, Copy)] pub struct LookupRangeCheckConfigOptimized { @@ -122,15 +120,15 @@ impl LookupRangeCheckConfigOptimized { // - 0 otherwise let num_bits = q_range_check_5.clone() * Expression::Constant(F::from(5_u64)) + (one.clone() - q_range_check_5) - * q_range_check_4 - * Expression::Constant(F::from(4_u64)); + * q_range_check_4 + * Expression::Constant(F::from(4_u64)); // Combine the running sum, short lookups and optimized range checks: vec![ ( q_lookup.clone() * ((one - q_range_check.clone()) * (running_sum_lookup + short_lookup) - + q_range_check.clone() * z_cur), + + q_range_check.clone() * z_cur), config.base.table_idx, ), ( @@ -167,7 +165,7 @@ impl LookupRangeCheckConfigOptimized { } impl LookupRangeCheck -for LookupRangeCheckConfigOptimized + for LookupRangeCheckConfigOptimized { fn base(&self) -> &LookupRangeCheckConfig { &self.base @@ -512,8 +510,8 @@ mod tests { // => element = shifted * 2^{s-K} let element = shifted * pallas::Base::from(1 << (K as u64 - num_bits)) - .invert() - .unwrap(); + .invert() + .unwrap(); let circuit: MyCircuit = MyCircuit { element: Value::known(element), num_bits: num_bits as usize, @@ -560,4 +558,4 @@ mod tests { ); } } -} \ No newline at end of file +} diff --git a/halo2_proofs/src/circuit.rs b/halo2_proofs/src/circuit.rs index 0822d8d8aa..9a792942cc 100644 --- a/halo2_proofs/src/circuit.rs +++ b/halo2_proofs/src/circuit.rs @@ -139,6 +139,15 @@ impl AssignedCell, F> { } } +impl From> for AssignedCell, F> { + fn from(ac: AssignedCell) -> Self { + AssignedCell { + value: ac.value.map(|a| a.into()), + cell: ac.cell, + _marker: Default::default(), + } + } +} impl AssignedCell where for<'v> Assigned: From<&'v V>,