Skip to content

Commit

Permalink
Replace gadget::utilities::copy with AssignedCell::copy_advice
Browse files Browse the repository at this point in the history
Also replaces other copy-advice implementations that weren't using
`copy`.
  • Loading branch information
str4d committed Dec 8, 2021
1 parent 3079800 commit 65a89f0
Show file tree
Hide file tree
Showing 19 changed files with 297 additions and 394 deletions.
34 changes: 8 additions & 26 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use gadget::{
},
note_commit::NoteCommitConfig,
},
utilities::{copy, CellValue, UtilitiesInstructions},
utilities::{CellValue, UtilitiesInstructions},
};

use std::convert::TryInto;
Expand Down Expand Up @@ -501,20 +501,8 @@ impl plonk::Circuit<pallas::Base> for Circuit {
|mut region| {
config.q_add.enable(&mut region, 0)?;

copy(
&mut region,
|| "copy hash_old",
config.advices[7],
0,
&hash_old,
)?;
copy(
&mut region,
|| "copy psi_old",
config.advices[8],
0,
&psi_old,
)?;
hash_old.copy_advice(|| "copy hash_old", &mut region, config.advices[7], 0)?;
psi_old.copy_advice(|| "copy psi_old", &mut region, config.advices[8], 0)?;

let scalar_val = hash_old
.value()
Expand Down Expand Up @@ -691,19 +679,13 @@ impl plonk::Circuit<pallas::Base> for Circuit {
layouter.assign_region(
|| "v_old - v_new = magnitude * sign",
|mut region| {
copy(&mut region, || "v_old", config.advices[0], 0, &v_old)?;
copy(&mut region, || "v_new", config.advices[1], 0, &v_new)?;
v_old.copy_advice(|| "v_old", &mut region, config.advices[0], 0)?;
v_new.copy_advice(|| "v_new", &mut region, config.advices[1], 0)?;
let (magnitude, sign) = v_net.clone();
copy(
&mut region,
|| "v_net magnitude",
config.advices[2],
0,
&magnitude,
)?;
copy(&mut region, || "v_net sign", config.advices[3], 0, &sign)?;
magnitude.copy_advice(|| "v_net magnitude", &mut region, config.advices[2], 0)?;
sign.copy_advice(|| "v_net sign", &mut region, config.advices[3], 0)?;

copy(&mut region, || "anchor", config.advices[4], 0, &anchor)?;
anchor.copy_advice(|| "anchor", &mut region, config.advices[4], 0)?;
region.assign_advice_from_instance(
|| "pub input anchor",
config.primary,
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/gadget/ecc/chip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::EccInstructions;
use crate::{
circuit::gadget::utilities::{
copy, lookup_range_check::LookupRangeCheckConfig, CellValue, UtilitiesInstructions,
lookup_range_check::LookupRangeCheckConfig, CellValue, UtilitiesInstructions,
},
constants::{self, NullifierK, OrchardFixedBasesFull, ValueCommitV},
primitives::sinsemilla,
Expand Down
10 changes: 5 additions & 5 deletions src/circuit/gadget/ecc/chip/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::array;

use super::{copy, EccPoint};
use super::EccPoint;
use ff::{BatchInvert, Field};
use halo2::{
circuit::Region,
Expand Down Expand Up @@ -223,12 +223,12 @@ impl Config {
self.q_add.enable(region, offset)?;

// Copy point `p` into `x_p`, `y_p` columns
copy(region, || "x_p", self.x_p, offset, &p.x)?;
copy(region, || "y_p", self.y_p, offset, &p.y)?;
p.x.copy_advice(|| "x_p", region, self.x_p, offset)?;
p.y.copy_advice(|| "y_p", region, self.y_p, offset)?;

// Copy point `q` into `x_qr`, `y_qr` columns
copy(region, || "x_q", self.x_qr, offset, &q.x)?;
copy(region, || "y_q", self.y_qr, offset, &q.y)?;
q.x.copy_advice(|| "x_q", region, self.x_qr, offset)?;
q.y.copy_advice(|| "y_q", region, self.y_qr, offset)?;

let (x_p, y_p) = (p.x.value(), p.y.value());
let (x_q, y_q) = (q.x.value(), q.y.value());
Expand Down
10 changes: 5 additions & 5 deletions src/circuit/gadget/ecc/chip/add_incomplete.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{array, collections::HashSet};

use super::{copy, NonIdentityEccPoint};
use super::NonIdentityEccPoint;
use ff::Field;
use group::Curve;
use halo2::{
Expand Down Expand Up @@ -111,12 +111,12 @@ impl Config {
.transpose()?;

// Copy point `p` into `x_p`, `y_p` columns
copy(region, || "x_p", self.x_p, offset, &p.x)?;
copy(region, || "y_p", self.y_p, offset, &p.y)?;
p.x.copy_advice(|| "x_p", region, self.x_p, offset)?;
p.y.copy_advice(|| "y_p", region, self.y_p, offset)?;

// Copy point `q` into `x_qr`, `y_qr` columns
copy(region, || "x_q", self.x_qr, offset, &q.x)?;
copy(region, || "y_q", self.y_qr, offset, &q.y)?;
q.x.copy_advice(|| "x_q", region, self.x_qr, offset)?;
q.y.copy_advice(|| "y_q", region, self.y_qr, offset)?;

// Compute the sum `P + Q = R`
let r = {
Expand Down
22 changes: 5 additions & 17 deletions src/circuit/gadget/ecc/chip/mul.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use super::{add, CellValue, EccPoint, NonIdentityEccPoint};
use crate::{
circuit::gadget::utilities::{
bool_check, copy, lookup_range_check::LookupRangeCheckConfig, ternary,
},
circuit::gadget::utilities::{bool_check, lookup_range_check::LookupRangeCheckConfig, ternary},
constants::T_Q,
primitives::sinsemilla,
};
Expand Down Expand Up @@ -342,20 +340,10 @@ impl Config {
};

// Copy in `base_x`, `base_y` to use in the LSB gate
copy(
region,
|| "copy base_x",
self.add_config.x_p,
offset + 1,
&base.x(),
)?;
copy(
region,
|| "copy base_y",
self.add_config.y_p,
offset + 1,
&base.y(),
)?;
base.x()
.copy_advice(|| "copy base_x", region, self.add_config.x_p, offset + 1)?;
base.y()
.copy_advice(|| "copy base_y", region, self.add_config.y_p, offset + 1)?;

// If `lsb` is 0, return `Acc + (-P)`. If `lsb` is 1, simply return `Acc + 0`.
let x = if let Some(lsb) = lsb {
Expand Down
12 changes: 5 additions & 7 deletions src/circuit/gadget/ecc/chip/mul/complete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::{add, copy, EccPoint};
use super::super::{add, EccPoint};
use super::{COMPLETE_RANGE, X, Y, Z};
use crate::circuit::gadget::utilities::{bool_check, ternary};

Expand Down Expand Up @@ -110,12 +110,11 @@ impl Config {

// Copy running sum `z` from incomplete addition
let mut z = {
let z = copy(
region,
let z = z.copy_advice(
|| "Copy `z` running sum from incomplete addition",
region,
self.z_complete,
offset,
&z,
)?;
Z(z)
};
Expand Down Expand Up @@ -152,12 +151,11 @@ impl Config {

// Assign `y_p` for complete addition.
let y_p = {
let base_y = copy(
region,
let base_y = base.y.copy_advice(
|| "Copy `base.y`",
region,
self.z_complete,
row + offset + 1,
&base.y,
)?;

// If the bit is set, use `y`; if the bit is not set, use `-y`
Expand Down
12 changes: 8 additions & 4 deletions src/circuit/gadget/ecc/chip/mul/incomplete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::{copy, NonIdentityEccPoint};
use super::super::NonIdentityEccPoint;
use super::{X, Y, Z};
use crate::circuit::gadget::utilities::bool_check;
use ff::Field;
Expand Down Expand Up @@ -223,11 +223,15 @@ impl<const NUM_BITS: usize> Config<NUM_BITS> {
// Initialise double-and-add
let (mut x_a, mut y_a, mut z) = {
// Initialise the running `z` sum for the scalar bits.
let z = copy(region, || "starting z", self.z, offset, &acc.2)?;
let z = acc.2.copy_advice(|| "starting z", region, self.z, offset)?;

// Initialise acc
let x_a = copy(region, || "starting x_a", self.x_a, offset + 1, &acc.0)?;
let y_a = copy(region, || "starting y_a", self.lambda1, offset, &acc.1)?;
let x_a = acc
.0
.copy_advice(|| "starting x_a", region, self.x_a, offset + 1)?;
let y_a = acc
.1
.copy_advice(|| "starting y_a", region, self.lambda1, offset)?;

(x_a, y_a.value().cloned(), z)
};
Expand Down
32 changes: 9 additions & 23 deletions src/circuit/gadget/ecc/chip/mul/overflow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::{copy, CellValue};
use super::super::CellValue;
use super::Z;
use crate::{
circuit::gadget::utilities::lookup_range_check::LookupRangeCheckConfig, constants::T_Q,
Expand Down Expand Up @@ -140,16 +140,10 @@ impl Config {
self.q_mul_overflow.enable(&mut region, offset + 1)?;

// Copy `z_0`
copy(&mut region, || "copy z_0", self.advices[0], offset, &*zs[0])?;
zs[0].copy_advice(|| "copy z_0", &mut region, self.advices[0], offset)?;

// Copy `z_130`
copy(
&mut region,
|| "copy z_130",
self.advices[0],
offset + 1,
&*zs[130],
)?;
zs[130].copy_advice(|| "copy z_130", &mut region, self.advices[0], offset + 1)?;

// Witness η = inv0(z_130), where inv0(x) = 0 if x = 0, 1/x otherwise
{
Expand All @@ -169,34 +163,26 @@ impl Config {
}

// Copy `k_254` = z_254
copy(
&mut region,
|| "copy k_254",
self.advices[1],
offset,
&*zs[254],
)?;
zs[254].copy_advice(|| "copy k_254", &mut region, self.advices[1], offset)?;

// Copy original alpha
copy(
&mut region,
alpha.copy_advice(
|| "copy original alpha",
&mut region,
self.advices[1],
offset + 1,
&alpha,
)?;

// Copy weighted sum of the decomposition of s = alpha + k_254 ⋅ 2^130.
copy(
&mut region,
s_minus_lo_130.copy_advice(
|| "copy s_minus_lo_130",
&mut region,
self.advices[1],
offset + 2,
&s_minus_lo_130,
)?;

// Copy witnessed s to check that it was properly derived from alpha and k_254.
copy(&mut region, || "copy s", self.advices[2], offset + 1, &s)?;
s.copy_advice(|| "copy s", &mut region, self.advices[2], offset + 1)?;

Ok(())
},
Expand Down
35 changes: 12 additions & 23 deletions src/circuit/gadget/ecc/chip/mul_fixed/base_field_elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::H_BASE;

use crate::{
circuit::gadget::utilities::{
bitrange_subset, copy, lookup_range_check::LookupRangeCheckConfig, range_check, CellValue,
bitrange_subset, lookup_range_check::LookupRangeCheckConfig, range_check, CellValue,
},
constants::{self, T_P},
primitives::sinsemilla,
Expand Down Expand Up @@ -290,21 +290,14 @@ impl Config {
let offset = 0;

// Copy α
copy(
&mut region,
|| "Copy α",
self.canon_advices[0],
offset,
&alpha,
)?;
alpha.copy_advice(|| "Copy α", &mut region, self.canon_advices[0], offset)?;

// z_84_alpha = the top three bits of alpha.
copy(
&mut region,
z_84_alpha.copy_advice(
|| "Copy z_84_alpha",
&mut region,
self.canon_advices[2],
offset,
&z_84_alpha,
)?;
}

Expand All @@ -313,12 +306,11 @@ impl Config {
let offset = 1;
// Copy alpha_0_prime = alpha_0 + 2^130 - t_p.
// We constrain this in the custom gate to be derived correctly.
copy(
&mut region,
alpha_0_prime.copy_advice(
|| "Copy α_0 + 2^130 - t_p",
&mut region,
self.canon_advices[0],
offset,
&alpha_0_prime,
)?;

// Decompose α into three pieces,
Expand Down Expand Up @@ -347,30 +339,27 @@ impl Config {
{
let offset = 2;
// Copy z_13_alpha_0_prime
copy(
&mut region,
z_13_alpha_0_prime.copy_advice(
|| "Copy z_13_alpha_0_prime",
&mut region,
self.canon_advices[0],
offset,
&z_13_alpha_0_prime,
)?;

// Copy z_44_alpha
copy(
&mut region,
z_44_alpha.copy_advice(
|| "Copy z_44_alpha",
&mut region,
self.canon_advices[1],
offset,
&z_44_alpha,
)?;

// Copy z_43_alpha
copy(
&mut region,
z_43_alpha.copy_advice(
|| "Copy z_43_alpha",
&mut region,
self.canon_advices[2],
offset,
&z_43_alpha,
)?;
}

Expand Down
Loading

0 comments on commit 65a89f0

Please sign in to comment.