-
Notifications
You must be signed in to change notification settings - Fork 34
/
tests.rs
460 lines (380 loc) · 18.5 KB
/
tests.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
#[cfg(test)]
mod test {
use crate::circuits::WithInstances;
use crate::merkle_sum_tree::{MerkleSumTree, Tree};
use crate::{
circuits::{
merkle_sum_tree::MstInclusionCircuit,
utils::{full_prover, full_verifier, generate_setup_artifacts},
},
merkle_sum_tree::Entry,
};
use halo2_proofs::{
dev::{FailureLocation, MockProver, VerifyFailure},
halo2curves::bn256::Fr as Fp,
plonk::Any,
};
use num_bigint::ToBigUint;
const N_CURRENCIES: usize = 2;
const LEVELS: usize = 4;
const N_BYTES: usize = 8;
const K: u32 = 11;
#[test]
fn test_valid_merkle_sum_tree() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
for user_index in 0..16 {
// get proof for entry ˆuser_indexˆ
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let valid_prover = MockProver::run(K, &circuit, circuit.instances()).unwrap();
assert_eq!(circuit.instances()[0].len(), circuit.num_instances());
assert_eq!(circuit.instances()[0].len(), 2 + N_CURRENCIES);
valid_prover.assert_satisfied();
}
}
#[test]
fn test_valid_merkle_sum_tree_with_full_prover() {
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init_empty();
// Generate a universal trusted setup for testing purposes.
//
// The verification key (vk) and the proving key (pk) are then generated.
// An empty circuit is used here to emphasize that the circuit inputs are not relevant when generating the keys.
// Important: The dimensions of the circuit used to generate the keys must match those of the circuit used to generate the proof.
// In this case, the dimensions are represented by the height of the Merkle tree.
let (params, pk, vk) = generate_setup_artifacts(K, None, circuit).unwrap();
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
let user_entry = merkle_sum_tree.get_entry(user_index);
// Only now we can instantiate the circuit with the actual inputs
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
// Generate the proof
let proof = full_prover(¶ms, &pk, circuit.clone(), circuit.instances());
// verify the proof to be true
assert!(full_verifier(¶ms, &vk, proof, circuit.instances()));
// the user should perform the check on the public inputs
// public input #0 is the leaf hash
let expected_leaf_hash = user_entry.compute_leaf().hash;
assert_eq!(circuit.instances()[0][0], expected_leaf_hash);
// public input #1 is the root hash
let expected_root_hash = merkle_sum_tree.root().hash;
assert_eq!(circuit.instances()[0][1], expected_root_hash);
// public inputs [2, 2+N_CURRENCIES - 1] are the root balances
let expected_root_balances = merkle_sum_tree.root().balances;
for i in 0..N_CURRENCIES {
assert_eq!(circuit.instances()[0][2 + i], expected_root_balances[i]);
}
}
// Passing an invalid root hash in the instance column should fail the permutation check between the computed root hash and the instance column root hash
#[test]
fn test_invalid_root_hash() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let mut instances = circuit.instances();
let invalid_root_hash = Fp::from(1000u64);
instances[0][1] = invalid_root_hash;
let invalid_prover = MockProver::run(K, &circuit, instances).unwrap();
assert_eq!(
invalid_prover.verify(),
Err(vec![
VerifyFailure::Permutation {
column: (Any::advice(), 0).into(),
location: FailureLocation::InRegion {
region: (121, "permute state").into(),
offset: 36
}
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 1 }
},
])
);
}
#[test]
fn test_invalid_root_hash_as_instance_with_full_prover() {
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init_empty();
// generate a universal trusted setup for testing, along with the verification key (vk) and the proving key (pk).
let (params, pk, vk) = generate_setup_artifacts(K, None, circuit).unwrap();
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
// Only now we can instantiate the circuit with the actual inputs
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let invalid_root_hash = Fp::from(1000u64);
let mut instances = circuit.instances();
instances[0][1] = invalid_root_hash;
// Generate the proof
let proof = full_prover(¶ms, &pk, circuit, instances.clone());
// verify the proof to be false
assert!(!full_verifier(¶ms, &vk, proof, instances));
}
// Passing an invalid entry balance as input for the witness generation should fail:
// - the permutation check between the leaf hash and the instance column leaf hash
// - the permutation check between the computed root hash and the instance column root hash
// - the permutations checks between the computed root balances and the instance column root balances
#[test]
fn test_invalid_entry_balance_as_witness() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
// Only now we can instantiate the circuit with the actual inputs
let mut circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let instances = circuit.instances();
let invalid_leaf_balances = [1000.to_biguint().unwrap(), 1000.to_biguint().unwrap()];
// invalidate user entry
let invalid_entry = Entry::new(circuit.entry.username().to_string(), invalid_leaf_balances);
circuit.entry = invalid_entry;
let invalid_prover = MockProver::run(K, &circuit, instances).unwrap();
assert_eq!(
invalid_prover.verify(),
Err(vec![
VerifyFailure::Permutation {
column: (Any::advice(), 0).into(),
location: FailureLocation::InRegion {
region: (26, "assign nodes hashes per merkle tree level").into(),
offset: 0
}
},
VerifyFailure::Permutation {
column: (Any::advice(), 0).into(),
location: FailureLocation::InRegion {
region: (121, "permute state").into(),
offset: 36
}
},
VerifyFailure::Permutation {
column: (Any::advice(), 2).into(),
location: FailureLocation::InRegion {
region: (111, "sum nodes balances per currency").into(),
offset: 0
}
},
VerifyFailure::Permutation {
column: (Any::advice(), 2).into(),
location: FailureLocation::InRegion {
region: (112, "sum nodes balances per currency").into(),
offset: 0
}
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 0 }
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 1 }
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 2 }
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 3 }
},
])
);
}
// Passing an invalid leaf hash in the instance column should fail the permutation check between the (valid) leaf hash added as part of the witness and the instance column leaf hash
#[test]
fn test_invalid_leaf_hash_as_instance() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
// Only now we can instantiate the circuit with the actual inputs
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let mut instances = circuit.instances();
let invalid_leaf_hash = Fp::from(1000u64);
instances[0][0] = invalid_leaf_hash;
let invalid_prover = MockProver::run(K, &circuit, instances).unwrap();
assert_eq!(
invalid_prover.verify(),
Err(vec![
VerifyFailure::Permutation {
column: (Any::advice(), 0).into(),
location: FailureLocation::InRegion {
region: (26, "assign nodes hashes per merkle tree level").into(),
offset: 0
}
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 0 }
},
])
);
}
// Building a proof using as input a csv file with an entry that is not in range [0, 2^N_BYTES*8 - 1] should fail the range check constraint on the leaf balance
#[test]
fn test_balance_not_in_range() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16_overflow.csv")
.unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let invalid_prover = MockProver::run(K, &circuit, circuit.instances()).unwrap();
assert_eq!(
invalid_prover.verify(),
Err(vec![
VerifyFailure::Permutation {
column: (Any::Fixed, 2).into(),
location: FailureLocation::OutsideRegion { row: 246 }
},
VerifyFailure::Permutation {
column: (Any::advice(), 0).into(),
location: FailureLocation::InRegion {
region: (21, "assign value to perform range check").into(),
offset: 8
}
},
])
);
}
// Passing a non binary index should fail the bool constraint inside "assign nodes hashes per merkle tree level" and "assign nodes balances per currency" region and the permutation check between the computed root hash and the instance column root hash
#[test]
fn test_non_binary_index() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
// Only now we can instantiate the circuit with the actual inputs
let mut circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let instances = circuit.instances();
// invalidate path index inside the circuit
circuit.path_indices[0] = Fp::from(2);
let invalid_prover = MockProver::run(K, &circuit, instances).unwrap();
assert_eq!(
invalid_prover.verify(),
Err(vec![
VerifyFailure::ConstraintNotSatisfied {
constraint: ((6, "bool constraint").into(), 0, "").into(),
location: FailureLocation::InRegion {
region: (26, "assign nodes hashes per merkle tree level").into(),
offset: 0
},
cell_values: vec![(((Any::advice(), 2).into(), 0).into(), "0x2".to_string()),]
},
VerifyFailure::ConstraintNotSatisfied {
constraint: ((7, "swap constraint").into(), 0, "").into(),
location: FailureLocation::InRegion {
region: (26, "assign nodes hashes per merkle tree level").into(),
offset: 0
},
cell_values: vec![
(
((Any::advice(), 0).into(), 0).into(),
"0x167505f45c4ef4a0b051c30e881d2e8f881f26f5edb231396198a2cc1712f5ad"
.to_string()
),
(
((Any::advice(), 0).into(), 1).into(),
"0x2c688f624d2bca741a1c2ad1ad2880721fbfd1613bbc5fe3d2ba66eb672e3aab"
.to_string()
),
(
((Any::advice(), 1).into(), 0).into(),
"0x2c688f624d2bca741a1c2ad1ad2880721fbfd1613bbc5fe3d2ba66eb672e3aab"
.to_string()
),
(((Any::advice(), 2).into(), 0).into(), "0x2".to_string()),
]
},
VerifyFailure::ConstraintNotSatisfied {
constraint: ((7, "swap constraint").into(), 1, "").into(),
location: FailureLocation::InRegion {
region: (26, "assign nodes hashes per merkle tree level").into(),
offset: 0
},
cell_values: vec![
(
((Any::advice(), 0).into(), 0).into(),
"0x167505f45c4ef4a0b051c30e881d2e8f881f26f5edb231396198a2cc1712f5ad"
.to_string()
),
(
((Any::advice(), 1).into(), 0).into(),
"0x2c688f624d2bca741a1c2ad1ad2880721fbfd1613bbc5fe3d2ba66eb672e3aab"
.to_string()
),
(
((Any::advice(), 1).into(), 1).into(),
"0x167505f45c4ef4a0b051c30e881d2e8f881f26f5edb231396198a2cc1712f5ad"
.to_string()
),
(((Any::advice(), 2).into(), 0).into(), "0x2".to_string()),
]
},
VerifyFailure::Permutation {
column: (Any::advice(), 0).into(),
location: FailureLocation::InRegion {
region: (121, "permute state").into(),
offset: 36
}
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 1 }
},
])
);
}
// Swapping the indices should fail the permutation check between the computed root hash and the instance column root hash
#[test]
fn test_swapping_index() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
// Only now we can instantiate the circuit with the actual inputs
let mut circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let instances = circuit.instances();
// swap indices
circuit.path_indices[0] = Fp::from(1);
let invalid_prover = MockProver::run(K, &circuit, instances).unwrap();
assert_eq!(
invalid_prover.verify(),
Err(vec![
VerifyFailure::Permutation {
column: (Any::advice(), 0).into(),
location: FailureLocation::InRegion {
region: (121, "permute state").into(),
offset: 36
}
},
VerifyFailure::Permutation {
column: (Any::Instance, 0).into(),
location: FailureLocation::OutsideRegion { row: 1 }
},
])
);
}
#[cfg(feature = "dev-graph")]
#[test]
fn print_mst_inclusion() {
use plotters::prelude::*;
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();
let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
let root = BitMapBackend::new("prints/mst-inclusion-layout.png", (2048, 32768))
.into_drawing_area();
root.fill(&WHITE).unwrap();
let root = root
.titled("Merkle Sum Tree Inclusion Layout", ("sans-serif", 60))
.unwrap();
halo2_proofs::dev::CircuitLayout::default()
.render(K, &circuit, &root)
.unwrap();
}
}