Rename CommitmentTreeWitness -> MerklePath

This commit is contained in:
Jack Grigg 2020-02-08 00:25:24 +00:00
parent 3a3008caf9
commit 76e0f658c1
6 changed files with 41 additions and 46 deletions

View File

@ -50,7 +50,7 @@ use zcash_primitives::{
fs::{Fs, FsRepr}, fs::{Fs, FsRepr},
FixedGenerators, JubjubEngine, JubjubParams, PrimeOrder, ToUniform, Unknown, FixedGenerators, JubjubEngine, JubjubParams, PrimeOrder, ToUniform, Unknown,
}, },
merkle_tree::CommitmentTreeWitness, merkle_tree::MerklePath,
note_encryption::sapling_ka_agree, note_encryption::sapling_ka_agree,
primitives::{Diversifier, Note, PaymentAddress, ProofGenerationKey, ViewingKey}, primitives::{Diversifier, Note, PaymentAddress, ProofGenerationKey, ViewingKey},
redjubjub::{self, Signature}, redjubjub::{self, Signature},
@ -980,7 +980,7 @@ pub extern "C" fn librustzcash_sapling_spend_proof(
ar: *const [c_uchar; 32], ar: *const [c_uchar; 32],
value: u64, value: u64,
anchor: *const [c_uchar; 32], anchor: *const [c_uchar; 32],
witness: *const [c_uchar; 1 + 33 * SAPLING_TREE_DEPTH + 8], merkle_path: *const [c_uchar; 1 + 33 * SAPLING_TREE_DEPTH + 8],
cv: *mut [c_uchar; 32], cv: *mut [c_uchar; 32],
rk_out: *mut [c_uchar; 32], rk_out: *mut [c_uchar; 32],
zkproof: *mut [c_uchar; GROTH_PROOF_SIZE], zkproof: *mut [c_uchar; GROTH_PROOF_SIZE],
@ -1030,9 +1030,8 @@ pub extern "C" fn librustzcash_sapling_spend_proof(
Err(_) => return false, Err(_) => return false,
}; };
// The witness contains the incremental tree witness information, in a // Parse the Merkle path from the caller
// weird serialized format. let merkle_path = match MerklePath::from_slice(unsafe { &(&*merkle_path)[..] }) {
let witness = match CommitmentTreeWitness::from_slice(unsafe { &(&*witness)[..] }) {
Ok(w) => w, Ok(w) => w,
Err(_) => return false, Err(_) => return false,
}; };
@ -1046,7 +1045,7 @@ pub extern "C" fn librustzcash_sapling_spend_proof(
ar, ar,
value, value,
anchor, anchor,
witness, merkle_path,
unsafe { SAPLING_SPEND_PARAMS.as_ref() }.unwrap(), unsafe { SAPLING_SPEND_PARAMS.as_ref() }.unwrap(),
unsafe { SAPLING_SPEND_VK.as_ref() }.unwrap(), unsafe { SAPLING_SPEND_VK.as_ref() }.unwrap(),
&JUBJUB, &JUBJUB,

View File

@ -375,11 +375,11 @@ impl<Node: Hashable> IncrementalWitness<Node> {
} }
/// Returns the current witness, or None if the tree is empty. /// Returns the current witness, or None if the tree is empty.
pub fn path(&self) -> Option<CommitmentTreeWitness<Node>> { pub fn path(&self) -> Option<MerklePath<Node>> {
self.path_inner(SAPLING_COMMITMENT_TREE_DEPTH) self.path_inner(SAPLING_COMMITMENT_TREE_DEPTH)
} }
fn path_inner(&self, depth: usize) -> Option<CommitmentTreeWitness<Node>> { fn path_inner(&self, depth: usize) -> Option<MerklePath<Node>> {
let mut filler = self.filler(); let mut filler = self.filler();
let mut auth_path = Vec::new(); let mut auth_path = Vec::new();
@ -406,31 +406,27 @@ impl<Node: Hashable> IncrementalWitness<Node> {
} }
assert_eq!(auth_path.len(), depth); assert_eq!(auth_path.len(), depth);
Some(CommitmentTreeWitness::from_path( Some(MerklePath::from_path(auth_path, self.position() as u64))
auth_path,
self.position() as u64,
))
} }
} }
/// A witness to a path from a position in a particular commitment tree to the root of /// A path from a position in a particular commitment tree to the root of that tree.
/// that tree.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct CommitmentTreeWitness<Node: Hashable> { pub struct MerklePath<Node: Hashable> {
pub auth_path: Vec<(Node, bool)>, pub auth_path: Vec<(Node, bool)>,
pub position: u64, pub position: u64,
} }
impl<Node: Hashable> CommitmentTreeWitness<Node> { impl<Node: Hashable> MerklePath<Node> {
/// Constructs a witness directly from its path and position. /// Constructs a Merkle path directly from a path and position.
pub fn from_path(auth_path: Vec<(Node, bool)>, position: u64) -> Self { pub fn from_path(auth_path: Vec<(Node, bool)>, position: u64) -> Self {
CommitmentTreeWitness { MerklePath {
auth_path, auth_path,
position, position,
} }
} }
/// Reads a witness from its serialized form. /// Reads a Merkle path from its serialized form.
pub fn from_slice(witness: &[u8]) -> Result<Self, ()> { pub fn from_slice(witness: &[u8]) -> Result<Self, ()> {
Self::from_slice_with_depth(witness, SAPLING_COMMITMENT_TREE_DEPTH) Self::from_slice_with_depth(witness, SAPLING_COMMITMENT_TREE_DEPTH)
} }
@ -486,7 +482,7 @@ impl<Node: Hashable> CommitmentTreeWitness<Node> {
// have provided more information than they should have, indicating // have provided more information than they should have, indicating
// a bug downstream // a bug downstream
if witness.is_empty() { if witness.is_empty() {
Ok(CommitmentTreeWitness { Ok(MerklePath {
auth_path, auth_path,
position, position,
}) })
@ -495,7 +491,7 @@ impl<Node: Hashable> CommitmentTreeWitness<Node> {
} }
} }
/// Returns the root of the tree corresponding to the witness. /// Returns the root of the tree corresponding to this path applied to `leaf`.
pub fn root(&self, leaf: Node) -> Node { pub fn root(&self, leaf: Node) -> Node {
self.auth_path self.auth_path
.iter() .iter()
@ -512,7 +508,7 @@ impl<Node: Hashable> CommitmentTreeWitness<Node> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{CommitmentTree, CommitmentTreeWitness, Hashable, IncrementalWitness, PathFiller}; use super::{CommitmentTree, Hashable, IncrementalWitness, MerklePath, PathFiller};
use crate::sapling::Node; use crate::sapling::Node;
use ff::PrimeFieldRepr; use ff::PrimeFieldRepr;
@ -611,7 +607,7 @@ mod tests {
self.0.root_inner(TESTING_DEPTH) self.0.root_inner(TESTING_DEPTH)
} }
fn path(&self) -> Option<CommitmentTreeWitness<Node>> { fn path(&self) -> Option<MerklePath<Node>> {
self.0.path_inner(TESTING_DEPTH) self.0.path_inner(TESTING_DEPTH)
} }
} }
@ -1047,7 +1043,7 @@ mod tests {
if let Some(leaf) = leaf { if let Some(leaf) = leaf {
let path = witness.path().expect("should be able to create a path"); let path = witness.path().expect("should be able to create a path");
let expected = CommitmentTreeWitness::from_slice_with_depth( let expected = MerklePath::from_slice_with_depth(
&mut hex::decode(paths[paths_i]).unwrap(), &mut hex::decode(paths[paths_i]).unwrap(),
TESTING_DEPTH, TESTING_DEPTH,
) )

View File

@ -7,7 +7,7 @@ use crate::{
use pairing::bls12_381::{Bls12, Fr}; use pairing::bls12_381::{Bls12, Fr};
use crate::{ use crate::{
merkle_tree::CommitmentTreeWitness, merkle_tree::MerklePath,
redjubjub::{PublicKey, Signature}, redjubjub::{PublicKey, Signature},
sapling::Node, sapling::Node,
transaction::components::{Amount, GROTH_PROOF_SIZE}, transaction::components::{Amount, GROTH_PROOF_SIZE},
@ -35,7 +35,7 @@ pub trait TxProver {
ar: Fs, ar: Fs,
value: u64, value: u64,
anchor: Fr, anchor: Fr,
witness: CommitmentTreeWitness<Node>, merkle_path: MerklePath<Node>,
) -> Result< ) -> Result<
( (
[u8; GROTH_PROOF_SIZE], [u8; GROTH_PROOF_SIZE],
@ -82,7 +82,7 @@ pub(crate) mod mock {
}; };
use crate::{ use crate::{
merkle_tree::CommitmentTreeWitness, merkle_tree::MerklePath,
redjubjub::{PublicKey, Signature}, redjubjub::{PublicKey, Signature},
sapling::Node, sapling::Node,
transaction::components::{Amount, GROTH_PROOF_SIZE}, transaction::components::{Amount, GROTH_PROOF_SIZE},
@ -108,7 +108,7 @@ pub(crate) mod mock {
ar: Fs, ar: Fs,
value: u64, value: u64,
_anchor: Fr, _anchor: Fr,
_witness: CommitmentTreeWitness<Node>, _merkle_path: MerklePath<Node>,
) -> Result< ) -> Result<
( (
[u8; GROTH_PROOF_SIZE], [u8; GROTH_PROOF_SIZE],

View File

@ -13,7 +13,7 @@ use crate::{
consensus, consensus,
keys::OutgoingViewingKey, keys::OutgoingViewingKey,
legacy::TransparentAddress, legacy::TransparentAddress,
merkle_tree::CommitmentTreeWitness, merkle_tree::MerklePath,
note_encryption::{generate_esk, Memo, SaplingNoteEncryption}, note_encryption::{generate_esk, Memo, SaplingNoteEncryption},
prover::TxProver, prover::TxProver,
redjubjub::PrivateKey, redjubjub::PrivateKey,
@ -53,7 +53,7 @@ struct SpendDescriptionInfo {
diversifier: Diversifier, diversifier: Diversifier,
note: Note<Bls12>, note: Note<Bls12>,
alpha: Fs, alpha: Fs,
witness: CommitmentTreeWitness<Node>, merkle_path: MerklePath<Node>,
} }
pub struct SaplingOutput { pub struct SaplingOutput {
@ -334,24 +334,24 @@ impl<R: RngCore + CryptoRng> Builder<R> {
/// Adds a Sapling note to be spent in this transaction. /// Adds a Sapling note to be spent in this transaction.
/// ///
/// Returns an error if the given witness does not have the same anchor as previous /// Returns an error if the given Merkle path does not have the same anchor as the
/// witnesses, or has no path. /// paths for previous Sapling notes.
pub fn add_sapling_spend( pub fn add_sapling_spend(
&mut self, &mut self,
extsk: ExtendedSpendingKey, extsk: ExtendedSpendingKey,
diversifier: Diversifier, diversifier: Diversifier,
note: Note<Bls12>, note: Note<Bls12>,
witness: CommitmentTreeWitness<Node>, merkle_path: MerklePath<Node>,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Consistency check: all anchors must equal the first one // Consistency check: all anchors must equal the first one
let cm = Node::new(note.cm(&JUBJUB).into()); let cm = Node::new(note.cm(&JUBJUB).into());
if let Some(anchor) = self.anchor { if let Some(anchor) = self.anchor {
let witness_root: Fr = witness.root(cm).into(); let path_root: Fr = merkle_path.root(cm).into();
if witness_root != anchor { if path_root != anchor {
return Err(Error::AnchorMismatch); return Err(Error::AnchorMismatch);
} }
} else { } else {
self.anchor = Some(witness.root(cm).into()) self.anchor = Some(merkle_path.root(cm).into())
} }
let alpha = Fs::random(&mut self.rng); let alpha = Fs::random(&mut self.rng);
@ -363,7 +363,7 @@ impl<R: RngCore + CryptoRng> Builder<R> {
diversifier, diversifier,
note, note,
alpha, alpha,
witness, merkle_path,
}); });
Ok(()) Ok(())
@ -521,7 +521,7 @@ impl<R: RngCore + CryptoRng> Builder<R> {
let mut nullifier = [0u8; 32]; let mut nullifier = [0u8; 32];
nullifier.copy_from_slice(&spend.note.nf( nullifier.copy_from_slice(&spend.note.nf(
&proof_generation_key.to_viewing_key(&JUBJUB), &proof_generation_key.to_viewing_key(&JUBJUB),
spend.witness.position, spend.merkle_path.position,
&JUBJUB, &JUBJUB,
)); ));
@ -534,7 +534,7 @@ impl<R: RngCore + CryptoRng> Builder<R> {
spend.alpha, spend.alpha,
spend.note.value, spend.note.value,
anchor, anchor,
spend.witness.clone(), spend.merkle_path.clone(),
) )
.map_err(|()| Error::SpendProof)?; .map_err(|()| Error::SpendProof)?;

View File

@ -9,7 +9,7 @@ use zcash_primitives::{
primitives::{Diversifier, PaymentAddress, ProofGenerationKey}, primitives::{Diversifier, PaymentAddress, ProofGenerationKey},
}; };
use zcash_primitives::{ use zcash_primitives::{
merkle_tree::CommitmentTreeWitness, merkle_tree::MerklePath,
prover::TxProver, prover::TxProver,
redjubjub::{PublicKey, Signature}, redjubjub::{PublicKey, Signature},
sapling::Node, sapling::Node,
@ -127,7 +127,7 @@ impl TxProver for LocalTxProver {
ar: Fs, ar: Fs,
value: u64, value: u64,
anchor: Fr, anchor: Fr,
witness: CommitmentTreeWitness<Node>, merkle_path: MerklePath<Node>,
) -> Result< ) -> Result<
( (
[u8; GROTH_PROOF_SIZE], [u8; GROTH_PROOF_SIZE],
@ -143,7 +143,7 @@ impl TxProver for LocalTxProver {
ar, ar,
value, value,
anchor, anchor,
witness, merkle_path,
&self.spend_params, &self.spend_params,
&self.spend_vk, &self.spend_vk,
&JUBJUB, &JUBJUB,

View File

@ -10,7 +10,7 @@ use zcash_primitives::{
primitives::{Diversifier, Note, PaymentAddress, ProofGenerationKey, ValueCommitment}, primitives::{Diversifier, Note, PaymentAddress, ProofGenerationKey, ValueCommitment},
}; };
use zcash_primitives::{ use zcash_primitives::{
merkle_tree::CommitmentTreeWitness, merkle_tree::MerklePath,
redjubjub::{PrivateKey, PublicKey, Signature}, redjubjub::{PrivateKey, PublicKey, Signature},
sapling::Node, sapling::Node,
transaction::components::Amount, transaction::components::Amount,
@ -46,7 +46,7 @@ impl SaplingProvingContext {
ar: Fs, ar: Fs,
value: u64, value: u64,
anchor: Fr, anchor: Fr,
witness: CommitmentTreeWitness<Node>, merkle_path: MerklePath<Node>,
proving_key: &Parameters<Bls12>, proving_key: &Parameters<Bls12>,
verifying_key: &PreparedVerifyingKey<Bls12>, verifying_key: &PreparedVerifyingKey<Bls12>,
params: &JubjubBls12, params: &JubjubBls12,
@ -104,7 +104,7 @@ impl SaplingProvingContext {
r: rcm, r: rcm,
}; };
let nullifier = note.nf(&viewing_key, witness.position, params); let nullifier = note.nf(&viewing_key, merkle_path.position, params);
// We now have the full witness for our circuit // We now have the full witness for our circuit
let instance = Spend { let instance = Spend {
@ -114,7 +114,7 @@ impl SaplingProvingContext {
payment_address: Some(payment_address), payment_address: Some(payment_address),
commitment_randomness: Some(rcm), commitment_randomness: Some(rcm),
ar: Some(ar), ar: Some(ar),
auth_path: witness auth_path: merkle_path
.auth_path .auth_path
.iter() .iter()
.map(|(node, b)| Some(((*node).into(), *b))) .map(|(node, b)| Some(((*node).into(), *b)))