From 687e220c36a11d25c8525e364009c3df299617d0 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Tue, 30 Nov 2021 22:20:24 -0500 Subject: [PATCH] mul_fixed::short: Refactor short::Config. This commit does not result in circuit changes. --- src/circuit/gadget/ecc/chip.rs | 15 ++++----- .../gadget/ecc/chip/mul_fixed/short.rs | 31 +++++++++++-------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/circuit/gadget/ecc/chip.rs b/src/circuit/gadget/ecc/chip.rs index 230625bc..2c5dcdb2 100644 --- a/src/circuit/gadget/ecc/chip.rs +++ b/src/circuit/gadget/ecc/chip.rs @@ -147,7 +147,7 @@ pub struct EccConfig { /// Fixed-base full-width scalar multiplication mul_fixed_full: mul_fixed::full_width::Config, /// Fixed-base signed short scalar multiplication - pub q_mul_fixed_short: Selector, + 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, @@ -238,6 +238,9 @@ impl EccChip { // Create gate that is only used in full-width fixed-base scalar mul. let mul_fixed_full = mul_fixed::full_width::Config::configure(meta, mul_fixed); + // Create gate that is only used in short fixed-base scalar mul. + let mul_fixed_short = mul_fixed::short::Config::configure(meta, mul_fixed); + let config = EccConfig { advices, add_incomplete, @@ -245,18 +248,12 @@ impl EccChip { mul, mul_fixed, mul_fixed_full, - q_mul_fixed_short: meta.selector(), + mul_fixed_short, q_mul_fixed_base_field: meta.selector(), witness_point, lookup_config: range_check, }; - // Create gate that is only used in short fixed-base scalar mul. - { - let short_config: mul_fixed::short::Config = (&config).into(); - short_config.create_gate(meta); - } - // 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(); @@ -437,7 +434,7 @@ impl EccInstructions for EccChip { magnitude_sign: (CellValue, CellValue), base: &Self::FixedPointsShort, ) -> Result<(Self::Point, Self::ScalarFixedShort), Error> { - let config: mul_fixed::short::Config = self.config().into(); + let config: mul_fixed::short::Config = self.config().mul_fixed_short; config.assign( layouter.namespace(|| format!("short fixed-base mul of {:?}", base)), magnitude_sign, diff --git a/src/circuit/gadget/ecc/chip/mul_fixed/short.rs b/src/circuit/gadget/ecc/chip/mul_fixed/short.rs index 50f3550f..d3cbc4f7 100644 --- a/src/circuit/gadget/ecc/chip/mul_fixed/short.rs +++ b/src/circuit/gadget/ecc/chip/mul_fixed/short.rs @@ -1,6 +1,6 @@ use std::{array, convert::TryInto}; -use super::super::{EccConfig, EccPoint, EccScalarFixedShort}; +use super::super::{EccPoint, EccScalarFixedShort}; use crate::{ circuit::gadget::utilities::{bool_check, copy, CellValue, Var}, constants::{ValueCommitV, L_VALUE, NUM_WINDOWS_SHORT}, @@ -13,24 +13,29 @@ use halo2::{ }; use pasta_curves::pallas; -#[derive(Clone)] +#[derive(Clone, Debug, Copy, Eq, PartialEq)] pub struct Config { // Selector used for fixed-base scalar mul with short signed exponent. q_mul_fixed_short: Selector, super_config: super::Config, } -impl From<&EccConfig> for Config { - fn from(config: &EccConfig) -> Self { - Self { - q_mul_fixed_short: config.q_mul_fixed_short, - super_config: config.mul_fixed, - } - } -} - impl Config { - pub(crate) fn create_gate(&self, meta: &mut ConstraintSystem) { + pub(crate) fn configure( + meta: &mut ConstraintSystem, + super_config: super::Config, + ) -> Self { + let config = Self { + q_mul_fixed_short: meta.selector(), + super_config, + }; + + config.create_gate(meta); + + config + } + + fn create_gate(&self, meta: &mut ConstraintSystem) { meta.create_gate("Short fixed-base mul gate", |meta| { let q_mul_fixed_short = meta.query_selector(self.q_mul_fixed_short); let y_p = meta.query_advice(self.super_config.y_p, Rotation::cur()); @@ -444,7 +449,7 @@ pub mod tests { ) -> Result<(), Error> { let column = config.advices[0]; - let short_config: super::Config = (&config).into(); + let short_config = config.mul_fixed_short; let magnitude_sign = { let magnitude = self.load_private( layouter.namespace(|| "load magnitude"),