diff --git a/src/circuit/gadget/ecc/chip.rs b/src/circuit/gadget/ecc/chip.rs index 2c5dcdb2..f20a36d2 100644 --- a/src/circuit/gadget/ecc/chip.rs +++ b/src/circuit/gadget/ecc/chip.rs @@ -11,10 +11,12 @@ use arrayvec::ArrayVec; use group::prime::PrimeCurveAffine; use halo2::{ circuit::{Chip, Layouter}, - plonk::{Advice, Column, ConstraintSystem, Error, Fixed, Selector}, + plonk::{Advice, Column, ConstraintSystem, Error, Fixed}, }; use pasta_curves::{arithmetic::CurveAffine, pallas}; +use std::convert::TryInto; + pub(super) mod add; pub(super) mod add_incomplete; pub(super) mod mul; @@ -148,8 +150,8 @@ pub struct EccConfig { mul_fixed_full: mul_fixed::full_width::Config, /// Fixed-base signed short scalar multiplication mul_fixed_short: mul_fixed::short::Config, - /// Canonicity checks on base field element used as scalar in fixed-base mul - pub q_mul_fixed_base_field: Selector, + /// Fixed-base mul using a base field element as a scalar + mul_fixed_base_field: mul_fixed::base_field_elem::Config, /// Witness point witness_point: witness_point::Config, @@ -196,11 +198,6 @@ impl EccChip { lagrange_coeffs: [Column; 8], range_check: LookupRangeCheckConfig, ) -> >::Config { - // The following columns need to be equality-enabled for their use in sub-configs: - // - // mul_fixed::base_field_element::Config: - // - [advices[6], advices[7], advices[8]]: canon_advices - // // TODO: Refactor away from `impl From for _` so that sub-configs can // equality-enable the columns they need to. for column in &advices { @@ -241,6 +238,14 @@ impl EccChip { // Create gate that is only used in short fixed-base scalar mul. let mul_fixed_short = mul_fixed::short::Config::configure(meta, mul_fixed); + // 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::configure( + meta, + advices[6..9].try_into().unwrap(), + range_check, + mul_fixed, + ); + let config = EccConfig { advices, add_incomplete, @@ -249,17 +254,11 @@ impl EccChip { mul_fixed, mul_fixed_full, mul_fixed_short, - q_mul_fixed_base_field: meta.selector(), + mul_fixed_base_field, witness_point, lookup_config: range_check, }; - // Create gate that is only used in fixed-base mul using a base field element. - { - let base_field_config: mul_fixed::base_field_elem::Config = (&config).into(); - base_field_config.create_gate(meta); - } - config } } @@ -448,7 +447,7 @@ impl EccInstructions for EccChip { base_field_elem: CellValue, base: &Self::FixedPointsBaseField, ) -> Result { - let config: mul_fixed::base_field_elem::Config = self.config().into(); + let config = self.config().mul_fixed_base_field; config.assign( layouter.namespace(|| format!("base-field elem fixed-base mul of {:?}", base)), base_field_elem, diff --git a/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs b/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs index e1c7169c..bf4a0765 100644 --- a/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs +++ b/src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs @@ -1,4 +1,4 @@ -use super::super::{EccBaseFieldElemFixed, EccConfig, EccPoint, NullifierK}; +use super::super::{EccBaseFieldElemFixed, EccPoint, NullifierK}; use super::H_BASE; use crate::{ @@ -18,6 +18,7 @@ use pasta_curves::{arithmetic::FieldExt, pallas}; use std::convert::TryInto; +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct Config { q_mul_fixed_base_field: Selector, canon_advices: [Column; 3], @@ -25,13 +26,22 @@ pub struct Config { super_config: super::Config, } -impl From<&EccConfig> for Config { - fn from(config: &EccConfig) -> Self { +impl Config { + pub(crate) fn configure( + meta: &mut ConstraintSystem, + canon_advices: [Column; 3], + lookup_config: LookupRangeCheckConfig, + super_config: super::Config, + ) -> Self { + for advice in canon_advices.iter() { + meta.enable_equality((*advice).into()); + } + let config = Self { - q_mul_fixed_base_field: config.q_mul_fixed_base_field, - canon_advices: [config.advices[6], config.advices[7], config.advices[8]], - lookup_config: config.lookup_config, - super_config: config.mul_fixed, + q_mul_fixed_base_field: meta.selector(), + canon_advices, + lookup_config, + super_config, }; let add_incomplete_advices = config.super_config.add_incomplete_config.advice_columns(); @@ -42,12 +52,12 @@ impl From<&EccConfig> for Config { ); } + config.create_gate(meta); + config } -} -impl Config { - pub fn create_gate(&self, meta: &mut ConstraintSystem) { + fn create_gate(&self, meta: &mut ConstraintSystem) { // Check that the base field element is canonical. meta.create_gate("Canonicity checks", |meta| { let q_mul_fixed_base_field = meta.query_selector(self.q_mul_fixed_base_field);