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
use crate::anon_xfr::{commit, decrypt_memo};
use crate::keys::{KeyPair, PublicKey, SecretKey};
use crate::xfr::structs::AssetType;
use noah_algebra::{bls12_381::BLSScalar, prelude::*};
use noah_plonk::plonk::constraint_system::VarIndex;
use serde::Serialize;
use wasm_bindgen::prelude::*;
pub type Nullifier = BLSScalar;
pub type Commitment = BLSScalar;
pub type BlindFactor = BLSScalar;
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MTNode {
pub left: BLSScalar,
pub mid: BLSScalar,
pub right: BLSScalar,
pub is_left_child: u8,
pub is_mid_child: u8,
pub is_right_child: u8,
}
#[wasm_bindgen]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnonAssetRecord {
pub commitment: BLSScalar,
}
impl AnonAssetRecord {
pub fn from_oabar(oabar: &OpenAnonAssetRecord) -> AnonAssetRecord {
let (commitment, _) = commit(
oabar.pub_key_ref(),
oabar.get_blind(),
oabar.get_amount(),
oabar.get_asset_type().as_scalar(),
)
.unwrap();
AnonAssetRecord { commitment }
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MTLeafInfo {
pub path: MTPath,
pub root: BLSScalar,
pub root_version: u64,
pub uid: u64,
}
impl Default for MTLeafInfo {
fn default() -> Self {
MTLeafInfo {
path: MTPath::new(vec![]),
root: BLSScalar::zero(),
root_version: 0,
uid: 0,
}
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct OpenAnonAssetRecord {
pub(crate) amount: u64,
pub(crate) asset_type: AssetType,
pub(crate) blind: BLSScalar,
pub(crate) pub_key: PublicKey,
pub(crate) owner_memo: Option<AxfrOwnerMemo>,
pub(crate) mt_leaf_info: Option<MTLeafInfo>,
}
impl Default for OpenAnonAssetRecord {
fn default() -> Self {
Self {
amount: 0,
asset_type: AssetType::default(),
blind: BLSScalar::default(),
pub_key: PublicKey::default_secp256k1(),
owner_memo: None,
mt_leaf_info: None,
}
}
}
impl OpenAnonAssetRecord {
pub fn update_mt_leaf_info(&mut self, mt_leat_info: MTLeafInfo) {
self.mt_leaf_info = Some(mt_leat_info);
}
}
impl OpenAnonAssetRecord {
pub fn get_amount(&self) -> u64 {
self.amount
}
pub fn get_asset_type(&self) -> AssetType {
self.asset_type
}
pub fn pub_key_ref(&self) -> &PublicKey {
&self.pub_key
}
pub fn get_blind(&self) -> BLSScalar {
self.blind
}
pub fn get_owner_memo(&self) -> Option<AxfrOwnerMemo> {
self.owner_memo.clone()
}
}
#[derive(Default)]
pub struct OpenAnonAssetRecordBuilder {
pub(crate) oabar: OpenAnonAssetRecord,
}
impl OpenAnonAssetRecordBuilder {
pub fn new() -> Self {
OpenAnonAssetRecordBuilder {
..Default::default()
}
}
pub fn amount(mut self, amount: u64) -> Self {
self.oabar.amount = amount;
self
}
pub fn asset_type(mut self, asset_type: AssetType) -> Self {
self.oabar.asset_type = asset_type;
self
}
pub fn pub_key(mut self, pub_key: &PublicKey) -> Self {
self.oabar.pub_key = pub_key.clone();
self
}
pub fn mt_leaf_info(mut self, mt_leaf_info: MTLeafInfo) -> Self {
self.oabar.update_mt_leaf_info(mt_leaf_info);
self
}
pub fn finalize<R: CryptoRng + RngCore>(mut self, prng: &mut R) -> Result<Self> {
if self.oabar.owner_memo.is_some() {
return Err(eg!(NoahError::InconsistentStructureError));
}
self.oabar.blind = BLSScalar::random(prng);
let mut msg = vec![];
msg.extend_from_slice(&self.oabar.amount.to_le_bytes());
msg.extend_from_slice(&self.oabar.asset_type.0);
msg.extend_from_slice(&self.oabar.blind.to_bytes());
self.oabar.owner_memo = Some(AxfrOwnerMemo::new(prng, &self.oabar.pub_key, &msg)?);
Ok(self)
}
pub fn build(self) -> Result<OpenAnonAssetRecord> {
self.sanity_check().c(d!())?;
Ok(self.oabar)
}
}
impl OpenAnonAssetRecordBuilder {
pub fn from_abar(
record: &AnonAssetRecord,
owner_memo: AxfrOwnerMemo,
key_pair: &KeyPair,
) -> Result<Self> {
let (amount, asset_type, blind) = decrypt_memo(&owner_memo, key_pair, record).c(d!())?;
let mut builder = OpenAnonAssetRecordBuilder::new()
.pub_key(&key_pair.get_pk())
.amount(amount)
.asset_type(asset_type);
builder.oabar.blind = blind;
builder.oabar.owner_memo = Some(owner_memo);
Ok(builder)
}
fn sanity_check(&self) -> Result<()> {
if self.oabar.pub_key == PublicKey::default_secp256k1() {
return Err(eg!(NoahError::InconsistentStructureError));
}
if self.oabar.owner_memo.is_none() {
return Err(eg!(NoahError::InconsistentStructureError));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MTPath {
pub nodes: Vec<MTNode>,
}
impl MTPath {
pub fn new(nodes: Vec<MTNode>) -> Self {
Self { nodes }
}
}
pub(crate) struct PayerWitnessVars {
pub(crate) uid: VarIndex,
pub(crate) amount: VarIndex,
pub(crate) asset_type: VarIndex,
pub(crate) path: MerklePathVars,
pub(crate) blind: VarIndex,
}
pub(crate) struct PayeeWitnessVars {
pub(crate) amount: VarIndex,
pub(crate) blind: VarIndex,
pub(crate) asset_type: VarIndex,
pub(crate) public_key_type: VarIndex,
pub(crate) public_key_scalars: [VarIndex; 3],
}
pub struct MerkleNodeVars {
pub left: VarIndex,
pub mid: VarIndex,
pub right: VarIndex,
pub is_left_child: VarIndex,
pub is_mid_child: VarIndex,
pub is_right_child: VarIndex,
}
pub struct MerklePathVars {
pub nodes: Vec<MerkleNodeVars>,
}
pub struct AccElemVars {
pub uid: VarIndex,
pub commitment: VarIndex,
}
#[derive(Debug, Clone)]
pub struct PayerWitness {
pub secret_key: SecretKey,
pub amount: u64,
pub asset_type: BLSScalar,
pub uid: u64,
pub path: MTPath,
pub blind: BlindFactor,
}
#[derive(Debug, Clone)]
pub struct PayeeWitness {
pub amount: u64,
pub blind: BlindFactor,
pub asset_type: BLSScalar,
pub public_key: PublicKey,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct AxfrOwnerMemo(Vec<u8>);
impl AxfrOwnerMemo {
pub fn new<R: CryptoRng + RngCore>(
prng: &mut R,
pub_key: &PublicKey,
msg: &[u8],
) -> Result<Self> {
let ctext = pub_key.hybrid_encrypt(prng, msg)?;
Ok(Self(ctext))
}
pub fn decrypt(&self, secret_key: &SecretKey) -> Result<Vec<u8>> {
secret_key.hybrid_decrypt(&self.0)
}
}
#[cfg(test)]
mod test {
use crate::anon_xfr::structs::PublicKey;
use crate::keys::KeyPair;
use noah_algebra::prelude::*;
#[test]
fn test_axfr_pub_key_serialization() {
let mut prng = test_rng();
let keypair: KeyPair = KeyPair::generate_secp256k1(&mut prng);
let pub_key: PublicKey = keypair.get_pk();
let bytes = pub_key.noah_to_bytes();
assert_ne!(bytes.len(), 0);
let reformed_pub_key = PublicKey::noah_from_bytes(bytes.as_slice()).unwrap();
assert_eq!(pub_key, reformed_pub_key);
}
#[test]
fn test_axfr_key_pair_serialization() {
let mut prng = test_rng();
let keypair: KeyPair = KeyPair::generate_secp256k1(&mut prng);
let bytes: Vec<u8> = keypair.noah_to_bytes();
assert_ne!(bytes.len(), 0);
let reformed_key_pair = KeyPair::noah_from_bytes(bytes.as_slice()).unwrap();
assert_eq!(keypair, reformed_key_pair);
}
}