2021-06-21 05:38:43 -07:00
|
|
|
|
//! The Orchard Action circuit implementation.
|
|
|
|
|
|
2022-04-28 13:20:23 -07:00
|
|
|
|
use core::fmt;
|
2022-04-05 13:18:10 -07:00
|
|
|
|
|
2021-06-07 03:45:03 -07:00
|
|
|
|
use group::{Curve, GroupEncoding};
|
2022-01-28 07:53:03 -08:00
|
|
|
|
use halo2_proofs::{
|
2022-06-08 17:01:10 -07:00
|
|
|
|
circuit::{floor_planner, Layouter, Value},
|
2022-01-20 07:05:10 -08:00
|
|
|
|
plonk::{
|
2022-06-21 08:07:31 -07:00
|
|
|
|
self, Advice, BatchVerifier, Column, Constraints, Expression, Instance as InstanceColumn,
|
|
|
|
|
Selector, SingleVerifier,
|
2022-01-20 07:05:10 -08:00
|
|
|
|
},
|
2021-07-15 04:52:15 -07:00
|
|
|
|
poly::Rotation,
|
2021-04-14 21:35:48 -07:00
|
|
|
|
transcript::{Blake2bRead, Blake2bWrite},
|
|
|
|
|
};
|
2021-09-03 13:22:22 -07:00
|
|
|
|
use memuse::DynamicUsage;
|
2021-12-07 09:47:03 -08:00
|
|
|
|
use pasta_curves::{arithmetic::CurveAffine, pallas, vesta};
|
2022-01-04 21:58:59 -08:00
|
|
|
|
use rand::RngCore;
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
2022-04-29 10:54:28 -07:00
|
|
|
|
use self::{
|
2022-05-02 05:35:16 -07:00
|
|
|
|
commit_ivk::{CommitIvkChip, CommitIvkConfig},
|
2022-05-03 13:02:29 -07:00
|
|
|
|
gadget::{
|
|
|
|
|
add_chip::{AddChip, AddConfig},
|
|
|
|
|
assign_free_advice,
|
|
|
|
|
},
|
2022-05-03 19:55:42 -07:00
|
|
|
|
note_commit::{NoteCommitChip, NoteCommitConfig},
|
2022-04-29 10:54:28 -07:00
|
|
|
|
};
|
2021-04-14 21:35:48 -07:00
|
|
|
|
use crate::{
|
2022-09-20 08:08:54 -07:00
|
|
|
|
builder::SpendInfo,
|
2021-06-06 04:44:24 -07:00
|
|
|
|
constants::{
|
2022-04-29 10:54:28 -07:00
|
|
|
|
OrchardCommitDomains, OrchardFixedBases, OrchardFixedBasesFull, OrchardHashDomains,
|
2022-04-29 13:05:00 -07:00
|
|
|
|
MERKLE_DEPTH_ORCHARD,
|
2021-06-06 04:44:24 -07:00
|
|
|
|
},
|
2021-06-06 04:21:27 -07:00
|
|
|
|
keys::{
|
|
|
|
|
CommitIvkRandomness, DiversifiedTransmissionKey, NullifierDerivingKey, SpendValidatingKey,
|
|
|
|
|
},
|
|
|
|
|
note::{
|
|
|
|
|
commitment::{NoteCommitTrapdoor, NoteCommitment},
|
|
|
|
|
nullifier::Nullifier,
|
2022-09-20 08:08:54 -07:00
|
|
|
|
ExtractedNoteCommitment, Note,
|
2021-06-06 04:21:27 -07:00
|
|
|
|
},
|
2022-01-28 07:53:03 -08:00
|
|
|
|
primitives::redpallas::{SpendAuth, VerificationKey},
|
2021-06-06 04:21:27 -07:00
|
|
|
|
spec::NonIdentityPallasPoint,
|
2021-09-15 09:47:13 -07:00
|
|
|
|
tree::{Anchor, MerkleHashOrchard},
|
2021-06-06 04:21:27 -07:00
|
|
|
|
value::{NoteValue, ValueCommitTrapdoor, ValueCommitment},
|
2021-04-14 21:35:48 -07:00
|
|
|
|
};
|
2022-01-28 07:53:03 -08:00
|
|
|
|
use halo2_gadgets::{
|
2021-06-06 21:26:02 -07:00
|
|
|
|
ecc::{
|
|
|
|
|
chip::{EccChip, EccConfig},
|
2022-05-10 13:53:49 -07:00
|
|
|
|
FixedPoint, NonIdentityPoint, Point, ScalarFixed, ScalarFixedShort, ScalarVar,
|
2021-06-06 05:05:51 -07:00
|
|
|
|
},
|
2022-05-10 13:53:49 -07:00
|
|
|
|
poseidon::{primitives as poseidon, Pow5Chip as PoseidonChip, Pow5Config as PoseidonConfig},
|
2021-06-06 04:26:37 -07:00
|
|
|
|
sinsemilla::{
|
2022-01-28 07:53:03 -08:00
|
|
|
|
chip::{SinsemillaChip, SinsemillaConfig},
|
2021-06-06 04:32:59 -07:00
|
|
|
|
merkle::{
|
|
|
|
|
chip::{MerkleChip, MerkleConfig},
|
|
|
|
|
MerklePath,
|
|
|
|
|
},
|
2021-06-06 04:26:37 -07:00
|
|
|
|
},
|
2022-05-03 13:02:29 -07:00
|
|
|
|
utilities::lookup_range_check::LookupRangeCheckConfig,
|
2021-06-06 04:26:37 -07:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-20 21:04:49 -08:00
|
|
|
|
mod commit_ivk;
|
2021-11-03 18:07:28 -07:00
|
|
|
|
pub mod gadget;
|
2021-12-20 21:04:49 -08:00
|
|
|
|
mod note_commit;
|
2021-02-25 10:11:46 -08:00
|
|
|
|
|
2021-04-14 21:35:48 -07:00
|
|
|
|
/// Size of the Orchard circuit.
|
2021-07-21 09:19:15 -07:00
|
|
|
|
const K: u32 = 11;
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
2021-07-19 09:42:22 -07:00
|
|
|
|
// Absolute offsets for public inputs.
|
2021-07-20 01:22:08 -07:00
|
|
|
|
const ANCHOR: usize = 0;
|
2021-07-22 07:14:34 -07:00
|
|
|
|
const CV_NET_X: usize = 1;
|
|
|
|
|
const CV_NET_Y: usize = 2;
|
|
|
|
|
const NF_OLD: usize = 3;
|
2021-07-20 01:22:08 -07:00
|
|
|
|
const RK_X: usize = 4;
|
|
|
|
|
const RK_Y: usize = 5;
|
|
|
|
|
const CMX: usize = 6;
|
|
|
|
|
const ENABLE_SPEND: usize = 7;
|
|
|
|
|
const ENABLE_OUTPUT: usize = 8;
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
2021-06-06 04:26:37 -07:00
|
|
|
|
/// Configuration needed to use the Orchard Action circuit.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct Config {
|
|
|
|
|
primary: Column<InstanceColumn>,
|
2021-07-19 09:42:22 -07:00
|
|
|
|
q_orchard: Selector,
|
2021-06-06 04:26:37 -07:00
|
|
|
|
advices: [Column<Advice>; 10],
|
2022-04-29 10:54:28 -07:00
|
|
|
|
add_config: AddConfig,
|
2021-08-18 23:59:39 -07:00
|
|
|
|
ecc_config: EccConfig<OrchardFixedBases>,
|
2021-11-02 07:52:11 -07:00
|
|
|
|
poseidon_config: PoseidonConfig<pallas::Base, 3, 2>,
|
2021-08-18 23:59:39 -07:00
|
|
|
|
merkle_config_1: MerkleConfig<OrchardHashDomains, OrchardCommitDomains, OrchardFixedBases>,
|
|
|
|
|
merkle_config_2: MerkleConfig<OrchardHashDomains, OrchardCommitDomains, OrchardFixedBases>,
|
|
|
|
|
sinsemilla_config_1:
|
|
|
|
|
SinsemillaConfig<OrchardHashDomains, OrchardCommitDomains, OrchardFixedBases>,
|
|
|
|
|
sinsemilla_config_2:
|
|
|
|
|
SinsemillaConfig<OrchardHashDomains, OrchardCommitDomains, OrchardFixedBases>,
|
2021-06-06 10:00:24 -07:00
|
|
|
|
commit_ivk_config: CommitIvkConfig,
|
2021-06-07 03:24:58 -07:00
|
|
|
|
old_note_commit_config: NoteCommitConfig,
|
2021-06-07 03:45:03 -07:00
|
|
|
|
new_note_commit_config: NoteCommitConfig,
|
2021-06-06 04:26:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 21:35:48 -07:00
|
|
|
|
/// The Orchard Action circuit.
|
2022-02-11 18:04:52 -08:00
|
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-06-06 04:21:27 -07:00
|
|
|
|
pub struct Circuit {
|
2022-06-08 17:01:10 -07:00
|
|
|
|
pub(crate) path: Value<[MerkleHashOrchard; MERKLE_DEPTH_ORCHARD]>,
|
|
|
|
|
pub(crate) pos: Value<u32>,
|
|
|
|
|
pub(crate) g_d_old: Value<NonIdentityPallasPoint>,
|
|
|
|
|
pub(crate) pk_d_old: Value<DiversifiedTransmissionKey>,
|
|
|
|
|
pub(crate) v_old: Value<NoteValue>,
|
|
|
|
|
pub(crate) rho_old: Value<Nullifier>,
|
|
|
|
|
pub(crate) psi_old: Value<pallas::Base>,
|
|
|
|
|
pub(crate) rcm_old: Value<NoteCommitTrapdoor>,
|
|
|
|
|
pub(crate) cm_old: Value<NoteCommitment>,
|
|
|
|
|
pub(crate) alpha: Value<pallas::Scalar>,
|
|
|
|
|
pub(crate) ak: Value<SpendValidatingKey>,
|
|
|
|
|
pub(crate) nk: Value<NullifierDerivingKey>,
|
|
|
|
|
pub(crate) rivk: Value<CommitIvkRandomness>,
|
|
|
|
|
pub(crate) g_d_new: Value<NonIdentityPallasPoint>,
|
|
|
|
|
pub(crate) pk_d_new: Value<DiversifiedTransmissionKey>,
|
|
|
|
|
pub(crate) v_new: Value<NoteValue>,
|
|
|
|
|
pub(crate) psi_new: Value<pallas::Base>,
|
|
|
|
|
pub(crate) rcm_new: Value<NoteCommitTrapdoor>,
|
|
|
|
|
pub(crate) rcv: Value<ValueCommitTrapdoor>,
|
2021-06-06 04:21:27 -07:00
|
|
|
|
}
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
2022-09-20 08:08:54 -07:00
|
|
|
|
impl Circuit {
|
|
|
|
|
/// This constructor is public to enable creation of custom builders.
|
|
|
|
|
/// If you are not creating a custom builder, use [`Builder`] to compose
|
|
|
|
|
/// and authorize a transaction.
|
|
|
|
|
///
|
|
|
|
|
/// Constructs a `Circuit` from the following components:
|
|
|
|
|
/// - `spend`: [`SpendInfo`] of the note spent in scope of the action
|
|
|
|
|
/// - `output_note`: a note created in scope of the action
|
|
|
|
|
/// - `alpha`: a scalar used for randomization of the action spend validating key
|
|
|
|
|
/// - `rcv`: trapdoor for the action value commitment
|
|
|
|
|
///
|
|
|
|
|
/// Returns `None` if the `rho` of the `output_note` is not equal
|
|
|
|
|
/// to the nullifier of the spent note.
|
|
|
|
|
///
|
|
|
|
|
/// [`SpendInfo`]: crate::builder::SpendInfo
|
|
|
|
|
/// [`Builder`]: crate::builder::Builder
|
|
|
|
|
pub fn from_action_context(
|
|
|
|
|
spend: SpendInfo,
|
|
|
|
|
output_note: Note,
|
|
|
|
|
alpha: pallas::Scalar,
|
|
|
|
|
rcv: ValueCommitTrapdoor,
|
|
|
|
|
) -> Option<Circuit> {
|
|
|
|
|
(spend.note.nullifier(&spend.fvk) == output_note.rho())
|
|
|
|
|
.then(|| Self::from_action_context_unchecked(spend, output_note, alpha, rcv))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn from_action_context_unchecked(
|
|
|
|
|
spend: SpendInfo,
|
|
|
|
|
output_note: Note,
|
|
|
|
|
alpha: pallas::Scalar,
|
|
|
|
|
rcv: ValueCommitTrapdoor,
|
|
|
|
|
) -> Circuit {
|
|
|
|
|
let sender_address = spend.note.recipient();
|
|
|
|
|
let rho_old = spend.note.rho();
|
|
|
|
|
let psi_old = spend.note.rseed().psi(&rho_old);
|
|
|
|
|
let rcm_old = spend.note.rseed().rcm(&rho_old);
|
|
|
|
|
|
|
|
|
|
let rho_new = output_note.rho();
|
|
|
|
|
let psi_new = output_note.rseed().psi(&rho_new);
|
|
|
|
|
let rcm_new = output_note.rseed().rcm(&rho_new);
|
|
|
|
|
|
|
|
|
|
Circuit {
|
|
|
|
|
path: Value::known(spend.merkle_path.auth_path()),
|
|
|
|
|
pos: Value::known(spend.merkle_path.position()),
|
|
|
|
|
g_d_old: Value::known(sender_address.g_d()),
|
|
|
|
|
pk_d_old: Value::known(*sender_address.pk_d()),
|
|
|
|
|
v_old: Value::known(spend.note.value()),
|
|
|
|
|
rho_old: Value::known(rho_old),
|
|
|
|
|
psi_old: Value::known(psi_old),
|
|
|
|
|
rcm_old: Value::known(rcm_old),
|
|
|
|
|
cm_old: Value::known(spend.note.commitment()),
|
|
|
|
|
alpha: Value::known(alpha),
|
|
|
|
|
ak: Value::known(spend.fvk.clone().into()),
|
|
|
|
|
nk: Value::known(*spend.fvk.nk()),
|
|
|
|
|
rivk: Value::known(spend.fvk.rivk(spend.scope)),
|
|
|
|
|
g_d_new: Value::known(output_note.recipient().g_d()),
|
|
|
|
|
pk_d_new: Value::known(*output_note.recipient().pk_d()),
|
|
|
|
|
v_new: Value::known(output_note.value()),
|
|
|
|
|
psi_new: Value::known(psi_new),
|
|
|
|
|
rcm_new: Value::known(rcm_new),
|
|
|
|
|
rcv: Value::known(rcv),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 21:35:48 -07:00
|
|
|
|
impl plonk::Circuit<pallas::Base> for Circuit {
|
2021-06-06 04:26:37 -07:00
|
|
|
|
type Config = Config;
|
2021-07-21 09:19:15 -07:00
|
|
|
|
type FloorPlanner = floor_planner::V1;
|
2021-07-08 18:56:27 -07:00
|
|
|
|
|
|
|
|
|
fn without_witnesses(&self) -> Self {
|
2021-06-06 04:26:37 -07:00
|
|
|
|
Self::default()
|
2021-07-08 18:56:27 -07:00
|
|
|
|
}
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
|
|
|
|
fn configure(meta: &mut plonk::ConstraintSystem<pallas::Base>) -> Self::Config {
|
2021-06-06 04:26:37 -07:00
|
|
|
|
// Advice columns used in the Orchard circuit.
|
|
|
|
|
let advices = [
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
];
|
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// Constrain v_old - v_new = magnitude * sign (https://p.z.cash/ZKS:action-cv-net-integrity?partial).
|
|
|
|
|
// Either v_old = 0, or calculated root = anchor (https://p.z.cash/ZKS:action-merkle-path-validity?partial).
|
|
|
|
|
// Constrain v_old = 0 or enable_spends = 1 (https://p.z.cash/ZKS:action-enable-spend).
|
|
|
|
|
// Constrain v_new = 0 or enable_outputs = 1 (https://p.z.cash/ZKS:action-enable-output).
|
2021-07-19 09:42:22 -07:00
|
|
|
|
let q_orchard = meta.selector();
|
|
|
|
|
meta.create_gate("Orchard circuit checks", |meta| {
|
|
|
|
|
let q_orchard = meta.query_selector(q_orchard);
|
2021-06-06 04:44:24 -07:00
|
|
|
|
let v_old = meta.query_advice(advices[0], Rotation::cur());
|
|
|
|
|
let v_new = meta.query_advice(advices[1], Rotation::cur());
|
|
|
|
|
let magnitude = meta.query_advice(advices[2], Rotation::cur());
|
|
|
|
|
let sign = meta.query_advice(advices[3], Rotation::cur());
|
|
|
|
|
|
2022-05-04 16:57:26 -07:00
|
|
|
|
let root = meta.query_advice(advices[4], Rotation::cur());
|
|
|
|
|
let anchor = meta.query_advice(advices[5], Rotation::cur());
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
2022-04-29 13:24:28 -07:00
|
|
|
|
let enable_spends = meta.query_advice(advices[6], Rotation::cur());
|
|
|
|
|
let enable_outputs = meta.query_advice(advices[7], Rotation::cur());
|
|
|
|
|
|
2021-07-19 10:01:39 -07:00
|
|
|
|
let one = Expression::Constant(pallas::Base::one());
|
|
|
|
|
|
2022-04-28 10:29:49 -07:00
|
|
|
|
Constraints::with_selector(
|
|
|
|
|
q_orchard,
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
"v_old - v_new = magnitude * sign",
|
|
|
|
|
v_old.clone() - v_new.clone() - magnitude * sign,
|
|
|
|
|
),
|
|
|
|
|
(
|
2022-05-04 16:57:26 -07:00
|
|
|
|
"Either v_old = 0, or root = anchor",
|
|
|
|
|
v_old.clone() * (root - anchor),
|
2022-04-28 10:29:49 -07:00
|
|
|
|
),
|
2022-04-29 13:24:28 -07:00
|
|
|
|
(
|
|
|
|
|
"v_old = 0 or enable_spends = 1",
|
|
|
|
|
v_old * (one.clone() - enable_spends),
|
|
|
|
|
),
|
2022-04-28 10:29:49 -07:00
|
|
|
|
(
|
|
|
|
|
"v_new = 0 or enable_outputs = 1",
|
2022-04-29 13:24:28 -07:00
|
|
|
|
v_new * (one - enable_outputs),
|
2022-04-28 10:29:49 -07:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
2021-06-06 04:44:24 -07:00
|
|
|
|
});
|
|
|
|
|
|
2022-04-29 10:54:28 -07:00
|
|
|
|
// Addition of two field elements.
|
|
|
|
|
let add_config = AddChip::configure(meta, advices[7], advices[8], advices[6]);
|
2021-07-23 02:58:37 -07:00
|
|
|
|
|
2021-06-06 04:26:37 -07:00
|
|
|
|
// Fixed columns for the Sinsemilla generator lookup table
|
2021-07-27 10:32:32 -07:00
|
|
|
|
let table_idx = meta.lookup_table_column();
|
|
|
|
|
let lookup = (
|
|
|
|
|
table_idx,
|
|
|
|
|
meta.lookup_table_column(),
|
|
|
|
|
meta.lookup_table_column(),
|
|
|
|
|
);
|
2021-06-06 04:26:37 -07:00
|
|
|
|
|
2021-07-20 01:22:08 -07:00
|
|
|
|
// Instance column used for public inputs
|
2021-07-19 09:42:22 -07:00
|
|
|
|
let primary = meta.instance_column();
|
2022-01-04 21:28:16 -08:00
|
|
|
|
meta.enable_equality(primary);
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
2021-07-20 01:22:08 -07:00
|
|
|
|
// Permutation over all advice columns.
|
2021-06-06 04:26:37 -07:00
|
|
|
|
for advice in advices.iter() {
|
2022-01-04 21:28:16 -08:00
|
|
|
|
meta.enable_equality(*advice);
|
2021-06-06 04:26:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 04:13:47 -07:00
|
|
|
|
// Poseidon requires four advice columns, while ECC incomplete addition requires
|
|
|
|
|
// six, so we could choose to configure them in parallel. However, we only use a
|
2022-02-18 13:31:27 -08:00
|
|
|
|
// single Poseidon invocation, and we have the rows to accommodate it serially.
|
2021-07-21 04:13:47 -07:00
|
|
|
|
// Instead, we reduce the proof size by sharing fixed columns between the ECC and
|
|
|
|
|
// Poseidon chips.
|
|
|
|
|
let lagrange_coeffs = [
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
meta.fixed_column(),
|
|
|
|
|
];
|
|
|
|
|
let rc_a = lagrange_coeffs[2..5].try_into().unwrap();
|
|
|
|
|
let rc_b = lagrange_coeffs[5..8].try_into().unwrap();
|
|
|
|
|
|
2021-07-21 09:20:39 -07:00
|
|
|
|
// Also use the first Lagrange coefficient column for loading global constants.
|
|
|
|
|
// It's free real estate :)
|
|
|
|
|
meta.enable_constant(lagrange_coeffs[0]);
|
|
|
|
|
|
2021-07-21 07:59:08 -07:00
|
|
|
|
// We have a lot of free space in the right-most advice columns; use one of them
|
|
|
|
|
// for all of our range checks.
|
|
|
|
|
let range_check = LookupRangeCheckConfig::configure(meta, advices[9], table_idx);
|
|
|
|
|
|
2021-06-06 04:26:37 -07:00
|
|
|
|
// Configuration for curve point operations.
|
|
|
|
|
// This uses 10 advice columns and spans the whole circuit.
|
2021-08-18 23:59:39 -07:00
|
|
|
|
let ecc_config =
|
|
|
|
|
EccChip::<OrchardFixedBases>::configure(meta, advices, lagrange_coeffs, range_check);
|
2021-06-06 04:26:37 -07:00
|
|
|
|
|
|
|
|
|
// Configuration for the Poseidon hash.
|
2021-11-03 06:12:24 -07:00
|
|
|
|
let poseidon_config = PoseidonChip::configure::<poseidon::P128Pow5T3>(
|
2021-06-06 04:26:37 -07:00
|
|
|
|
meta,
|
2021-07-23 15:13:10 -07:00
|
|
|
|
// We place the state columns after the partial_sbox column so that the
|
2022-02-18 13:31:27 -08:00
|
|
|
|
// pad-and-add region can be laid out more efficiently.
|
2021-07-23 15:13:10 -07:00
|
|
|
|
advices[6..9].try_into().unwrap(),
|
|
|
|
|
advices[5],
|
2021-07-21 04:13:47 -07:00
|
|
|
|
rc_a,
|
|
|
|
|
rc_b,
|
2021-06-06 04:26:37 -07:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Configuration for a Sinsemilla hash instantiation and a
|
|
|
|
|
// Merkle hash instantiation using this Sinsemilla instance.
|
|
|
|
|
// Since the Sinsemilla config uses only 5 advice columns,
|
|
|
|
|
// we can fit two instances side-by-side.
|
|
|
|
|
let (sinsemilla_config_1, merkle_config_1) = {
|
2021-07-21 07:59:08 -07:00
|
|
|
|
let sinsemilla_config_1 = SinsemillaChip::configure(
|
|
|
|
|
meta,
|
|
|
|
|
advices[..5].try_into().unwrap(),
|
2021-07-21 08:47:49 -07:00
|
|
|
|
advices[6],
|
2021-07-23 05:25:02 -07:00
|
|
|
|
lagrange_coeffs[0],
|
2021-07-21 07:59:08 -07:00
|
|
|
|
lookup,
|
2021-11-30 11:31:42 -08:00
|
|
|
|
range_check,
|
2021-07-21 07:59:08 -07:00
|
|
|
|
);
|
2021-06-06 04:26:37 -07:00
|
|
|
|
let merkle_config_1 = MerkleChip::configure(meta, sinsemilla_config_1.clone());
|
|
|
|
|
|
|
|
|
|
(sinsemilla_config_1, merkle_config_1)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Configuration for a Sinsemilla hash instantiation and a
|
|
|
|
|
// Merkle hash instantiation using this Sinsemilla instance.
|
|
|
|
|
// Since the Sinsemilla config uses only 5 advice columns,
|
|
|
|
|
// we can fit two instances side-by-side.
|
|
|
|
|
let (sinsemilla_config_2, merkle_config_2) = {
|
2021-07-21 07:59:08 -07:00
|
|
|
|
let sinsemilla_config_2 = SinsemillaChip::configure(
|
|
|
|
|
meta,
|
|
|
|
|
advices[5..].try_into().unwrap(),
|
2021-07-21 08:47:49 -07:00
|
|
|
|
advices[7],
|
2021-07-23 05:25:02 -07:00
|
|
|
|
lagrange_coeffs[1],
|
2021-07-21 07:59:08 -07:00
|
|
|
|
lookup,
|
|
|
|
|
range_check,
|
|
|
|
|
);
|
2021-06-06 04:26:37 -07:00
|
|
|
|
let merkle_config_2 = MerkleChip::configure(meta, sinsemilla_config_2.clone());
|
|
|
|
|
|
|
|
|
|
(sinsemilla_config_2, merkle_config_2)
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-06 10:00:24 -07:00
|
|
|
|
// Configuration to handle decomposition and canonicity checking
|
|
|
|
|
// for CommitIvk.
|
2022-05-02 05:35:16 -07:00
|
|
|
|
let commit_ivk_config = CommitIvkChip::configure(meta, advices);
|
2021-06-06 10:00:24 -07:00
|
|
|
|
|
2021-06-07 03:24:58 -07:00
|
|
|
|
// Configuration to handle decomposition and canonicity checking
|
|
|
|
|
// for NoteCommit_old.
|
|
|
|
|
let old_note_commit_config =
|
2022-05-03 19:55:42 -07:00
|
|
|
|
NoteCommitChip::configure(meta, advices, sinsemilla_config_1.clone());
|
2021-06-07 03:24:58 -07:00
|
|
|
|
|
2021-06-07 03:45:03 -07:00
|
|
|
|
// Configuration to handle decomposition and canonicity checking
|
|
|
|
|
// for NoteCommit_new.
|
|
|
|
|
let new_note_commit_config =
|
2022-05-03 19:55:42 -07:00
|
|
|
|
NoteCommitChip::configure(meta, advices, sinsemilla_config_2.clone());
|
2021-06-07 03:45:03 -07:00
|
|
|
|
|
2021-06-06 04:26:37 -07:00
|
|
|
|
Config {
|
|
|
|
|
primary,
|
2021-07-19 09:42:22 -07:00
|
|
|
|
q_orchard,
|
2021-06-06 04:26:37 -07:00
|
|
|
|
advices,
|
2022-04-29 10:54:28 -07:00
|
|
|
|
add_config,
|
2021-06-06 04:26:37 -07:00
|
|
|
|
ecc_config,
|
|
|
|
|
poseidon_config,
|
|
|
|
|
merkle_config_1,
|
|
|
|
|
merkle_config_2,
|
|
|
|
|
sinsemilla_config_1,
|
|
|
|
|
sinsemilla_config_2,
|
2021-06-06 10:00:24 -07:00
|
|
|
|
commit_ivk_config,
|
2021-06-07 03:24:58 -07:00
|
|
|
|
old_note_commit_config,
|
2021-06-07 03:45:03 -07:00
|
|
|
|
new_note_commit_config,
|
2021-06-06 04:26:37 -07:00
|
|
|
|
}
|
2021-04-14 21:35:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 04:18:42 -07:00
|
|
|
|
#[allow(non_snake_case)]
|
2021-04-14 21:35:48 -07:00
|
|
|
|
fn synthesize(
|
|
|
|
|
&self,
|
2021-06-06 04:36:01 -07:00
|
|
|
|
config: Self::Config,
|
|
|
|
|
mut layouter: impl Layouter<pallas::Base>,
|
2021-04-14 21:35:48 -07:00
|
|
|
|
) -> Result<(), plonk::Error> {
|
2021-06-06 04:36:01 -07:00
|
|
|
|
// Load the Sinsemilla generator lookup table used by the whole circuit.
|
2021-06-06 21:26:02 -07:00
|
|
|
|
SinsemillaChip::load(config.sinsemilla_config_1.clone(), &mut layouter)?;
|
|
|
|
|
|
|
|
|
|
// Construct the ECC chip.
|
|
|
|
|
let ecc_chip = config.ecc_chip();
|
|
|
|
|
|
|
|
|
|
// Witness private inputs that are used across multiple checks.
|
2022-04-29 04:18:42 -07:00
|
|
|
|
let (psi_old, rho_old, cm_old, g_d_old, ak_P, nk, v_old, v_new) = {
|
2021-06-06 21:26:02 -07:00
|
|
|
|
// Witness psi_old
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let psi_old = assign_free_advice(
|
2021-06-06 21:26:02 -07:00
|
|
|
|
layouter.namespace(|| "witness psi_old"),
|
|
|
|
|
config.advices[0],
|
|
|
|
|
self.psi_old,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Witness rho_old
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let rho_old = assign_free_advice(
|
2021-06-06 21:26:02 -07:00
|
|
|
|
layouter.namespace(|| "witness rho_old"),
|
|
|
|
|
config.advices[0],
|
|
|
|
|
self.rho_old.map(|rho| rho.0),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Witness cm_old
|
2021-09-28 09:00:01 -07:00
|
|
|
|
let cm_old = Point::new(
|
2021-07-27 00:41:26 -07:00
|
|
|
|
ecc_chip.clone(),
|
2021-06-06 21:26:02 -07:00
|
|
|
|
layouter.namespace(|| "cm_old"),
|
2021-07-22 07:14:34 -07:00
|
|
|
|
self.cm_old.as_ref().map(|cm| cm.inner().to_affine()),
|
2021-06-06 21:26:02 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Witness g_d_old
|
2021-09-27 01:51:57 -07:00
|
|
|
|
let g_d_old = NonIdentityPoint::new(
|
2021-07-27 00:41:26 -07:00
|
|
|
|
ecc_chip.clone(),
|
2021-06-06 21:26:02 -07:00
|
|
|
|
layouter.namespace(|| "gd_old"),
|
|
|
|
|
self.g_d_old.as_ref().map(|gd| gd.to_affine()),
|
|
|
|
|
)?;
|
|
|
|
|
|
2022-04-29 04:18:42 -07:00
|
|
|
|
// Witness ak_P.
|
2022-06-08 17:01:10 -07:00
|
|
|
|
let ak_P: Value<pallas::Point> = self.ak.as_ref().map(|ak| ak.into());
|
2022-04-29 04:18:42 -07:00
|
|
|
|
let ak_P = NonIdentityPoint::new(
|
2021-06-06 21:26:02 -07:00
|
|
|
|
ecc_chip.clone(),
|
2022-04-29 04:18:42 -07:00
|
|
|
|
layouter.namespace(|| "witness ak_P"),
|
|
|
|
|
ak_P.map(|ak_P| ak_P.to_affine()),
|
2021-06-06 21:26:02 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Witness nk.
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let nk = assign_free_advice(
|
2021-06-06 21:26:02 -07:00
|
|
|
|
layouter.namespace(|| "witness nk"),
|
|
|
|
|
config.advices[0],
|
2021-07-22 07:14:34 -07:00
|
|
|
|
self.nk.map(|nk| nk.inner()),
|
2021-06-06 21:26:02 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Witness v_old.
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let v_old = assign_free_advice(
|
2021-06-06 21:26:02 -07:00
|
|
|
|
layouter.namespace(|| "witness v_old"),
|
|
|
|
|
config.advices[0],
|
2022-05-03 13:26:41 -07:00
|
|
|
|
self.v_old,
|
2021-06-06 21:26:02 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Witness v_new.
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let v_new = assign_free_advice(
|
2021-06-06 21:26:02 -07:00
|
|
|
|
layouter.namespace(|| "witness v_new"),
|
|
|
|
|
config.advices[0],
|
2022-05-03 13:26:41 -07:00
|
|
|
|
self.v_new,
|
2021-06-06 21:26:02 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
2022-04-29 04:18:42 -07:00
|
|
|
|
(psi_old, rho_old, cm_old, g_d_old, ak_P, nk, v_old, v_new)
|
2021-06-06 21:26:02 -07:00
|
|
|
|
};
|
2021-06-06 04:36:01 -07:00
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// Merkle path validity check (https://p.z.cash/ZKS:action-merkle-path-validity?partial).
|
2022-04-29 13:24:28 -07:00
|
|
|
|
let root = {
|
2022-04-29 06:23:01 -07:00
|
|
|
|
let path = self
|
|
|
|
|
.path
|
|
|
|
|
.map(|typed_path| typed_path.map(|node| node.inner()));
|
2022-01-28 07:53:03 -08:00
|
|
|
|
let merkle_inputs = MerklePath::construct(
|
2022-05-10 13:53:49 -07:00
|
|
|
|
[config.merkle_chip_1(), config.merkle_chip_2()],
|
2022-01-28 07:53:03 -08:00
|
|
|
|
OrchardHashDomains::MerkleCrh,
|
|
|
|
|
self.pos,
|
2021-09-15 09:42:48 -07:00
|
|
|
|
path,
|
2022-01-28 07:53:03 -08:00
|
|
|
|
);
|
2021-12-01 04:51:33 -08:00
|
|
|
|
let leaf = cm_old.extract_p().inner().clone();
|
2022-04-29 13:24:28 -07:00
|
|
|
|
merkle_inputs.calculate_root(layouter.namespace(|| "Merkle path"), leaf)?
|
2021-06-06 04:32:59 -07:00
|
|
|
|
};
|
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// Value commitment integrity (https://p.z.cash/ZKS:action-cv-net-integrity?partial).
|
2022-05-10 13:53:49 -07:00
|
|
|
|
let v_net_magnitude_sign = {
|
2022-04-29 06:54:42 -07:00
|
|
|
|
// Witness the magnitude and sign of v_net = v_old - v_new
|
2022-05-10 13:53:49 -07:00
|
|
|
|
let v_net_magnitude_sign = {
|
2022-06-08 17:01:10 -07:00
|
|
|
|
let v_net = self.v_old - self.v_new;
|
|
|
|
|
let magnitude_sign = v_net.map(|v_net| {
|
2022-04-29 06:54:42 -07:00
|
|
|
|
let (magnitude, sign) = v_net.magnitude_sign();
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
// magnitude is guaranteed to be an unsigned 64-bit value.
|
|
|
|
|
// Therefore, we can move it into the base field.
|
|
|
|
|
pallas::Base::from(magnitude),
|
|
|
|
|
match sign {
|
|
|
|
|
crate::value::Sign::Positive => pallas::Base::one(),
|
|
|
|
|
crate::value::Sign::Negative => -pallas::Base::one(),
|
|
|
|
|
},
|
|
|
|
|
)
|
2021-06-06 04:44:24 -07:00
|
|
|
|
});
|
2021-07-20 01:22:08 -07:00
|
|
|
|
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let magnitude = assign_free_advice(
|
2021-06-06 04:44:24 -07:00
|
|
|
|
layouter.namespace(|| "v_net magnitude"),
|
|
|
|
|
config.advices[9],
|
|
|
|
|
magnitude_sign.map(|m_s| m_s.0),
|
|
|
|
|
)?;
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let sign = assign_free_advice(
|
2021-06-06 04:44:24 -07:00
|
|
|
|
layouter.namespace(|| "v_net sign"),
|
|
|
|
|
config.advices[9],
|
|
|
|
|
magnitude_sign.map(|m_s| m_s.1),
|
|
|
|
|
)?;
|
|
|
|
|
(magnitude, sign)
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-10 13:53:49 -07:00
|
|
|
|
let v_net = ScalarFixedShort::new(
|
|
|
|
|
ecc_chip.clone(),
|
|
|
|
|
layouter.namespace(|| "v_net"),
|
|
|
|
|
v_net_magnitude_sign.clone(),
|
|
|
|
|
)?;
|
|
|
|
|
let rcv = ScalarFixed::new(
|
|
|
|
|
ecc_chip.clone(),
|
|
|
|
|
layouter.namespace(|| "rcv"),
|
|
|
|
|
self.rcv.as_ref().map(|rcv| rcv.inner()),
|
|
|
|
|
)?;
|
|
|
|
|
|
2022-04-29 13:05:00 -07:00
|
|
|
|
let cv_net = gadget::value_commit_orchard(
|
|
|
|
|
layouter.namespace(|| "cv_net = ValueCommit^Orchard_rcv(v_net)"),
|
|
|
|
|
ecc_chip.clone(),
|
2022-05-10 13:53:49 -07:00
|
|
|
|
v_net,
|
|
|
|
|
rcv,
|
2022-04-29 13:05:00 -07:00
|
|
|
|
)?;
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
|
|
|
|
// Constrain cv_net to equal public input
|
|
|
|
|
layouter.constrain_instance(cv_net.inner().x().cell(), config.primary, CV_NET_X)?;
|
|
|
|
|
layouter.constrain_instance(cv_net.inner().y().cell(), config.primary, CV_NET_Y)?;
|
|
|
|
|
|
2022-05-10 13:53:49 -07:00
|
|
|
|
// Return the magnitude and sign so we can use them in the Orchard gate.
|
|
|
|
|
v_net_magnitude_sign
|
2021-06-06 04:44:24 -07:00
|
|
|
|
};
|
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// Nullifier integrity (https://p.z.cash/ZKS:action-nullifier-integrity).
|
2021-06-07 03:45:03 -07:00
|
|
|
|
let nf_old = {
|
2022-04-29 10:54:28 -07:00
|
|
|
|
let nf_old = gadget::derive_nullifier(
|
|
|
|
|
layouter.namespace(|| "nf_old = DeriveNullifier_nk(rho_old, psi_old, cm_old)"),
|
|
|
|
|
config.poseidon_chip(),
|
|
|
|
|
config.add_chip(),
|
|
|
|
|
ecc_chip.clone(),
|
|
|
|
|
rho_old.clone(),
|
|
|
|
|
&psi_old,
|
|
|
|
|
&cm_old,
|
|
|
|
|
nk.clone(),
|
2021-07-23 02:58:37 -07:00
|
|
|
|
)?;
|
2021-06-06 05:05:51 -07:00
|
|
|
|
|
2021-07-19 09:42:22 -07:00
|
|
|
|
// Constrain nf_old to equal public input
|
|
|
|
|
layouter.constrain_instance(nf_old.inner().cell(), config.primary, NF_OLD)?;
|
|
|
|
|
|
|
|
|
|
nf_old
|
2021-06-06 05:05:51 -07:00
|
|
|
|
};
|
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// Spend authority (https://p.z.cash/ZKS:action-spend-authority)
|
2021-07-19 09:42:22 -07:00
|
|
|
|
{
|
2022-05-10 13:53:49 -07:00
|
|
|
|
let alpha =
|
|
|
|
|
ScalarFixed::new(ecc_chip.clone(), layouter.namespace(|| "alpha"), self.alpha)?;
|
|
|
|
|
|
2021-06-06 05:07:18 -07:00
|
|
|
|
// alpha_commitment = [alpha] SpendAuthG
|
|
|
|
|
let (alpha_commitment, _) = {
|
|
|
|
|
let spend_auth_g = OrchardFixedBasesFull::SpendAuthG;
|
|
|
|
|
let spend_auth_g = FixedPoint::from_inner(ecc_chip.clone(), spend_auth_g);
|
2022-05-10 13:53:49 -07:00
|
|
|
|
spend_auth_g.mul(layouter.namespace(|| "[alpha] SpendAuthG"), alpha)?
|
2021-06-06 05:07:18 -07:00
|
|
|
|
};
|
|
|
|
|
|
2022-04-29 04:18:42 -07:00
|
|
|
|
// [alpha] SpendAuthG + ak_P
|
|
|
|
|
let rk = alpha_commitment.add(layouter.namespace(|| "rk"), &ak_P)?;
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
|
|
|
|
// Constrain rk to equal public input
|
|
|
|
|
layouter.constrain_instance(rk.inner().x().cell(), config.primary, RK_X)?;
|
|
|
|
|
layouter.constrain_instance(rk.inner().y().cell(), config.primary, RK_Y)?;
|
|
|
|
|
}
|
2021-06-06 05:07:18 -07:00
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// Diversified address integrity (https://p.z.cash/ZKS:action-addr-integrity?partial).
|
2021-07-19 09:42:22 -07:00
|
|
|
|
let pk_d_old = {
|
2021-06-06 10:00:24 -07:00
|
|
|
|
let ivk = {
|
2022-04-29 04:18:42 -07:00
|
|
|
|
let ak = ak_P.extract_p().inner().clone();
|
2022-05-10 13:53:49 -07:00
|
|
|
|
let rivk = ScalarFixed::new(
|
|
|
|
|
ecc_chip.clone(),
|
2022-06-23 15:33:10 -07:00
|
|
|
|
layouter.namespace(|| "rivk"),
|
2022-05-10 13:53:49 -07:00
|
|
|
|
self.rivk.map(|rivk| rivk.inner()),
|
|
|
|
|
)?;
|
2021-06-06 10:00:24 -07:00
|
|
|
|
|
2022-05-02 05:35:16 -07:00
|
|
|
|
gadget::commit_ivk(
|
2021-06-06 10:00:24 -07:00
|
|
|
|
config.sinsemilla_chip_1(),
|
|
|
|
|
ecc_chip.clone(),
|
2022-05-02 05:35:16 -07:00
|
|
|
|
config.commit_ivk_chip(),
|
2021-06-06 10:00:24 -07:00
|
|
|
|
layouter.namespace(|| "CommitIvk"),
|
2022-04-29 04:18:42 -07:00
|
|
|
|
ak,
|
2021-06-06 10:00:24 -07:00
|
|
|
|
nk,
|
|
|
|
|
rivk,
|
|
|
|
|
)?
|
|
|
|
|
};
|
2022-04-19 03:58:48 -07:00
|
|
|
|
let ivk =
|
|
|
|
|
ScalarVar::from_base(ecc_chip.clone(), layouter.namespace(|| "ivk"), ivk.inner())?;
|
2021-06-06 10:00:24 -07:00
|
|
|
|
|
|
|
|
|
// [ivk] g_d_old
|
2021-07-20 01:22:08 -07:00
|
|
|
|
// The scalar value is passed through and discarded.
|
|
|
|
|
let (derived_pk_d_old, _ivk) =
|
2022-04-19 03:58:48 -07:00
|
|
|
|
g_d_old.mul(layouter.namespace(|| "[ivk] g_d_old"), ivk)?;
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
|
|
|
|
// Constrain derived pk_d_old to equal witnessed pk_d_old
|
2022-04-29 13:24:28 -07:00
|
|
|
|
//
|
|
|
|
|
// This equality constraint is technically superfluous, because the assigned
|
|
|
|
|
// value of `derived_pk_d_old` is an equivalent witness. But it's nice to see
|
|
|
|
|
// an explicit connection between circuit-synthesized values, and explicit
|
|
|
|
|
// prover witnesses. We could get the best of both worlds with a write-on-copy
|
|
|
|
|
// abstraction (https://github.com/zcash/halo2/issues/334).
|
2021-09-27 01:51:57 -07:00
|
|
|
|
let pk_d_old = NonIdentityPoint::new(
|
2021-07-19 09:42:22 -07:00
|
|
|
|
ecc_chip.clone(),
|
|
|
|
|
layouter.namespace(|| "witness pk_d_old"),
|
2021-07-22 07:14:34 -07:00
|
|
|
|
self.pk_d_old.map(|pk_d_old| pk_d_old.inner().to_affine()),
|
2021-07-19 09:42:22 -07:00
|
|
|
|
)?;
|
|
|
|
|
derived_pk_d_old
|
|
|
|
|
.constrain_equal(layouter.namespace(|| "pk_d_old equality"), &pk_d_old)?;
|
|
|
|
|
|
|
|
|
|
pk_d_old
|
2021-06-06 10:00:24 -07:00
|
|
|
|
};
|
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// Old note commitment integrity (https://p.z.cash/ZKS:action-cm-old-integrity?partial).
|
2021-07-19 09:42:22 -07:00
|
|
|
|
{
|
2022-05-10 13:53:49 -07:00
|
|
|
|
let rcm_old = ScalarFixed::new(
|
|
|
|
|
ecc_chip.clone(),
|
|
|
|
|
layouter.namespace(|| "rcm_old"),
|
|
|
|
|
self.rcm_old.as_ref().map(|rcm_old| rcm_old.inner()),
|
|
|
|
|
)?;
|
2021-06-07 03:24:58 -07:00
|
|
|
|
|
|
|
|
|
// g★_d || pk★_d || i2lebsp_{64}(v) || i2lebsp_{255}(rho) || i2lebsp_{255}(psi)
|
2022-05-03 19:55:42 -07:00
|
|
|
|
let derived_cm_old = gadget::note_commit(
|
2021-06-07 03:24:58 -07:00
|
|
|
|
layouter.namespace(|| {
|
|
|
|
|
"g★_d || pk★_d || i2lebsp_{64}(v) || i2lebsp_{255}(rho) || i2lebsp_{255}(psi)"
|
|
|
|
|
}),
|
|
|
|
|
config.sinsemilla_chip_1(),
|
|
|
|
|
config.ecc_chip(),
|
2022-05-03 19:55:42 -07:00
|
|
|
|
config.note_commit_chip_old(),
|
2021-06-07 03:24:58 -07:00
|
|
|
|
g_d_old.inner(),
|
|
|
|
|
pk_d_old.inner(),
|
2021-12-01 04:51:33 -08:00
|
|
|
|
v_old.clone(),
|
2021-06-07 03:24:58 -07:00
|
|
|
|
rho_old,
|
|
|
|
|
psi_old,
|
|
|
|
|
rcm_old,
|
2021-07-19 09:42:22 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Constrain derived cm_old to equal witnessed cm_old
|
|
|
|
|
derived_cm_old.constrain_equal(layouter.namespace(|| "cm_old equality"), &cm_old)?;
|
|
|
|
|
}
|
2021-06-07 03:24:58 -07:00
|
|
|
|
|
2022-04-29 15:39:03 -07:00
|
|
|
|
// New note commitment integrity (https://p.z.cash/ZKS:action-cmx-new-integrity?partial).
|
2021-07-19 09:42:22 -07:00
|
|
|
|
{
|
2022-04-29 05:44:57 -07:00
|
|
|
|
// Witness g_d_new
|
2021-06-07 03:45:03 -07:00
|
|
|
|
let g_d_new = {
|
2022-04-29 05:44:57 -07:00
|
|
|
|
let g_d_new = self.g_d_new.map(|g_d_new| g_d_new.to_affine());
|
2021-09-27 01:51:57 -07:00
|
|
|
|
NonIdentityPoint::new(
|
2021-06-07 03:45:03 -07:00
|
|
|
|
ecc_chip.clone(),
|
|
|
|
|
layouter.namespace(|| "witness g_d_new_star"),
|
|
|
|
|
g_d_new,
|
|
|
|
|
)?
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-29 05:44:57 -07:00
|
|
|
|
// Witness pk_d_new
|
2021-06-07 03:45:03 -07:00
|
|
|
|
let pk_d_new = {
|
2022-04-29 05:44:57 -07:00
|
|
|
|
let pk_d_new = self.pk_d_new.map(|pk_d_new| pk_d_new.inner().to_affine());
|
2021-09-27 01:51:57 -07:00
|
|
|
|
NonIdentityPoint::new(
|
2022-05-10 13:53:49 -07:00
|
|
|
|
ecc_chip.clone(),
|
2021-06-07 03:45:03 -07:00
|
|
|
|
layouter.namespace(|| "witness pk_d_new"),
|
|
|
|
|
pk_d_new,
|
|
|
|
|
)?
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-29 13:24:28 -07:00
|
|
|
|
// ρ^new = nf^old
|
|
|
|
|
let rho_new = nf_old.inner().clone();
|
|
|
|
|
|
2021-06-07 03:45:03 -07:00
|
|
|
|
// Witness psi_new
|
2022-05-03 13:02:29 -07:00
|
|
|
|
let psi_new = assign_free_advice(
|
2021-06-07 03:45:03 -07:00
|
|
|
|
layouter.namespace(|| "witness psi_new"),
|
|
|
|
|
config.advices[0],
|
|
|
|
|
self.psi_new,
|
|
|
|
|
)?;
|
|
|
|
|
|
2022-05-10 13:53:49 -07:00
|
|
|
|
let rcm_new = ScalarFixed::new(
|
|
|
|
|
ecc_chip,
|
2022-06-23 15:33:10 -07:00
|
|
|
|
layouter.namespace(|| "rcm_new"),
|
2022-05-10 13:53:49 -07:00
|
|
|
|
self.rcm_new.as_ref().map(|rcm_new| rcm_new.inner()),
|
|
|
|
|
)?;
|
2021-06-07 03:45:03 -07:00
|
|
|
|
|
|
|
|
|
// g★_d || pk★_d || i2lebsp_{64}(v) || i2lebsp_{255}(rho) || i2lebsp_{255}(psi)
|
2022-05-03 19:55:42 -07:00
|
|
|
|
let cm_new = gadget::note_commit(
|
2021-06-07 03:45:03 -07:00
|
|
|
|
layouter.namespace(|| {
|
|
|
|
|
"g★_d || pk★_d || i2lebsp_{64}(v) || i2lebsp_{255}(rho) || i2lebsp_{255}(psi)"
|
|
|
|
|
}),
|
|
|
|
|
config.sinsemilla_chip_2(),
|
|
|
|
|
config.ecc_chip(),
|
2022-05-03 19:55:42 -07:00
|
|
|
|
config.note_commit_chip_new(),
|
2021-06-07 03:45:03 -07:00
|
|
|
|
g_d_new.inner(),
|
|
|
|
|
pk_d_new.inner(),
|
2021-12-01 04:51:33 -08:00
|
|
|
|
v_new.clone(),
|
2022-04-29 13:24:28 -07:00
|
|
|
|
rho_new,
|
2021-06-07 03:45:03 -07:00
|
|
|
|
psi_new,
|
|
|
|
|
rcm_new,
|
|
|
|
|
)?;
|
|
|
|
|
|
2021-07-19 09:42:22 -07:00
|
|
|
|
let cmx = cm_new.extract_p();
|
|
|
|
|
|
|
|
|
|
// Constrain cmx to equal public input
|
|
|
|
|
layouter.constrain_instance(cmx.inner().cell(), config.primary, CMX)?;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 13:24:28 -07:00
|
|
|
|
// Constrain the remaining Orchard circuit checks.
|
2021-07-19 09:42:22 -07:00
|
|
|
|
layouter.assign_region(
|
2022-04-29 13:24:28 -07:00
|
|
|
|
|| "Orchard circuit checks",
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|mut region| {
|
2021-12-01 18:10:00 -08:00
|
|
|
|
v_old.copy_advice(|| "v_old", &mut region, config.advices[0], 0)?;
|
|
|
|
|
v_new.copy_advice(|| "v_new", &mut region, config.advices[1], 0)?;
|
2022-05-10 13:53:49 -07:00
|
|
|
|
v_net_magnitude_sign.0.copy_advice(
|
|
|
|
|
|| "v_net magnitude",
|
|
|
|
|
&mut region,
|
|
|
|
|
config.advices[2],
|
|
|
|
|
0,
|
|
|
|
|
)?;
|
|
|
|
|
v_net_magnitude_sign.1.copy_advice(
|
|
|
|
|
|| "v_net sign",
|
|
|
|
|
&mut region,
|
|
|
|
|
config.advices[3],
|
|
|
|
|
0,
|
|
|
|
|
)?;
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
2022-04-29 13:24:28 -07:00
|
|
|
|
root.copy_advice(|| "calculated root", &mut region, config.advices[4], 0)?;
|
2021-07-19 09:42:22 -07:00
|
|
|
|
region.assign_advice_from_instance(
|
|
|
|
|
|| "pub input anchor",
|
|
|
|
|
config.primary,
|
|
|
|
|
ANCHOR,
|
|
|
|
|
config.advices[5],
|
|
|
|
|
0,
|
|
|
|
|
)?;
|
|
|
|
|
|
2021-07-19 10:01:39 -07:00
|
|
|
|
region.assign_advice_from_instance(
|
|
|
|
|
|| "enable spends",
|
|
|
|
|
config.primary,
|
|
|
|
|
ENABLE_SPEND,
|
|
|
|
|
config.advices[6],
|
|
|
|
|
0,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
region.assign_advice_from_instance(
|
|
|
|
|
|| "enable outputs",
|
|
|
|
|
config.primary,
|
|
|
|
|
ENABLE_OUTPUT,
|
|
|
|
|
config.advices[7],
|
|
|
|
|
0,
|
|
|
|
|
)?;
|
|
|
|
|
|
2021-07-19 09:42:22 -07:00
|
|
|
|
config.q_orchard.enable(&mut region, 0)
|
|
|
|
|
},
|
|
|
|
|
)?;
|
2021-06-07 03:45:03 -07:00
|
|
|
|
|
2021-04-14 21:35:48 -07:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The verifying key for the Orchard Action circuit.
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct VerifyingKey {
|
2022-06-21 08:07:31 -07:00
|
|
|
|
pub(crate) params: halo2_proofs::poly::commitment::Params<vesta::Affine>,
|
|
|
|
|
pub(crate) vk: plonk::VerifyingKey<vesta::Affine>,
|
2021-04-14 21:35:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VerifyingKey {
|
|
|
|
|
/// Builds the verifying key.
|
|
|
|
|
pub fn build() -> Self {
|
2022-01-28 07:53:03 -08:00
|
|
|
|
let params = halo2_proofs::poly::commitment::Params::new(K);
|
2021-07-20 01:22:08 -07:00
|
|
|
|
let circuit: Circuit = Default::default();
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
|
|
|
|
let vk = plonk::keygen_vk(¶ms, &circuit).unwrap();
|
|
|
|
|
|
|
|
|
|
VerifyingKey { params, vk }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The proving key for the Orchard Action circuit.
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct ProvingKey {
|
2022-01-28 07:53:03 -08:00
|
|
|
|
params: halo2_proofs::poly::commitment::Params<vesta::Affine>,
|
2021-04-14 21:35:48 -07:00
|
|
|
|
pk: plonk::ProvingKey<vesta::Affine>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ProvingKey {
|
|
|
|
|
/// Builds the proving key.
|
|
|
|
|
pub fn build() -> Self {
|
2022-01-28 07:53:03 -08:00
|
|
|
|
let params = halo2_proofs::poly::commitment::Params::new(K);
|
2021-07-20 01:22:08 -07:00
|
|
|
|
let circuit: Circuit = Default::default();
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
|
|
|
|
let vk = plonk::keygen_vk(¶ms, &circuit).unwrap();
|
|
|
|
|
let pk = plonk::keygen_pk(¶ms, vk, &circuit).unwrap();
|
|
|
|
|
|
|
|
|
|
ProvingKey { params, pk }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Public inputs to the Orchard Action circuit.
|
2021-11-04 20:30:43 -07:00
|
|
|
|
#[derive(Clone, Debug)]
|
2021-04-14 21:35:48 -07:00
|
|
|
|
pub struct Instance {
|
|
|
|
|
pub(crate) anchor: Anchor,
|
|
|
|
|
pub(crate) cv_net: ValueCommitment,
|
|
|
|
|
pub(crate) nf_old: Nullifier,
|
|
|
|
|
pub(crate) rk: VerificationKey<SpendAuth>,
|
|
|
|
|
pub(crate) cmx: ExtractedNoteCommitment,
|
|
|
|
|
pub(crate) enable_spend: bool,
|
|
|
|
|
pub(crate) enable_output: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Instance {
|
2021-11-04 14:54:30 -07:00
|
|
|
|
/// Constructs an [`Instance`] from its constituent parts.
|
|
|
|
|
///
|
|
|
|
|
/// This API can be used in combination with [`Proof::verify`] to build verification
|
|
|
|
|
/// pipelines for many proofs, where you don't want to pass around the full bundle.
|
|
|
|
|
/// Use [`Bundle::verify_proof`] instead if you have the full bundle.
|
|
|
|
|
///
|
|
|
|
|
/// [`Bundle::verify_proof`]: crate::Bundle::verify_proof
|
2021-11-03 20:15:27 -07:00
|
|
|
|
pub fn from_parts(
|
|
|
|
|
anchor: Anchor,
|
|
|
|
|
cv_net: ValueCommitment,
|
|
|
|
|
nf_old: Nullifier,
|
|
|
|
|
rk: VerificationKey<SpendAuth>,
|
|
|
|
|
cmx: ExtractedNoteCommitment,
|
|
|
|
|
enable_spend: bool,
|
|
|
|
|
enable_output: bool,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Instance {
|
|
|
|
|
anchor,
|
|
|
|
|
cv_net,
|
|
|
|
|
nf_old,
|
|
|
|
|
rk,
|
|
|
|
|
cmx,
|
|
|
|
|
enable_spend,
|
|
|
|
|
enable_output,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 01:22:08 -07:00
|
|
|
|
fn to_halo2_instance(&self) -> [[vesta::Scalar; 9]; 1] {
|
|
|
|
|
let mut instance = [vesta::Scalar::zero(); 9];
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
2021-07-22 07:14:34 -07:00
|
|
|
|
instance[ANCHOR] = self.anchor.inner();
|
2021-07-19 09:42:22 -07:00
|
|
|
|
instance[CV_NET_X] = self.cv_net.x();
|
|
|
|
|
instance[CV_NET_Y] = self.cv_net.y();
|
2021-07-22 07:14:34 -07:00
|
|
|
|
instance[NF_OLD] = self.nf_old.0;
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
|
|
|
|
let rk = pallas::Point::from_bytes(&self.rk.clone().into())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_affine()
|
|
|
|
|
.coordinates()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
instance[RK_X] = *rk.x();
|
|
|
|
|
instance[RK_Y] = *rk.y();
|
2021-07-22 07:14:34 -07:00
|
|
|
|
instance[CMX] = self.cmx.inner();
|
2021-12-07 09:47:03 -08:00
|
|
|
|
instance[ENABLE_SPEND] = vesta::Scalar::from(u64::from(self.enable_spend));
|
|
|
|
|
instance[ENABLE_OUTPUT] = vesta::Scalar::from(u64::from(self.enable_output));
|
2021-07-19 09:42:22 -07:00
|
|
|
|
|
|
|
|
|
[instance]
|
2021-04-14 21:35:48 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A proof of the validity of an Orchard [`Bundle`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Bundle`]: crate::bundle::Bundle
|
2022-04-05 13:18:10 -07:00
|
|
|
|
#[derive(Clone)]
|
2021-05-05 10:46:24 -07:00
|
|
|
|
pub struct Proof(Vec<u8>);
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
2022-04-05 13:18:10 -07:00
|
|
|
|
impl fmt::Debug for Proof {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
if f.alternate() {
|
|
|
|
|
f.debug_tuple("Proof").field(&self.0).finish()
|
|
|
|
|
} else {
|
|
|
|
|
// By default, only show the proof length, not its contents.
|
|
|
|
|
f.debug_tuple("Proof")
|
|
|
|
|
.field(&format_args!("{} bytes", self.0.len()))
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 10:37:56 -07:00
|
|
|
|
impl AsRef<[u8]> for Proof {
|
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
|
&self.0
|
2021-04-21 13:32:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:22:22 -07:00
|
|
|
|
impl DynamicUsage for Proof {
|
|
|
|
|
fn dynamic_usage(&self) -> usize {
|
|
|
|
|
self.0.dynamic_usage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.0.dynamic_usage_bounds()
|
2021-06-22 08:06:49 -07:00
|
|
|
|
}
|
2021-09-03 13:22:22 -07:00
|
|
|
|
}
|
2021-06-22 08:06:49 -07:00
|
|
|
|
|
2021-09-03 13:22:22 -07:00
|
|
|
|
impl Proof {
|
2021-04-14 21:35:48 -07:00
|
|
|
|
/// Creates a proof for the given circuits and instances.
|
|
|
|
|
pub fn create(
|
|
|
|
|
pk: &ProvingKey,
|
|
|
|
|
circuits: &[Circuit],
|
|
|
|
|
instances: &[Instance],
|
2022-01-04 21:58:59 -08:00
|
|
|
|
mut rng: impl RngCore,
|
2021-04-14 21:35:48 -07:00
|
|
|
|
) -> Result<Self, plonk::Error> {
|
2021-07-15 04:52:15 -07:00
|
|
|
|
let instances: Vec<_> = instances.iter().map(|i| i.to_halo2_instance()).collect();
|
|
|
|
|
let instances: Vec<Vec<_>> = instances
|
2021-04-14 21:35:48 -07:00
|
|
|
|
.iter()
|
2021-07-15 04:52:15 -07:00
|
|
|
|
.map(|i| i.iter().map(|c| &c[..]).collect())
|
2021-04-14 21:35:48 -07:00
|
|
|
|
.collect();
|
|
|
|
|
let instances: Vec<_> = instances.iter().map(|i| &i[..]).collect();
|
|
|
|
|
|
2021-06-01 09:37:44 -07:00
|
|
|
|
let mut transcript = Blake2bWrite::<_, vesta::Affine, _>::init(vec![]);
|
2022-01-04 21:58:59 -08:00
|
|
|
|
plonk::create_proof(
|
|
|
|
|
&pk.params,
|
|
|
|
|
&pk.pk,
|
|
|
|
|
circuits,
|
|
|
|
|
&instances,
|
|
|
|
|
&mut rng,
|
|
|
|
|
&mut transcript,
|
|
|
|
|
)?;
|
2021-04-14 21:35:48 -07:00
|
|
|
|
Ok(Proof(transcript.finalize()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Verifies this proof with the given instances.
|
|
|
|
|
pub fn verify(&self, vk: &VerifyingKey, instances: &[Instance]) -> Result<(), plonk::Error> {
|
2021-07-15 04:52:15 -07:00
|
|
|
|
let instances: Vec<_> = instances.iter().map(|i| i.to_halo2_instance()).collect();
|
|
|
|
|
let instances: Vec<Vec<_>> = instances
|
2021-04-14 21:35:48 -07:00
|
|
|
|
.iter()
|
2021-07-15 04:52:15 -07:00
|
|
|
|
.map(|i| i.iter().map(|c| &c[..]).collect())
|
2021-04-14 21:35:48 -07:00
|
|
|
|
.collect();
|
|
|
|
|
let instances: Vec<_> = instances.iter().map(|i| &i[..]).collect();
|
|
|
|
|
|
2022-01-20 07:05:10 -08:00
|
|
|
|
let strategy = SingleVerifier::new(&vk.params);
|
2021-04-14 21:35:48 -07:00
|
|
|
|
let mut transcript = Blake2bRead::init(&self.0[..]);
|
2022-01-20 07:05:10 -08:00
|
|
|
|
plonk::verify_proof(&vk.params, &vk.vk, strategy, &instances, &mut transcript)
|
2021-04-14 21:35:48 -07:00
|
|
|
|
}
|
2021-04-21 08:57:48 -07:00
|
|
|
|
|
2022-07-04 13:05:47 -07:00
|
|
|
|
/// Adds this proof to the given batch for verification with the given instances.
|
|
|
|
|
///
|
|
|
|
|
/// Use this API if you want more control over how proof batches are processed. If you
|
|
|
|
|
/// just want to batch-validate Orchard bundles, use [`bundle::BatchValidator`].
|
|
|
|
|
///
|
|
|
|
|
/// [`bundle::BatchValidator`]: crate::bundle::BatchValidator
|
|
|
|
|
pub fn add_to_batch(&self, batch: &mut BatchVerifier<vesta::Affine>, instances: Vec<Instance>) {
|
2022-06-21 08:07:31 -07:00
|
|
|
|
let instances = instances
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|i| {
|
|
|
|
|
i.to_halo2_instance()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|c| c.into_iter().collect())
|
|
|
|
|
.collect()
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
batch.add_proof(instances, self.0.clone());
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 10:37:56 -07:00
|
|
|
|
/// Constructs a new Proof value.
|
2021-04-21 08:57:48 -07:00
|
|
|
|
pub fn new(bytes: Vec<u8>) -> Self {
|
|
|
|
|
Proof(bytes)
|
|
|
|
|
}
|
2021-04-14 21:35:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2022-04-28 13:20:23 -07:00
|
|
|
|
use core::iter;
|
|
|
|
|
|
2021-04-14 21:35:48 -07:00
|
|
|
|
use ff::Field;
|
2022-06-08 17:01:10 -07:00
|
|
|
|
use halo2_proofs::{circuit::Value, dev::MockProver};
|
2021-04-14 21:35:48 -07:00
|
|
|
|
use pasta_curves::pallas;
|
2022-03-16 12:47:00 -07:00
|
|
|
|
use rand::{rngs::OsRng, RngCore};
|
2021-04-14 21:35:48 -07:00
|
|
|
|
|
|
|
|
|
use super::{Circuit, Instance, Proof, ProvingKey, VerifyingKey, K};
|
|
|
|
|
use crate::{
|
|
|
|
|
keys::SpendValidatingKey,
|
|
|
|
|
note::Note,
|
2021-06-06 04:21:27 -07:00
|
|
|
|
tree::MerklePath,
|
2021-04-14 21:35:48 -07:00
|
|
|
|
value::{ValueCommitTrapdoor, ValueCommitment},
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-16 12:47:00 -07:00
|
|
|
|
fn generate_circuit_instance<R: RngCore>(mut rng: R) -> (Circuit, Instance) {
|
|
|
|
|
let (_, fvk, spent_note) = Note::dummy(&mut rng, None);
|
|
|
|
|
|
|
|
|
|
let sender_address = spent_note.recipient();
|
|
|
|
|
let nk = *fvk.nk();
|
2022-03-30 04:53:46 -07:00
|
|
|
|
let rivk = fvk.rivk(fvk.scope_for_address(&spent_note.recipient()).unwrap());
|
2022-03-16 12:47:00 -07:00
|
|
|
|
let nf_old = spent_note.nullifier(&fvk);
|
|
|
|
|
let ak: SpendValidatingKey = fvk.into();
|
|
|
|
|
let alpha = pallas::Scalar::random(&mut rng);
|
|
|
|
|
let rk = ak.randomize(&alpha);
|
|
|
|
|
|
|
|
|
|
let (_, _, output_note) = Note::dummy(&mut rng, Some(nf_old));
|
|
|
|
|
let cmx = output_note.commitment().into();
|
|
|
|
|
|
|
|
|
|
let value = spent_note.value() - output_note.value();
|
|
|
|
|
let rcv = ValueCommitTrapdoor::random(&mut rng);
|
2022-04-29 06:54:42 -07:00
|
|
|
|
let cv_net = ValueCommitment::derive(value, rcv.clone());
|
2022-03-16 12:47:00 -07:00
|
|
|
|
|
|
|
|
|
let path = MerklePath::dummy(&mut rng);
|
|
|
|
|
let anchor = path.root(spent_note.commitment().into());
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
Circuit {
|
2022-06-08 17:01:10 -07:00
|
|
|
|
path: Value::known(path.auth_path()),
|
|
|
|
|
pos: Value::known(path.position()),
|
|
|
|
|
g_d_old: Value::known(sender_address.g_d()),
|
|
|
|
|
pk_d_old: Value::known(*sender_address.pk_d()),
|
|
|
|
|
v_old: Value::known(spent_note.value()),
|
|
|
|
|
rho_old: Value::known(spent_note.rho()),
|
|
|
|
|
psi_old: Value::known(spent_note.rseed().psi(&spent_note.rho())),
|
|
|
|
|
rcm_old: Value::known(spent_note.rseed().rcm(&spent_note.rho())),
|
|
|
|
|
cm_old: Value::known(spent_note.commitment()),
|
|
|
|
|
alpha: Value::known(alpha),
|
|
|
|
|
ak: Value::known(ak),
|
|
|
|
|
nk: Value::known(nk),
|
|
|
|
|
rivk: Value::known(rivk),
|
|
|
|
|
g_d_new: Value::known(output_note.recipient().g_d()),
|
|
|
|
|
pk_d_new: Value::known(*output_note.recipient().pk_d()),
|
|
|
|
|
v_new: Value::known(output_note.value()),
|
|
|
|
|
psi_new: Value::known(output_note.rseed().psi(&output_note.rho())),
|
|
|
|
|
rcm_new: Value::known(output_note.rseed().rcm(&output_note.rho())),
|
|
|
|
|
rcv: Value::known(rcv),
|
2022-03-16 12:47:00 -07:00
|
|
|
|
},
|
|
|
|
|
Instance {
|
|
|
|
|
anchor,
|
|
|
|
|
cv_net,
|
|
|
|
|
nf_old,
|
|
|
|
|
rk,
|
|
|
|
|
cmx,
|
|
|
|
|
enable_spend: true,
|
|
|
|
|
enable_output: true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 08:57:48 -07:00
|
|
|
|
// TODO: recast as a proptest
|
2021-04-14 21:35:48 -07:00
|
|
|
|
#[test]
|
|
|
|
|
fn round_trip() {
|
|
|
|
|
let mut rng = OsRng;
|
|
|
|
|
|
|
|
|
|
let (circuits, instances): (Vec<_>, Vec<_>) = iter::once(())
|
2022-03-16 12:47:00 -07:00
|
|
|
|
.map(|()| generate_circuit_instance(&mut rng))
|
2021-04-14 21:35:48 -07:00
|
|
|
|
.unzip();
|
|
|
|
|
|
|
|
|
|
let vk = VerifyingKey::build();
|
2021-09-28 11:06:38 -07:00
|
|
|
|
|
|
|
|
|
// Test that the pinned verification key (representing the circuit)
|
|
|
|
|
// is as expected.
|
|
|
|
|
{
|
|
|
|
|
// panic!("{:#?}", vk.vk.pinned());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
format!("{:#?}\n", vk.vk.pinned()),
|
|
|
|
|
include_str!("circuit_description").replace("\r\n", "\n")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 10:01:30 -08:00
|
|
|
|
// Test that the proof size is as expected.
|
2021-12-06 11:44:44 -08:00
|
|
|
|
let expected_proof_size = {
|
2022-01-28 07:53:03 -08:00
|
|
|
|
let circuit_cost =
|
|
|
|
|
halo2_proofs::dev::CircuitCost::<pasta_curves::vesta::Point, _>::measure(
|
2023-02-28 14:21:37 -08:00
|
|
|
|
K,
|
2022-01-28 07:53:03 -08:00
|
|
|
|
&circuits[0],
|
|
|
|
|
);
|
2021-12-06 10:01:30 -08:00
|
|
|
|
assert_eq!(usize::from(circuit_cost.proof_size(1)), 4992);
|
|
|
|
|
assert_eq!(usize::from(circuit_cost.proof_size(2)), 7264);
|
2021-12-06 11:44:44 -08:00
|
|
|
|
usize::from(circuit_cost.proof_size(instances.len()))
|
|
|
|
|
};
|
2021-12-06 10:01:30 -08:00
|
|
|
|
|
2021-04-14 21:35:48 -07:00
|
|
|
|
for (circuit, instance) in circuits.iter().zip(instances.iter()) {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
MockProver::run(
|
|
|
|
|
K,
|
|
|
|
|
circuit,
|
|
|
|
|
instance
|
2021-07-15 04:52:15 -07:00
|
|
|
|
.to_halo2_instance()
|
2021-04-14 21:35:48 -07:00
|
|
|
|
.iter()
|
2021-07-15 04:52:15 -07:00
|
|
|
|
.map(|p| p.to_vec())
|
2021-04-14 21:35:48 -07:00
|
|
|
|
.collect()
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.verify(),
|
|
|
|
|
Ok(())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let pk = ProvingKey::build();
|
2022-01-04 21:58:59 -08:00
|
|
|
|
let proof = Proof::create(&pk, &circuits, &instances, &mut rng).unwrap();
|
2021-04-14 21:35:48 -07:00
|
|
|
|
assert!(proof.verify(&vk, &instances).is_ok());
|
2021-12-06 11:44:44 -08:00
|
|
|
|
assert_eq!(proof.0.len(), expected_proof_size);
|
2021-04-14 21:35:48 -07:00
|
|
|
|
}
|
2021-07-20 01:22:08 -07:00
|
|
|
|
|
2022-03-16 12:47:00 -07:00
|
|
|
|
#[test]
|
|
|
|
|
fn serialized_proof_test_case() {
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
|
|
|
|
|
let vk = VerifyingKey::build();
|
|
|
|
|
|
|
|
|
|
fn write_test_case<W: Write>(
|
|
|
|
|
mut w: W,
|
|
|
|
|
instance: &Instance,
|
|
|
|
|
proof: &Proof,
|
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
|
w.write_all(&instance.anchor.to_bytes())?;
|
|
|
|
|
w.write_all(&instance.cv_net.to_bytes())?;
|
|
|
|
|
w.write_all(&instance.nf_old.to_bytes())?;
|
|
|
|
|
w.write_all(&<[u8; 32]>::from(instance.rk.clone()))?;
|
|
|
|
|
w.write_all(&instance.cmx.to_bytes())?;
|
|
|
|
|
w.write_all(&[
|
2023-09-08 10:40:03 -07:00
|
|
|
|
u8::from(instance.enable_spend),
|
|
|
|
|
u8::from(instance.enable_output),
|
2022-03-16 12:47:00 -07:00
|
|
|
|
])?;
|
|
|
|
|
|
|
|
|
|
w.write_all(proof.as_ref())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_test_case<R: Read>(mut r: R) -> std::io::Result<(Instance, Proof)> {
|
|
|
|
|
let read_32_bytes = |r: &mut R| {
|
|
|
|
|
let mut ret = [0u8; 32];
|
|
|
|
|
r.read_exact(&mut ret).unwrap();
|
|
|
|
|
ret
|
|
|
|
|
};
|
|
|
|
|
let read_bool = |r: &mut R| {
|
|
|
|
|
let mut byte = [0u8; 1];
|
|
|
|
|
r.read_exact(&mut byte).unwrap();
|
|
|
|
|
match byte {
|
|
|
|
|
[0] => false,
|
|
|
|
|
[1] => true,
|
|
|
|
|
_ => panic!("Unexpected non-boolean byte"),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let anchor = crate::Anchor::from_bytes(read_32_bytes(&mut r)).unwrap();
|
|
|
|
|
let cv_net = ValueCommitment::from_bytes(&read_32_bytes(&mut r)).unwrap();
|
|
|
|
|
let nf_old = crate::note::Nullifier::from_bytes(&read_32_bytes(&mut r)).unwrap();
|
|
|
|
|
let rk = read_32_bytes(&mut r).try_into().unwrap();
|
|
|
|
|
let cmx =
|
|
|
|
|
crate::note::ExtractedNoteCommitment::from_bytes(&read_32_bytes(&mut r)).unwrap();
|
|
|
|
|
let enable_spend = read_bool(&mut r);
|
|
|
|
|
let enable_output = read_bool(&mut r);
|
|
|
|
|
let instance =
|
|
|
|
|
Instance::from_parts(anchor, cv_net, nf_old, rk, cmx, enable_spend, enable_output);
|
|
|
|
|
|
|
|
|
|
let mut proof_bytes = vec![];
|
|
|
|
|
r.read_to_end(&mut proof_bytes)?;
|
|
|
|
|
let proof = Proof::new(proof_bytes);
|
|
|
|
|
|
|
|
|
|
Ok((instance, proof))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if std::env::var_os("ORCHARD_CIRCUIT_TEST_GENERATE_NEW_PROOF").is_some() {
|
|
|
|
|
let create_proof = || -> std::io::Result<()> {
|
2022-03-22 10:59:56 -07:00
|
|
|
|
let mut rng = OsRng;
|
|
|
|
|
|
2022-03-16 12:47:00 -07:00
|
|
|
|
let (circuit, instance) = generate_circuit_instance(OsRng);
|
|
|
|
|
let instances = &[instance.clone()];
|
|
|
|
|
|
|
|
|
|
let pk = ProvingKey::build();
|
2022-03-22 10:59:56 -07:00
|
|
|
|
let proof = Proof::create(&pk, &[circuit], instances, &mut rng).unwrap();
|
2022-03-16 12:47:00 -07:00
|
|
|
|
assert!(proof.verify(&vk, instances).is_ok());
|
|
|
|
|
|
|
|
|
|
let file = std::fs::File::create("circuit_proof_test_case.bin")?;
|
|
|
|
|
write_test_case(file, &instance, &proof)
|
|
|
|
|
};
|
|
|
|
|
create_proof().expect("should be able to write new proof");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse the hardcoded proof test case.
|
|
|
|
|
let (instance, proof) = {
|
|
|
|
|
let test_case_bytes = include_bytes!("circuit_proof_test_case.bin");
|
|
|
|
|
read_test_case(&test_case_bytes[..]).expect("proof must be valid")
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(proof.0.len(), 4992);
|
|
|
|
|
|
|
|
|
|
assert!(proof.verify(&vk, &[instance]).is_ok());
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 01:22:08 -07:00
|
|
|
|
#[cfg(feature = "dev-graph")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn print_action_circuit() {
|
|
|
|
|
use plotters::prelude::*;
|
|
|
|
|
|
|
|
|
|
let root = BitMapBackend::new("action-circuit-layout.png", (1024, 768)).into_drawing_area();
|
|
|
|
|
root.fill(&WHITE).unwrap();
|
|
|
|
|
let root = root
|
|
|
|
|
.titled("Orchard Action Circuit", ("sans-serif", 60))
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let circuit = Circuit {
|
2022-06-08 17:01:10 -07:00
|
|
|
|
path: Value::unknown(),
|
|
|
|
|
pos: Value::unknown(),
|
|
|
|
|
g_d_old: Value::unknown(),
|
|
|
|
|
pk_d_old: Value::unknown(),
|
|
|
|
|
v_old: Value::unknown(),
|
|
|
|
|
rho_old: Value::unknown(),
|
|
|
|
|
psi_old: Value::unknown(),
|
|
|
|
|
rcm_old: Value::unknown(),
|
|
|
|
|
cm_old: Value::unknown(),
|
|
|
|
|
alpha: Value::unknown(),
|
|
|
|
|
ak: Value::unknown(),
|
|
|
|
|
nk: Value::unknown(),
|
|
|
|
|
rivk: Value::unknown(),
|
|
|
|
|
g_d_new: Value::unknown(),
|
|
|
|
|
pk_d_new: Value::unknown(),
|
|
|
|
|
v_new: Value::unknown(),
|
|
|
|
|
psi_new: Value::unknown(),
|
|
|
|
|
rcm_new: Value::unknown(),
|
|
|
|
|
rcv: Value::unknown(),
|
2021-07-20 01:22:08 -07:00
|
|
|
|
};
|
2022-01-28 07:53:03 -08:00
|
|
|
|
halo2_proofs::dev::CircuitLayout::default()
|
2021-07-20 01:22:08 -07:00
|
|
|
|
.show_labels(false)
|
|
|
|
|
.view_height(0..(1 << 11))
|
2021-11-23 16:21:44 -08:00
|
|
|
|
.render(K, &circuit, &root)
|
2021-07-20 01:22:08 -07:00
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
2021-04-14 21:35:48 -07:00
|
|
|
|
}
|