1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! PCZT support for Sapling.
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use getset::Getters;
use redjubjub::{Binding, SpendAuth};
use zcash_note_encryption::{
ENC_CIPHERTEXT_SIZE, EphemeralKeyBytes, OUT_CIPHERTEXT_SIZE, OutgoingCipherKey,
};
use zip32::ChildIndex;
use crate::{
Anchor, MerklePath, Nullifier, PaymentAddress, ProofGenerationKey, Rseed,
bundle::GrothProofBytes,
keys::SpendAuthorizingKey,
note::ExtractedNoteCommitment,
value::{NoteValue, ValueCommitTrapdoor, ValueCommitment, ValueSum},
};
mod parse;
pub use parse::ParseError;
mod verify;
pub use verify::VerifyError;
mod io_finalizer;
pub use io_finalizer::IoFinalizerError;
mod updater;
pub use updater::{OutputUpdater, SpendUpdater, Updater, UpdaterError};
#[cfg(feature = "circuit")]
mod prover;
#[cfg(feature = "circuit")]
pub use prover::ProverError;
mod signer;
pub use signer::SignerError;
mod tx_extractor;
pub use tx_extractor::{TxExtractorError, Unbound};
/// PCZT fields that are specific to producing the transaction's Sapling bundle (if any).
///
/// This struct is for representing Sapling in a partially-created transaction. If you
/// have a fully-created transaction, use [the regular `Bundle` struct].
///
/// [the regular `Bundle` struct]: crate::Bundle
#[derive(Debug, Getters)]
#[getset(get = "pub")]
pub struct Bundle {
/// The Sapling spends in this bundle.
///
/// Entries are added by the Constructor, and modified by an Updater, IO Finalizer,
/// Prover, Signer, Combiner, or Spend Finalizer.
pub(crate) spends: Vec<Spend>,
/// The Sapling outputs in this bundle.
///
/// Entries are added by the Constructor, and modified by an Updater, IO Finalizer,
/// Prover, Signer, Combiner, or Spend Finalizer.
pub(crate) outputs: Vec<Output>,
/// The net value of Sapling `spends` minus `outputs`.
///
/// This is initialized by the Creator, and updated by the Constructor as spends or
/// outputs are added to the PCZT. It enables per-spend and per-output values to be
/// redacted from the PCZT after they are no longer necessary.
pub(crate) value_sum: ValueSum,
/// The Sapling anchor for this transaction.
///
/// Set by the Creator.
pub(crate) anchor: Anchor,
/// The Sapling binding signature signing key.
///
/// - This is `None` until it is set by the IO Finalizer.
/// - The Transaction Extractor uses this to produce the binding signature.
pub(crate) bsk: Option<redjubjub::SigningKey<Binding>>,
}
impl Bundle {
/// Returns a mutable reference to the spends in this bundle.
///
/// This is used by Signers to apply signatures with [`Spend::sign`].
pub fn spends_mut(&mut self) -> &mut [Spend] {
&mut self.spends
}
}
/// Information about a Sapling spend within a transaction.
///
/// This struct is for representing Sapling spends in a partially-created transaction. If
/// you have a fully-created transaction, use [the regular `SpendDescription` struct].
///
/// [the regular `SpendDescription` struct]: crate::bundle::SpendDescription
#[derive(Debug, Getters)]
#[getset(get = "pub")]
pub struct Spend {
/// A commitment to the value consumed by this spend.
pub(crate) cv: ValueCommitment,
/// The nullifier of the note being spent.
pub(crate) nullifier: Nullifier,
/// The randomized verification key for the note being spent.
pub(crate) rk: redjubjub::VerificationKey<SpendAuth>,
/// The Spend proof.
///
/// This is set by the Prover.
pub(crate) zkproof: Option<GrothProofBytes>,
/// The spend authorization signature.
///
/// This is set by the Signer.
pub(crate) spend_auth_sig: Option<redjubjub::Signature<SpendAuth>>,
/// The address that received the note being spent.
///
/// - This is set by the Constructor (or Updater?).
/// - This is required by the Prover.
pub(crate) recipient: Option<PaymentAddress>,
/// The value of the input being spent.
///
/// This may be used by Signers to verify that the value matches `cv`, and to confirm
/// the values and change involved in the transaction.
///
/// This exposes the input value to all participants. For Signers who don't need this
/// information, or after signatures have been applied, this can be redacted.
pub(crate) value: Option<NoteValue>,
/// The seed randomness for the note being spent.
///
/// - This is set by the Constructor.
/// - This is required by the Prover.
pub(crate) rseed: Option<Rseed>,
/// The value commitment randomness.
///
/// - This is set by the Constructor.
/// - The IO Finalizer compresses it into the `bsk`.
/// - This is required by the Prover.
/// - This may be used by Signers to verify that the value correctly matches `cv`.
///
/// This opens `cv` for all participants. For Signers who don't need this information,
/// or after proofs / signatures have been applied, this can be redacted.
pub(crate) rcv: Option<ValueCommitTrapdoor>,
/// The proof generation key `(ak, nsk)` corresponding to the recipient that received
/// the note being spent.
///
/// - This is set by the Updater.
/// - This is required by the Prover.
pub(crate) proof_generation_key: Option<ProofGenerationKey>,
/// A witness from the note to the bundle's anchor.
///
/// - This is set by the Updater.
/// - This is required by the Prover.
pub(crate) witness: Option<MerklePath>,
/// The spend authorization randomizer.
///
/// - This is chosen by the Constructor.
/// - This is required by the Signer for creating `spend_auth_sig`, and may be used to
/// validate `rk`.
/// - After`zkproof` / `spend_auth_sig` has been set, this can be redacted.
pub(crate) alpha: Option<jubjub::Scalar>,
/// The ZIP 32 derivation path at which the spending key can be found for the note
/// being spent.
pub(crate) zip32_derivation: Option<Zip32Derivation>,
/// The spend authorizing key for this spent note, if it is a dummy note.
///
/// - This is chosen by the Constructor.
/// - This is required by the IO Finalizer, and is cleared by it once used.
/// - Signers MUST reject PCZTs that contain `dummy_ask` values.
pub(crate) dummy_ask: Option<SpendAuthorizingKey>,
/// Proprietary fields related to the note being spent.
pub(crate) proprietary: BTreeMap<String, Vec<u8>>,
}
/// Information about a Sapling output within a transaction.
///
/// This struct is for representing Sapling outputs in a partially-created transaction. If
/// you have a fully-created transaction, use [the regular `OutputDescription` struct].
///
/// [the regular `OutputDescription` struct]: crate::bundle::OutputDescription
#[derive(Getters)]
#[getset(get = "pub")]
pub struct Output {
/// A commitment to the value created by this output.
pub(crate) cv: ValueCommitment,
/// A commitment to the new note being created.
pub(crate) cmu: ExtractedNoteCommitment,
/// The ephemeral key used to encrypt the note plaintext.
pub(crate) ephemeral_key: EphemeralKeyBytes,
/// The encrypted note plaintext for the output.
///
/// Once we have memo bundles, we will be able to set memos independently of Outputs.
/// For now, the Constructor sets both at the same time.
pub(crate) enc_ciphertext: [u8; ENC_CIPHERTEXT_SIZE],
/// The encrypted output plaintext for the output.
pub(crate) out_ciphertext: [u8; OUT_CIPHERTEXT_SIZE],
/// The Output proof.
///
/// This is set by the Prover.
pub(crate) zkproof: Option<GrothProofBytes>,
/// The address that will receive the output.
///
/// - This is set by the Constructor.
/// - This is required by the Prover.
/// - The Signer can use `recipient` and `rseed` (if present) to verify that
/// `enc_ciphertext` is correctly encrypted (and contains a note plaintext matching
/// the public commitments), and to confirm the value of the memo.
pub(crate) recipient: Option<PaymentAddress>,
/// The value of the output.
///
/// This may be used by Signers to verify that the value matches `cv`, and to confirm
/// the values and change involved in the transaction.
///
/// This exposes the output value to all participants. For Signers who don't need this
/// information, or after signatures have been applied, this can be redacted.
pub(crate) value: Option<NoteValue>,
/// The seed randomness for the output.
///
/// - This is set by the Constructor.
/// - This is required by the Prover.
/// - The Signer can use `recipient` and `rseed` (if present) to verify that
/// `enc_ciphertext` is correctly encrypted (and contains a note plaintext matching
/// the public commitments), and to confirm the value of the memo.
pub(crate) rseed: Option<[u8; 32]>,
/// The value commitment randomness.
///
/// - This is set by the Constructor.
/// - The IO Finalizer compresses it into the bsk.
/// - This is required by the Prover.
/// - This may be used by Signers to verify that the value correctly matches `cv`.
///
/// This opens `cv` for all participants. For Signers who don't need this information,
/// or after proofs / signatures have been applied, this can be redacted.
pub(crate) rcv: Option<ValueCommitTrapdoor>,
/// The `ock` value used to encrypt `out_ciphertext`.
///
/// This enables Signers to verify that `out_ciphertext` is correctly encrypted.
///
/// This may be `None` if the Constructor added the output using an OVK policy of
/// "None", to make the output unrecoverable from the chain by the sender.
pub(crate) ock: Option<OutgoingCipherKey>,
/// The ZIP 32 derivation path at which the spending key can be found for the output.
pub(crate) zip32_derivation: Option<Zip32Derivation>,
/// The user-facing address to which this output is being sent, if any.
///
/// - This is set by an Updater.
/// - Signers must parse this address (if present) and confirm that it contains
/// `recipient` (either directly, or e.g. as a receiver within a Unified Address).
pub(crate) user_address: Option<String>,
/// Proprietary fields related to the note being created.
pub(crate) proprietary: BTreeMap<String, Vec<u8>>,
}
impl fmt::Debug for Output {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Output")
.field("cv", &self.cv)
.field("cmu", &self.cmu)
.field("ephemeral_key", &self.ephemeral_key)
.field("enc_ciphertext", &self.enc_ciphertext)
.field("out_ciphertext", &self.out_ciphertext)
.field("zkproof", &self.zkproof)
.field("recipient", &self.recipient)
.field("value", &self.value)
.field("rseed", &self.rseed)
.field("rcv", &self.rcv)
.field("zip32_derivation", &self.zip32_derivation)
.field("user_address", &self.user_address)
.field("proprietary", &self.proprietary)
.finish_non_exhaustive()
}
}
/// The ZIP 32 derivation path at which a key can be found.
#[derive(Debug, Getters, PartialEq, Eq)]
#[getset(get = "pub")]
pub struct Zip32Derivation {
/// The [ZIP 32 seed fingerprint](https://zips.z.cash/zip-0032#seed-fingerprints).
seed_fingerprint: [u8; 32],
/// The sequence of indices corresponding to the shielded HD path.
derivation_path: Vec<ChildIndex>,
}
impl Zip32Derivation {
/// Extracts the ZIP 32 account index from this derivation path.
///
/// Returns `None` if the seed fingerprints don't match, or if this is a non-standard
/// derivation path.
pub fn extract_account_index(
&self,
seed_fp: &zip32::fingerprint::SeedFingerprint,
expected_coin_type: zip32::ChildIndex,
) -> Option<zip32::AccountId> {
if self.seed_fingerprint == seed_fp.to_bytes() {
match &self.derivation_path[..] {
[purpose, coin_type, account_index]
if purpose == &zip32::ChildIndex::hardened(32)
&& coin_type == &expected_coin_type =>
{
Some(
zip32::AccountId::try_from(account_index.index() - (1 << 31))
.expect("zip32::ChildIndex only supports hardened"),
)
}
_ => None,
}
} else {
None
}
}
}