Skip to content

Commit

Permalink
Expose a bunch of fields
Browse files Browse the repository at this point in the history
TODO: Restrict access with accessors
  • Loading branch information
hhanh00 committed Apr 18, 2023
1 parent f39dd6c commit e2fe0b8
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 112 deletions.
6 changes: 3 additions & 3 deletions zcash_primitives/src/sapling/keys.rs
Expand Up @@ -115,7 +115,7 @@ impl ExpandedSpendingKey {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct ProofGenerationKey {
pub ak: jubjub::SubgroupPoint,
pub nsk: jubjub::Fr,
Expand Down Expand Up @@ -347,7 +347,7 @@ impl ConditionallySelectable for DiversifiedTransmissionKey {
///
/// [concretesaplingkeyagreement]: https://zips.z.cash/protocol/protocol.pdf#concretesaplingkeyagreement
#[derive(Debug)]
pub struct EphemeralSecretKey(pub(crate) jubjub::Scalar);
pub struct EphemeralSecretKey(pub jubjub::Scalar);

impl ConstantTimeEq for EphemeralSecretKey {
fn ct_eq(&self, other: &Self) -> subtle::Choice {
Expand Down Expand Up @@ -391,7 +391,7 @@ impl EphemeralPublicKey {
jubjub::ExtendedPoint::from_bytes(bytes).map(EphemeralPublicKey)
}

pub(crate) fn to_bytes(&self) -> EphemeralKeyBytes {
pub fn to_bytes(&self) -> EphemeralKeyBytes {
EphemeralKeyBytes(self.0.to_bytes())
}
}
Expand Down
39 changes: 12 additions & 27 deletions zcash_primitives/src/sapling/prover.rs
@@ -1,5 +1,7 @@
//! Abstractions over the proving system and parameters.

use rand_core::RngCore;

use crate::{
merkle_tree::MerklePath,
sapling::{
Expand All @@ -25,7 +27,7 @@ pub trait TxProver {
///
/// [`SpendDescription`]: crate::transaction::components::SpendDescription
#[allow(clippy::too_many_arguments)]
fn spend_proof(
fn spend_proof<R: RngCore>(
&self,
ctx: &mut Self::SaplingProvingContext,
proof_generation_key: ProofGenerationKey,
Expand All @@ -35,33 +37,22 @@ pub trait TxProver {
value: u64,
anchor: bls12_381::Scalar,
merkle_path: MerklePath<Node>,
) -> Result<([u8; GROTH_PROOF_SIZE], ValueCommitment, PublicKey), ()>;

fn spend_proof_with_rcv(
&self,
ctx: &mut Self::SaplingProvingContext,
rcv: ValueCommitTrapdoor,
proof_generation_key: ProofGenerationKey,
diversifier: Diversifier,
rseed: Rseed,
ar: jubjub::Fr,
value: u64,
anchor: bls12_381::Scalar,
merkle_path: MerklePath<Node>,
rng: R,
) -> Result<([u8; GROTH_PROOF_SIZE], ValueCommitment, PublicKey), ()>;

/// Create the value commitment and proof for a Sapling [`OutputDescription`],
/// while accumulating its value commitment randomness inside the context for later
/// use.
///
/// [`OutputDescription`]: crate::transaction::components::OutputDescription
fn output_proof(
fn output_proof<R: RngCore>(
&self,
ctx: &mut Self::SaplingProvingContext,
esk: jubjub::Fr,
payment_address: PaymentAddress,
rcm: jubjub::Fr,
value: u64,
rng: R,
) -> ([u8; GROTH_PROOF_SIZE], ValueCommitment);

fn output_proof_with_rcv(
Expand Down Expand Up @@ -89,7 +80,7 @@ pub trait TxProver {
pub mod mock {
use bls12_381::Scalar;

Check failure on line 81 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `bls12_381::Scalar`

error: unused import: `bls12_381::Scalar` --> zcash_primitives/src/sapling/prover.rs:81:9 | 81 | use bls12_381::Scalar; | ^^^^^^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings`

Check failure on line 81 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `bls12_381::Scalar`

error: unused import: `bls12_381::Scalar` --> zcash_primitives/src/sapling/prover.rs:81:9 | 81 | use bls12_381::Scalar; | ^^^^^^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings`
use jubjub::{ExtendedPoint, Fr};

Check failure on line 82 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `ExtendedPoint`

error: unused import: `ExtendedPoint` --> zcash_primitives/src/sapling/prover.rs:82:18 | 82 | use jubjub::{ExtendedPoint, Fr}; | ^^^^^^^^^^^^^

Check failure on line 82 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `ExtendedPoint`

error: unused import: `ExtendedPoint` --> zcash_primitives/src/sapling/prover.rs:82:18 | 82 | use jubjub::{ExtendedPoint, Fr}; | ^^^^^^^^^^^^^
use rand_core::OsRng;
use rand_core::{OsRng, RngCore};

Check failure on line 83 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `OsRng`

error: unused import: `OsRng` --> zcash_primitives/src/sapling/prover.rs:83:21 | 83 | use rand_core::{OsRng, RngCore}; | ^^^^^

Check failure on line 83 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `OsRng`

error: unused import: `OsRng` --> zcash_primitives/src/sapling/prover.rs:83:21 | 83 | use rand_core::{OsRng, RngCore}; | ^^^^^

use crate::{
constants::SPENDING_KEY_GENERATOR,
Expand All @@ -111,7 +102,7 @@ pub mod mock {

fn new_sapling_proving_context(&self) -> Self::SaplingProvingContext {}

fn spend_proof(
fn spend_proof<R: RngCore>(
&self,
_ctx: &mut Self::SaplingProvingContext,
proof_generation_key: ProofGenerationKey,
Expand All @@ -121,9 +112,8 @@ pub mod mock {
value: u64,
_anchor: bls12_381::Scalar,
_merkle_path: MerklePath<Node>,
rng: R,
) -> Result<([u8; GROTH_PROOF_SIZE], ValueCommitment, PublicKey), ()> {
let mut rng = OsRng;

let value = NoteValue::from_raw(value);
let rcv = ValueCommitTrapdoor::random(&mut rng);

Check failure on line 118 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

cannot borrow `rng` as mutable, as it is not declared as mutable

error[E0596]: cannot borrow `rng` as mutable, as it is not declared as mutable --> zcash_primitives/src/sapling/prover.rs:118:51 | 115 | rng: R, | --- help: consider changing this to be mutable: `mut rng` ... 118 | let rcv = ValueCommitTrapdoor::random(&mut rng); | ^^^^^^^^ cannot borrow as mutable

Check failure on line 118 in zcash_primitives/src/sapling/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

cannot borrow `rng` as mutable, as it is not declared as mutable

error[E0596]: cannot borrow `rng` as mutable, as it is not declared as mutable --> zcash_primitives/src/sapling/prover.rs:118:51 | 115 | rng: R, | --- help: consider changing this to be mutable: `mut rng` ... 118 | let rcv = ValueCommitTrapdoor::random(&mut rng); | ^^^^^^^^ cannot borrow as mutable
let cv = ValueCommitment::derive(value, rcv);
Expand All @@ -134,22 +124,17 @@ pub mod mock {
Ok(([0u8; GROTH_PROOF_SIZE], cv, rk))
}

fn spend_proof_with_rcv(&self, ctx: &mut Self::SaplingProvingContext, rcv: ValueCommitTrapdoor, proof_generation_key: ProofGenerationKey, diversifier: Diversifier, rseed: Rseed, ar: Fr, value: u64, anchor: Scalar, merkle_path: MerklePath<Node>) -> Result<([u8; GROTH_PROOF_SIZE], ValueCommitment, PublicKey), ()> {
unimplemented!()
}

fn output_proof(
fn output_proof<R: RngCore>(
&self,
_ctx: &mut Self::SaplingProvingContext,
_esk: jubjub::Fr,
_payment_address: PaymentAddress,
_rcm: jubjub::Fr,
value: u64,
rng: R,
) -> ([u8; GROTH_PROOF_SIZE], ValueCommitment) {
let mut rng = OsRng;

let value = NoteValue::from_raw(value);
let rcv = ValueCommitTrapdoor::random(&mut rng);
let rcv = ValueCommitTrapdoor::random(rng);
let cv = ValueCommitment::derive(value, rcv);

([0u8; GROTH_PROOF_SIZE], cv)
Expand Down
4 changes: 2 additions & 2 deletions zcash_primitives/src/sapling/redjubjub.rs
Expand Up @@ -30,8 +30,8 @@ fn h_star(a: &[u8], b: &[u8]) -> jubjub::Fr {

#[derive(Copy, Clone, Debug)]
pub struct Signature {
rbar: [u8; 32],
sbar: [u8; 32],
pub rbar: [u8; 32],
pub sbar: [u8; 32],
}

pub struct PrivateKey(pub jubjub::Fr);
Expand Down
2 changes: 1 addition & 1 deletion zcash_primitives/src/sapling/tree.rs
Expand Up @@ -62,7 +62,7 @@ pub fn merkle_hash(depth: usize, lhs: &[u8; 32], rhs: &[u8; 32]) -> [u8; 32] {
/// A node within the Sapling commitment tree.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Node {
pub(super) repr: [u8; 32],
pub repr: [u8; 32],
}

impl Node {
Expand Down
27 changes: 13 additions & 14 deletions zcash_primitives/src/transaction/components/sapling.rs
Expand Up @@ -122,7 +122,7 @@ impl<A: Authorization> Bundle<A> {
}

/// Constructs a `Bundle` from its constituent parts.
pub(crate) fn from_parts(
pub fn from_parts(
shielded_spends: Vec<SpendDescription<A>>,
shielded_outputs: Vec<OutputDescription<A::OutputProof>>,
value_balance: Amount,
Expand Down Expand Up @@ -194,12 +194,12 @@ impl<A: Authorization> Bundle<A> {

#[derive(Clone)]
pub struct SpendDescription<A: Authorization> {
cv: ValueCommitment,
anchor: bls12_381::Scalar,
pub cv: ValueCommitment,
pub anchor: bls12_381::Scalar,
pub nullifier: Nullifier,
rk: PublicKey,
zkproof: A::SpendProof,
spend_auth_sig: A::AuthSig,
pub rk: PublicKey,
pub zkproof: A::SpendProof,
pub spend_auth_sig: A::AuthSig,
}

impl<A: Authorization> std::fmt::Debug for SpendDescription<A> {
Expand All @@ -213,8 +213,7 @@ impl<A: Authorization> std::fmt::Debug for SpendDescription<A> {
}

impl<A: Authorization> SpendDescription<A> {
#[cfg(feature = "temporary-zcashd")]
pub fn temporary_zcashd_from_parts(
pub fn from_parts(
cv: ValueCommitment,
anchor: bls12_381::Scalar,
nullifier: Nullifier,
Expand Down Expand Up @@ -408,12 +407,12 @@ impl SpendDescriptionV5 {

#[derive(Clone)]
pub struct OutputDescription<Proof> {
cv: ValueCommitment,
cmu: ExtractedNoteCommitment,
ephemeral_key: EphemeralKeyBytes,
enc_ciphertext: [u8; 580],
out_ciphertext: [u8; 80],
zkproof: Proof,
pub cv: ValueCommitment,
pub cmu: ExtractedNoteCommitment,
pub ephemeral_key: EphemeralKeyBytes,
pub enc_ciphertext: [u8; 580],
pub out_ciphertext: [u8; 80],
pub zkproof: Proof,
}

impl<Proof> OutputDescription<Proof> {
Expand Down
64 changes: 54 additions & 10 deletions zcash_primitives/src/transaction/components/sapling/builder.rs
Expand Up @@ -19,7 +19,7 @@ use crate::{
spend_sig_internal,
util::generate_random_rseed_internal,
value::{NoteValue, ValueSum},
Diversifier, Node, Note, PaymentAddress,
Diversifier, Node, Note, PaymentAddress, Rseed,
},
transaction::{
builder::Progress,
Expand Down Expand Up @@ -63,11 +63,11 @@ impl fmt::Display for Error {

#[derive(Debug, Clone)]
pub struct SpendDescriptionInfo {
extsk: ExtendedSpendingKey,
diversifier: Diversifier,
note: Note,
alpha: jubjub::Fr,
merkle_path: MerklePath<Node>,
pub extsk: ExtendedSpendingKey,
pub diversifier: Diversifier,
pub note: Note,
pub alpha: jubjub::Fr,
pub merkle_path: MerklePath<Node>,
}

impl fees::InputView<()> for SpendDescriptionInfo {
Expand Down Expand Up @@ -115,27 +115,45 @@ impl SaplingOutputInfo {
}
}

pub fn new_with_rseed(
ovk: Option<OutgoingViewingKey>,
to: PaymentAddress,
value: NoteValue,
memo: MemoBytes,
rseed: Rseed,
) -> Self {
let note = Note::from_parts(to, value, rseed);

SaplingOutputInfo {
ovk,
to,
note,
memo,
}
}

fn build<P: consensus::Parameters, Pr: TxProver, R: RngCore>(
self,
prover: &Pr,
ctx: &mut Pr::SaplingProvingContext,
rng: &mut R,
mut rng: R,
) -> OutputDescription<GrothProofBytes> {
let encryptor =
sapling_note_encryption::<R, P>(self.ovk, self.note.clone(), self.to, self.memo, rng);
sapling_note_encryption::<R, P>(self.ovk, self.note.clone(), self.to, self.memo, &mut rng);

let (zkproof, cv) = prover.output_proof(
ctx,
encryptor.esk().0,
self.to,
self.note.rcm(),
self.note.value().inner(),
&mut rng,
);

let cmu = self.note.cmu();

let enc_ciphertext = encryptor.encrypt_note_plaintext();
let out_ciphertext = encryptor.encrypt_outgoing_plaintext(&cv, &cmu, rng);
let out_ciphertext = encryptor.encrypt_outgoing_plaintext(&cv, &cmu, &mut rng);

let epk = encryptor.epk();

Expand Down Expand Up @@ -206,7 +224,7 @@ pub struct SaplingBuilder<P> {

#[derive(Clone)]
pub struct Unauthorized {
tx_metadata: SaplingMetadata,
pub tx_metadata: SaplingMetadata,
}

impl std::fmt::Debug for Unauthorized {
Expand Down Expand Up @@ -343,6 +361,30 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {
Ok(())
}

pub fn add_output_with_rseed(
&mut self,
ovk: Option<OutgoingViewingKey>,
to: PaymentAddress,
value: NoteValue,
memo: MemoBytes,
rseed: Rseed,
) -> Result<(), Error> {
let output = SaplingOutputInfo::new_with_rseed(
ovk,
to,
value,
memo,
rseed,
);

self.value_balance = (self.value_balance - value).ok_or(Error::InvalidAddress)?;
self.try_value_balance()?;

self.outputs.push(output);

Ok(())
}

pub fn build<Pr: TxProver, R: RngCore>(
self,
prover: &Pr,
Expand Down Expand Up @@ -411,6 +453,7 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {
spend.note.value().inner(),
anchor,
spend.merkle_path.clone(),
&mut rng,
)
.map_err(|_| Error::SpendProof)?;

Expand Down Expand Up @@ -486,6 +529,7 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {
dummy_note.recipient(),
dummy_note.rcm(),
dummy_note.value().inner(),
&mut rng,
);

let cmu = dummy_note.cmu();
Expand Down
16 changes: 8 additions & 8 deletions zcash_primitives/src/transaction/mod.rs
Expand Up @@ -305,14 +305,14 @@ impl PartialEq for Transaction {

#[derive(Debug)]
pub struct TransactionData<A: Authorization> {
version: TxVersion,
consensus_branch_id: BranchId,
lock_time: u32,
expiry_height: BlockHeight,
transparent_bundle: Option<transparent::Bundle<A::TransparentAuth>>,
sprout_bundle: Option<sprout::Bundle>,
sapling_bundle: Option<sapling::Bundle<A::SaplingAuth>>,
orchard_bundle: Option<orchard::bundle::Bundle<A::OrchardAuth, Amount>>,
pub version: TxVersion,
pub consensus_branch_id: BranchId,
pub lock_time: u32,
pub expiry_height: BlockHeight,
pub transparent_bundle: Option<transparent::Bundle<A::TransparentAuth>>,
pub sprout_bundle: Option<sprout::Bundle>,
pub sapling_bundle: Option<sapling::Bundle<A::SaplingAuth>>,
pub orchard_bundle: Option<orchard::bundle::Bundle<A::OrchardAuth, Amount>>,
#[cfg(feature = "zfuture")]
tze_bundle: Option<tze::Bundle<A::TzeAuth>>,
}
Expand Down
2 changes: 1 addition & 1 deletion zcash_primitives/src/zip32/sapling.rs
Expand Up @@ -652,7 +652,7 @@ impl ExtendedFullViewingKey {
/// [zip-0316-ufvk]: https://zips.z.cash/zip-0316#encoding-of-unified-full-incoming-viewing-keys
#[derive(Clone, Debug)]
pub struct DiversifiableFullViewingKey {
fvk: FullViewingKey,
pub fvk: FullViewingKey,
dk: DiversifierKey,
}

Expand Down

0 comments on commit e2fe0b8

Please sign in to comment.