Skip to content

Commit

Permalink
Add LookupRangeCheckConfigDomain trait, split LookupRangeCheckConfig …
Browse files Browse the repository at this point in the history
…into LookupRangeCheckConfig and LookupRangeCheckConfigOptimized and modify the code to use them
  • Loading branch information
dmidem committed Apr 7, 2024
1 parent 5d7ed7d commit 190639d
Show file tree
Hide file tree
Showing 14 changed files with 805 additions and 603 deletions.
31 changes: 29 additions & 2 deletions halo2_gadgets/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ use halo2_proofs::{
plonk::Error,
};

use crate::utilities::UtilitiesInstructions;
use pasta_curves::pallas;

use crate::{
sinsemilla::primitives as sinsemilla,
utilities::{
lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheckConfigDomain},
UtilitiesInstructions,
},
};

pub mod chip;

Expand Down Expand Up @@ -159,6 +167,19 @@ pub trait BaseFitsInScalarInstructions<C: CurveAffine>: EccInstructions<C> {
) -> Result<Self::ScalarVar, Error>;
}

/// FIXME: add doc
pub trait EccLookupRangeCheckConfig:
LookupRangeCheckConfigDomain<pallas::Base, { sinsemilla::K }>
+ Eq
+ PartialEq
+ Clone
+ Copy
+ Debug
{
}

impl EccLookupRangeCheckConfig for LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }> {}

/// Defines the fixed points for a given instantiation of the ECC chip.
pub trait FixedPoints<C: CurveAffine>: Debug + Eq + Clone {
/// Fixed points that can be used with full-width scalar multiplication.
Expand All @@ -167,6 +188,8 @@ pub trait FixedPoints<C: CurveAffine>: Debug + Eq + Clone {
type ShortScalar: Debug + Eq + Clone;
/// Fixed points that can be multiplied by base field elements.
type Base: Debug + Eq + Clone;
/// FIXME: add doc
type LookupRangeCheckConfig: EccLookupRangeCheckConfig;
}

/// An integer representing an element of the scalar field for a specific elliptic curve.
Expand Down Expand Up @@ -595,7 +618,10 @@ pub(crate) mod tests {
},
FixedPoints,
};
use crate::utilities::lookup_range_check::LookupRangeCheckConfig;
use crate::{
sinsemilla::primitives as sinsemilla,
utilities::lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheckConfigDomain},
};

#[derive(Debug, Eq, PartialEq, Clone)]
pub(crate) struct TestFixedBases;
Expand Down Expand Up @@ -721,6 +747,7 @@ pub(crate) mod tests {
type FullScalar = FullWidth;
type ShortScalar = Short;
type Base = BaseField;
type LookupRangeCheckConfig = LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>;
}

struct MyCircuit {
Expand Down
13 changes: 6 additions & 7 deletions halo2_gadgets/src/ecc/chip.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Chip implementations for the ECC gadgets.
use super::{BaseFitsInScalarInstructions, EccInstructions, FixedPoints};
use crate::{
sinsemilla::primitives as sinsemilla,
utilities::{lookup_range_check::LookupRangeCheckConfig, UtilitiesInstructions},
use super::{
BaseFitsInScalarInstructions, EccInstructions, EccLookupRangeCheckConfig, FixedPoints,
};
use crate::utilities::UtilitiesInstructions;
use arrayvec::ArrayVec;

use ff::PrimeField;
Expand Down Expand Up @@ -148,7 +147,7 @@ pub struct EccConfig<FixedPoints: super::FixedPoints<pallas::Affine>> {
add: add::Config,

/// Variable-base scalar multiplication
mul: mul::Config,
mul: mul::Config<FixedPoints::LookupRangeCheckConfig>,

/// Fixed-base full-width scalar multiplication
mul_fixed_full: mul_fixed::full_width::Config<FixedPoints>,
Expand All @@ -161,7 +160,7 @@ pub struct EccConfig<FixedPoints: super::FixedPoints<pallas::Affine>> {
pub(crate) witness_point: witness_point::Config,

/// Lookup range check using 10-bit lookup table
pub lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
pub lookup_config: FixedPoints::LookupRangeCheckConfig,
}

/// A trait representing the kind of scalar used with a particular `FixedPoint`.
Expand Down Expand Up @@ -264,7 +263,7 @@ impl<FixedPoints: super::FixedPoints<pallas::Affine>> EccChip<FixedPoints> {
meta: &mut ConstraintSystem<pallas::Base>,
advices: [Column<Advice>; 10],
lagrange_coeffs: [Column<Fixed>; 8],
range_check: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
range_check: FixedPoints::LookupRangeCheckConfig,
) -> <Self as Chip<pallas::Base>>::Config {
// Create witness point gate
let witness_point = witness_point::Config::configure(meta, advices[0], advices[1]);
Expand Down
15 changes: 6 additions & 9 deletions halo2_gadgets/src/ecc/chip/mul.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::{add, EccPoint, NonIdentityEccPoint, ScalarVar, T_Q};
use crate::{
sinsemilla::primitives as sinsemilla,
utilities::{bool_check, lookup_range_check::LookupRangeCheckConfig, ternary},
};
use super::{add, EccLookupRangeCheckConfig, EccPoint, NonIdentityEccPoint, ScalarVar, T_Q};
use crate::utilities::{bool_check, ternary};
use std::{
convert::TryInto,

Check warning on line 4 in halo2_gadgets/src/ecc/chip/mul.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `TryInto` is imported redundantly

warning: the item `TryInto` is imported redundantly --> halo2_gadgets/src/ecc/chip/mul.rs:4:5 | 4 | convert::TryInto, | ^^^^^^^^^^^^^^^^ --> /rustc/9eff5103597e0ce73244028c96c75866dbcbad1d/library/std/src/prelude/mod.rs:129:13 | = note: the item `TryInto` is already defined here

Check warning on line 4 in halo2_gadgets/src/ecc/chip/mul.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `TryInto` is imported redundantly

warning: the item `TryInto` is imported redundantly --> halo2_gadgets/src/ecc/chip/mul.rs:4:5 | 4 | convert::TryInto, | ^^^^^^^^^^^^^^^^ --> /rustc/9eff5103597e0ce73244028c96c75866dbcbad1d/library/std/src/prelude/mod.rs:129:13 | = note: the item `TryInto` is already defined here
ops::{Deref, Range},
Expand Down Expand Up @@ -46,7 +43,7 @@ const INCOMPLETE_LO_LEN: usize = INCOMPLETE_LEN - INCOMPLETE_HI_LEN;
const COMPLETE_RANGE: Range<usize> = INCOMPLETE_LEN..(INCOMPLETE_LEN + NUM_COMPLETE_BITS);

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Config {
pub struct Config<LookupRangeCheckConfig: EccLookupRangeCheckConfig> {
// Selector used to check switching logic on LSB
q_mul_lsb: Selector,
// Configuration used in complete addition
Expand All @@ -58,14 +55,14 @@ pub struct Config {
// Configuration used for complete addition part of double-and-add algorithm
complete_config: complete::Config,
// Configuration used to check for overflow
overflow_config: overflow::Config,
overflow_config: overflow::Config<LookupRangeCheckConfig>,
}

impl Config {
impl<LookupRangeCheckConfig: EccLookupRangeCheckConfig> Config<LookupRangeCheckConfig> {
pub(super) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
add_config: add::Config,
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: LookupRangeCheckConfig,
advices: [Column<Advice>; 10],
) -> Self {
let hi_config = incomplete::Config::configure(
Expand Down
14 changes: 6 additions & 8 deletions halo2_gadgets/src/ecc/chip/mul/overflow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::{T_Q, Z};
use crate::{
sinsemilla::primitives as sinsemilla, utilities::lookup_range_check::LookupRangeCheckConfig,
};
use super::{EccLookupRangeCheckConfig, T_Q, Z};
use crate::sinsemilla::primitives as sinsemilla;

use group::ff::PrimeField;
use halo2_proofs::circuit::AssignedCell;
Expand All @@ -15,19 +13,19 @@ use pasta_curves::pallas;
use std::iter;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Config {
pub struct Config<LookupRangeCheckConfig: EccLookupRangeCheckConfig> {
// Selector to check z_0 = alpha + t_q (mod p)
q_mul_overflow: Selector,
// 10-bit lookup table
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: LookupRangeCheckConfig,
// Advice columns
advices: [Column<Advice>; 3],
}

impl Config {
impl<LookupRangeCheckConfig: EccLookupRangeCheckConfig> Config<LookupRangeCheckConfig> {
pub(super) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: LookupRangeCheckConfig,
advices: [Column<Advice>; 3],
) -> Self {
for advice in advices.iter() {
Expand Down
10 changes: 4 additions & 6 deletions halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use super::super::{EccBaseFieldElemFixed, EccPoint, FixedPoints, NUM_WINDOWS, T_P};
use super::H_BASE;

use crate::utilities::bool_check;
use crate::{
sinsemilla::primitives as sinsemilla,
utilities::{bitrange_subset, lookup_range_check::LookupRangeCheckConfig, range_check},
use crate::utilities::{
bitrange_subset, bool_check, lookup_range_check::LookupRangeCheckConfigDomain, range_check,
};

use group::ff::PrimeField;
Expand All @@ -21,15 +19,15 @@ use std::convert::TryInto;
pub struct Config<Fixed: FixedPoints<pallas::Affine>> {
q_mul_fixed_base_field: Selector,
canon_advices: [Column<Advice>; 3],
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: Fixed::LookupRangeCheckConfig,
super_config: super::Config<Fixed>,
}

impl<Fixed: FixedPoints<pallas::Affine>> Config<Fixed> {
pub(crate) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
canon_advices: [Column<Advice>; 3],
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: Fixed::LookupRangeCheckConfig,
super_config: super::Config<Fixed>,
) -> Self {
for advice in canon_advices.iter() {
Expand Down
5 changes: 4 additions & 1 deletion halo2_gadgets/src/ecc/chip/mul_fixed/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ pub mod tests {
tests::{Short, TestFixedBases},
FixedPointShort, NonIdentityPoint, Point, ScalarFixedShort,
},
utilities::{lookup_range_check::LookupRangeCheckConfig, UtilitiesInstructions},
utilities::{
lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheckConfigDomain},
UtilitiesInstructions,
},
};

#[allow(clippy::op_ref)]
Expand Down
8 changes: 4 additions & 4 deletions halo2_gadgets/src/ecc_opt/chip/mul_fixed/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ pub mod tests {
use pasta_curves::pallas;

use crate::{
ecc::{
chip::{EccChip},
tests::{TestFixedBases}, Point,
ecc::{chip::EccChip, tests::TestFixedBases, Point},
utilities::{
lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheckConfigDomain},
UtilitiesInstructions,
},
utilities::{lookup_range_check::LookupRangeCheckConfig, UtilitiesInstructions},
};

pub(crate) fn test_mul_sign(
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/sinsemilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ pub(crate) mod tests {
tests::{FullWidth, TestFixedBases},
NonIdentityPoint,
},
utilities::lookup_range_check::LookupRangeCheckConfig,
utilities::lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheckConfigDomain},
},
};

Expand Down
14 changes: 8 additions & 6 deletions halo2_gadgets/src/sinsemilla/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
FixedPoints,
},
sinsemilla_opt::SinsemillaInstructionsOptimized,
utilities::lookup_range_check::LookupRangeCheckConfig,
utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized,
};
use std::marker::PhantomData;

Expand Down Expand Up @@ -61,7 +61,7 @@ where
/// generators of the Sinsemilla hash.
pub(crate) generator_table_optimized: GeneratorTableConfigOptimized,
/// An advice column configured to perform lookup range checks.
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: LookupRangeCheckConfigOptimized<pallas::Base, { sinsemilla::K }>,
_marker: PhantomData<(Hash, Commit, F)>,
}

Expand All @@ -83,7 +83,9 @@ where
}

/// Returns the lookup range check config used in this config.
pub fn lookup_config(&self) -> LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }> {
pub fn lookup_config(
&self,
) -> LookupRangeCheckConfigOptimized<pallas::Base, { sinsemilla::K }> {
self.lookup_config
}

Expand Down Expand Up @@ -157,13 +159,13 @@ where
witness_pieces: Column<Advice>,
fixed_y_q: Column<Fixed>,
lookup: (TableColumn, TableColumn, TableColumn),
range_check: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
range_check: LookupRangeCheckConfigOptimized<pallas::Base, { sinsemilla::K }>,
) -> <Self as Chip<pallas::Base>>::Config {
let lookup = (
lookup.0,
lookup.1,
lookup.2,
range_check.zsa.map(|zsa| zsa.table_range_check_tag),
Some(range_check.table_range_check_tag()),
);
Self::configure_with_tag(
meta,
Expand All @@ -186,7 +188,7 @@ where
witness_pieces: Column<Advice>,
fixed_y_q: Column<Fixed>,
lookup: (TableColumn, TableColumn, TableColumn, Option<TableColumn>),
range_check: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
range_check: LookupRangeCheckConfigOptimized<pallas::Base, { sinsemilla::K }>,
) -> <Self as Chip<pallas::Base>>::Config {
// Enable equality on all advice columns
for advice in advices.iter() {
Expand Down
6 changes: 5 additions & 1 deletion halo2_gadgets/src/sinsemilla/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ pub mod tests {
tests::{TestCommitDomain, TestHashDomain},
HashDomains,
},
utilities::{i2lebsp, lookup_range_check::LookupRangeCheckConfig, UtilitiesInstructions},
utilities::{
i2lebsp,
lookup_range_check::{LookupRangeCheckConfig, LookupRangeCheckConfigDomain},
UtilitiesInstructions,
},
};

use group::ff::{Field, PrimeField, PrimeFieldBits};
Expand Down
Loading

0 comments on commit 190639d

Please sign in to comment.