From c4bf8105f2d0d16372567ebf91b37de9519c3479 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 3 May 2022 20:26:41 +0000 Subject: [PATCH] Use `AssignedCell` for circuit note values --- src/circuit.rs | 4 ++-- src/circuit/note_commit.rs | 21 ++++++++++++--------- src/value.rs | 7 +++++++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/circuit.rs b/src/circuit.rs index 6cf58e11..7aeb77f8 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -377,14 +377,14 @@ impl plonk::Circuit for Circuit { let v_old = assign_free_advice( layouter.namespace(|| "witness v_old"), config.advices[0], - self.v_old.map(|v_old| pallas::Base::from(v_old.inner())), + self.v_old, )?; // Witness v_new. let v_new = assign_free_advice( layouter.namespace(|| "witness v_new"), config.advices[0], - self.v_new.map(|v_new| pallas::Base::from(v_new.inner())), + self.v_new, )?; (psi_old, rho_old, cm_old, g_d_old, ak_P, nk, v_old, v_new) diff --git a/src/circuit/note_commit.rs b/src/circuit/note_commit.rs index d98bf0d6..e61735f6 100644 --- a/src/circuit/note_commit.rs +++ b/src/circuit/note_commit.rs @@ -7,7 +7,10 @@ use halo2_proofs::{ }; use pasta_curves::{arithmetic::FieldExt, pallas}; -use crate::constants::{OrchardCommitDomains, OrchardFixedBases, OrchardHashDomains, T_P}; +use crate::{ + constants::{OrchardCommitDomains, OrchardFixedBases, OrchardHashDomains, T_P}, + value::NoteValue, +}; use halo2_gadgets::{ ecc::{ chip::{EccChip, NonIdentityEccPoint}, @@ -550,8 +553,7 @@ impl NoteCommitConfig { ecc_chip: EccChip, g_d: &NonIdentityEccPoint, pk_d: &NonIdentityEccPoint, - // TODO: Set V to Orchard value type - value: AssignedCell, + value: AssignedCell, rho: AssignedCell, psi: AssignedCell, rcm: Option, @@ -560,7 +562,7 @@ impl NoteCommitConfig { let (pkd_x, pkd_y) = (pk_d.x(), pk_d.y()); let (gd_x, gd_y) = (gd_x.value(), gd_y.value()); let (pkd_x, pkd_y) = (pkd_x.value(), pkd_y.value()); - let value_val = value.value(); + let value_val = value.value().map(|v| pallas::Base::from(v.inner())); let rho_val = rho.value(); let psi_val = psi.value(); @@ -621,12 +623,12 @@ impl NoteCommitConfig { let d_2 = RangeConstrained::witness_short( &self.sinsemilla_config.lookup_config(), layouter.namespace(|| "d_2"), - value_val, + value_val.as_ref(), 0..8, )?; // d_3 = z1_d from the SinsemillaHash(d) running sum output. - let d_3 = RangeConstrained::subset_of(value_val, 8..58); + let d_3 = RangeConstrained::subset_of(value_val.as_ref(), 8..58); let d = MessagePiece::from_subpieces( chip.clone(), @@ -643,7 +645,7 @@ impl NoteCommitConfig { let e_0 = RangeConstrained::witness_short( &self.sinsemilla_config.lookup_config(), layouter.namespace(|| "e_0"), - value_val, + value_val.as_ref(), 58..64, )?; @@ -1466,7 +1468,7 @@ struct GateCells { h_1: RangeConstrained>, gd_x: AssignedCell, pkd_x: AssignedCell, - value: AssignedCell, + value: AssignedCell, rho: AssignedCell, psi: AssignedCell, a_prime: AssignedCell, @@ -1494,6 +1496,7 @@ mod tests { fixed_bases::NOTE_COMMITMENT_PERSONALIZATION, OrchardCommitDomains, OrchardFixedBases, OrchardHashDomains, L_ORCHARD_BASE, L_VALUE, T_Q, }, + value::NoteValue, }; use halo2_gadgets::{ ecc::{ @@ -1665,7 +1668,7 @@ mod tests { // A note value cannot be negative. let value = { let mut rng = OsRng; - pallas::Base::from(rng.next_u64()) + NoteValue::from_raw(rng.next_u64()) }; let value_var = { assign_free_advice( diff --git a/src/value.rs b/src/value.rs index 751a80e6..38db80c9 100644 --- a/src/value.rs +++ b/src/value.rs @@ -42,6 +42,7 @@ use core::ops::{Add, RangeInclusive, Sub}; use bitvec::{array::BitArray, order::Lsb0}; use ff::{Field, PrimeField}; use group::{Curve, Group, GroupEncoding}; +use halo2_proofs::plonk::Assigned; use pasta_curves::{ arithmetic::{CurveAffine, CurveExt}, pallas, @@ -115,6 +116,12 @@ impl NoteValue { } } +impl From<&NoteValue> for Assigned { + fn from(v: &NoteValue) -> Self { + pallas::Base::from(v.inner()).into() + } +} + impl Sub for NoteValue { type Output = ValueSum;