zcash_primitives: Move `builder` module to `zcash_primitives::sapling`

This commit is contained in:
Jack Grigg 2023-11-08 07:48:12 +00:00
parent 74c12ea9ec
commit f5595122f9
9 changed files with 54 additions and 40 deletions

View File

@ -6,9 +6,10 @@ use std::fmt::{self, Debug, Display};
use shardtree::error::ShardTreeError; use shardtree::error::ShardTreeError;
use zcash_primitives::transaction::components::amount::NonNegativeAmount; use zcash_primitives::transaction::components::amount::NonNegativeAmount;
use zcash_primitives::{ use zcash_primitives::{
sapling,
transaction::{ transaction::{
builder, builder,
components::{amount::BalanceError, sapling, transparent}, components::{amount::BalanceError, transparent},
}, },
zip32::AccountId, zip32::AccountId,
}; };

View File

@ -11,6 +11,13 @@ and this library adheres to Rust's notion of
- `zcash_primitives::sapling`: - `zcash_primitives::sapling`:
- `BatchValidator` (moved from `zcash_proofs::sapling`). - `BatchValidator` (moved from `zcash_proofs::sapling`).
- `SaplingVerificationContext` (moved from `zcash_proofs::sapling`). - `SaplingVerificationContext` (moved from `zcash_proofs::sapling`).
- `builder` (moved from
`zcash_primitives::transaction::components::sapling::builder`).
- `builder::UnauthorizedBundle`
- `builder::InProgress`
- `builder::{InProgressProofs, Unproven, Proven}`
- `builder::{InProgressSignatures, Unsigned, PartiallyAuthorized}`
- `builder::{MaybeSigned, SigningParts}`
- `circuit` module (moved from `zcash_proofs::circuit::sapling`). - `circuit` module (moved from `zcash_proofs::circuit::sapling`).
- `circuit::{SpendParameters, OutputParameters}` - `circuit::{SpendParameters, OutputParameters}`
- `circuit::{SpendVerifyingKey, PreparedSpendVerifyingKey}` - `circuit::{SpendVerifyingKey, PreparedSpendVerifyingKey}`
@ -25,11 +32,6 @@ and this library adheres to Rust's notion of
- `zcash_primitives::transaction`: - `zcash_primitives::transaction`:
- `builder::get_fee` - `builder::get_fee`
- `components::sapling`: - `components::sapling`:
- `builder::UnauthorizedBundle`
- `builder::InProgress`
- `builder::{InProgressProofs, Unproven, Proven}`
- `builder::{InProgressSignatures, Unsigned, PartiallyAuthorized}`
- `builder::{MaybeSigned, SigningParts}`
- `Bundle::<InProgress<Unproven, _>>::create_proofs` - `Bundle::<InProgress<Unproven, _>>::create_proofs`
- `Bundle::<InProgress<_, Unsigned>>::prepare` - `Bundle::<InProgress<_, Unsigned>>::prepare`
- `Bundle::<InProgress<_, PartiallyAuthorized>>::{sign, append_signatures}` - `Bundle::<InProgress<_, PartiallyAuthorized>>::{sign, append_signatures}`
@ -70,13 +72,6 @@ and this library adheres to Rust's notion of
newtypes. newtypes.
- `address::PaymentAddress::create_note` now takes its `value` argument as a - `address::PaymentAddress::create_note` now takes its `value` argument as a
`NoteValue` instead of as a bare `u64`. `NoteValue` instead of as a bare `u64`.
- `circuit::ValueCommitmentOpening::value` is now represented as a `NoteValue`
instead of as a bare `u64`.
- `zcash_primitives::transaction`:
- `builder::Builder::{build, build_zfuture}` now take
`&impl SpendProver, &impl OutputProver` instead of `&impl TxProver`.
- `components::sapling`:
- `MapAuth` trait methods now take `&mut self` instead of `&self`.
- `builder::SaplingBuilder::add_spend` now takes `extsk` by reference. - `builder::SaplingBuilder::add_spend` now takes `extsk` by reference.
- `builder::SaplingBuilder::build` no longer takes a prover, proving context, - `builder::SaplingBuilder::build` no longer takes a prover, proving context,
or progress notifier. Instead, it has `SpendProver, OutputProver` generic or progress notifier. Instead, it has `SpendProver, OutputProver` generic
@ -87,6 +82,13 @@ and this library adheres to Rust's notion of
- `Error::DuplicateSignature` - `Error::DuplicateSignature`
- `Error::InvalidExternalSignature` - `Error::InvalidExternalSignature`
- `Error::MissingSignatures` - `Error::MissingSignatures`
- `circuit::ValueCommitmentOpening::value` is now represented as a `NoteValue`
instead of as a bare `u64`.
- `zcash_primitives::transaction`:
- `builder::Builder::{build, build_zfuture}` now take
`&impl SpendProver, &impl OutputProver` instead of `&impl TxProver`.
- `components::sapling`:
- `MapAuth` trait methods now take `&mut self` instead of `&self`.
- `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>`.
@ -113,6 +115,7 @@ and this library adheres to Rust's notion of
- All `const` values (moved to `zcash_primitives::sapling::constants`). - All `const` values (moved to `zcash_primitives::sapling::constants`).
- `zcash_primitives::transaction::components::sapling`: - `zcash_primitives::transaction::components::sapling`:
- `Unproven` - `Unproven`
- `builder` (moved to `zcash_primitives::sapling::builder`).
- `builder::Unauthorized` (use `builder::InProgress` instead). - `builder::Unauthorized` (use `builder::InProgress` instead).
- `SpendDescription::<Unauthorized>::apply_signature` - `SpendDescription::<Unauthorized>::apply_signature`
- `Bundle::<Unauthorized>::apply_signatures` (use - `Bundle::<Unauthorized>::apply_signatures` (use

View File

@ -8,6 +8,7 @@ use zcash_primitives::{
consensus::{NetworkUpgrade::Canopy, Parameters, TEST_NETWORK}, consensus::{NetworkUpgrade::Canopy, Parameters, TEST_NETWORK},
memo::MemoBytes, memo::MemoBytes,
sapling::{ sapling::{
builder::SaplingBuilder,
note_encryption::{ note_encryption::{
try_sapling_compact_note_decryption, try_sapling_note_decryption, try_sapling_compact_note_decryption, try_sapling_note_decryption,
PreparedIncomingViewingKey, SaplingDomain, PreparedIncomingViewingKey, SaplingDomain,
@ -16,7 +17,7 @@ use zcash_primitives::{
value::NoteValue, value::NoteValue,
Diversifier, SaplingIvk, Diversifier, SaplingIvk,
}, },
transaction::components::sapling::{builder::SaplingBuilder, CompactOutputDescription}, transaction::components::sapling::CompactOutputDescription,
}; };
#[cfg(unix)] #[cfg(unix)]

View File

@ -1,6 +1,7 @@
//! Structs and constants specific to the Sapling shielded pool. //! Structs and constants specific to the Sapling shielded pool.
mod address; mod address;
pub mod builder;
pub mod circuit; pub mod circuit;
pub mod constants; pub mod constants;
pub mod group_hash; pub mod group_hash;

View File

@ -147,17 +147,17 @@ impl SpendDescriptionInfo {
) )
.ok_or(Error::SpendProof)?; .ok_or(Error::SpendProof)?;
Ok(SpendDescription { Ok(SpendDescription::from_parts(
cv, cv,
anchor, anchor,
nullifier, nullifier,
rk, rk,
zkproof, zkproof,
spend_auth_sig: SigningParts { SigningParts {
ak, ak,
alpha: self.alpha, alpha: self.alpha,
}, },
}) ))
} }
} }
@ -248,14 +248,14 @@ impl SaplingOutputInfo {
let epk = encryptor.epk(); let epk = encryptor.epk();
OutputDescription { OutputDescription::from_parts(
cv, cv,
cmu, cmu,
ephemeral_key: epk.to_bytes(), epk.to_bytes(),
enc_ciphertext, enc_ciphertext,
out_ciphertext, out_ciphertext,
zkproof, zkproof,
} )
} }
} }
@ -341,7 +341,7 @@ impl<P> SaplingBuilder<P> {
/// ///
/// This may be larger than the number of outputs that have been added to the builder, /// This may be larger than the number of outputs that have been added to the builder,
/// depending on whether padding is going to be applied. /// depending on whether padding is going to be applied.
pub(in crate::transaction) fn bundle_output_count(&self) -> usize { pub(crate) fn bundle_output_count(&self) -> usize {
// This matches the padding behaviour in `Self::build`. // This matches the padding behaviour in `Self::build`.
match self.spends.len() { match self.spends.len() {
0 => self.outputs.len(), 0 => self.outputs.len(),
@ -529,15 +529,15 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {
None None
} else { } else {
Some(( Some((
Bundle { Bundle::from_parts(
shielded_spends, shielded_spends,
shielded_outputs, shielded_outputs,
value_balance, value_balance,
authorization: InProgress { InProgress {
sigs: Unsigned { bsk }, sigs: Unsigned { bsk },
_proof_state: PhantomData::default(), _proof_state: PhantomData::default(),
}, },
}, ),
tx_metadata, tx_metadata,
)) ))
}; };
@ -676,7 +676,8 @@ impl<S: InProgressSignatures> Bundle<InProgress<Unproven, S>> {
rng: impl RngCore, rng: impl RngCore,
progress_notifier: Option<&Sender<Progress>>, progress_notifier: Option<&Sender<Progress>>,
) -> Bundle<InProgress<Proven, S>> { ) -> Bundle<InProgress<Proven, S>> {
let total_progress = self.shielded_spends.len() as u32 + self.shielded_outputs.len() as u32; let total_progress =
self.shielded_spends().len() as u32 + self.shielded_outputs().len() as u32;
self.map_authorization(CreateProofs::new( self.map_authorization(CreateProofs::new(
spend_prover, spend_prover,
output_prover, output_prover,
@ -803,7 +804,7 @@ impl<P: InProgressProofs> Bundle<InProgress<P, PartiallyAuthorized>> {
/// This will apply signatures for all notes controlled by this spending key. /// This will apply signatures for all notes controlled by this spending key.
pub fn sign<R: RngCore + CryptoRng>(self, mut rng: R, ask: &PrivateKey) -> Self { pub fn sign<R: RngCore + CryptoRng>(self, mut rng: R, ask: &PrivateKey) -> Self {
let expected_ak = PublicKey::from_private(ask, SPENDING_KEY_GENERATOR); let expected_ak = PublicKey::from_private(ask, SPENDING_KEY_GENERATOR);
let sighash = self.authorization.sigs.sighash; let sighash = self.authorization().sigs.sighash;
self.map_authorization(( self.map_authorization((
|proof| proof, |proof| proof,
|proof| proof, |proof| proof,
@ -827,7 +828,7 @@ impl<P: InProgressProofs> Bundle<InProgress<P, PartiallyAuthorized>> {
} }
fn append_signature(self, signature: &Signature) -> Result<Self, Error> { fn append_signature(self, signature: &Signature) -> Result<Self, Error> {
let sighash = self.authorization.sigs.sighash; let sighash = self.authorization().sigs.sighash;
let mut signature_valid_for = 0usize; let mut signature_valid_for = 0usize;
let bundle = self.map_authorization(( let bundle = self.map_authorization((
|proof| proof, |proof| proof,

View File

@ -33,8 +33,8 @@
//! //!
//! [`Bundle`]: crate::transaction::components::sapling::Bundle //! [`Bundle`]: crate::transaction::components::sapling::Bundle
//! [`Bundle::value_balance`]: crate::transaction::components::sapling::Bundle::value_balance //! [`Bundle::value_balance`]: crate::transaction::components::sapling::Bundle::value_balance
//! [`SaplingBuilder::value_balance`]: crate::transaction::components::sapling::builder::SaplingBuilder::value_balance //! [`SaplingBuilder::value_balance`]: crate::sapling::builder::SaplingBuilder::value_balance
//! [`SaplingBuilder::add_output`]: crate::transaction::components::sapling::builder::SaplingBuilder::add_output //! [`SaplingBuilder::add_output`]: crate::sapling::builder::SaplingBuilder::add_output
//! [Rust documentation]: https://doc.rust-lang.org/stable/std/primitive.i64.html //! [Rust documentation]: https://doc.rust-lang.org/stable/std/primitive.i64.html
use bitvec::{array::BitArray, order::Lsb0}; use bitvec::{array::BitArray, order::Lsb0};

View File

@ -14,16 +14,14 @@ use crate::{
memo::MemoBytes, memo::MemoBytes,
sapling::{ sapling::{
self, self,
builder::{self as sapling_builder, SaplingBuilder, SaplingMetadata},
prover::{OutputProver, SpendProver}, prover::{OutputProver, SpendProver},
redjubjub, Diversifier, Note, PaymentAddress, redjubjub, Diversifier, Note, PaymentAddress,
}, },
transaction::{ transaction::{
components::{ components::{
amount::{Amount, BalanceError}, amount::{Amount, BalanceError},
sapling::{ sapling::fees as sapling_fees,
builder::{self as sapling_builder, SaplingBuilder, SaplingMetadata},
fees as sapling_fees,
},
transparent::{self, builder::TransparentBuilder}, transparent::{self, builder::TransparentBuilder},
}, },
fees::FeeRule, fees::FeeRule,

View File

@ -24,7 +24,6 @@ use super::{amount::Amount, GROTH_PROOF_SIZE};
pub type GrothProofBytes = [u8; GROTH_PROOF_SIZE]; pub type GrothProofBytes = [u8; GROTH_PROOF_SIZE];
pub mod builder;
pub mod fees; pub mod fees;
/// Defines the authorization type of a Sapling bundle. /// Defines the authorization type of a Sapling bundle.
@ -341,6 +340,17 @@ impl<A: Authorization> SpendDescription<A> {
rk: PublicKey, rk: PublicKey,
zkproof: A::SpendProof, zkproof: A::SpendProof,
spend_auth_sig: A::AuthSig, spend_auth_sig: A::AuthSig,
) -> Self {
Self::from_parts(cv, anchor, nullifier, rk, zkproof, spend_auth_sig)
}
pub(crate) fn from_parts(
cv: ValueCommitment,
anchor: bls12_381::Scalar,
nullifier: Nullifier,
rk: PublicKey,
zkproof: A::SpendProof,
spend_auth_sig: A::AuthSig,
) -> Self { ) -> Self {
Self { Self {
cv, cv,
@ -595,7 +605,6 @@ impl<Proof> OutputDescription<Proof> {
) )
} }
#[cfg(any(test, feature = "temporary-zcashd"))]
pub(crate) fn from_parts( pub(crate) fn from_parts(
cv: ValueCommitment, cv: ValueCommitment,
cmu: ExtractedNoteCommitment, cmu: ExtractedNoteCommitment,

View File

@ -24,7 +24,7 @@ use zcash_encoding::{Array, CompactSize, Vector};
use crate::{ use crate::{
consensus::{BlockHeight, BranchId}, consensus::{BlockHeight, BranchId},
sapling::redjubjub, sapling::{builder as sapling_builder, redjubjub},
}; };
use self::{ use self::{
@ -276,7 +276,7 @@ pub struct Unauthorized;
impl Authorization for Unauthorized { impl Authorization for Unauthorized {
type TransparentAuth = transparent::builder::Unauthorized; type TransparentAuth = transparent::builder::Unauthorized;
type SaplingAuth = type SaplingAuth =
sapling::builder::InProgress<sapling::builder::Proven, sapling::builder::Unsigned>; sapling_builder::InProgress<sapling_builder::Proven, sapling_builder::Unsigned>;
type OrchardAuth = type OrchardAuth =
orchard::builder::InProgress<orchard::builder::Unproven, orchard::builder::Unauthorized>; orchard::builder::InProgress<orchard::builder::Unproven, orchard::builder::Unauthorized>;