Skip to content

Commit 9c30463

Browse files
Rebase on Utilities chip
1 parent 65ac816 commit 9c30463

File tree

12 files changed

+61
-112
lines changed

12 files changed

+61
-112
lines changed

src/circuit/gadget/ecc/chip.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use super::EccInstructions;
2+
use crate::circuit::gadget::utilities::{copy, CellValue, Var};
23
use crate::constants::{self, OrchardFixedBasesFull, ValueCommitV};
34
use arrayvec::ArrayVec;
45
use ff::Field;
56
use halo2::{
67
arithmetic::CurveAffine,
7-
circuit::{Cell, Chip, Layouter},
8+
circuit::{Chip, Layouter},
89
plonk::{Advice, Column, ConstraintSystem, Error, Fixed, Permutation, Selector},
910
};
1011
use std::marker::PhantomData;
@@ -13,26 +14,9 @@ pub(super) mod add;
1314
pub(super) mod add_incomplete;
1415
pub(super) mod mul;
1516
pub(super) mod mul_fixed;
16-
pub(super) mod util;
1717
pub(super) mod witness_point;
1818
pub(super) mod witness_scalar_fixed;
1919

20-
/// A structure containing a cell and its assigned value.
21-
#[derive(Clone, Debug)]
22-
pub struct CellValue<T> {
23-
/// The cell of this `CellValue`
24-
pub cell: Cell,
25-
/// The value assigned to this `CellValue`
26-
pub value: Option<T>,
27-
}
28-
29-
impl<T> CellValue<T> {
30-
/// Construct a `CellValue`.
31-
pub fn new(cell: Cell, value: Option<T>) -> Self {
32-
CellValue { cell, value }
33-
}
34-
}
35-
3620
/// A curve point represented in affine (x, y) coordinates. Each coordinate is
3721
/// assigned to a cell.
3822
#[derive(Clone, Debug)]
@@ -46,7 +30,7 @@ pub struct EccPoint<C: CurveAffine> {
4630
impl<C: CurveAffine> EccPoint<C> {
4731
/// Returns the value of this curve point, if known.
4832
pub fn point(&self) -> Option<C> {
49-
match (self.x.value, self.y.value) {
33+
match (self.x.value(), self.y.value()) {
5034
(Some(x), Some(y)) => {
5135
if x == C::Base::zero() && y == C::Base::zero() {
5236
Some(C::identity())

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{util, CellValue, EccConfig, EccPoint};
1+
use super::{copy, CellValue, EccConfig, EccPoint, Var};
22
use ff::Field;
33
use halo2::{
44
arithmetic::{CurveAffine, FieldExt},
@@ -206,15 +206,15 @@ impl Config {
206206
self.q_add.enable(region, offset)?;
207207

208208
// Copy point `p` into `x_p`, `y_p` columns
209-
util::assign_and_constrain(region, || "x_p", self.x_p, offset, &p.x, &self.perm)?;
210-
util::assign_and_constrain(region, || "y_p", self.y_p, offset, &p.y, &self.perm)?;
209+
copy(region, || "x_p", self.x_p, offset, &p.x, &self.perm)?;
210+
copy(region, || "y_p", self.y_p, offset, &p.y, &self.perm)?;
211211

212212
// Copy point `q` into `x_qr`, `y_qr` columns
213-
util::assign_and_constrain(region, || "x_q", self.x_qr, offset, &q.x, &self.perm)?;
214-
util::assign_and_constrain(region, || "y_q", self.y_qr, offset, &q.y, &self.perm)?;
213+
copy(region, || "x_q", self.x_qr, offset, &q.x, &self.perm)?;
214+
copy(region, || "y_q", self.y_qr, offset, &q.y, &self.perm)?;
215215

216-
let (x_p, y_p) = (p.x.value, p.y.value);
217-
let (x_q, y_q) = (q.x.value, q.y.value);
216+
let (x_p, y_p) = (p.x.value(), p.y.value());
217+
let (x_q, y_q) = (q.x.value(), q.y.value());
218218

219219
// inv0(x) evaluates to 0 if x = 0, and 1/x otherwise.
220220

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{util, CellValue, EccConfig, EccPoint};
1+
use super::{copy, CellValue, EccConfig, EccPoint, Var};
22
use ff::Field;
33
use group::Curve;
44
use halo2::{
@@ -76,8 +76,8 @@ impl Config {
7676
self.q_add_incomplete.enable(region, offset)?;
7777

7878
// Handle exceptional cases
79-
let (x_p, y_p) = (p.x.value, p.y.value);
80-
let (x_q, y_q) = (q.x.value, q.y.value);
79+
let (x_p, y_p) = (p.x.value(), p.y.value());
80+
let (x_q, y_q) = (q.x.value(), q.y.value());
8181
x_p.zip(y_p)
8282
.zip(x_q)
8383
.zip(y_q)
@@ -97,12 +97,12 @@ impl Config {
9797
.transpose()?;
9898

9999
// Copy point `p` into `x_p`, `y_p` columns
100-
util::assign_and_constrain(region, || "x_p", self.x_p, offset, &p.x, &self.perm)?;
101-
util::assign_and_constrain(region, || "y_p", self.y_p, offset, &p.y, &self.perm)?;
100+
copy(region, || "x_p", self.x_p, offset, &p.x, &self.perm)?;
101+
copy(region, || "y_p", self.y_p, offset, &p.y, &self.perm)?;
102102

103103
// Copy point `q` into `x_qr`, `y_qr` columns
104-
util::assign_and_constrain(region, || "x_q", self.x_qr, offset, &q.x, &self.perm)?;
105-
util::assign_and_constrain(region, || "y_q", self.y_qr, offset, &q.y, &self.perm)?;
104+
copy(region, || "x_q", self.x_qr, offset, &q.x, &self.perm)?;
105+
copy(region, || "y_q", self.y_qr, offset, &q.y, &self.perm)?;
106106

107107
// Compute the sum `P + Q = R`
108108
let r = {

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{add, util, CellValue, EccConfig, EccPoint};
1+
use super::{add, copy, CellValue, EccConfig, EccPoint, Var};
22
use crate::constants::NUM_COMPLETE_BITS;
33
use std::ops::{Deref, Range};
44

@@ -113,7 +113,7 @@ impl<C: CurveAffine> Config<C> {
113113
let offset = offset + 1;
114114

115115
// Decompose the scalar bitwise (big-endian bit order).
116-
let bits = decompose_for_scalar_mul::<C>(scalar.value);
116+
let bits = decompose_for_scalar_mul::<C>(scalar.value());
117117

118118
// Initialize the running sum for scalar decomposition to zero
119119
let z_val = C::Base::zero();
@@ -131,7 +131,7 @@ impl<C: CurveAffine> Config<C> {
131131
offset,
132132
&base,
133133
bits_incomplete_hi,
134-
(X(acc.x.clone()), Y(acc.y.value), Z(z)),
134+
(X(acc.x.clone()), Y(acc.y.value()), Z(z)),
135135
)?;
136136

137137
// Double-and-add (incomplete addition) for the `lo` half of the scalar decomposition
@@ -164,7 +164,7 @@ impl<C: CurveAffine> Config<C> {
164164
};
165165

166166
// Initialize `z` running sum for complete addition
167-
util::assign_and_constrain(
167+
copy(
168168
region,
169169
|| "Initialize `z` running sum for complete addition",
170170
self.z_complete,
@@ -182,7 +182,7 @@ impl<C: CurveAffine> Config<C> {
182182
// Bits used in complete addition. k_{3} to k_{1} inclusive
183183
// The LSB k_{0} is handled separately.
184184
let bits_complete = &bits[complete_range::<C>()];
185-
complete_config.assign_region(region, offset, bits_complete, base, acc, z.value)?
185+
complete_config.assign_region(region, offset, bits_complete, base, acc, z.value())?
186186
};
187187

188188
let offset = offset + complete_len::<C>() * 2;
@@ -198,7 +198,7 @@ impl<C: CurveAffine> Config<C> {
198198

199199
let base = base.point();
200200
let scalar = scalar
201-
.value
201+
.value()
202202
.map(|scalar| C::Scalar::from_bytes(&scalar.to_bytes()).unwrap());
203203
let real_mul = base.zip(scalar).map(|(base, scalar)| base * scalar);
204204
let result = result.point();
@@ -241,7 +241,7 @@ impl<C: CurveAffine> Config<C> {
241241
// is in deriving diversified addresses `[ivk] g_d`, and `ivk` is guaranteed
242242
// to be in the base field of the curve. (See non-normative notes in
243243
// https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents.)
244-
util::assign_and_constrain(
244+
copy(
245245
region,
246246
|| "original scalar",
247247
self.scalar,
@@ -254,7 +254,7 @@ impl<C: CurveAffine> Config<C> {
254254
// If `lsb` is 0, return `Acc + (-P)`. If `lsb` is 1, simply return `Acc + 0`.
255255
let x_p = if let Some(lsb) = lsb {
256256
if !lsb {
257-
base.x.value
257+
base.x.value()
258258
} else {
259259
Some(C::Base::zero())
260260
}
@@ -263,7 +263,7 @@ impl<C: CurveAffine> Config<C> {
263263
};
264264
let y_p = if let Some(lsb) = lsb {
265265
if !lsb {
266-
base.y.value.map(|y_p| -y_p)
266+
base.y.value().map(|y_p| -y_p)
267267
} else {
268268
Some(C::Base::zero())
269269
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::super::{add, util, CellValue, EccPoint};
1+
use super::super::{add, copy, CellValue, EccPoint, Var};
22
use super::complete_len;
33
use ff::Field;
44

@@ -102,7 +102,7 @@ impl<C: CurveAffine> Config<C> {
102102
)?;
103103

104104
// Assign `x_p` for complete addition
105-
let x_p = base.x.value;
105+
let x_p = base.x.value();
106106
let x_p_cell = region.assign_advice(
107107
|| "x_p",
108108
self.add_config.x_p,
@@ -112,7 +112,7 @@ impl<C: CurveAffine> Config<C> {
112112

113113
// Assign `y_p` for complete addition.
114114
// If the bit is set, use `y`; if the bit is not set, use `-y`
115-
let y_p = base.y.value;
115+
let y_p = base.y.value();
116116
let y_p = y_p
117117
.zip(k.as_ref())
118118
.map(|(y_p, k)| if !k { -y_p } else { y_p });
@@ -134,15 +134,15 @@ impl<C: CurveAffine> Config<C> {
134134
.assign_region(&p, &acc, row + offset, region)?;
135135

136136
// Copy acc from `x_a`, `y_a` over to `x_p`, `y_p` on the next row
137-
let acc_x = util::assign_and_constrain(
137+
let acc_x = copy(
138138
region,
139139
|| "copy acc x_a",
140140
self.add_config.x_p,
141141
row + offset + 1,
142142
&acc.x,
143143
&self.perm,
144144
)?;
145-
let acc_y = util::assign_and_constrain(
145+
let acc_y = copy(
146146
region,
147147
|| "copy acc y_a",
148148
self.add_config.y_p,

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

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::super::{util, CellValue, EccConfig, EccPoint};
1+
use super::super::{copy, CellValue, EccConfig, EccPoint, Var};
22
use super::{incomplete_hi_len, incomplete_lo_len, X, Y, Z};
33
use ff::Field;
44
use halo2::{
@@ -161,8 +161,8 @@ impl<C: CurveAffine> Config<C> {
161161
assert_eq!(bits.len(), self.num_bits);
162162

163163
// Handle exceptional cases
164-
let (x_p, y_p) = (base.x.value, base.y.value);
165-
let (x_a, y_a) = (acc.0.value, acc.1 .0);
164+
let (x_p, y_p) = (base.x.value(), base.y.value());
165+
let (x_a, y_a) = (acc.0.value(), acc.1 .0);
166166
x_p.zip(y_p)
167167
.zip(x_a)
168168
.zip(y_a)
@@ -186,24 +186,17 @@ impl<C: CurveAffine> Config<C> {
186186
}
187187

188188
// Initialise the running `z` sum for the scalar bits.
189-
let mut z = util::assign_and_constrain(
190-
region,
191-
|| "starting z",
192-
self.z,
193-
offset,
194-
&acc.2,
195-
&self.perm,
196-
)?;
189+
let mut z = copy(region, || "starting z", self.z, offset, &acc.2, &self.perm)?;
197190

198191
// Increase offset by 1; we used row 0 for initializing `z`.
199192
let offset = offset + 1;
200193

201194
// Define `x_p`, `y_p`
202-
let x_p = base.x.value;
203-
let y_p = base.y.value;
195+
let x_p = base.x.value();
196+
let y_p = base.y.value();
204197

205198
// Initialise acc
206-
let mut x_a = util::assign_and_constrain(
199+
let mut x_a = copy(
207200
region,
208201
|| "starting x_a",
209202
self.x_a,
@@ -217,7 +210,7 @@ impl<C: CurveAffine> Config<C> {
217210
for (row, k) in bits.iter().enumerate() {
218211
// z_{i} = 2 * z_{i+1} + k_i
219212
let z_val = z
220-
.value
213+
.value()
221214
.zip(k.as_ref())
222215
.map(|(z_val, k)| C::Base::from_u64(2) * z_val + C::Base::from_u64(*k as u64));
223216
let z_cell = region.assign_advice(
@@ -250,7 +243,7 @@ impl<C: CurveAffine> Config<C> {
250243
// Compute and assign λ1⋅(x_A − x_P) = y_A − y_P
251244
let lambda1 = y_a
252245
.zip(y_p)
253-
.zip(x_a.value)
246+
.zip(x_a.value())
254247
.zip(x_p)
255248
.map(|(((y_a, y_p), x_a), x_p)| (y_a - y_p) * (x_a - x_p).invert().unwrap());
256249
region.assign_advice(
@@ -262,15 +255,15 @@ impl<C: CurveAffine> Config<C> {
262255

263256
// x_R = λ1^2 - x_A - x_P
264257
let x_r = lambda1
265-
.zip(x_a.value)
258+
.zip(x_a.value())
266259
.zip(x_p)
267260
.map(|((lambda1, x_a), x_p)| lambda1 * lambda1 - x_a - x_p);
268261

269262
// λ2 = (2(y_A) / (x_A - x_R)) - λ1
270263
let lambda2 =
271264
lambda1
272265
.zip(y_a)
273-
.zip(x_a.value)
266+
.zip(x_a.value())
274267
.zip(x_r)
275268
.map(|(((lambda1, y_a), x_a), x_r)| {
276269
C::Base::from_u64(2) * y_a * (x_a - x_r).invert().unwrap() - lambda1
@@ -284,11 +277,11 @@ impl<C: CurveAffine> Config<C> {
284277

285278
// Compute and assign `x_a` for the next row
286279
let x_a_new = lambda2
287-
.zip(x_a.value)
280+
.zip(x_a.value())
288281
.zip(x_r)
289282
.map(|((lambda2, x_a), x_r)| lambda2 * lambda2 - x_a - x_r);
290283
y_a = lambda2
291-
.zip(x_a.value)
284+
.zip(x_a.value())
292285
.zip(x_a_new)
293286
.zip(y_a)
294287
.map(|(((lambda2, x_a), x_a_new), y_a)| lambda2 * (x_a - x_a_new) - y_a);

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
add, add_incomplete, util, witness_point, CellValue, EccConfig, EccPoint, EccScalarFixed,
3-
EccScalarFixedShort,
2+
add, add_incomplete, copy, witness_point, CellValue, EccConfig, EccPoint, EccScalarFixed,
3+
EccScalarFixedShort, Var,
44
};
55
use crate::constants::{
66
self,
@@ -281,7 +281,7 @@ impl<C: CurveAffine, const NUM_WINDOWS: usize> Config<C, NUM_WINDOWS> {
281281
) -> Result<(), Error> {
282282
// Copy the scalar decomposition (`k`-bit windows)
283283
for (window_idx, window) in scalar.windows().iter().enumerate() {
284-
util::assign_and_constrain(
284+
copy(
285285
region,
286286
|| format!("k[{:?}]", window),
287287
self.window,
@@ -320,15 +320,15 @@ impl<C: CurveAffine, const NUM_WINDOWS: usize> Config<C, NUM_WINDOWS> {
320320
}
321321

322322
// Copy `m0` into `x_qr`, `y_qr` cells on row 1
323-
let x = util::assign_and_constrain(
323+
let x = copy(
324324
region,
325325
|| "initialize acc x",
326326
self.add_incomplete_config.x_qr,
327327
offset + 1,
328328
&m0.x,
329329
&self.perm,
330330
)?;
331-
let y = util::assign_and_constrain(
331+
let y = copy(
332332
region,
333333
|| "initialize acc y",
334334
self.add_incomplete_config.y_qr,
@@ -465,7 +465,7 @@ impl<C: CurveAffine> ScalarFixed<C> {
465465
self.windows()
466466
.iter()
467467
.map(|bits| {
468-
bits.value
468+
bits.value()
469469
.map(|value| C::Scalar::from_bytes(&value.to_bytes()).unwrap())
470470
})
471471
.collect::<Vec<_>>()
@@ -477,7 +477,7 @@ impl<C: CurveAffine> ScalarFixed<C> {
477477
fn windows_usize(&self) -> Vec<Option<usize>> {
478478
self.windows()
479479
.iter()
480-
.map(|bits| bits.value.map(|value| value.to_bytes()[0] as usize))
480+
.map(|bits| bits.value().map(|value| value.to_bytes()[0] as usize))
481481
.collect::<Vec<_>>()
482482
}
483483
}

0 commit comments

Comments
 (0)