-
Notifications
You must be signed in to change notification settings - Fork 29
/
vanchor.rs
1878 lines (1684 loc) · 59.5 KB
/
vanchor.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// VAnchor is the variable deposit/withdraw/transfer shielded pool
// It supports join split transactions, meaning you can take unspent deposits
// in the pool and join them together, split them, and any combination
// of the two.
// The inputs to the VAnchor are unspent outputs we want to spend (we are
// spending the inputs), and we create outputs which are new, unspent UTXOs. We
// create commitments for each output and these are inserted into merkle trees.
// The VAnchor is also a bridged system. It takes as a public input
// a set of merkle roots that it will use to verify the membership
// of unspent deposits within. The VAnchor prevents double-spending
// through the use of a public input chain identifier `chain_id`.
// We will take inputs and do a merkle tree reconstruction for each input.
// Then we will verify that the reconstructed root from each input's
// membership path is within a set of public merkle roots.
use ark_ec::models::TEModelParameters;
use ark_ff::PrimeField;
use arkworks_native_gadgets::merkle_tree::Path;
use arkworks_plonk_gadgets::{
add_public_input_variable, add_public_input_variables, merkle_tree::PathGadget,
poseidon::FieldHasherGadget, set::SetGadget,
};
use plonk_core::{circuit::Circuit, constraint_system::StandardComposer, error::Error};
pub struct VariableAnchorCircuit<
F: PrimeField,
P: TEModelParameters<BaseField = F>,
HG: FieldHasherGadget<F, P>,
// Tree height
const N: usize,
// Size of the root set (bridge length)
const M: usize,
// Number of inputs
const INS: usize,
// Number of outputs
const OUTS: usize,
> {
// sum of input amounts + public amount == sum of output amounts
public_amount: F, // Public
public_chain_id: F, // Public
// Input transactions
in_amounts: [F; INS],
in_blindings: [F; INS],
in_nullifier_hashes: [F; INS], // Public
in_private_keys: [F; INS],
in_paths: [Path<F, HG::Native, N>; INS],
in_indices: [F; INS],
in_root_set: [F; M],
// Output transactions
out_amounts: [F; OUTS],
out_blindings: [F; OUTS],
out_chain_ids: [F; OUTS],
out_public_keys: [F; OUTS],
out_commitments: [F; OUTS], // Public
// Arbitrary data to be added to the transcript
arbitrary_data: F, // Public
// All the hashers used in this circuit
// Used for hashing private_key -- width 2
public_key_hasher: HG::Native,
// Used for hashing nodes in the tree -- width 3
tree_hasher: HG::Native,
// Used for creating leaf signature and the nullifier hash -- width 4
signature_hasher: HG::Native,
// Used for creating leaf -- width 5
leaf_hasher: HG::Native,
}
impl<F, P, HG, const N: usize, const M: usize, const INS: usize, const OUTS: usize>
VariableAnchorCircuit<F, P, HG, N, M, INS, OUTS>
where
F: PrimeField,
P: TEModelParameters<BaseField = F>,
HG: FieldHasherGadget<F, P>,
{
pub fn new(
public_amount: F,
public_chain_id: F,
in_amounts: [F; INS],
in_blindings: [F; INS],
in_nullifier_hashes: [F; INS],
in_private_keys: [F; INS],
in_paths: [Path<F, HG::Native, N>; INS],
in_indices: [F; INS],
in_root_set: [F; M],
out_amounts: [F; OUTS],
out_blindings: [F; OUTS],
out_chain_ids: [F; OUTS],
out_public_keys: [F; OUTS],
out_commitments: [F; OUTS],
arbitrary_data: F,
public_key_hasher: HG::Native,
tree_hasher: HG::Native,
signature_hasher: HG::Native,
leaf_hasher: HG::Native,
) -> Self {
Self {
public_amount,
public_chain_id,
in_amounts,
in_blindings,
in_nullifier_hashes,
in_private_keys,
in_paths,
in_indices,
in_root_set,
out_amounts,
out_blindings,
out_chain_ids,
out_public_keys,
out_commitments,
arbitrary_data,
public_key_hasher,
tree_hasher,
signature_hasher,
leaf_hasher,
}
}
}
impl<F, P, HG, const N: usize, const M: usize, const INS: usize, const OUTS: usize> Circuit<F, P>
for VariableAnchorCircuit<F, P, HG, N, M, INS, OUTS>
where
F: PrimeField,
P: TEModelParameters<BaseField = F>,
HG: FieldHasherGadget<F, P>,
{
const CIRCUIT_ID: [u8; 32] = [0xff; 32];
fn gadget(&mut self, composer: &mut StandardComposer<F, P>) -> Result<(), Error> {
// Initialize public inputs
let public_amount = add_public_input_variable(composer, self.public_amount);
let arbitrary_data = add_public_input_variable(composer, self.arbitrary_data);
// Allocate nullifier hashes
let nullifier_hash_vars =
add_public_input_variables(composer, self.in_nullifier_hashes.to_vec());
// Allocate output commitments
let commitment_vars = add_public_input_variables(composer, self.out_commitments.to_vec());
let public_chain_id = add_public_input_variable(composer, self.public_chain_id);
let set_gadget = SetGadget::from_native(composer, self.in_root_set.to_vec());
// Initialize hashers
let pk_hasher_gadget: HG =
FieldHasherGadget::<F, P>::from_native(composer, self.public_key_hasher.clone());
let tree_hasher_gadget: HG =
FieldHasherGadget::<F, P>::from_native(composer, self.tree_hasher.clone());
let sig_hasher_gadget: HG =
FieldHasherGadget::<F, P>::from_native(composer, self.signature_hasher.clone());
let leaf_hasher_gadget: HG =
FieldHasherGadget::<F, P>::from_native(composer, self.leaf_hasher.clone());
// Sum of input amounts + public amount must equal output amounts at the end
let mut input_sum = public_amount;
let mut output_sum = composer.zero_var();
// General strategy
// 1. Reconstruct the commitments (along the way reconstruct other values)
// 2. Reconstruct the target merkle root with the input's merkle path
// 3. Verify that the target merkle root is within the root set
// 4. Sum the input amounts
for i in 0..INS {
// Private inputs for each input UTXO being spent
let in_private_key_i = composer.add_input(self.in_private_keys[i]);
let in_amount_i = composer.add_input(self.in_amounts[i]);
let in_blinding_i = composer.add_input(self.in_blindings[i]);
let in_index_i = composer.add_input(self.in_indices[i]);
let path_gadget =
PathGadget::<F, P, HG, N>::from_native(composer, self.in_paths[i].clone());
// Computing the public key, which is done just by hashing the private key
let calc_public_key = pk_hasher_gadget.hash(composer, &[in_private_key_i])?;
// Computing the leaf
let calc_leaf = leaf_hasher_gadget.hash(composer, &[
public_chain_id,
in_amount_i,
calc_public_key,
in_blinding_i,
])?;
// Computing the signature: sign(private_key, leaf, input_index)
let calc_signature =
sig_hasher_gadget.hash(composer, &[in_private_key_i, calc_leaf, in_index_i])?;
// Computing the nullifier hash. This is used to prevent spending
// already spent UTXOs.
let calc_nullifier =
sig_hasher_gadget.hash(composer, &[calc_leaf, in_index_i, calc_signature])?;
// Checking if the passed nullifier hash is the same as the calculated one
// Optimized version of allocating public nullifier input and constraining
// to the calculated one.
composer.assert_equal(calc_nullifier, nullifier_hash_vars[i]);
// Calculate the root hash
let calc_root_hash =
path_gadget.calculate_root(composer, &calc_leaf, &tree_hasher_gadget)?;
// Check if calculated root hash is in the set
// Note that if `in_amount_i = 0` then the input is a
// "dummy" input, so the check is not needed. The
// `check_set_membership_is_enabled` function accounts for this.
let is_member =
set_gadget.check_set_membership_is_enabled(composer, calc_root_hash, in_amount_i);
composer.constrain_to_constant(is_member, F::one(), None);
// Finally add the amount to the sum
// TODO: Investigate improvements to accumulating sums
input_sum = composer.arithmetic_gate(|gate| {
gate.witness(input_sum, in_amount_i, None)
.add(F::one(), F::one())
});
}
// Check all the nullifiers are unique to prevent double-spending
// TODO: Investigate checking nullifier uniqueness this check to the application
// side
for i in 0..INS {
for j in (i + 1)..INS {
let result =
composer.is_eq_with_output(nullifier_hash_vars[i], nullifier_hash_vars[j]);
composer.assert_equal(result, composer.zero_var());
}
}
for i in 0..OUTS {
let out_chain_id_i = composer.add_input(self.out_chain_ids[i]);
let out_amount_i = composer.add_input(self.out_amounts[i]);
let out_public_key_i = composer.add_input(self.out_public_keys[i]);
let out_blinding_i = composer.add_input(self.out_blindings[i]);
// Calculate the leaf commitment
let calc_leaf = leaf_hasher_gadget.hash(composer, &[
out_chain_id_i,
out_amount_i,
out_public_key_i,
out_blinding_i,
])?;
// Check if calculated leaf is the same as the passed one
composer.assert_equal(calc_leaf, commitment_vars[i]);
// Each amount should not be greater than the limit constant
// TODO: The field size can be gotten as F::size_in_bits()
// What is the correct transaction limit?
// Each amount should be less than (field size)/2 to prevent
// overflow, which suggests that F::size_in_bits() - 1 would
// be small enough. Maybe use F::size_in_bits() - 100 to be safe?
composer.range_gate(out_amount_i, 254);
// Add in to the sum
output_sum = composer.arithmetic_gate(|gate| {
gate.witness(output_sum, out_amount_i, None)
.add(F::one(), F::one())
});
}
composer.assert_equal(input_sum, output_sum);
let _arbitrary_data_squared = composer.arithmetic_gate(|gate| {
gate.witness(arbitrary_data, arbitrary_data, None)
.mul(F::one())
});
Ok(())
}
fn padded_circuit_size(&self) -> usize {
1 << 21
}
}
#[cfg(test)]
mod test {
use std::marker::PhantomData;
use super::VariableAnchorCircuit;
use crate::utils::{prove_then_verify, ToConstraintFieldHelper};
use ark_bn254::Bn254;
use ark_ed_on_bn254::{EdwardsParameters as JubjubParameters, Fq};
use ark_ff::{Field, PrimeField};
use ark_std::{test_rng, UniformRand};
use arkworks_native_gadgets::{
merkle_tree::{Path, SparseMerkleTree},
poseidon::{sbox::PoseidonSbox, FieldHasher, Poseidon, PoseidonParameters},
};
use arkworks_plonk_gadgets::poseidon::PoseidonGadget;
use arkworks_utils::{
bytes_matrix_to_f, bytes_vec_to_f, poseidon_params::setup_poseidon_params, Curve,
};
use plonk_core::prelude::*;
type PoseidonBn254 = Poseidon<Fq>;
pub fn setup_params<F: PrimeField>(curve: Curve, exp: i8, width: u8) -> PoseidonParameters<F> {
let pos_data = setup_poseidon_params(curve, exp, width).unwrap();
let mds_f = bytes_matrix_to_f(&pos_data.mds);
let rounds_f = bytes_vec_to_f(&pos_data.rounds);
let pos = PoseidonParameters {
mds_matrix: mds_f,
round_keys: rounds_f,
full_rounds: pos_data.full_rounds,
partial_rounds: pos_data.partial_rounds,
sbox: PoseidonSbox(pos_data.exp),
width: pos_data.width,
};
pos
}
// Helper that outputs the hash functions of each width we need.
// I have not made this generic over the curve
fn make_vanchor_hashers() -> [PoseidonBn254; 4] {
let curve = Curve::Bn254;
let params2 = setup_params::<Fq>(curve, 5, 2);
let poseidon_native2 = PoseidonBn254 { params: params2 };
let params3 = setup_params::<Fq>(curve, 5, 3);
let poseidon_native3 = PoseidonBn254 { params: params3 };
let params4 = setup_params::<Fq>(curve, 5, 4);
let poseidon_native4 = PoseidonBn254 { params: params4 };
let params5 = setup_params::<Fq>(curve, 5, 5);
let poseidon_native5 = PoseidonBn254 { params: params5 };
[
poseidon_native2,
poseidon_native3,
poseidon_native4,
poseidon_native5,
]
}
// This is the only test of a 16-2 vanchor transaction. All others
// test 2-2 transactions.
#[test]
fn should_verify_correct_16_2_vanchor_plonk() {
const TREE_HEIGHT: usize = 5;
const BRIDGE_SIZE: usize = 2;
const INS: usize = 16;
const OUTS: usize = 2;
let rng = &mut test_rng();
let [poseidon_native2, poseidon_native3, poseidon_native4, poseidon_native5] =
make_vanchor_hashers();
// Randomly generated public inputs
let public_amount = Fq::rand(rng);
let public_chain_id = Fq::rand(rng);
let arbitrary_data = Fq::rand(rng);
// Randomly generated private inputs
// Initialize arrays
let mut in_private_keys = [Fq::from(0u64); INS];
let mut in_blindings = [Fq::from(0u64); INS];
let mut in_amounts = [Fq::from(0u64); INS];
let mut in_nullifier_hashes = [Fq::from(0u64); INS];
let mut in_leaf_hashes = [Fq::from(0u64); INS];
let mut in_root_set = [Fq::from(0u64); BRIDGE_SIZE];
// Default path to initialize the `in_paths` array
let default_path = Path::<Fq, PoseidonBn254, TREE_HEIGHT> {
path: [(Fq::from(0u64), Fq::from(0u64)); TREE_HEIGHT],
marker: PhantomData,
};
let mut in_paths: [Path<_, _, TREE_HEIGHT>; INS] = [
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path.clone(),
default_path,
];
// We index the paths from 0 to 15:
let mut in_indices = [Fq::from(0u32); INS];
for i in 0..INS {
in_indices[i] = Fq::from(i as u32);
}
// We will let a few of the inputs be non-zero, the rest will be
// dummy inputs
in_amounts[0] = Fq::from(1u32);
in_amounts[1] = Fq::from(2u32);
in_amounts[3] = Fq::from(3u32);
// Compute nullifier hash and leaf hash for each input
for i in 0..INS {
in_private_keys[i] = Fq::rand(rng);
in_blindings[i] = Fq::rand(rng);
// Calculate what the input nullifier hashes would be based on these:
let public_key = poseidon_native2.hash(&in_private_keys[i..i + 1]).unwrap();
in_leaf_hashes[i] = poseidon_native5
.hash(&[public_chain_id, in_amounts[i], public_key, in_blindings[i]])
.unwrap();
let signature = poseidon_native4
.hash(&[in_private_keys[i], in_leaf_hashes[i], in_indices[i]])
.unwrap();
in_nullifier_hashes[i] = poseidon_native4
.hash(&[in_leaf_hashes[i], in_indices[i], signature])
.unwrap();
}
// Now put all input leaves into a merkle tree
// (we assume here that all came from same chain)
let default_leaf = [0u8; 32];
let merkle_tree = SparseMerkleTree::<Fq, PoseidonBn254, TREE_HEIGHT>::new_sequential(
&in_leaf_hashes,
&poseidon_native3,
&default_leaf,
)
.unwrap();
// Store the path of each leaf
for i in 0..INS {
in_paths[i] = merkle_tree.generate_membership_proof(i as u64);
}
// The root set should contain this merkle tree's root
in_root_set[0] = merkle_tree.root();
// Output amounts: (remember input amounts sum to 6 and there is also the public
// amount)
let mut out_amounts = [Fq::from(0u64); OUTS];
out_amounts[0] = Fq::from(2u32) + public_amount;
out_amounts[1] = Fq::from(4u32);
// Other output quantities can be randomly generated
let mut out_private_keys = [Fq::from(0u64); OUTS];
let mut out_public_keys = [Fq::from(0u64); OUTS];
let mut out_blindings = [Fq::from(0u64); OUTS];
let mut out_chain_ids = [Fq::from(0u64); OUTS];
let mut out_commitments = [Fq::from(0u64); OUTS];
for i in 0..OUTS {
out_blindings[i] = Fq::rand(rng);
out_private_keys[i] = Fq::rand(rng);
out_chain_ids[i] = Fq::rand(rng);
out_public_keys[i] = poseidon_native2.hash(&out_private_keys[i..i + 1]).unwrap();
// Compute the out commitment
out_commitments[i] = poseidon_native5
.hash(&[
out_chain_ids[i],
out_amounts[i],
out_public_keys[i],
out_blindings[i],
])
.unwrap();
}
// Create the VAnchor circuit
let mut circuit = VariableAnchorCircuit::<
Fq,
JubjubParameters,
PoseidonGadget,
TREE_HEIGHT,
BRIDGE_SIZE,
INS,
OUTS,
>::new(
public_amount,
public_chain_id,
in_amounts,
in_blindings,
in_nullifier_hashes,
in_private_keys,
in_paths,
in_indices,
in_root_set,
out_amounts,
out_blindings,
out_chain_ids,
out_public_keys,
out_commitments,
arbitrary_data,
poseidon_native2,
poseidon_native3,
poseidon_native4,
poseidon_native5,
);
// Verify proof
let res = prove_then_verify::<Bn254, JubjubParameters, _>(
&mut |c| circuit.gadget(c),
1 << 22,
None, // Use None argument to give verifier the same public input data
);
match res {
Ok(()) => (),
Err(err) => panic!("Unexpected error: {:?}", err),
};
}
// All subsequent tests are for a 2-2 vanchor transaction
const TREE_HEIGHT: usize = 3;
const BRIDGE_SIZE: usize = 3;
const INS: usize = 2;
const OUTS: usize = 2;
#[test]
fn should_verify_correct_vanchor_plonk() {
let rng = &mut test_rng();
let [poseidon_native2, poseidon_native3, poseidon_native4, poseidon_native5] =
make_vanchor_hashers();
// Randomly generated public inputs
let public_amount = Fq::rand(rng);
let public_chain_id = Fq::rand(rng);
let arbitrary_data = Fq::rand(rng);
// Randomly generated private inputs
// Initialize arrays
let mut in_private_keys = [Fq::from(0u64); INS];
let mut in_blindings = [Fq::from(0u64); INS];
let mut in_amounts = [Fq::from(0u64); INS];
let mut in_nullifier_hashes = [Fq::from(0u64); INS];
let mut in_root_set = [Fq::from(0u64); BRIDGE_SIZE];
// Default path to initialize the `in_paths` array
let default_path = Path::<Fq, PoseidonBn254, TREE_HEIGHT> {
path: [(Fq::from(0u64), Fq::from(0u64)); TREE_HEIGHT],
marker: PhantomData,
};
let mut in_paths: [Path<_, _, TREE_HEIGHT>; INS] = [default_path.clone(), default_path];
// We'll say the index of each input is index:
let index = 0u64;
let in_indices = [Fq::from(index); INS];
// First input will be a dummy input, so its
// data is left as zeros. Nullifier hashes must be
// computed properly, and we need to add a fake merkle
// tree membership proof since the gadget checks this,
// but the tree's root does not belong to the root set.
let public_key = poseidon_native2.hash(&in_private_keys[0..1]).unwrap();
let leaf = poseidon_native5
.hash(&[public_chain_id, in_amounts[0], public_key, in_blindings[0]])
.unwrap();
let signature = poseidon_native4
.hash(&[in_private_keys[0], leaf, in_indices[0]])
.unwrap();
in_nullifier_hashes[0] = poseidon_native4
.hash(&[leaf, in_indices[0], signature])
.unwrap();
// Simulate a Merkle tree path for this dummy input
let default_leaf = [0u8; 32];
let merkle_tree = SparseMerkleTree::<Fq, PoseidonBn254, TREE_HEIGHT>::new_sequential(
&[leaf],
&poseidon_native3,
&default_leaf,
)
.unwrap();
in_paths[0] = merkle_tree.generate_membership_proof(index);
// The remaining input can be a random number
in_private_keys[1] = Fq::rand(rng);
in_blindings[1] = Fq::rand(rng);
// Multiplying by 1/20 prevents the amounts from summing to more than
// the size of the field (at least for fewer than 20 inputs)
in_amounts[1] = Fq::rand(rng) * (Fq::from(20u64).inverse().unwrap());
// Calculate what the input nullifier hashes would be based on these:
let public_key = poseidon_native2.hash(&in_private_keys[1..2]).unwrap();
let leaf = poseidon_native5
.hash(&[public_chain_id, in_amounts[1], public_key, in_blindings[1]])
.unwrap();
let signature = poseidon_native4
.hash(&[in_private_keys[1], leaf, in_indices[1]])
.unwrap();
in_nullifier_hashes[1] = poseidon_native4
.hash(&[leaf, in_indices[1], signature])
.unwrap();
// Simulate a Merkle tree for each input
let default_leaf = [0u8; 32];
let merkle_tree = SparseMerkleTree::<Fq, PoseidonBn254, TREE_HEIGHT>::new_sequential(
&[leaf],
&poseidon_native3,
&default_leaf,
)
.unwrap();
in_paths[1] = merkle_tree.generate_membership_proof(index);
// Add the root of this Merkle tree to the root set.
in_root_set[0] = merkle_tree.root();
// Output amounts should sum to inputs plus public amount.
// In this case the first output is 0 and the remaining output
// contains the full value of the transaction
let mut out_amounts = [Fq::from(0u64); OUTS];
out_amounts[0] = in_amounts[0];
out_amounts[1] = public_amount + in_amounts[1];
// Other output quantities can be randomly generated
let mut out_private_keys = [Fq::from(0u64); OUTS];
let mut out_public_keys = [Fq::from(0u64); OUTS];
let mut out_blindings = [Fq::from(0u64); OUTS];
let mut out_chain_ids = [Fq::from(0u64); OUTS];
let mut out_commitments = [Fq::from(0u64); OUTS];
for i in 0..OUTS {
out_blindings[i] = Fq::rand(rng);
out_private_keys[i] = Fq::rand(rng);
out_chain_ids[i] = Fq::rand(rng);
out_public_keys[i] = poseidon_native2.hash(&out_private_keys[i..i + 1]).unwrap();
// Compute the out commitment
out_commitments[i] = poseidon_native5
.hash(&[
out_chain_ids[i],
out_amounts[i],
out_public_keys[i],
out_blindings[i],
])
.unwrap();
}
// Create the VAnchor circuit
let mut circuit = VariableAnchorCircuit::<
Fq,
JubjubParameters,
PoseidonGadget,
TREE_HEIGHT,
BRIDGE_SIZE,
INS,
OUTS,
>::new(
public_amount,
public_chain_id,
in_amounts,
in_blindings,
in_nullifier_hashes,
in_private_keys,
in_paths,
in_indices,
in_root_set,
out_amounts,
out_blindings,
out_chain_ids,
out_public_keys,
out_commitments,
arbitrary_data,
poseidon_native2,
poseidon_native3,
poseidon_native4,
poseidon_native5,
);
// Verify proof
let res = prove_then_verify::<Bn254, JubjubParameters, _>(
&mut |c| circuit.gadget(c),
1 << 17,
None, // Use None argument to give verifier the same public input data
);
match res {
Ok(()) => (),
Err(err) => panic!("Unexpected error: {:?}", err),
};
}
#[test]
fn should_fail_with_invalid_root_plonk() {
let rng = &mut test_rng();
let [poseidon_native2, poseidon_native3, poseidon_native4, poseidon_native5] =
make_vanchor_hashers();
// Randomly generated public inputs
let public_amount = Fq::rand(rng);
let public_chain_id = Fq::rand(rng);
let arbitrary_data = Fq::rand(rng);
// Randomly generated private inputs
// Initialize arrays
let mut in_private_keys = [Fq::from(0u64); INS];
let mut in_blindings = [Fq::from(0u64); INS];
let mut in_amounts = [Fq::from(0u64); INS];
let mut in_nullifier_hashes = [Fq::from(0u64); INS];
let mut in_root_set = [Fq::from(0u64); BRIDGE_SIZE];
// Default path to initialize the `in_paths` array
let default_path = Path::<Fq, PoseidonBn254, TREE_HEIGHT> {
path: [(Fq::from(0u64), Fq::from(0u64)); TREE_HEIGHT],
marker: PhantomData,
};
let mut in_paths: [Path<_, _, TREE_HEIGHT>; INS] = [default_path.clone(), default_path];
// We'll say the index of each input is index:
let index = 0u64;
let in_indices = [Fq::from(index); INS];
// First input will be a dummy input, so its
// data is left as zeros. Nullifier hashes must be
// computed properly, and we need to add a fake merkle
// tree membership proof since the gadget checks this,
// but the tree's root does not belong to the root set.
let public_key = poseidon_native2.hash(&in_private_keys[0..1]).unwrap();
let leaf = poseidon_native5
.hash(&[public_chain_id, in_amounts[0], public_key, in_blindings[0]])
.unwrap();
let signature = poseidon_native4
.hash(&[in_private_keys[0], leaf, in_indices[0]])
.unwrap();
in_nullifier_hashes[0] = poseidon_native4
.hash(&[leaf, in_indices[0], signature])
.unwrap();
// Simulate a Merkle tree path for this dummy input
let default_leaf = [0u8; 32];
let merkle_tree = SparseMerkleTree::<Fq, PoseidonBn254, TREE_HEIGHT>::new_sequential(
&[leaf],
&poseidon_native3,
&default_leaf,
)
.unwrap();
in_paths[0] = merkle_tree.generate_membership_proof(index);
// The remaining input can be a random number
in_private_keys[1] = Fq::rand(rng);
in_blindings[1] = Fq::rand(rng);
// Multiplying by 1/20 prevents the amounts from summing to more than
// the size of the field (at least for fewer than 20 inputs)
in_amounts[1] = Fq::rand(rng) * (Fq::from(20u64).inverse().unwrap());
// Calculate what the input nullifier hashes would be based on these:
let public_key = poseidon_native2.hash(&in_private_keys[1..2]).unwrap();
let leaf = poseidon_native5
.hash(&[public_chain_id, in_amounts[1], public_key, in_blindings[1]])
.unwrap();
let signature = poseidon_native4
.hash(&[in_private_keys[1], leaf, in_indices[1]])
.unwrap();
in_nullifier_hashes[1] = poseidon_native4
.hash(&[leaf, in_indices[1], signature])
.unwrap();
// Simulate a Merkle tree for each input
let default_leaf = [0u8; 32];
let merkle_tree = SparseMerkleTree::<Fq, PoseidonBn254, TREE_HEIGHT>::new_sequential(
&[leaf],
&poseidon_native3,
&default_leaf,
)
.unwrap();
in_paths[1] = merkle_tree.generate_membership_proof(index);
// Add the root of this Merkle tree to the root set.
in_root_set[0] = merkle_tree.root();
// Output amounts cannot be randomly generated since they may then exceed input
// amount.
let mut out_amounts = [Fq::from(0u64); OUTS];
out_amounts[0] = in_amounts[0];
out_amounts[1] = public_amount + in_amounts[1];
// Other output quantities can be randomly generated
let mut out_private_keys = [Fq::from(0u64); OUTS];
let mut out_public_keys = [Fq::from(0u64); OUTS];
let mut out_blindings = [Fq::from(0u64); OUTS];
let mut out_chain_ids = [Fq::from(0u64); OUTS];
let mut out_commitments = [Fq::from(0u64); OUTS];
for i in 0..OUTS {
out_blindings[i] = Fq::rand(rng);
out_private_keys[i] = Fq::rand(rng);
out_chain_ids[i] = Fq::rand(rng);
out_public_keys[i] = poseidon_native2.hash(&out_private_keys[i..i + 1]).unwrap();
// Compute the out commitment
out_commitments[i] = poseidon_native5
.hash(&[
out_chain_ids[i],
out_amounts[i],
out_public_keys[i],
out_blindings[i],
])
.unwrap();
}
// Create the VAnchor circuit
let mut circuit = VariableAnchorCircuit::<
Fq,
JubjubParameters,
PoseidonGadget,
TREE_HEIGHT,
BRIDGE_SIZE,
INS,
OUTS,
>::new(
public_amount,
public_chain_id,
in_amounts,
in_blindings,
in_nullifier_hashes,
in_private_keys,
in_paths,
in_indices,
in_root_set,
out_amounts,
out_blindings,
out_chain_ids,
out_public_keys,
out_commitments,
arbitrary_data,
poseidon_native2,
poseidon_native3,
poseidon_native4,
poseidon_native5,
);
let verifier_public_inputs = vec![
ToConstraintFieldHelper::from(public_amount),
ToConstraintFieldHelper::from(public_chain_id),
ToConstraintFieldHelper::from(arbitrary_data),
ToConstraintFieldHelper::from(in_nullifier_hashes[0]),
ToConstraintFieldHelper::from(in_nullifier_hashes[0]),
ToConstraintFieldHelper::from(in_root_set[0].double()), // Give the verifier a different root set
ToConstraintFieldHelper::from(in_root_set[1]),
ToConstraintFieldHelper::from(in_nullifier_hashes[1]),
ToConstraintFieldHelper::from(in_nullifier_hashes[1]),
ToConstraintFieldHelper::from(in_root_set[0]),
ToConstraintFieldHelper::from(in_root_set[1]),
ToConstraintFieldHelper::from(out_commitments[0]),
ToConstraintFieldHelper::from(out_commitments[1]),
];
// Verify proof
let res = prove_then_verify::<Bn254, JubjubParameters, _>(
&mut |c| circuit.gadget(c),
1 << 19,
Some(verifier_public_inputs),
);
match res {
Err(Error::ProofVerificationError) => (),
Err(err) => panic!("Unexpected error: {:?}", err),
Ok(()) => panic!("Proof was successfully verified when error was expected"),
};
}
#[test]
fn should_fail_with_wrong_secret_plonk() {
let rng = &mut test_rng();
let [poseidon_native2, poseidon_native3, poseidon_native4, poseidon_native5] =
make_vanchor_hashers();
// Randomly generated public inputs
let public_amount = Fq::rand(rng);
let public_chain_id = Fq::rand(rng);
let arbitrary_data = Fq::rand(rng);
// Randomly generated private inputs
// Initialize arrays
let mut in_private_keys = [Fq::from(0u64); INS];
let mut in_blindings = [Fq::from(0u64); INS];
let mut in_amounts = [Fq::from(0u64); INS];
let mut in_nullifier_hashes = [Fq::from(0u64); INS];
let mut in_root_set = [Fq::from(0u64); BRIDGE_SIZE];
// Default path to initialize the `in_paths` array
let default_path = Path::<Fq, PoseidonBn254, TREE_HEIGHT> {
path: [(Fq::from(0u64), Fq::from(0u64)); TREE_HEIGHT],
marker: PhantomData,
};
let mut in_paths: [Path<_, _, TREE_HEIGHT>; INS] = [default_path.clone(), default_path];
// We'll say the index of each input is index:
let index = 0u64;
let in_indices = [Fq::from(index); INS];
// First input will be a dummy input, so its
// data is left as zeros. Nullifier hashes must be
// computed properly, and we need to add a fake merkle
// tree membership proof since the gadget checks this,
// but the tree's root does not belong to the root set.
let public_key = poseidon_native2.hash(&in_private_keys[0..1]).unwrap();
let leaf = poseidon_native5
.hash(&[public_chain_id, in_amounts[0], public_key, in_blindings[0]])
.unwrap();
let signature = poseidon_native4
.hash(&[in_private_keys[0], leaf, in_indices[0]])
.unwrap();
in_nullifier_hashes[0] = poseidon_native4
.hash(&[leaf, in_indices[0], signature])
.unwrap();
// Simulate a Merkle tree path for this dummy input
let default_leaf = [0u8; 32];
let merkle_tree = SparseMerkleTree::<Fq, PoseidonBn254, TREE_HEIGHT>::new_sequential(
&[leaf],
&poseidon_native3,
&default_leaf,
)
.unwrap();
in_paths[0] = merkle_tree.generate_membership_proof(index);
// The remaining input can be a random number
in_private_keys[1] = Fq::rand(rng);
in_blindings[1] = Fq::rand(rng);
// Multiplying by 1/20 prevents the amounts from summing to more than
// the size of the field (at least for fewer than 20 inputs)
in_amounts[1] = Fq::rand(rng) * (Fq::from(20u64).inverse().unwrap());
// Calculate what the input nullifier hashes would be based on these:
let public_key = poseidon_native2.hash(&in_private_keys[1..2]).unwrap();
let leaf = poseidon_native5
.hash(&[public_chain_id, in_amounts[1], public_key, in_blindings[1]])
.unwrap();
let signature = poseidon_native4
.hash(&[in_private_keys[1], leaf, in_indices[1]])
.unwrap();
in_nullifier_hashes[1] = poseidon_native4
.hash(&[leaf, in_indices[1], signature])
.unwrap();
// Simulate a Merkle tree for each input
let default_leaf = [0u8; 32];
let merkle_tree = SparseMerkleTree::<Fq, PoseidonBn254, TREE_HEIGHT>::new_sequential(
&[leaf],
&poseidon_native3,
&default_leaf,
)
.unwrap();
in_paths[1] = merkle_tree.generate_membership_proof(index);
// Add the root of this Merkle tree to the root set.
in_root_set[0] = merkle_tree.root();
// Change the second secret to something incorrect
in_private_keys[1] = Fq::from(0u32);
// Output amounts cannot be randomly generated since they may then exceed input
// amount.
let mut out_amounts = [Fq::from(0u64); OUTS];
out_amounts[0] = in_amounts[0];
out_amounts[1] = public_amount + in_amounts[1]; // fix for INS > 2
// Other output quantities can be randomly generated
let mut out_private_keys = [Fq::from(0u64); OUTS];
let mut out_public_keys = [Fq::from(0u64); OUTS];
let mut out_blindings = [Fq::from(0u64); OUTS];
let mut out_chain_ids = [Fq::from(0u64); OUTS];
let mut out_commitments = [Fq::from(0u64); OUTS];
for i in 0..OUTS {
out_blindings[i] = Fq::rand(rng);
out_private_keys[i] = Fq::rand(rng);
out_chain_ids[i] = Fq::rand(rng);
out_public_keys[i] = poseidon_native2.hash(&out_private_keys[i..i + 1]).unwrap();
// Compute the out commitment
out_commitments[i] = poseidon_native5
.hash(&[
out_chain_ids[i],
out_amounts[i],
out_public_keys[i],
out_blindings[i],
])
.unwrap();
}
// Create the VAnchor circuit
let mut circuit = VariableAnchorCircuit::<
Fq,
JubjubParameters,
PoseidonGadget,
TREE_HEIGHT,
BRIDGE_SIZE,
INS,
OUTS,
>::new(
public_amount,
public_chain_id,
in_amounts,
in_blindings,
in_nullifier_hashes,
in_private_keys,
in_paths,
in_indices,
in_root_set,
out_amounts,
out_blindings,
out_chain_ids,
out_public_keys,
out_commitments,
arbitrary_data,
poseidon_native2,
poseidon_native3,
poseidon_native4,