sn_ringct 0.1.0

A pure-Rust implementation of Ring Confidential Transactions
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use sn_bulletproofs::{
    blstrs::{G1Affine, G1Projective, Scalar},
    group::ff::Field,
    group::Curve,
    group::GroupEncoding,
    merlin::Transcript,
    rand::{CryptoRng, RngCore},
    BulletproofGens, PedersenGens, RangeProof,
};
use std::collections::BTreeSet;
use tiny_keccak::{Hasher, Sha3};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{Error, MlsagMaterial, MlsagSignature, Result, RevealedCommitment};
pub(crate) const RANGE_PROOF_BITS: usize = 64; // note: Range Proof max-bits is 64. allowed are: 8, 16, 32, 64 (only)
                                               //       This limits our amount field to 64 bits also.
pub(crate) const RANGE_PROOF_PARTIES: usize = 1; // The maximum number of parties that can produce an aggregated proof
pub(crate) const MERLIN_TRANSCRIPT_LABEL: &[u8] = b"BLST_RINGCT";

/// Represents a Dbc's value.
pub type Amount = u64;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Output {
    pub public_key: G1Affine,
    pub amount: Amount,
}

impl Output {
    pub fn new<G: Into<G1Affine>>(public_key: G, amount: Amount) -> Self {
        Self {
            public_key: public_key.into(),
            amount,
        }
    }

    pub fn public_key(&self) -> G1Affine {
        self.public_key
    }

    pub fn amount(&self) -> Amount {
        self.amount
    }

    /// Generate a commitment to the input amount
    pub fn random_commitment(&self, rng: impl RngCore) -> RevealedCommitment {
        RevealedCommitment::from_value(self.amount, rng)
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
struct RevealedOutputCommitment {
    pub public_key: G1Affine,
    pub revealed_commitment: RevealedCommitment,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Default)]
pub struct RingCtMaterial {
    pub inputs: Vec<MlsagMaterial>,
    pub outputs: Vec<Output>,
}

impl RingCtMaterial {
    pub fn sign(
        &self,
        mut rng: impl RngCore + CryptoRng,
    ) -> Result<(RingCtTransaction, Vec<RevealedCommitment>)> {
        // We need to gather a bunch of things for our message to sign.
        //   All public keys in all (input) rings
        //   All key-images,
        //   All PseudoCommitments
        //   All output public keys.
        //   All output commitments
        //   All output range proofs
        //
        //   notes:
        //     1. the real pk is randomly mixed with decoys by MlsagMaterial
        //     2. output commitments, range_proofs, and public_keys are bundled
        //        together in OutputProofs
        //     3. all these must be generated in proper order. It would be nice
        //        to make RingCtMaterial deterministic by instantiating with a seed.
        let revealed_pseudo_commitments = self.revealed_pseudo_commitments(&mut rng);
        let pseudo_commitments = self.pseudo_commitments(&revealed_pseudo_commitments);
        let revealed_output_commitments =
            self.revealed_output_commitments(&revealed_pseudo_commitments, &mut rng);
        let output_proofs = self.output_range_proofs(&revealed_output_commitments, &mut rng)?;

        // Generate message to sign.
        // note: must match message generated by RingCtTransaction::verify()
        let msg = gen_message_for_signing(
            &self.public_keys(),
            &self.key_images(),
            &pseudo_commitments,
            &output_proofs,
        );

        // We create a ring signature for each input
        let mlsags: Vec<MlsagSignature> = self
            .inputs
            .iter()
            .zip(revealed_pseudo_commitments.iter())
            .map(|(m, r)| m.sign(&msg, r, &Self::pc_gens()))
            .collect();

        let revealed_output_commitments = revealed_output_commitments
            .iter()
            .map(|r| r.revealed_commitment)
            .collect::<Vec<_>>();

        Ok((
            RingCtTransaction {
                mlsags,
                outputs: output_proofs,
            },
            revealed_output_commitments,
        ))
    }

    fn bp_gens() -> BulletproofGens {
        BulletproofGens::new(RANGE_PROOF_BITS, RANGE_PROOF_PARTIES)
    }

    fn pc_gens() -> PedersenGens {
        Default::default()
    }

    pub fn public_keys(&self) -> Vec<G1Affine> {
        self.inputs.iter().flat_map(|m| m.public_keys()).collect()
    }

    pub fn key_images(&self) -> Vec<G1Affine> {
        self.inputs
            .iter()
            .map(|m| m.true_input.key_image().to_affine())
            .collect()
    }

    fn revealed_pseudo_commitments(&self, mut rng: impl RngCore) -> Vec<RevealedCommitment> {
        self.inputs
            .iter()
            .map(|m| m.true_input.random_pseudo_commitment(&mut rng))
            .collect()
    }

    fn pseudo_commitments(
        &self,
        revealed_pseudo_commitments: &[RevealedCommitment],
    ) -> Vec<G1Affine> {
        revealed_pseudo_commitments
            .iter()
            .map(|r| r.commit(&Self::pc_gens()).to_affine())
            .collect()
    }

    fn revealed_output_commitments(
        &self,
        revealed_pseudo_commitments: &[RevealedCommitment],
        mut rng: impl RngCore,
    ) -> Vec<RevealedOutputCommitment> {
        // avoid subtraction underflow in next step.
        if self.outputs.is_empty() {
            return vec![];
        }

        let mut revealed_output_commitments: Vec<RevealedOutputCommitment> = self
            .outputs
            .iter()
            .map(|out| RevealedOutputCommitment {
                public_key: out.public_key,
                revealed_commitment: out.random_commitment(&mut rng),
            })
            .take(self.outputs.len() - 1)
            .collect();

        // todo: replace fold() with sum() when supported in blstrs
        let input_sum: Scalar = revealed_pseudo_commitments
            .iter()
            .map(RevealedCommitment::blinding)
            .fold(Scalar::zero(), |sum, x| sum + x);

        // todo: replace fold() with sum() when supported in blstrs
        let output_sum: Scalar = revealed_output_commitments
            .iter()
            .map(|r| r.revealed_commitment.blinding())
            .fold(Scalar::zero(), |sum, x| sum + x);

        let output_blinding_correction = input_sum - output_sum;

        if let Some(last_output) = self.outputs.last() {
            revealed_output_commitments.push(RevealedOutputCommitment {
                public_key: last_output.public_key,
                revealed_commitment: RevealedCommitment {
                    value: last_output.amount,
                    blinding: output_blinding_correction,
                },
            });
        } else {
            panic!("Expected at least one output")
        }
        revealed_output_commitments
    }

    fn output_range_proofs(
        &self,
        revealed_output_commitments: &[RevealedOutputCommitment],
        mut rng: impl RngCore + CryptoRng,
    ) -> Result<Vec<OutputProof>> {
        let mut prover_ts = Transcript::new(MERLIN_TRANSCRIPT_LABEL);

        let bp_gens = Self::bp_gens();

        revealed_output_commitments
            .iter()
            .map(|c| {
                let (range_proof, commitment) = RangeProof::prove_single_with_rng(
                    &bp_gens,
                    &Self::pc_gens(),
                    &mut prover_ts,
                    c.revealed_commitment.value,
                    &c.revealed_commitment.blinding,
                    RANGE_PROOF_BITS,
                    &mut rng,
                )?;

                Ok(OutputProof {
                    public_key: c.public_key,
                    range_proof,
                    commitment,
                })
            })
            .collect::<Result<Vec<_>>>()
    }
}

// note: used by both RingCtMaterial::sign and RingCtTransaction::verify()
//       which must match.
fn gen_message_for_signing(
    public_keys: &[G1Affine],
    key_images: &[G1Affine],
    pseudo_commitments: &[G1Affine],
    output_proofs: &[OutputProof],
) -> Vec<u8> {
    // Generate message to sign.
    let mut msg: Vec<u8> = Default::default();
    for pk in public_keys.iter() {
        msg.extend(pk.to_bytes().as_ref());
    }
    for t in key_images.iter() {
        msg.extend(t.to_bytes().as_ref());
    }
    for r in pseudo_commitments.iter() {
        msg.extend(r.to_bytes().as_ref());
    }
    for o in output_proofs.iter() {
        msg.extend(o.to_bytes());
    }
    msg
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct OutputProof {
    public_key: G1Affine,
    range_proof: RangeProof,
    commitment: G1Affine,
}

impl OutputProof {
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut v: Vec<u8> = Default::default();
        v.extend(self.public_key.to_bytes().as_ref());
        v.extend(&self.range_proof.to_bytes());
        v.extend(self.commitment.to_bytes().as_ref());
        v
    }

    pub fn public_key(&self) -> &G1Affine {
        &self.public_key
    }

    pub fn range_proof(&self) -> &RangeProof {
        &self.range_proof
    }

    pub fn commitment(&self) -> G1Affine {
        self.commitment
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct RingCtTransaction {
    pub mlsags: Vec<MlsagSignature>,
    pub outputs: Vec<OutputProof>,
}

impl RingCtTransaction {
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut v: Vec<u8> = Default::default();
        for m in self.mlsags.iter() {
            v.extend(&m.to_bytes());
        }
        for o in self.outputs.iter() {
            v.extend(&o.to_bytes());
        }
        v
    }

    pub fn hash(&self) -> [u8; 32] {
        let mut sha3 = Sha3::v256();

        sha3.update(&self.to_bytes());

        let mut hash = [0; 32];
        sha3.finalize(&mut hash);
        hash
    }

    // note: must match message generated by RingCtMaterial::sign()
    pub fn gen_message(&self) -> Vec<u8> {
        // All public keys in all rings
        let public_keys: Vec<G1Affine> = self.mlsags.iter().flat_map(|m| m.public_keys()).collect();

        // All key-images (of true inputs),
        let key_images: Vec<G1Affine> = self.mlsags.iter().map(|m| m.key_image).collect();

        // All PseudoCommitments.
        let pseudo_commitments: Vec<G1Affine> =
            self.mlsags.iter().map(|m| m.pseudo_commitment()).collect();

        gen_message_for_signing(
            &public_keys,
            &key_images,
            &pseudo_commitments,
            &self.outputs,
        )
    }

    pub fn verify(&self, public_commitments_per_ring: &[Vec<G1Affine>]) -> Result<()> {
        let msg = self.gen_message();
        for (mlsag, public_commitments) in self.mlsags.iter().zip(public_commitments_per_ring) {
            mlsag.verify(&msg, public_commitments)?
        }

        let mut prover_ts = Transcript::new(MERLIN_TRANSCRIPT_LABEL);
        let bp_gens = RingCtMaterial::bp_gens();

        for output in self.outputs.iter() {
            // Verification requires a transcript with identical initial state:
            output.range_proof.verify_single(
                &bp_gens,
                &RingCtMaterial::pc_gens(),
                &mut prover_ts,
                &output.commitment,
                RANGE_PROOF_BITS,
            )?;
        }

        // Verify that the tx has at least one input
        if self.mlsags.is_empty() {
            return Err(Error::TransactionMustHaveAnInput);
        }

        // Verify that each KeyImage is unique in this tx.
        let keyimage_unique: BTreeSet<_> = self
            .mlsags
            .iter()
            .map(|m| m.key_image.to_compressed())
            .collect();
        if keyimage_unique.len() != self.mlsags.len() {
            return Err(Error::KeyImageNotUniqueAcrossInputs);
        }

        // Verify that each public_key is unique across all input mlsag
        let pk_unique: BTreeSet<_> = self
            .mlsags
            .iter()
            .flat_map(|m| {
                m.public_keys()
                    .iter()
                    .map(|pk| pk.to_compressed())
                    .collect::<Vec<[u8; 48]>>()
            })
            .collect();

        let pk_count = self.mlsags.iter().map(|m| m.public_keys().len()).sum();

        if pk_unique.len() != pk_count {
            return Err(Error::PublicKeyNotUniqueAcrossInputs);
        }

        let input_sum: G1Projective = self
            .mlsags
            .iter()
            .map(MlsagSignature::pseudo_commitment)
            .map(G1Projective::from)
            .sum();
        let output_sum: G1Projective = self
            .outputs
            .iter()
            .map(OutputProof::commitment)
            .map(G1Projective::from)
            .sum();

        if input_sum != output_sum {
            Err(Error::InputPseudoCommitmentsDoNotSumToOutputCommitments)
        } else {
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeMap, BTreeSet};

    use bulletproofs::{
        group::{ff::Field, Curve, Group},
        rand::rngs::OsRng,
    };

    use crate::{DecoyInput, MlsagMaterial, TrueInput};

    use super::*;

    #[derive(Default)]
    struct TestLedger {
        commitments: BTreeMap<[u8; 48], G1Affine>, // Compressed public keys -> Commitments
    }

    impl TestLedger {
        fn log(&mut self, public_key: impl Into<G1Affine>, commitment: impl Into<G1Affine>) {
            self.commitments
                .insert(public_key.into().to_compressed(), commitment.into());
        }

        fn lookup(&self, public_key: impl Into<G1Affine>) -> Option<G1Affine> {
            self.commitments
                .get(&public_key.into().to_compressed())
                .copied()
        }

        fn fetch_decoys(&self, n: usize, exclude: &[G1Projective]) -> Vec<DecoyInput> {
            let exclude_set = BTreeSet::from_iter(exclude.iter().map(G1Projective::to_compressed));

            self.commitments
                .iter()
                .filter(|(pk, _)| !exclude_set.contains(*pk))
                .map(|(pk, c)| DecoyInput {
                    public_key: G1Affine::from_compressed(pk).unwrap(),
                    commitment: *c,
                })
                .take(n)
                .collect()
        }
    }

    #[test]
    fn test_ringct_sign() {
        let mut rng = OsRng::default();
        let pc_gens = PedersenGens::default();

        let true_input = TrueInput {
            secret_key: Scalar::random(&mut rng),
            revealed_commitment: RevealedCommitment {
                value: 3,
                blinding: 5.into(),
            },
        };

        let mut ledger = TestLedger::default();
        ledger.log(
            true_input.public_key(),
            true_input.revealed_commitment.commit(&pc_gens),
        );
        ledger.log(
            G1Projective::random(&mut rng),
            G1Projective::random(&mut rng),
        );
        ledger.log(
            G1Projective::random(&mut rng),
            G1Projective::random(&mut rng),
        );

        let decoy_inputs = ledger.fetch_decoys(2, &[true_input.public_key()]);

        let ring_ct = RingCtMaterial {
            inputs: vec![MlsagMaterial::new(true_input, decoy_inputs, &mut rng)],
            outputs: vec![Output {
                public_key: G1Projective::random(&mut rng).to_affine(),
                amount: 3,
            }],
        };

        let (signed_tx, _revealed_output_commitments) =
            ring_ct.sign(rng).expect("Failed to sign transaction");

        let public_commitments = Vec::from_iter(signed_tx.mlsags.iter().map(|mlsag| {
            Vec::from_iter(
                mlsag
                    .public_keys()
                    .into_iter()
                    .map(|pk| ledger.lookup(pk).unwrap()),
            )
        }));

        assert!(signed_tx.verify(&public_commitments).is_ok());
    }
}