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
use core::convert::{TryFrom, TryInto};
use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use dusk_jubjub::{dhke, GENERATOR_EXTENDED, GENERATOR_NUMS_EXTENDED};
use dusk_pki::{
Ownable, PublicSpendKey, SecretSpendKey, StealthAddress, ViewKey,
};
use dusk_poseidon::cipher::PoseidonCipher;
use dusk_poseidon::sponge::hash;
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "canon")]
use canonical::Canon;
#[cfg(feature = "canon")]
use canonical_derive::Canon;
use crate::{BlsScalar, Error, JubJubAffine, JubJubExtended, JubJubScalar};
pub(crate) const TRANSPARENT_BLINDER: JubJubScalar = JubJubScalar::zero();
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "canon", derive(Canon))]
pub enum NoteType {
Transparent = 0,
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()
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "canon", derive(Canon))]
pub struct Note {
pub(crate) note_type: NoteType,
pub(crate) value_commitment: JubJubExtended,
pub(crate) nonce: BlsScalar,
pub(crate) stealth_address: StealthAddress,
pub(crate) pos: u64,
pub(crate) encrypted_data: PoseidonCipher,
}
impl PartialEq for Note {
fn eq(&self, other: &Self) -> bool {
self.hash() == other.hash()
}
}
impl Eq for Note {}
impl Note {
pub fn new<R: RngCore + CryptoRng>(
rng: &mut R,
note_type: NoteType,
psk: &PublicSpendKey,
value: u64,
blinding_factor: JubJubScalar,
) -> Self {
let r = JubJubScalar::random(rng);
let nonce = BlsScalar::random(rng);
Self::deterministic(note_type, &r, nonce, psk, value, blinding_factor)
}
pub fn transparent<R: RngCore + CryptoRng>(
rng: &mut R,
psk: &PublicSpendKey,
value: u64,
) -> Self {
Self::new(rng, NoteType::Transparent, psk, value, TRANSPARENT_BLINDER)
}
pub fn obfuscated<R: RngCore + CryptoRng>(
rng: &mut R,
psk: &PublicSpendKey,
value: u64,
blinding_factor: JubJubScalar,
) -> Self {
Self::new(rng, NoteType::Obfuscated, psk, value, blinding_factor)
}
pub fn deterministic(
note_type: NoteType,
r: &JubJubScalar,
nonce: BlsScalar,
psk: &PublicSpendKey,
value: u64,
blinding_factor: JubJubScalar,
) -> Self {
let stealth_address = psk.gen_stealth_address(r);
let value_commitment = JubJubScalar::from(value);
let value_commitment = (GENERATOR_EXTENDED * value_commitment)
+ (GENERATOR_NUMS_EXTENDED * blinding_factor);
let pos = u64::MAX;
let encrypted_data = match note_type {
NoteType::Transparent => {
let zero = TRANSPARENT_BLINDER.into();
let mut encrypted_data = [zero; PoseidonCipher::cipher_size()];
encrypted_data[0] = BlsScalar::from(value);
PoseidonCipher::new(encrypted_data)
}
NoteType::Obfuscated => {
let shared_secret = dhke(r, psk.A());
let nonce = BlsScalar::from(nonce);
let value = BlsScalar::from(value);
let blinding_factor = BlsScalar::from(blinding_factor);
PoseidonCipher::encrypt(
&[value, blinding_factor],
&shared_secret,
&nonce,
)
}
};
Note {
note_type,
value_commitment,
nonce,
stealth_address,
pos,
encrypted_data,
}
}
fn decrypt_data(
&self,
vk: &ViewKey,
) -> Result<(u64, JubJubScalar), BytesError> {
let R = self.stealth_address.R();
let shared_secret = dhke(vk.a(), R);
let data = self
.encrypted_data
.decrypt(&shared_secret, &self.nonce)
.map_err(|_| BytesError::InvalidData)?;
let value = data[0].reduce();
let value = value.0[0];
let blinding_factor = JubJubScalar::from_bytes(&data[1].to_bytes())?;
Ok((value, blinding_factor))
}
pub fn gen_nullifier(&self, sk: &SecretSpendKey) -> BlsScalar {
let sk_r = sk.sk_r(&self.stealth_address);
let sk_r = BlsScalar::from(*sk_r.as_ref());
let pos = BlsScalar::from(self.pos);
hash(&[sk_r, pos])
}
pub fn hash_inputs(&self) -> [BlsScalar; 12] {
let value_commitment = self.value_commitment().to_hash_inputs();
let pk_r = self.stealth_address().pk_r().as_ref().to_hash_inputs();
let R = self.stealth_address().R().to_hash_inputs();
let cipher = self.encrypted_data.cipher();
[
BlsScalar::from(self.note_type as u64),
value_commitment[0],
value_commitment[1],
self.nonce,
pk_r[0],
pk_r[1],
R[0],
R[1],
BlsScalar::from(self.pos),
cipher[0],
cipher[1],
cipher[2],
]
}
pub fn hash(&self) -> BlsScalar {
hash(&self.hash_inputs())
}
pub const fn note(&self) -> NoteType {
self.note_type
}
pub const fn pos(&self) -> &u64 {
&self.pos
}
pub fn set_pos(&mut self, pos: u64) {
self.pos = pos;
}
pub const fn nonce(&self) -> &BlsScalar {
&self.nonce
}
pub const fn value_commitment(&self) -> &JubJubExtended {
&self.value_commitment
}
pub const fn cipher(&self) -> &[BlsScalar; PoseidonCipher::cipher_size()] {
self.encrypted_data.cipher()
}
pub fn value(&self, vk: Option<&ViewKey>) -> Result<u64, Error> {
match (self.note_type, vk) {
(NoteType::Transparent, _) => {
let value = self.encrypted_data.cipher();
let value = value[0].reduce();
Ok(value.0[0])
}
(NoteType::Obfuscated, Some(vk)) => self
.decrypt_data(vk)
.map(|(value, _)| value)
.map_err(|_| Error::InvalidCipher),
_ => Err(Error::MissingViewKey),
}
}
pub fn blinding_factor(
&self,
vk: Option<&ViewKey>,
) -> Result<JubJubScalar, Error> {
match (self.note_type, vk) {
(NoteType::Transparent, _) => Ok(TRANSPARENT_BLINDER),
(NoteType::Obfuscated, Some(vk)) => self
.decrypt_data(vk)
.map(|(_, blinding_factor)| blinding_factor)
.map_err(|_| Error::InvalidCipher),
_ => Err(Error::MissingViewKey),
}
}
}
impl Ownable for Note {
fn stealth_address(&self) -> &StealthAddress {
&self.stealth_address
}
}
impl Serializable<{ 137 + PoseidonCipher::SIZE }> for Note {
type Error = BytesError;
fn to_bytes(&self) -> [u8; Self::SIZE] {
let mut buf = [0u8; Self::SIZE];
buf[0] = self.note_type as u8;
buf[1..33].copy_from_slice(
&JubJubAffine::from(&self.value_commitment).to_bytes(),
);
buf[33..65].copy_from_slice(&self.nonce.to_bytes());
buf[65..129].copy_from_slice(&self.stealth_address.to_bytes());
buf[129..137].copy_from_slice(&self.pos.to_le_bytes());
buf[137..].copy_from_slice(&self.encrypted_data.to_bytes());
buf
}
fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
let mut one_u64 = [0u8; 8];
let note_type =
bytes[0].try_into().map_err(|_| BytesError::InvalidData)?;
let value_commitment =
JubJubExtended::from(JubJubAffine::from_slice(&bytes[1..33])?);
let nonce = BlsScalar::from_slice(&bytes[33..65])?;
let stealth_address = StealthAddress::from_slice(&bytes[65..129])?;
one_u64.copy_from_slice(&bytes[129..137]);
let pos = u64::from_le_bytes(one_u64);
let encrypted_data = PoseidonCipher::from_slice(&bytes[137..])?;
Ok(Note {
note_type,
value_commitment,
nonce,
stealth_address,
pos,
encrypted_data,
})
}
}