phoenix_core/
note.rs

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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::convert::{TryFrom, TryInto};

use dusk_bls12_381::BlsScalar;
use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use dusk_jubjub::{dhke, JubJubAffine, JubJubScalar, GENERATOR_NUMS_EXTENDED};
use dusk_poseidon::{Domain, Hash};
use ff::Field;
use jubjub_elgamal as elgamal;
use jubjub_schnorr::{PublicKey as NotePublicKey, SecretKey as NoteSecretKey};
use rand::{CryptoRng, RngCore};

use crate::{
    aes, transparent_value_commitment, value_commitment, Error, PublicKey,
    SecretKey, StealthAddress, ViewKey,
};

#[cfg(feature = "rkyv-impl")]
use rkyv::{Archive, Deserialize, Serialize};

/// Blinder used for transparent notes.
pub(crate) const TRANSPARENT_BLINDER: JubJubScalar = JubJubScalar::zero();

/// Size of the Phoenix notes plaintext: value (8 bytes) + blinder (32 bytes)
pub(crate) const PLAINTEXT_SIZE: usize = 40;

/// Size of the Phoenix notes value_enc
pub const VALUE_ENC_SIZE: usize = PLAINTEXT_SIZE + aes::ENCRYPTION_EXTRA_SIZE;

/// The types of a Note
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(
    feature = "rkyv-impl",
    derive(Archive, Serialize, Deserialize),
    archive_attr(derive(bytecheck::CheckBytes))
)]
pub enum NoteType {
    /// Defines a Transparent type of Note
    Transparent = 0,
    /// Defines an Obfuscated type of Note
    Obfuscated = 1,
}

impl TryFrom<u8> for NoteType {
    type Error = Error;

    fn try_from(note_type: u8) -> Result<Self, Self::Error> {
        match note_type {
            0 => Ok(NoteType::Transparent),
            1 => Ok(NoteType::Obfuscated),
            n => Err(Error::InvalidNoteType(n)),
        }
    }
}

impl TryFrom<i32> for NoteType {
    type Error = Error;

    fn try_from(note_type: i32) -> Result<Self, Self::Error> {
        (note_type as u8).try_into()
    }
}

/// Phoenix Note struct
#[derive(Clone, Debug, Eq)]
#[cfg_attr(
    feature = "rkyv-impl",
    derive(Archive, Serialize, Deserialize),
    archive_attr(derive(bytecheck::CheckBytes))
)]
pub struct Note {
    pub(crate) note_type: NoteType,
    pub(crate) value_commitment: JubJubAffine,
    pub(crate) stealth_address: StealthAddress,
    pub(crate) pos: u64,
    pub(crate) value_enc: [u8; VALUE_ENC_SIZE],
    pub(crate) sender: Sender,
}

impl PartialEq for Note {
    fn eq(&self, other: &Self) -> bool {
        self.value_enc == other.value_enc
            && self.sender == other.sender
            && self.hash() == other.hash()
    }
}

impl Note {
    /// Creates a new phoenix output note
    pub fn new<R: RngCore + CryptoRng>(
        rng: &mut R,
        note_type: NoteType,
        sender_pk: &PublicKey,
        receiver_pk: &PublicKey,
        value: u64,
        value_blinder: JubJubScalar,
        sender_blinder: [JubJubScalar; 2],
    ) -> Self {
        let r = JubJubScalar::random(&mut *rng);
        let stealth_address = receiver_pk.gen_stealth_address(&r);

        let value_commitment = value_commitment(value, value_blinder);

        // Output notes have undefined position, equals to u64's MAX value
        let pos = u64::MAX;

        let value_enc = match note_type {
            NoteType::Transparent => {
                let mut value_enc = [0u8; VALUE_ENC_SIZE];
                value_enc[..u64::SIZE].copy_from_slice(&value.to_bytes());

                value_enc
            }
            NoteType::Obfuscated => {
                let shared_secret = dhke(&r, receiver_pk.A());
                let value_blinder = BlsScalar::from(value_blinder);

                let mut plaintext = value.to_bytes().to_vec();
                plaintext.append(&mut value_blinder.to_bytes().to_vec());

                let salt = stealth_address.to_bytes();

                aes::encrypt(&shared_secret, &salt, &plaintext, rng)
                    .expect("Encrypted correctly.")
            }
        };

        Note {
            note_type,
            value_commitment,
            stealth_address,
            pos,
            value_enc,
            sender: Sender::encrypt(
                stealth_address.note_pk(),
                sender_pk,
                &sender_blinder,
            ),
        }
    }

    /// Creates a new transparent note
    ///
    /// The blinding factor will be constant zero since the value commitment
    /// exists only to shield the value. The value is not hidden for transparent
    /// notes, so this can be trivially treated as a constant.
    pub fn transparent<R: RngCore + CryptoRng>(
        rng: &mut R,
        sender_pk: &PublicKey,
        receiver_pk: &PublicKey,
        value: u64,
        sender_blinder: [JubJubScalar; 2],
    ) -> Self {
        Self::new(
            rng,
            NoteType::Transparent,
            sender_pk,
            receiver_pk,
            value,
            TRANSPARENT_BLINDER,
            sender_blinder,
        )
    }

    /// Creates a new transparent note
    ///
    /// This is equivalent to [`transparent`] but taking only a stealth address
    /// and a value. This is done to be able to generate a note
    /// directly for a stealth address, as opposed to a public key.
    pub fn transparent_stealth(
        stealth_address: StealthAddress,
        value: u64,
        sender: impl Into<Sender>,
    ) -> Self {
        let value_commitment = transparent_value_commitment(value);

        let pos = u64::MAX;

        let mut value_enc = [0u8; VALUE_ENC_SIZE];
        value_enc[..u64::SIZE].copy_from_slice(&value.to_bytes());

        Note {
            note_type: NoteType::Transparent,
            value_commitment,
            stealth_address,
            pos,
            value_enc,
            sender: sender.into(),
        }
    }

    /// Creates a new obfuscated note
    ///
    /// The provided blinding factor will be used to calculate the value
    /// commitment of the note. The tuple (value, value_blinder), known by
    /// the caller of this function, must be later used to prove the
    /// knowledge of the value commitment of this note.
    pub fn obfuscated<R: RngCore + CryptoRng>(
        rng: &mut R,
        sender_pk: &PublicKey,
        receiver_pk: &PublicKey,
        value: u64,
        value_blinder: JubJubScalar,
        sender_blinder: [JubJubScalar; 2],
    ) -> Self {
        Self::new(
            rng,
            NoteType::Obfuscated,
            sender_pk,
            receiver_pk,
            value,
            value_blinder,
            sender_blinder,
        )
    }

    /// Creates a new empty [`Note`]
    pub fn empty() -> Self {
        Self {
            note_type: NoteType::Transparent,
            value_commitment: JubJubAffine::default(),
            stealth_address: StealthAddress::default(),
            pos: 0,
            value_enc: [0; VALUE_ENC_SIZE],
            sender: Sender::Encryption(
                [(JubJubAffine::default(), JubJubAffine::default()); 2],
            ),
        }
    }

    fn decrypt_value(
        &self,
        vk: &ViewKey,
    ) -> Result<(u64, JubJubScalar), Error> {
        let R = self.stealth_address.R();
        let shared_secret = dhke(vk.a(), R);

        let salt = self.stealth_address.to_bytes();

        let dec_plaintext: [u8; PLAINTEXT_SIZE] =
            aes::decrypt(&shared_secret, &salt, &self.value_enc)?;

        let value = u64::from_slice(&dec_plaintext[..u64::SIZE])?;

        // Converts the BLS Scalar into a JubJub Scalar.
        // If the `vk` is wrong it might fails since the resulting BLS Scalar
        // might not fit into a JubJub Scalar.
        let value_blinder =
            match JubJubScalar::from_slice(&dec_plaintext[u64::SIZE..])?.into()
            {
                Some(scalar) => scalar,
                None => return Err(Error::InvalidData),
            };

        Ok((value, value_blinder))
    }

    /// Create a unique nullifier for the note
    ///
    /// This nullifier is represeted as `H(note_sk ยท G', pos)`
    pub fn gen_nullifier(&self, sk: &SecretKey) -> BlsScalar {
        let note_sk = sk.gen_note_sk(&self.stealth_address);
        let pk_prime = GENERATOR_NUMS_EXTENDED * note_sk.as_ref();
        let pk_prime = pk_prime.to_hash_inputs();

        let pos = BlsScalar::from(self.pos);

        Hash::digest(Domain::Other, &[pk_prime[0], pk_prime[1], pos])[0]
    }

    /// Return the internal representation of scalars to be hashed
    pub fn hash_inputs(&self) -> [BlsScalar; 6] {
        let note_pk =
            self.stealth_address().note_pk().as_ref().to_hash_inputs();

        [
            BlsScalar::from(self.note_type as u64),
            self.value_commitment.get_u(),
            self.value_commitment.get_v(),
            note_pk[0],
            note_pk[1],
            BlsScalar::from(self.pos),
        ]
    }

    /// Return a hash represented by `H(note_type, value_commitment,
    /// H(StealthAddress), pos, encrypted_data)
    pub fn hash(&self) -> BlsScalar {
        Hash::digest(Domain::Other, &self.hash_inputs())[0]
    }

    /// Return the type of the note
    pub const fn note_type(&self) -> NoteType {
        self.note_type
    }

    /// Return the position of the note on the tree.
    pub const fn pos(&self) -> &u64 {
        &self.pos
    }

    /// Returns the the stealth address associated with the note.
    pub const fn stealth_address(&self) -> &StealthAddress {
        &self.stealth_address
    }

    /// Set the position of the note on the tree.
    /// This, naturally, won't reflect immediatelly on the data storage
    pub fn set_pos(&mut self, pos: u64) {
        self.pos = pos;
    }

    /// Return the value commitment `H(value, value_blinder)`
    pub const fn value_commitment(&self) -> &JubJubAffine {
        &self.value_commitment
    }

    /// Returns the cipher of the encrypted data
    pub const fn value_enc(&self) -> &[u8; VALUE_ENC_SIZE] {
        &self.value_enc
    }

    /// Returns elgamal encryption of the sender's [`PublicKey`] encrypted using
    /// the [`StealthAddress::note_pk`] so only the receiver of the [`Note`]
    /// can decrypt.
    pub const fn sender(&self) -> &Sender {
        &self.sender
    }

    /// Attempt to decrypt the note value provided a [`ViewKey`]. Always
    /// succeeds for transparent notes, might fails or return random values for
    /// obfuscated notes if the provided view key is wrong.
    pub fn value(&self, vk: Option<&ViewKey>) -> Result<u64, Error> {
        match (self.note_type, vk) {
            (NoteType::Transparent, _) => {
                let value =
                    u64::from_slice(&self.value_enc[..u64::SIZE]).unwrap();
                Ok(value)
            }
            (NoteType::Obfuscated, Some(vk)) => {
                self.decrypt_value(vk).map(|(value, _)| value)
            }
            _ => Err(Error::MissingViewKey),
        }
    }

    /// Decrypt the blinding factor with the provided [`ViewKey`]
    ///
    /// If the decrypt fails, a random value is returned
    pub fn value_blinder(
        &self,
        vk: Option<&ViewKey>,
    ) -> Result<JubJubScalar, Error> {
        match (self.note_type, vk) {
            (NoteType::Transparent, _) => Ok(TRANSPARENT_BLINDER),
            (NoteType::Obfuscated, Some(vk)) => self
                .decrypt_value(vk)
                .map(|(_, value_blinder)| value_blinder),
            _ => Err(Error::MissingViewKey),
        }
    }
}

const SIZE: usize = 1
    + JubJubAffine::SIZE
    + StealthAddress::SIZE
    + u64::SIZE
    + VALUE_ENC_SIZE
    + Sender::SIZE;

impl Serializable<SIZE> for Note {
    type Error = BytesError;

    /// Converts a Note into a byte representation
    fn to_bytes(&self) -> [u8; Self::SIZE] {
        let mut buf = [0u8; Self::SIZE];

        buf[0] = self.note_type as u8;

        let mut start = 1;

        buf[start..start + JubJubAffine::SIZE]
            .copy_from_slice(&self.value_commitment.to_bytes());
        start += JubJubAffine::SIZE;

        buf[start..start + StealthAddress::SIZE]
            .copy_from_slice(&self.stealth_address.to_bytes());
        start += StealthAddress::SIZE;

        buf[start..start + u64::SIZE].copy_from_slice(&self.pos.to_le_bytes());
        start += u64::SIZE;

        buf[start..start + VALUE_ENC_SIZE].copy_from_slice(&self.value_enc);
        start += VALUE_ENC_SIZE;

        buf[start..start + Sender::SIZE]
            .copy_from_slice(&self.sender.to_bytes());

        buf
    }

    /// Attempts to convert a byte representation of a note into a `Note`,
    /// failing if the input is invalid
    fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
        let note_type =
            bytes[0].try_into().map_err(|_| BytesError::InvalidData)?;

        let mut buf = &bytes[1..];

        let value_commitment = JubJubAffine::from_reader(&mut buf)?;

        let stealth_address = StealthAddress::from_reader(&mut buf)?;

        let pos = u64::from_reader(&mut buf)?;

        let mut value_enc = [0u8; VALUE_ENC_SIZE];
        value_enc.copy_from_slice(&buf[..VALUE_ENC_SIZE]);
        buf = &buf[VALUE_ENC_SIZE..];

        let sender = Sender::from_reader(&mut buf)?;

        Ok(Note {
            note_type,
            value_commitment,
            stealth_address,
            pos,
            value_enc,
            sender,
        })
    }
}

/// The sender of the `Note`.
/// This can be either the encrypted sender's [`PublicKey`], if the [`Note`] was
/// created as an output note of a phoenix-transaction, or some contract-data if
/// the [`Note`] was created in another way, e.g. by withdrawing from a
/// contract.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "rkyv-impl",
    derive(Archive, Serialize, Deserialize),
    archive_attr(derive(bytecheck::CheckBytes))
)]
pub enum Sender {
    /// The sender's [`PublicKey`], encrypted using the note_pk of the
    /// stealth-address.
    Encryption([(JubJubAffine, JubJubAffine); 2]),
    /// Information to identify the origin of a `Note`, if it wasn't created as
    /// a phoenix-transaction output-note.
    ContractInfo([u8; 4 * JubJubAffine::SIZE]),
}

impl Sender {
    /// Create a new [`Sender`] enum by encrypting the sender's [`PublicKey`] in
    /// a way that only the receiver of the note can decrypt.
    pub fn encrypt(
        note_pk: &NotePublicKey,
        sender_pk: &PublicKey,
        blinder: &[JubJubScalar; 2],
    ) -> Self {
        let sender_enc_A =
            elgamal::encrypt(note_pk.as_ref(), sender_pk.A(), &blinder[0]);

        let sender_enc_B =
            elgamal::encrypt(note_pk.as_ref(), sender_pk.B(), &blinder[1]);

        let sender_enc_A: (JubJubAffine, JubJubAffine) =
            (sender_enc_A.0.into(), sender_enc_A.1.into());
        let sender_enc_B: (JubJubAffine, JubJubAffine) =
            (sender_enc_B.0.into(), sender_enc_B.1.into());

        Self::Encryption([sender_enc_A, sender_enc_B])
    }

    /// Decrypts the [`PublicKey`] of the sender of the [`Note`], using the
    /// [`NoteSecretKey`] generated by the receiver's [`SecretKey`] and the
    /// [`StealthAddress`] of the [`Note`].
    ///
    /// Note: Decryption with an *incorrect* [`NoteSecretKey`] will still yield
    /// a [`PublicKey`], but in this case, the public-key will be a random one
    /// that has nothing to do with the sender's [`PublicKey`].
    ///
    /// Returns an error if the sender is of type [`Sender::ContractInfo`].
    pub fn decrypt(&self, note_sk: &NoteSecretKey) -> Result<PublicKey, Error> {
        let sender_enc = match self {
            Sender::Encryption(enc) => enc,
            Sender::ContractInfo(_) => {
                return Err(Error::InvalidEncryption);
            }
        };

        let sender_enc_A = sender_enc[0];
        let sender_enc_B = sender_enc[1];

        let decrypt_A = elgamal::decrypt(
            note_sk.as_ref(),
            &(sender_enc_A.0.into(), sender_enc_A.1.into()),
        );
        let decrypt_B = elgamal::decrypt(
            note_sk.as_ref(),
            &(sender_enc_B.0.into(), sender_enc_B.1.into()),
        );

        Ok(PublicKey::new(decrypt_A, decrypt_B))
    }
}

impl Serializable<{ 1 + 4 * JubJubAffine::SIZE }> for Sender {
    type Error = BytesError;

    /// Converts a Note into a byte representation
    fn to_bytes(&self) -> [u8; Self::SIZE] {
        let mut buf = [0u8; Self::SIZE];

        match self {
            Sender::Encryption(sender_enc) => {
                buf[0] = 0;
                let mut start = 1;

                buf[start..start + JubJubAffine::SIZE]
                    .copy_from_slice(&sender_enc[0].0.to_bytes());
                start += JubJubAffine::SIZE;

                buf[start..start + JubJubAffine::SIZE]
                    .copy_from_slice(&sender_enc[0].1.to_bytes());
                start += JubJubAffine::SIZE;

                buf[start..start + JubJubAffine::SIZE]
                    .copy_from_slice(&sender_enc[1].0.to_bytes());
                start += JubJubAffine::SIZE;

                buf[start..start + JubJubAffine::SIZE]
                    .copy_from_slice(&sender_enc[1].1.to_bytes());
            }
            Sender::ContractInfo(contract_data) => {
                buf[0] = 1;
                buf[1..].copy_from_slice(&contract_data[..]);
            }
        }

        buf
    }

    /// Attempts to convert a byte representation of a note into a `Note`,
    /// failing if the input is invalid
    fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
        let sender = match bytes[0] {
            0 => {
                let mut buf = &bytes[1..];
                let sender_enc_A_0 = JubJubAffine::from_reader(&mut buf)?;
                let sender_enc_A_1 = JubJubAffine::from_reader(&mut buf)?;
                let sender_enc_B_0 = JubJubAffine::from_reader(&mut buf)?;
                let sender_enc_B_1 = JubJubAffine::from_reader(&mut buf)?;
                Sender::Encryption([
                    (sender_enc_A_0, sender_enc_A_1),
                    (sender_enc_B_0, sender_enc_B_1),
                ])
            }
            1 => {
                let mut contract_data = [0u8; 4 * JubJubAffine::SIZE];
                contract_data.copy_from_slice(&bytes[1..Self::SIZE]);
                Sender::ContractInfo(contract_data)
            }
            _ => return Err(BytesError::InvalidData),
        };

        Ok(sender)
    }
}