mirror of https://github.com/zcash/halo2.git
chip::mul_fixed: Refactor mul_fixed::Config.
This commit does not introduce circuit changes.
This commit is contained in:
parent
a7dad1d611
commit
1a7e832ed4
|
@ -133,11 +133,6 @@ pub struct EccConfig {
|
|||
/// Advice columns needed by instructions in the ECC chip.
|
||||
pub advices: [Column<Advice>; 10],
|
||||
|
||||
/// Coefficients of interpolation polynomials for x-coordinates (used in fixed-base scalar multiplication)
|
||||
pub lagrange_coeffs: [Column<Fixed>; constants::H],
|
||||
/// Fixed z such that y + z = u^2 some square, and -y + z is a non-square. (Used in fixed-base scalar multiplication)
|
||||
pub fixed_z: Column<Fixed>,
|
||||
|
||||
/// Incomplete addition
|
||||
add_incomplete: add_incomplete::Config,
|
||||
|
||||
|
@ -147,6 +142,9 @@ pub struct EccConfig {
|
|||
/// Variable-base scalar multiplication
|
||||
mul: mul::Config,
|
||||
|
||||
/// TODO: Remove this.
|
||||
pub mul_fixed: mul_fixed::Config,
|
||||
|
||||
/// Fixed-base full-width scalar multiplication
|
||||
pub q_mul_fixed_full: Selector,
|
||||
/// Fixed-base signed short scalar multiplication
|
||||
|
@ -206,10 +204,6 @@ impl EccChip {
|
|||
) -> <Self as Chip<pallas::Base>>::Config {
|
||||
// The following columns need to be equality-enabled for their use in sub-configs:
|
||||
//
|
||||
// mul_fixed::Config:
|
||||
// - advices[4]: window
|
||||
// - advices[5]: u
|
||||
//
|
||||
// mul_fixed::base_field_element::Config:
|
||||
// - [advices[6], advices[7], advices[8]]: canon_advices
|
||||
//
|
||||
|
@ -238,13 +232,26 @@ impl EccChip {
|
|||
// Create variable-base scalar mul gates
|
||||
let mul = mul::Config::configure(meta, add, range_check, advices);
|
||||
|
||||
// Create config that is shared across short, base-field, and full-width
|
||||
// fixed-base scalar mul.
|
||||
let mul_fixed = mul_fixed::Config::configure(
|
||||
meta,
|
||||
q_mul_fixed_running_sum,
|
||||
lagrange_coeffs,
|
||||
advices[4],
|
||||
advices[0],
|
||||
advices[1],
|
||||
advices[5],
|
||||
add,
|
||||
add_incomplete,
|
||||
);
|
||||
|
||||
let config = EccConfig {
|
||||
advices,
|
||||
lagrange_coeffs,
|
||||
fixed_z: meta.fixed_column(),
|
||||
add_incomplete,
|
||||
add,
|
||||
mul,
|
||||
mul_fixed,
|
||||
q_mul_fixed_full: meta.selector(),
|
||||
q_mul_fixed_short: meta.selector(),
|
||||
q_mul_fixed_base_field: meta.selector(),
|
||||
|
@ -254,14 +261,6 @@ impl EccChip {
|
|||
running_sum_config,
|
||||
};
|
||||
|
||||
// Create gate that is used both in fixed-base mul using a short signed exponent,
|
||||
// and fixed-base mul using a base field element.
|
||||
{
|
||||
// The const generic does not matter when creating gates.
|
||||
let mul_fixed_config: mul_fixed::Config<{ constants::NUM_WINDOWS }> = (&config).into();
|
||||
mul_fixed_config.running_sum_coords_gate(meta);
|
||||
}
|
||||
|
||||
// Create gate that is only used in full-width fixed-base scalar mul.
|
||||
{
|
||||
let mul_fixed_full_config: mul_fixed::full_width::Config = (&config).into();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use super::{
|
||||
add, add_incomplete, CellValue, EccBaseFieldElemFixed, EccConfig, EccScalarFixed,
|
||||
EccScalarFixedShort, NonIdentityEccPoint, Var,
|
||||
add, add_incomplete, CellValue, EccBaseFieldElemFixed, EccScalarFixed, EccScalarFixedShort,
|
||||
NonIdentityEccPoint, Var,
|
||||
};
|
||||
use crate::constants::{
|
||||
self,
|
||||
|
@ -75,8 +75,8 @@ impl OrchardFixedBases {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Config<const NUM_WINDOWS: usize> {
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct Config {
|
||||
q_mul_fixed_running_sum: Selector,
|
||||
// The fixed Lagrange interpolation coefficients for `x_p`.
|
||||
lagrange_coeffs: [Column<Fixed>; constants::H],
|
||||
|
@ -97,18 +97,32 @@ pub struct Config<const NUM_WINDOWS: usize> {
|
|||
add_incomplete_config: add_incomplete::Config,
|
||||
}
|
||||
|
||||
impl<const NUM_WINDOWS: usize> From<&EccConfig> for Config<NUM_WINDOWS> {
|
||||
fn from(ecc_config: &EccConfig) -> Self {
|
||||
impl Config {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(super) fn configure(
|
||||
meta: &mut ConstraintSystem<pallas::Base>,
|
||||
q_mul_fixed_running_sum: Selector,
|
||||
lagrange_coeffs: [Column<Fixed>; constants::H],
|
||||
window: Column<Advice>,
|
||||
x_p: Column<Advice>,
|
||||
y_p: Column<Advice>,
|
||||
u: Column<Advice>,
|
||||
add_config: add::Config,
|
||||
add_incomplete_config: add_incomplete::Config,
|
||||
) -> Self {
|
||||
meta.enable_equality(window.into());
|
||||
meta.enable_equality(u.into());
|
||||
|
||||
let config = Self {
|
||||
q_mul_fixed_running_sum: ecc_config.q_mul_fixed_running_sum,
|
||||
lagrange_coeffs: ecc_config.lagrange_coeffs,
|
||||
fixed_z: ecc_config.fixed_z,
|
||||
x_p: ecc_config.advices[0],
|
||||
y_p: ecc_config.advices[1],
|
||||
window: ecc_config.advices[4],
|
||||
u: ecc_config.advices[5],
|
||||
add_config: ecc_config.add,
|
||||
add_incomplete_config: ecc_config.add_incomplete,
|
||||
q_mul_fixed_running_sum,
|
||||
lagrange_coeffs,
|
||||
fixed_z: meta.fixed_column(),
|
||||
window,
|
||||
x_p,
|
||||
y_p,
|
||||
u,
|
||||
add_config,
|
||||
add_incomplete_config,
|
||||
};
|
||||
|
||||
// Check relationships between this config and `add_config`.
|
||||
|
@ -141,11 +155,11 @@ impl<const NUM_WINDOWS: usize> From<&EccConfig> for Config<NUM_WINDOWS> {
|
|||
);
|
||||
}
|
||||
|
||||
config.running_sum_coords_gate(meta);
|
||||
|
||||
config
|
||||
}
|
||||
}
|
||||
|
||||
impl<const NUM_WINDOWS: usize> Config<NUM_WINDOWS> {
|
||||
/// Check that each window in the running sum decomposition uses the correct y_p
|
||||
/// and interpolated x_p.
|
||||
///
|
||||
|
@ -155,7 +169,7 @@ impl<const NUM_WINDOWS: usize> Config<NUM_WINDOWS> {
|
|||
/// This gate is not used in the mul_fixed::full_width helper, since the full-width
|
||||
/// scalar is witnessed directly as three-bit windows instead of being decomposed
|
||||
/// via a running sum.
|
||||
pub(crate) fn running_sum_coords_gate(&self, meta: &mut ConstraintSystem<pallas::Base>) {
|
||||
fn running_sum_coords_gate(&self, meta: &mut ConstraintSystem<pallas::Base>) {
|
||||
meta.create_gate("Running sum coordinates check", |meta| {
|
||||
let q_mul_fixed_running_sum = meta.query_selector(self.q_mul_fixed_running_sum);
|
||||
|
||||
|
@ -213,7 +227,7 @@ impl<const NUM_WINDOWS: usize> Config<NUM_WINDOWS> {
|
|||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn assign_region_inner(
|
||||
fn assign_region_inner<const NUM_WINDOWS: usize>(
|
||||
&self,
|
||||
region: &mut Region<'_, pallas::Base>,
|
||||
offset: usize,
|
||||
|
@ -222,7 +236,7 @@ impl<const NUM_WINDOWS: usize> Config<NUM_WINDOWS> {
|
|||
coords_check_toggle: Selector,
|
||||
) -> Result<(NonIdentityEccPoint, NonIdentityEccPoint), Error> {
|
||||
// Assign fixed columns for given fixed base
|
||||
self.assign_fixed_constants(region, offset, base, coords_check_toggle)?;
|
||||
self.assign_fixed_constants::<NUM_WINDOWS>(region, offset, base, coords_check_toggle)?;
|
||||
|
||||
// Initialize accumulator
|
||||
let acc = self.initialize_accumulator(region, offset, base, scalar)?;
|
||||
|
@ -231,12 +245,12 @@ impl<const NUM_WINDOWS: usize> Config<NUM_WINDOWS> {
|
|||
let acc = self.add_incomplete(region, offset, acc, base, scalar)?;
|
||||
|
||||
// Process most significant window using complete addition
|
||||
let mul_b = self.process_msb(region, offset, base, scalar)?;
|
||||
let mul_b = self.process_msb::<NUM_WINDOWS>(region, offset, base, scalar)?;
|
||||
|
||||
Ok((acc, mul_b))
|
||||
}
|
||||
|
||||
fn assign_fixed_constants(
|
||||
fn assign_fixed_constants<const NUM_WINDOWS: usize>(
|
||||
&self,
|
||||
region: &mut Region<'_, pallas::Base>,
|
||||
offset: usize,
|
||||
|
@ -411,7 +425,7 @@ impl<const NUM_WINDOWS: usize> Config<NUM_WINDOWS> {
|
|||
Ok(acc)
|
||||
}
|
||||
|
||||
fn process_msb(
|
||||
fn process_msb<const NUM_WINDOWS: usize>(
|
||||
&self,
|
||||
region: &mut Region<'_, pallas::Base>,
|
||||
offset: usize,
|
||||
|
|
|
@ -24,7 +24,7 @@ pub struct Config {
|
|||
canon_advices: [Column<Advice>; 3],
|
||||
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
|
||||
running_sum_config: RunningSumConfig<pallas::Base, { constants::FIXED_BASE_WINDOW_SIZE }>,
|
||||
super_config: super::Config<{ constants::NUM_WINDOWS }>,
|
||||
super_config: super::Config,
|
||||
}
|
||||
|
||||
impl From<&EccConfig> for Config {
|
||||
|
@ -35,7 +35,7 @@ impl From<&EccConfig> for Config {
|
|||
canon_advices: [config.advices[6], config.advices[7], config.advices[8]],
|
||||
lookup_config: config.lookup_config,
|
||||
running_sum_config: config.running_sum_config.clone(),
|
||||
super_config: config.into(),
|
||||
super_config: config.mul_fixed,
|
||||
};
|
||||
|
||||
let add_incomplete_advices = config.super_config.add_incomplete_config.advice_columns();
|
||||
|
@ -180,13 +180,15 @@ impl Config {
|
|||
}
|
||||
};
|
||||
|
||||
let (acc, mul_b) = self.super_config.assign_region_inner(
|
||||
&mut region,
|
||||
offset,
|
||||
&(&scalar).into(),
|
||||
base.into(),
|
||||
self.q_mul_fixed_running_sum,
|
||||
)?;
|
||||
let (acc, mul_b) = self
|
||||
.super_config
|
||||
.assign_region_inner::<{ constants::NUM_WINDOWS }>(
|
||||
&mut region,
|
||||
offset,
|
||||
&(&scalar).into(),
|
||||
base.into(),
|
||||
self.q_mul_fixed_running_sum,
|
||||
)?;
|
||||
|
||||
Ok((scalar, acc, mul_b))
|
||||
},
|
||||
|
|
|
@ -14,14 +14,14 @@ use pasta_curves::{arithmetic::FieldExt, pallas};
|
|||
|
||||
pub struct Config {
|
||||
q_mul_fixed_full: Selector,
|
||||
super_config: super::Config<NUM_WINDOWS>,
|
||||
super_config: super::Config,
|
||||
}
|
||||
|
||||
impl From<&EccConfig> for Config {
|
||||
fn from(config: &EccConfig) -> Self {
|
||||
Self {
|
||||
q_mul_fixed_full: config.q_mul_fixed_full,
|
||||
super_config: config.into(),
|
||||
super_config: config.mul_fixed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -124,7 +124,7 @@ impl Config {
|
|||
|
||||
let scalar = self.witness(&mut region, offset, scalar)?;
|
||||
|
||||
let (acc, mul_b) = self.super_config.assign_region_inner(
|
||||
let (acc, mul_b) = self.super_config.assign_region_inner::<NUM_WINDOWS>(
|
||||
&mut region,
|
||||
offset,
|
||||
&(&scalar).into(),
|
||||
|
|
|
@ -21,7 +21,7 @@ pub struct Config {
|
|||
q_mul_fixed_short: Selector,
|
||||
q_mul_fixed_running_sum: Selector,
|
||||
running_sum_config: RunningSumConfig<pallas::Base, { FIXED_BASE_WINDOW_SIZE }>,
|
||||
super_config: super::Config<NUM_WINDOWS_SHORT>,
|
||||
super_config: super::Config,
|
||||
}
|
||||
|
||||
impl From<&EccConfig> for Config {
|
||||
|
@ -30,7 +30,7 @@ impl From<&EccConfig> for Config {
|
|||
q_mul_fixed_short: config.q_mul_fixed_short,
|
||||
q_mul_fixed_running_sum: config.q_mul_fixed_running_sum,
|
||||
running_sum_config: config.running_sum_config.clone(),
|
||||
super_config: config.into(),
|
||||
super_config: config.mul_fixed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ impl Config {
|
|||
// Decompose the scalar
|
||||
let scalar = self.decompose(&mut region, offset, magnitude_sign)?;
|
||||
|
||||
let (acc, mul_b) = self.super_config.assign_region_inner(
|
||||
let (acc, mul_b) = self.super_config.assign_region_inner::<NUM_WINDOWS_SHORT>(
|
||||
&mut region,
|
||||
offset,
|
||||
&(&scalar).into(),
|
||||
|
|
Loading…
Reference in New Issue