Skip to content

Commit 53803b0

Browse files
Cleanups and clippy fixes
1 parent e87faa8 commit 53803b0

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

src/circuit/gadget/ecc/chip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub struct EccConfig {
103103
}
104104

105105
/// A chip implementing EccInstructions
106-
#[derive(Debug)]
106+
#[derive(Clone, Debug)]
107107
pub struct EccChip<C: CurveAffine> {
108108
pub config: EccConfig,
109109
pub loaded: EccLoaded<C>,

src/circuit/gadget/ecc/chip/mul.rs

+46-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{add, double, util, CellValue, EccConfig, EccPoint};
22
use crate::constants::NUM_COMPLETE_BITS;
3+
use std::ops::Deref;
34

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

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

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

@@ -183,8 +183,7 @@ pub(super) fn assign_region<C: CurveAffine>(
183183
offset + 1,
184184
hi_columns,
185185
k_incomplete_hi,
186-
z,
187-
(acc.x.clone(), acc.y.value),
186+
(X(acc.x.clone()), Y(acc.y.value), ZValue(z)),
188187
)?;
189188

190189
// Double-and-add (incomplete addition) for the `lo` half of the scalar decomposition
@@ -195,8 +194,7 @@ pub(super) fn assign_region<C: CurveAffine>(
195194
offset + 1,
196195
lo_columns,
197196
k_incomplete_lo,
198-
z,
199-
(x, y_a),
197+
(x, y_a, z),
200198
)?;
201199

202200
// Move from incomplete addition to complete addition
@@ -219,8 +217,8 @@ pub(super) fn assign_region<C: CurveAffine>(
219217
&config.perm_sum,
220218
)?;
221219
EccPoint {
222-
x,
223-
y: CellValue::<C::Base>::new(y_a_cell, y_a),
220+
x: x.0,
221+
y: CellValue::<C::Base>::new(y_a_cell, *y_a),
224222
}
225223
};
226224

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

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

348+
#[derive(Clone, Debug)]
349+
struct X<F: FieldExt>(CellValue<F>);
350+
impl<F: FieldExt> Deref for X<F> {
351+
type Target = CellValue<F>;
352+
353+
fn deref(&self) -> &Self::Target {
354+
&self.0
355+
}
356+
}
357+
358+
#[derive(Copy, Clone, Debug)]
359+
struct Y<F: FieldExt>(Option<F>);
360+
impl<F: FieldExt> Deref for Y<F> {
361+
type Target = Option<F>;
362+
363+
fn deref(&self) -> &Self::Target {
364+
&self.0
365+
}
366+
}
367+
368+
#[derive(Clone, Debug)]
369+
struct ZValue<F: FieldExt>(CellValue<F>);
370+
impl<F: FieldExt> Deref for ZValue<F> {
371+
type Target = CellValue<F>;
372+
373+
fn deref(&self) -> &Self::Target {
374+
&self.0
375+
}
376+
}
377+
350378
// We perform incomplete addition on all but the last three bits of the
351379
// decomposed scalar.
352380
// We split the bits in the incomplete addition range into "hi" and "lo"
@@ -360,13 +388,12 @@ fn add_incomplete<C: CurveAffine>(
360388
offset: usize,
361389
columns: IncompleteColumns,
362390
bits: &[bool],
363-
starting_z: CellValue<C::Base>,
364-
acc: (CellValue<C::Base>, Option<C::Base>),
365-
) -> Result<(CellValue<C::Base>, Option<C::Base>, CellValue<C::Base>), Error> {
391+
acc: (X<C::Base>, Y<C::Base>, ZValue<C::Base>),
392+
) -> Result<(X<C::Base>, Y<C::Base>, ZValue<C::Base>), Error> {
366393
// Initialise the running `z` sum for the scalar bits.
367-
let mut z_val = starting_z.value.unwrap();
394+
let mut z_val = acc.2.value.unwrap();
368395
let mut z_cell = region.assign_advice(|| "starting z", columns.z, offset, || Ok(z_val))?;
369-
region.constrain_equal(&config.perm_sum, z_cell, starting_z.cell)?;
396+
region.constrain_equal(&config.perm_sum, z_cell, acc.2.cell)?;
370397

371398
let offset = offset + 1;
372399

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

384411
// Enable `q_mul` on all but the last row of the incomplete range.
385412
for row in 1..(bits.len() - 1) {
@@ -461,9 +488,9 @@ fn add_incomplete<C: CurveAffine>(
461488
)?;
462489
}
463490
Ok((
464-
CellValue::<C::Base>::new(x_a_cell, x_a),
465-
y_a,
466-
CellValue::<C::Base>::new(z_cell, Some(z_val)),
491+
X(CellValue::<C::Base>::new(x_a_cell, x_a)),
492+
Y(y_a),
493+
ZValue(CellValue::<C::Base>::new(z_cell, Some(z_val))),
467494
))
468495
}
469496

0 commit comments

Comments
 (0)