Skip to content

Commit

Permalink
Cleanups and clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
therealyingtong committed May 6, 2021
1 parent 38dd871 commit 058e75b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/circuit/gadget/ecc/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub struct EccConfig {
}

/// A chip implementing EccInstructions
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct EccChip<C: CurveAffine> {
pub config: EccConfig,
pub loaded: EccLoaded<C>,
Expand Down
65 changes: 46 additions & 19 deletions src/circuit/gadget/ecc/chip/mul.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{add, double, util, CellValue, EccConfig, EccPoint};
use crate::constants::NUM_COMPLETE_BITS;
use std::ops::Deref;

use ff::PrimeField;
use halo2::{
Expand Down Expand Up @@ -55,8 +56,7 @@ pub(super) fn create_gate<F: FieldExt>(
// y_{A,i+1} = (λ_{1,i+1} + λ_{2,i+1})
// * (x_{A,i+1} - (λ_{1,i+1}^2 - x_{A,i+1} - x_{P,i+1})) / 2
let y_a_next = (lambda1_next.clone() + lambda2_next)
* (x_a_next.clone()
- (lambda1_next.clone() * lambda1_next - x_a_next.clone() - x_p_next.clone()))
* (x_a_next.clone() - (lambda1_next.clone() * lambda1_next - x_a_next.clone() - x_p_next))
* F::TWO_INV;

// λ_{1,i}⋅(x_{A,i} − x_{P,i}) − y_{A,i} + (2k_i - 1) y_{P,i} = 0
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(super) fn assign_region<C: CurveAffine>(

// Bits used in incomplete addition. k_{254} to k_{4} inclusive
let incomplete_range = 0..(C::Scalar::NUM_BITS as usize - 1 - NUM_COMPLETE_BITS);
let k_incomplete = &k_bits[incomplete_range.clone()];
let k_incomplete = &k_bits[incomplete_range];
let k_incomplete_hi = &k_incomplete[..k_incomplete.len() / 2];
let k_incomplete_lo = &k_incomplete[k_incomplete.len() / 2..];

Expand All @@ -183,8 +183,7 @@ pub(super) fn assign_region<C: CurveAffine>(
offset + 1,
hi_columns,
k_incomplete_hi,
z,
(acc.x.clone(), acc.y.value),
(X(acc.x.clone()), Y(acc.y.value), ZValue(z)),
)?;

// Double-and-add (incomplete addition) for the `lo` half of the scalar decomposition
Expand All @@ -195,8 +194,7 @@ pub(super) fn assign_region<C: CurveAffine>(
offset + 1,
lo_columns,
k_incomplete_lo,
z,
(x, y_a),
(x, y_a, z),
)?;

// Move from incomplete addition to complete addition
Expand All @@ -219,8 +217,8 @@ pub(super) fn assign_region<C: CurveAffine>(
&config.perm_sum,
)?;
EccPoint {
x,
y: CellValue::<C::Base>::new(y_a_cell, y_a),
x: x.0,
y: CellValue::<C::Base>::new(y_a_cell, *y_a),
}
};

Expand Down Expand Up @@ -332,7 +330,7 @@ pub(super) fn assign_region<C: CurveAffine>(
};

// Return the result of the final complete addition as `[scalar]B`
add::assign_region::<C>(&p, &acc, k_0_row + offset, region, config.clone())
add::assign_region::<C>(&p, &acc, k_0_row + offset, region, config)
} else {
// If `k_0` is 1, simply return `Acc`
Ok(acc)
Expand All @@ -347,6 +345,36 @@ struct IncompleteColumns {
lambda: (Column<Advice>, Column<Advice>),
}

#[derive(Clone, Debug)]
struct X<F: FieldExt>(CellValue<F>);
impl<F: FieldExt> Deref for X<F> {
type Target = CellValue<F>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[derive(Copy, Clone, Debug)]
struct Y<F: FieldExt>(Option<F>);
impl<F: FieldExt> Deref for Y<F> {
type Target = Option<F>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[derive(Clone, Debug)]
struct ZValue<F: FieldExt>(CellValue<F>);
impl<F: FieldExt> Deref for ZValue<F> {
type Target = CellValue<F>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

// We perform incomplete addition on all but the last three bits of the
// decomposed scalar.
// We split the bits in the incomplete addition range into "hi" and "lo"
Expand All @@ -360,13 +388,12 @@ fn add_incomplete<C: CurveAffine>(
offset: usize,
columns: IncompleteColumns,
bits: &[bool],
starting_z: CellValue<C::Base>,
acc: (CellValue<C::Base>, Option<C::Base>),
) -> Result<(CellValue<C::Base>, Option<C::Base>, CellValue<C::Base>), Error> {
acc: (X<C::Base>, Y<C::Base>, ZValue<C::Base>),
) -> Result<(X<C::Base>, Y<C::Base>, ZValue<C::Base>), Error> {
// Initialise the running `z` sum for the scalar bits.
let mut z_val = starting_z.value.unwrap();
let mut z_val = acc.2.value.unwrap();
let mut z_cell = region.assign_advice(|| "starting z", columns.z, offset, || Ok(z_val))?;
region.constrain_equal(&config.perm_sum, z_cell, starting_z.cell)?;
region.constrain_equal(&config.perm_sum, z_cell, acc.2.cell)?;

let offset = offset + 1;

Expand All @@ -379,7 +406,7 @@ fn add_incomplete<C: CurveAffine>(
|| x_a.ok_or(Error::SynthesisError),
)?;
region.constrain_equal(&config.perm_sum, x_a_cell, acc.0.cell)?;
let mut y_a = acc.1;
let mut y_a = *acc.1;

// Enable `q_mul` on all but the last row of the incomplete range.
for row in 1..(bits.len() - 1) {
Expand Down Expand Up @@ -461,9 +488,9 @@ fn add_incomplete<C: CurveAffine>(
)?;
}
Ok((
CellValue::<C::Base>::new(x_a_cell, x_a),
y_a,
CellValue::<C::Base>::new(z_cell, Some(z_val)),
X(CellValue::<C::Base>::new(x_a_cell, x_a)),
Y(y_a),
ZValue(CellValue::<C::Base>::new(z_cell, Some(z_val))),
))
}

Expand Down

0 comments on commit 058e75b

Please sign in to comment.