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
use super::masp_compute_value_balance;
use crate::circuit::convert::Convert;
use crate::circuit::sapling::{Output, Spend};
use bellman::{
gadgets::multipack,
groth16::{Parameters, PreparedVerifyingKey, Proof, create_random_proof, verify_proof},
};
use bls12_381::Bls12;
use group::ff::Field;
use group::{Curve, GroupEncoding};
use masp_primitives::{
asset_type::AssetType,
constants::{spending_key_generator, value_commitment_randomness_generator},
convert::AllowedConversion,
merkle_tree::MerklePath,
sapling::{
Diversifier, Node, Note, PaymentAddress, ProofGenerationKey, Rseed,
redjubjub::{PrivateKey, PublicKey, Signature},
},
transaction::components::I128Sum,
};
use rand_core::OsRng;
use std::ops::{AddAssign, Neg};
/// A context object for creating the Sapling components of a Zcash transaction.
pub struct SaplingProvingContext {
bsk: jubjub::Fr,
// (sum of the Spend value commitments) - (sum of the Output value commitments)
cv_sum: jubjub::ExtendedPoint,
}
impl Default for SaplingProvingContext {
fn default() -> Self {
SaplingProvingContext::new()
}
}
impl SaplingProvingContext {
/// Construct a new context to be used with a single transaction.
pub fn new() -> Self {
SaplingProvingContext {
bsk: jubjub::Fr::zero(),
cv_sum: jubjub::ExtendedPoint::identity(),
}
}
/// Create the value commitment, re-randomized key, and proof for a Sapling
/// SpendDescription, while accumulating its value commitment randomness
/// inside the context for later use.
#[allow(clippy::too_many_arguments)]
pub fn spend_proof(
&mut self,
proof_generation_key: ProofGenerationKey,
diversifier: Diversifier,
rseed: Rseed,
ar: jubjub::Fr,
asset_type: AssetType,
value: u64,
anchor: bls12_381::Scalar,
merkle_path: MerklePath<Node>,
proving_key: &Parameters<Bls12>,
verifying_key: &PreparedVerifyingKey<Bls12>,
rcv: jubjub::Fr,
) -> Result<(Proof<Bls12>, jubjub::ExtendedPoint, PublicKey), ()> {
// Initialize secure RNG
let mut rng = OsRng;
// Accumulate the value commitment randomness in the context
{
let mut tmp = rcv;
tmp.add_assign(&self.bsk);
// Update the context
self.bsk = tmp;
}
// Construct the value commitment
let value_commitment = asset_type.value_commitment(value, rcv);
// Construct the viewing key
let viewing_key = proof_generation_key.to_viewing_key();
// Construct the payment address with the viewing key / diversifier
let payment_address = viewing_key.to_payment_address(diversifier).ok_or(())?;
// This is the result of the re-randomization, we compute it for the caller
let rk = PublicKey(proof_generation_key.ak.into()).randomize(ar, spending_key_generator());
// Let's compute the nullifier while we have the position
let note = Note {
asset_type,
value,
g_d: diversifier.g_d().expect("was a valid diversifier before"),
pk_d: *payment_address.pk_d(),
rseed,
};
let nullifier = note.nf(&viewing_key.nk, merkle_path.position);
// We now have the full witness for our circuit
let instance = Spend {
value_commitment: Some(value_commitment.clone()),
proof_generation_key: Some(proof_generation_key),
payment_address: Some(payment_address),
commitment_randomness: Some(note.rcm()),
ar: Some(ar),
auth_path: merkle_path
.auth_path
.iter()
.map(|(node, b)| Some(((*node).into(), *b)))
.collect(),
anchor: Some(anchor),
};
// Create proof
let proof =
create_random_proof(instance, proving_key, &mut rng).expect("proving should not fail");
// Try to verify the proof:
// Construct public input for circuit
let mut public_input = [bls12_381::Scalar::ZERO; 7];
{
let affine = rk.0.to_affine();
let (u, v) = (affine.get_u(), affine.get_v());
public_input[0] = u;
public_input[1] = v;
}
{
let affine = jubjub::ExtendedPoint::from(value_commitment.commitment()).to_affine();
let (u, v) = (affine.get_u(), affine.get_v());
public_input[2] = u;
public_input[3] = v;
}
public_input[4] = anchor;
// Add the nullifier through multiscalar packing
{
let nullifier = multipack::bytes_to_bits_le(&nullifier.0);
let nullifier = multipack::compute_multipacking(&nullifier);
assert_eq!(nullifier.len(), 2);
public_input[5] = nullifier[0];
public_input[6] = nullifier[1];
}
// Verify the proof
verify_proof(verifying_key, &proof, &public_input[..]).map_err(|_| ())?;
// Compute value commitment
let value_commitment: jubjub::ExtendedPoint = value_commitment.commitment().into();
// Accumulate the value commitment in the context
self.cv_sum += value_commitment;
Ok((proof, value_commitment, rk))
}
/// Create the value commitment and proof for a Sapling OutputDescription,
/// while accumulating its value commitment randomness inside the context
/// for later use.
#[allow(clippy::too_many_arguments)]
pub fn output_proof(
&mut self,
esk: jubjub::Fr,
payment_address: PaymentAddress,
rcm: jubjub::Fr,
asset_type: AssetType,
value: u64,
proving_key: &Parameters<Bls12>,
rcv: jubjub::Fr,
) -> (Proof<Bls12>, jubjub::ExtendedPoint) {
// Initialize secure RNG
let mut rng = OsRng;
// Accumulate the value commitment randomness in the context
{
let mut tmp = rcv.neg(); // Outputs subtract from the total.
tmp.add_assign(&self.bsk);
// Update the context
self.bsk = tmp;
}
// Construct the value commitment for the proof instance
let value_commitment = asset_type.value_commitment(value, rcv);
// Compute the actual value commitment
let value_commitment_point: jubjub::ExtendedPoint = value_commitment.commitment().into();
// We now have a full witness for the output proof.
let instance = Output {
value_commitment: Some(value_commitment),
payment_address: Some(payment_address),
commitment_randomness: Some(rcm),
esk: Some(esk),
asset_identifier: asset_type.identifier_bits(),
};
// Create proof
let proof =
create_random_proof(instance, proving_key, &mut rng).expect("proving should not fail");
// Accumulate the value commitment in the context. We do this to check internal consistency.
self.cv_sum -= value_commitment_point; // Outputs subtract from the total.
(proof, value_commitment_point)
}
/// Create the value commitment and proof for a ConvertDescription,
/// while accumulating its value commitment randomness inside the context
/// for later use.
#[allow(clippy::too_many_arguments)]
pub fn convert_proof(
&mut self,
allowed_conversion: AllowedConversion,
value: u64,
anchor: bls12_381::Scalar,
merkle_path: MerklePath<Node>,
proving_key: &Parameters<Bls12>,
verifying_key: &PreparedVerifyingKey<Bls12>,
rcv: jubjub::Fr,
) -> Result<(Proof<Bls12>, jubjub::ExtendedPoint), ()> {
// Initialize secure RNG
let mut rng = OsRng;
// Accumulate the value commitment randomness in the context
{
let mut tmp = rcv;
tmp.add_assign(&self.bsk);
// Update the context
self.bsk = tmp;
}
// Construct the value commitment
let value_commitment = allowed_conversion.value_commitment(value, rcv);
// We now have the full witness for our circuit
let instance = Convert {
value_commitment: Some(value_commitment.clone()),
auth_path: merkle_path
.auth_path
.iter()
.map(|(node, b)| Some(((*node).into(), *b)))
.collect(),
anchor: Some(anchor),
};
// Create proof
let proof =
create_random_proof(instance, proving_key, &mut rng).expect("proving should not fail");
// Try to verify the proof:
// Construct public input for circuit
let mut public_input = [bls12_381::Scalar::ZERO; 3];
{
let affine = jubjub::ExtendedPoint::from(value_commitment.commitment()).to_affine();
let (u, v) = (affine.get_u(), affine.get_v());
public_input[0] = u;
public_input[1] = v;
}
public_input[2] = anchor;
// Verify the proof
verify_proof(verifying_key, &proof, &public_input[..]).map_err(|_| ())?;
// Compute value commitment
let value_commitment: jubjub::ExtendedPoint = value_commitment.commitment().into();
// Accumulate the value commitment in the context
self.cv_sum += value_commitment;
Ok((proof, value_commitment))
}
/// Create the bindingSig for a Sapling transaction. All calls to spend_proof()
/// and output_proof() must be completed before calling this function.
pub fn binding_sig(
&self,
assets_and_values: &I128Sum,
sighash: &[u8; 32],
) -> Result<Signature, ()> {
// Initialize secure RNG
let mut rng = OsRng;
// Grab the current `bsk` from the context
let bsk = PrivateKey(self.bsk);
// Grab the `bvk` using DerivePublic.
let bvk = PublicKey::from_private(&bsk, value_commitment_randomness_generator());
// In order to check internal consistency, let's use the accumulated value
// commitments (as the verifier would) and apply value_balance to compare
// against our derived bvk.
{
let final_bvk = assets_and_values
.components()
.map(|(asset_type, value_balance)| {
// Compute value balance for each asset
// Error for bad value balances (-INT128_MAX value)
masp_compute_value_balance(*asset_type, *value_balance)
})
.try_fold(self.cv_sum, |tmp, value_balance| {
// Compute cv_sum minus sum of all value balances
Ok(tmp - value_balance.ok_or(())?)
})?;
// The result should be the same, unless the provided valueBalance is wrong.
if bvk.0 != final_bvk {
return Err(());
}
}
// Construct signature message
let mut data_to_be_signed = [0u8; 64];
data_to_be_signed[0..32].copy_from_slice(&bvk.0.to_bytes());
data_to_be_signed[32..64].copy_from_slice(&sighash[..]);
// Sign
Ok(bsk.sign(
&data_to_be_signed,
&mut rng,
value_commitment_randomness_generator(),
))
}
}