zcash_primitives: Avoid passing duplicate diversifier information to Sapling builder.

The note provided to `add_sapling_spend` contains the recipient address,
and we can extract the diversifier from this address, so we should not
pass it separately.
This commit is contained in:
Kris Nuttycombe 2023-11-28 10:31:46 -07:00
parent ed761d5da1
commit 7f3d057a1b
5 changed files with 18 additions and 45 deletions

View File

@ -605,7 +605,7 @@ where
}) })
})?; })?;
builder.add_sapling_spend(key, selected.diversifier(), note, merkle_path)?; builder.add_sapling_spend(key, note, merkle_path)?;
} }
Ok(()) Ok(())
})?; })?;

View File

@ -823,7 +823,7 @@ mod tests {
let mut builder_a = demo_builder(tx_height); let mut builder_a = demo_builder(tx_height);
builder_a builder_a
.add_sapling_spend(extsk, *to.diversifier(), note1, witness1.path().unwrap()) .add_sapling_spend(extsk, note1, witness1.path().unwrap())
.unwrap(); .unwrap();
let value = NonNegativeAmount::const_from_u64(100000); let value = NonNegativeAmount::const_from_u64(100000);

View File

@ -111,7 +111,9 @@ and this library adheres to Rust's notion of
parameter. parameter.
- `builder::SaplingBuilder::new` now takes a `Zip212Enforcement` argument - `builder::SaplingBuilder::new` now takes a `Zip212Enforcement` argument
instead of a `P: consensus::Parameters` argument and a target height. instead of a `P: consensus::Parameters` argument and a target height.
- `builder::SaplingBuilder::add_spend` now takes `extsk` by reference. - `builder::SaplingBuilder::add_spend` now takes `extsk` by reference. Also,
it no longer takes a `diversifier` argument as the diversifier may be obtained
from the note.
- `builder::SaplingBuilder::add_output` now takes an `Option<[u8; 512]>` memo - `builder::SaplingBuilder::add_output` now takes an `Option<[u8; 512]>` memo
instead of a `MemoBytes`. instead of a `MemoBytes`.
- `builder::SaplingBuilder::build` no longer takes a prover, proving context, - `builder::SaplingBuilder::build` no longer takes a prover, proving context,
@ -151,6 +153,8 @@ and this library adheres to Rust's notion of
- `zcash_primitives::transaction`: - `zcash_primitives::transaction`:
- `builder::Builder::{build, build_zfuture}` now take - `builder::Builder::{build, build_zfuture}` now take
`&impl SpendProver, &impl OutputProver` instead of `&impl TxProver`. `&impl SpendProver, &impl OutputProver` instead of `&impl TxProver`.
- `builder::Builder::add_sapling_spend` no longer takes a `diversifier`
argument as the diversifier may be obtained from the note.
- `components::transparent::TxOut.value` now has type `NonNegativeAmount` - `components::transparent::TxOut.value` now has type `NonNegativeAmount`
instead of `Amount`. instead of `Amount`.
- `Unauthorized::SaplingAuth` now has type `InProgress<Proven, Unsigned>`. - `Unauthorized::SaplingAuth` now has type `InProgress<Proven, Unsigned>`.

View File

@ -74,7 +74,6 @@ impl fmt::Display for Error {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SpendDescriptionInfo { pub struct SpendDescriptionInfo {
proof_generation_key: ProofGenerationKey, proof_generation_key: ProofGenerationKey,
diversifier: Diversifier,
note: Note, note: Note,
alpha: jubjub::Fr, alpha: jubjub::Fr,
merkle_path: MerklePath, merkle_path: MerklePath,
@ -97,13 +96,11 @@ impl SpendDescriptionInfo {
fn new_internal<R: RngCore>( fn new_internal<R: RngCore>(
mut rng: &mut R, mut rng: &mut R,
extsk: &ExtendedSpendingKey, extsk: &ExtendedSpendingKey,
diversifier: Diversifier,
note: Note, note: Note,
merkle_path: MerklePath, merkle_path: MerklePath,
) -> Self { ) -> Self {
SpendDescriptionInfo { SpendDescriptionInfo {
proof_generation_key: extsk.expsk.proof_generation_key(), proof_generation_key: extsk.expsk.proof_generation_key(),
diversifier,
note, note,
alpha: jubjub::Fr::random(&mut rng), alpha: jubjub::Fr::random(&mut rng),
merkle_path, merkle_path,
@ -133,7 +130,7 @@ impl SpendDescriptionInfo {
let zkproof = Pr::prepare_circuit( let zkproof = Pr::prepare_circuit(
self.proof_generation_key, self.proof_generation_key,
self.diversifier, *self.note.recipient().diversifier(),
*self.note.rseed(), *self.note.rseed(),
self.note.value(), self.note.value(),
self.alpha, self.alpha,
@ -370,7 +367,6 @@ impl SaplingBuilder {
&mut self, &mut self,
mut rng: R, mut rng: R,
extsk: &ExtendedSpendingKey, extsk: &ExtendedSpendingKey,
diversifier: Diversifier,
note: Note, note: Note,
merkle_path: MerklePath, merkle_path: MerklePath,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -388,8 +384,7 @@ impl SaplingBuilder {
self.value_balance = (self.value_balance + note.value()).ok_or(Error::InvalidAmount)?; self.value_balance = (self.value_balance + note.value()).ok_or(Error::InvalidAmount)?;
self.try_value_balance::<i64>()?; self.try_value_balance::<i64>()?;
let spend = let spend = SpendDescriptionInfo::new_internal(&mut rng, extsk, note, merkle_path);
SpendDescriptionInfo::new_internal(&mut rng, extsk, diversifier, note, merkle_path);
self.spends.push(spend); self.spends.push(spend);
@ -881,7 +876,6 @@ pub mod testing {
testing::{arb_node, arb_note}, testing::{arb_node, arb_note},
value::testing::arb_positive_note_value, value::testing::arb_positive_note_value,
zip32::testing::arb_extended_spending_key, zip32::testing::arb_extended_spending_key,
Diversifier,
}; };
use incrementalmerkletree::{ use incrementalmerkletree::{
frontier::testing::arb_commitment_tree, witness::IncrementalWitness, frontier::testing::arb_commitment_tree, witness::IncrementalWitness,
@ -907,34 +901,20 @@ pub mod testing {
.prop_map(|t| IncrementalWitness::from_tree(t).path().unwrap()), .prop_map(|t| IncrementalWitness::from_tree(t).path().unwrap()),
n_notes, n_notes,
), ),
vec(
prop::array::uniform11(any::<u8>()).prop_map(Diversifier),
n_notes,
),
prop::array::uniform32(any::<u8>()), prop::array::uniform32(any::<u8>()),
prop::array::uniform32(any::<u8>()), prop::array::uniform32(any::<u8>()),
) )
}) })
.prop_map( .prop_map(
move |( move |(extsk, spendable_notes, commitment_trees, rng_seed, fake_sighash_bytes)| {
extsk,
spendable_notes,
commitment_trees,
diversifiers,
rng_seed,
fake_sighash_bytes,
)| {
let mut builder = SaplingBuilder::new(zip212_enforcement); let mut builder = SaplingBuilder::new(zip212_enforcement);
let mut rng = StdRng::from_seed(rng_seed); let mut rng = StdRng::from_seed(rng_seed);
for ((note, path), diversifier) in spendable_notes for (note, path) in spendable_notes
.into_iter() .into_iter()
.zip(commitment_trees.into_iter()) .zip(commitment_trees.into_iter())
.zip(diversifiers.into_iter())
{ {
builder builder.add_spend(&mut rng, &extsk, note, path).unwrap();
.add_spend(&mut rng, &extsk, diversifier, note, path)
.unwrap();
} }
let (bundle, _) = builder let (bundle, _) = builder

View File

@ -15,7 +15,7 @@ use crate::{
self, self,
builder::{self as sapling_builder, SaplingBuilder, SaplingMetadata}, builder::{self as sapling_builder, SaplingBuilder, SaplingMetadata},
prover::{OutputProver, SpendProver}, prover::{OutputProver, SpendProver},
redjubjub, Diversifier, Note, PaymentAddress, redjubjub, Note, PaymentAddress,
}, },
transaction::{ transaction::{
components::{ components::{
@ -328,12 +328,11 @@ impl<'a, P: consensus::Parameters, R: RngCore + CryptoRng> Builder<'a, P, R> {
pub fn add_sapling_spend( pub fn add_sapling_spend(
&mut self, &mut self,
extsk: sapling::zip32::ExtendedSpendingKey, extsk: sapling::zip32::ExtendedSpendingKey,
diversifier: Diversifier,
note: Note, note: Note,
merkle_path: sapling::MerklePath, merkle_path: sapling::MerklePath,
) -> Result<(), sapling_builder::Error> { ) -> Result<(), sapling_builder::Error> {
self.sapling_builder self.sapling_builder
.add_spend(&mut self.rng, &extsk, diversifier, note, merkle_path)?; .add_spend(&mut self.rng, &extsk, note, merkle_path)?;
self.sapling_asks self.sapling_asks
.push(redjubjub::PrivateKey(extsk.expsk.ask)); .push(redjubjub::PrivateKey(extsk.expsk.ask));
@ -842,7 +841,7 @@ mod tests {
// Create a tx with a sapling spend. binding_sig should be present // Create a tx with a sapling spend. binding_sig should be present
builder builder
.add_sapling_spend(extsk, *to.diversifier(), note1, witness1.path().unwrap()) .add_sapling_spend(extsk, note1, witness1.path().unwrap())
.unwrap(); .unwrap();
builder builder
@ -933,12 +932,7 @@ mod tests {
{ {
let mut builder = Builder::new(TEST_NETWORK, tx_height, None); let mut builder = Builder::new(TEST_NETWORK, tx_height, None);
builder builder
.add_sapling_spend( .add_sapling_spend(extsk.clone(), note1.clone(), witness1.path().unwrap())
extsk.clone(),
*to.diversifier(),
note1.clone(),
witness1.path().unwrap(),
)
.unwrap(); .unwrap();
builder builder
.add_sapling_output( .add_sapling_output(
@ -974,15 +968,10 @@ mod tests {
{ {
let mut builder = Builder::new(TEST_NETWORK, tx_height, None); let mut builder = Builder::new(TEST_NETWORK, tx_height, None);
builder builder
.add_sapling_spend( .add_sapling_spend(extsk.clone(), note1, witness1.path().unwrap())
extsk.clone(),
*to.diversifier(),
note1,
witness1.path().unwrap(),
)
.unwrap(); .unwrap();
builder builder
.add_sapling_spend(extsk, *to.diversifier(), note2, witness2.path().unwrap()) .add_sapling_spend(extsk, note2, witness2.path().unwrap())
.unwrap(); .unwrap();
builder builder
.add_sapling_output( .add_sapling_output(