Skip to content

Commit

Permalink
update code for hash
Browse files Browse the repository at this point in the history
  • Loading branch information
YaoGalteland committed Apr 29, 2024
1 parent 989f29b commit 507a53c
Show file tree
Hide file tree
Showing 22 changed files with 918 additions and 395 deletions.
1 change: 0 additions & 1 deletion halo2_gadgets/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ pub(crate) mod tests {
FixedPoints,
};
use crate::{
sinsemilla::primitives as sinsemilla,
utilities::lookup_range_check::{LookupRangeCheck, LookupRangeCheckConfig},
};

Expand Down
38 changes: 19 additions & 19 deletions halo2_gadgets/src/ecc/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl From<NonIdentityEccPoint> for EccPoint {
#[allow(non_snake_case)]
pub struct EccConfig<
FixedPoints: super::FixedPoints<pallas::Affine>,
LookupRangeCheckConfig: DefaultLookupRangeCheck,
Lookup: DefaultLookupRangeCheck,
> {
/// Advice columns needed by instructions in the ECC chip.
pub advices: [Column<Advice>; 10],
Expand All @@ -148,20 +148,20 @@ pub struct EccConfig<
add: add::Config,

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

/// Fixed-base full-width scalar multiplication
mul_fixed_full: mul_fixed::full_width::Config<FixedPoints>,
/// Fixed-base signed short scalar multiplication
pub(crate) mul_fixed_short: mul_fixed::short::Config<FixedPoints>,
/// Fixed-base mul using a base field element as a scalar
mul_fixed_base_field: mul_fixed::base_field_elem::Config<FixedPoints, LookupRangeCheckConfig>,
mul_fixed_base_field: mul_fixed::base_field_elem::Config<FixedPoints, Lookup>,

/// Witness point
pub(crate) witness_point: witness_point::Config,

/// Lookup range check using 10-bit lookup table
pub lookup_config: LookupRangeCheckConfig,
pub lookup_config: Lookup,
}

/// A trait representing the kind of scalar used with a particular `FixedPoint`.
Expand Down Expand Up @@ -229,17 +229,17 @@ pub trait FixedPoint<C: CurveAffine>: std::fmt::Debug + Eq + Clone {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EccChip<
FixedPoints: super::FixedPoints<pallas::Affine>,
LookupRangeCheckConfig: DefaultLookupRangeCheck,
Lookup: DefaultLookupRangeCheck,
> {
config: EccConfig<FixedPoints, LookupRangeCheckConfig>,
config: EccConfig<FixedPoints, Lookup>,
}

impl<
FixedPoints: super::FixedPoints<pallas::Affine>,
LookupRangeCheckConfig: DefaultLookupRangeCheck,
> Chip<pallas::Base> for EccChip<FixedPoints, LookupRangeCheckConfig>
Lookup: DefaultLookupRangeCheck,
> Chip<pallas::Base> for EccChip<FixedPoints, Lookup>
{
type Config = EccConfig<FixedPoints, LookupRangeCheckConfig>;
type Config = EccConfig<FixedPoints, Lookup>;
type Loaded = ();

fn config(&self) -> &Self::Config {
Expand All @@ -253,16 +253,16 @@ impl<

impl<
Fixed: super::FixedPoints<pallas::Affine>,
LookupRangeCheckConfig: DefaultLookupRangeCheck,
> UtilitiesInstructions<pallas::Base> for EccChip<Fixed, LookupRangeCheckConfig>
Lookup: DefaultLookupRangeCheck,
> UtilitiesInstructions<pallas::Base> for EccChip<Fixed, Lookup>
{
type Var = AssignedCell<pallas::Base, pallas::Base>;
}

impl<
FixedPoints: super::FixedPoints<pallas::Affine>,
LookupRangeCheckConfig: DefaultLookupRangeCheck,
> EccChip<FixedPoints, LookupRangeCheckConfig>
Lookup: DefaultLookupRangeCheck,
> EccChip<FixedPoints, Lookup>
{
/// Reconstructs this chip from the given config.
pub fn construct(config: <Self as Chip<pallas::Base>>::Config) -> Self {
Expand All @@ -277,7 +277,7 @@ impl<
meta: &mut ConstraintSystem<pallas::Base>,
advices: [Column<Advice>; 10],
lagrange_coeffs: [Column<Fixed>; 8],
range_check: LookupRangeCheckConfig,
range_check: Lookup,
) -> <Self as Chip<pallas::Base>>::Config {
// Create witness point gate
let witness_point = witness_point::Config::configure(meta, advices[0], advices[1]);
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<

// Create gate that is only used in fixed-base mul using a base field element.
let mul_fixed_base_field =
mul_fixed::base_field_elem::Config::<FixedPoints, LookupRangeCheckConfig>::configure(
mul_fixed::base_field_elem::Config::<FixedPoints, Lookup>::configure(
meta,
advices[6..9].try_into().unwrap(),
range_check,
Expand Down Expand Up @@ -421,8 +421,8 @@ pub enum ScalarVar {
FullWidth,
}

impl<Fixed: FixedPoints<pallas::Affine>, LookupRangeCheckConfig: DefaultLookupRangeCheck>
EccInstructions<pallas::Affine> for EccChip<Fixed, LookupRangeCheckConfig>
impl<Fixed: FixedPoints<pallas::Affine>, Lookup: DefaultLookupRangeCheck>
EccInstructions<pallas::Affine> for EccChip<Fixed, Lookup>
where
<Fixed as FixedPoints<pallas::Affine>>::Base:
FixedPoint<pallas::Affine, FixedScalarKind = BaseFieldElem>,
Expand Down Expand Up @@ -609,8 +609,8 @@ where
}
}

impl<Fixed: FixedPoints<pallas::Affine>, LookupRangeCheckConfig: DefaultLookupRangeCheck>
BaseFitsInScalarInstructions<pallas::Affine> for EccChip<Fixed, LookupRangeCheckConfig>
impl<Fixed: FixedPoints<pallas::Affine>, Lookup: DefaultLookupRangeCheck>
BaseFitsInScalarInstructions<pallas::Affine> for EccChip<Fixed, Lookup>
where
<Fixed as FixedPoints<pallas::Affine>>::Base:
FixedPoint<pallas::Affine, FixedScalarKind = BaseFieldElem>,
Expand Down
18 changes: 9 additions & 9 deletions halo2_gadgets/src/ecc/chip/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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<LookupRangeCheckConfig: DefaultLookupRangeCheck> {
pub struct Config<Lookup: DefaultLookupRangeCheck> {
// Selector used to check switching logic on LSB
q_mul_lsb: Selector,
// Configuration used in complete addition
Expand All @@ -58,14 +58,14 @@ pub struct Config<LookupRangeCheckConfig: DefaultLookupRangeCheck> {
// 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<LookupRangeCheckConfig>,
overflow_config: overflow::Config<Lookup>,
}

impl<LookupRangeCheckConfig: DefaultLookupRangeCheck> Config<LookupRangeCheckConfig> {
impl<Lookup: DefaultLookupRangeCheck> Config<Lookup> {
pub(crate) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
add_config: add::Config,
lookup_config: LookupRangeCheckConfig,
lookup_config: Lookup,
advices: [Column<Advice>; 10],
) -> Self {
let hi_config = incomplete::Config::configure(
Expand Down Expand Up @@ -461,13 +461,13 @@ pub mod tests {
Curve,
};
use halo2_proofs::{
circuit::{Chip, Layouter, Value},
circuit::{Layouter, Value},
plonk::Error,
};
use pasta_curves::pallas;
use rand::rngs::OsRng;

use crate::utilities::lookup_range_check::{DefaultLookupRangeCheck, LookupRangeCheckConfig};
use crate::utilities::lookup_range_check::{DefaultLookupRangeCheck};
use crate::{
ecc::{
chip::{EccChip, EccPoint},
Expand All @@ -477,10 +477,10 @@ pub mod tests {
utilities::UtilitiesInstructions,
};

pub(crate) fn test_mul<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
pub(crate) fn test_mul<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
p: &NonIdentityPoint<pallas::Affine, EccChip<TestFixedBases, LookupRangeCheckConfig>>,
p: &NonIdentityPoint<pallas::Affine, EccChip<TestFixedBases, Lookup>>,
p_val: pallas::Affine,
) -> Result<(), Error> {
let column = chip.config.advices[0];
Expand Down
8 changes: 4 additions & 4 deletions halo2_gadgets/src/ecc/chip/mul/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ use pasta_curves::pallas;
use std::iter;

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

impl<LookupRangeCheckConfig: DefaultLookupRangeCheck> Config<LookupRangeCheckConfig> {
impl<Lookup: DefaultLookupRangeCheck> Config<Lookup> {
pub(super) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
lookup_config: LookupRangeCheckConfig,
lookup_config: Lookup,
advices: [Column<Advice>; 3],
) -> Self {
for advice in advices.iter() {
Expand Down
26 changes: 13 additions & 13 deletions halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ use std::convert::TryInto;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Config<
Fixed: FixedPoints<pallas::Affine>,
LookupRangeCheckConfig: DefaultLookupRangeCheck,
Lookup: DefaultLookupRangeCheck,
> {
q_mul_fixed_base_field: Selector,
canon_advices: [Column<Advice>; 3],
lookup_config: LookupRangeCheckConfig,
lookup_config: Lookup,
super_config: super::Config<Fixed>,
}

impl<Fixed: FixedPoints<pallas::Affine>, LookupRangeCheckConfig: DefaultLookupRangeCheck>
Config<Fixed, LookupRangeCheckConfig>
impl<Fixed: FixedPoints<pallas::Affine>, Lookup: DefaultLookupRangeCheck>
Config<Fixed, Lookup>
{
pub(crate) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
canon_advices: [Column<Advice>; 3],
lookup_config: LookupRangeCheckConfig,
lookup_config: Lookup,
super_config: super::Config<Fixed>,
) -> Self {
for advice in canon_advices.iter() {
Expand Down Expand Up @@ -401,8 +401,8 @@ pub mod tests {
utilities::UtilitiesInstructions,
};

pub(crate) fn test_mul_fixed_base_field<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
pub(crate) fn test_mul_fixed_base_field<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
) -> Result<(), Error> {
test_single_base(
Expand All @@ -414,22 +414,22 @@ pub mod tests {
}

#[allow(clippy::op_ref)]
fn test_single_base<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
fn test_single_base<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
base: FixedPointBaseField<pallas::Affine, EccChip<TestFixedBases, LookupRangeCheckConfig>>,
base: FixedPointBaseField<pallas::Affine, EccChip<TestFixedBases, Lookup>>,
base_val: pallas::Affine,
) -> Result<(), Error> {
let rng = OsRng;

let column = chip.config().advices[0];

fn constrain_equal_non_id<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
fn constrain_equal_non_id<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
base_val: pallas::Affine,
scalar_val: pallas::Base,
result: Point<pallas::Affine, EccChip<TestFixedBases, LookupRangeCheckConfig>>,
result: Point<pallas::Affine, EccChip<TestFixedBases, Lookup>>,
) -> Result<(), Error> {
// Move scalar from base field into scalar field (which always fits for Pallas).
let scalar = pallas::Scalar::from_repr(scalar_val.to_repr()).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions halo2_gadgets/src/ecc/chip/mul_fixed/full_width.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ pub mod tests {
};
use crate::utilities::lookup_range_check::DefaultLookupRangeCheck;

pub(crate) fn test_mul_fixed<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
pub(crate) fn test_mul_fixed<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
) -> Result<(), Error> {
let test_base = FullWidth::from_pallas_generator();
Expand All @@ -210,18 +210,18 @@ pub mod tests {
}

#[allow(clippy::op_ref)]
fn test_single_base<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
fn test_single_base<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
base: FixedPoint<pallas::Affine, EccChip<TestFixedBases, LookupRangeCheckConfig>>,
base: FixedPoint<pallas::Affine, EccChip<TestFixedBases, Lookup>>,
base_val: pallas::Affine,
) -> Result<(), Error> {
fn constrain_equal_non_id<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
fn constrain_equal_non_id<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
base_val: pallas::Affine,
scalar_val: pallas::Scalar,
result: Point<pallas::Affine, EccChip<TestFixedBases, LookupRangeCheckConfig>>,
result: Point<pallas::Affine, EccChip<TestFixedBases, Lookup>>,
) -> Result<(), Error> {
let expected = NonIdentityPoint::new(
chip,
Expand Down
14 changes: 7 additions & 7 deletions halo2_gadgets/src/ecc/chip/mul_fixed/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@ pub mod tests {
};

#[allow(clippy::op_ref)]
pub(crate) fn test_mul_fixed_short<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
pub(crate) fn test_mul_fixed_short<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
) -> Result<(), Error> {
// test_short
let base_val = Short.generator();
let test_short = FixedPointShort::from_inner(chip.clone(), Short);

fn load_magnitude_sign<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
fn load_magnitude_sign<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
magnitude: pallas::Base,
sign: pallas::Base,
Expand All @@ -290,12 +290,12 @@ pub mod tests {
Ok((magnitude, sign))
}

fn constrain_equal_non_id<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
fn constrain_equal_non_id<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
base_val: pallas::Affine,
scalar_val: pallas::Scalar,
result: Point<pallas::Affine, EccChip<TestFixedBases, LookupRangeCheckConfig>>,
result: Point<pallas::Affine, EccChip<TestFixedBases, Lookup>>,
) -> Result<(), Error> {
let expected = NonIdentityPoint::new(
chip,
Expand Down
13 changes: 0 additions & 13 deletions halo2_gadgets/src/ecc_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,6 @@ pub(crate) mod tests {
find_zs_and_us(*BASE, NUM_WINDOWS_SHORT).unwrap();
}

impl FullWidth {
pub(crate) fn from_pallas_generator() -> Self {
FullWidth(*BASE, &ZS_AND_US)
}

pub(crate) fn from_parts(
base: pallas::Affine,
zs_and_us: &'static [(u64, [pallas::Base; H])],
) -> Self {
FullWidth(base, zs_and_us)
}
}

impl FixedPoint<pallas::Affine> for FullWidth {
type FixedScalarKind = FullScalar;

Expand Down
4 changes: 2 additions & 2 deletions halo2_gadgets/src/ecc_opt/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use super::EccInstructionsOptimized;
pub(crate) mod mul_fixed;
pub(super) mod witness_point;

impl<Fixed: FixedPoints<pallas::Affine>, LookupRangeCheckConfig: DefaultLookupRangeCheck>
EccInstructionsOptimized<pallas::Affine> for EccChip<Fixed, LookupRangeCheckConfig>
impl<Fixed: FixedPoints<pallas::Affine>, Lookup: DefaultLookupRangeCheck>
EccInstructionsOptimized<pallas::Affine> for EccChip<Fixed, Lookup>
where
<Fixed as FixedPoints<pallas::Affine>>::Base:
FixedPoint<pallas::Affine, FixedScalarKind = BaseFieldElem>,
Expand Down
5 changes: 2 additions & 3 deletions halo2_gadgets/src/ecc_opt/chip/mul_fixed/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@ pub mod tests {
use crate::{
ecc::{chip::EccChip, tests::TestFixedBases, Point},
utilities::{
lookup_range_check::{LookupRangeCheck, LookupRangeCheckConfig},
UtilitiesInstructions,
},
};

pub(crate) fn test_mul_sign<LookupRangeCheckConfig: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, LookupRangeCheckConfig>,
pub(crate) fn test_mul_sign<Lookup: DefaultLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
) -> Result<(), Error> {
// Generate a random non-identity point P
Expand Down
Loading

0 comments on commit 507a53c

Please sign in to comment.