This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcomposer.rs
1119 lines (957 loc) · 36.7 KB
/
composer.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
use crate::barretenberg_structures::{Assignments, ConstraintSystem};
use crate::crs::{CRS, G2};
use crate::{Barretenberg, FIELD_BYTES};
use std::slice;
const NUM_RESERVED_GATES: u32 = 4; // this must be >= num_roots_cut_out_of_vanishing_polynomial (found under prover settings in barretenberg)
pub(crate) trait Composer {
fn get_circuit_size(&self, constraint_system: &ConstraintSystem) -> u32;
fn get_exact_circuit_size(&self, constraint_system: &ConstraintSystem) -> u32;
fn proof_as_fields(&self, proof: &[u8], public_inputs: Assignments) -> Vec<u8>;
fn verification_key_as_fields(&self, verification_key: &[u8]) -> (Vec<u8>, Vec<u8>);
fn compute_proving_key(&self, constraint_system: &ConstraintSystem) -> Vec<u8>;
fn compute_verification_key(
&self,
constraint_system: &ConstraintSystem,
proving_key: &[u8],
) -> Vec<u8>;
fn create_proof_with_pk(
&self,
constraint_system: &ConstraintSystem,
witness: Assignments,
proving_key: &[u8],
is_recursive: bool,
) -> Vec<u8>;
fn verify_with_vk(
&self,
constraint_system: &ConstraintSystem,
// XXX: Important: This assumes that the proof does not have the public inputs pre-pended to it
// This is not the case, if you take the proof directly from Barretenberg
proof: &[u8],
public_inputs: Assignments,
verification_key: &[u8],
is_recursive: bool,
) -> bool;
}
#[cfg(feature = "native")]
impl Composer for Barretenberg {
// XXX: There seems to be a bug in the C++ code
// where it causes a `HeapAccessOutOfBound` error
// for certain circuit sizes.
//
// This method calls the WASM for the circuit size
// if an error is returned, then the circuit size is defaulted to 2^19.
//
// This method is primarily used to determine how many group
// elements we need from the CRS. So using 2^19 on an error
// should be an overestimation.
fn get_circuit_size(&self, constraint_system: &ConstraintSystem) -> u32 {
let cs_buf = constraint_system.to_bytes();
let circuit_size;
unsafe {
circuit_size =
barretenberg_sys::composer::get_total_circuit_size(cs_buf.as_slice().as_ptr());
}
pow2ceil(circuit_size + NUM_RESERVED_GATES)
}
fn get_exact_circuit_size(&self, constraint_system: &ConstraintSystem) -> u32 {
let cs_buf = constraint_system.to_bytes();
unsafe { barretenberg_sys::composer::get_exact_circuit_size(cs_buf.as_slice().as_ptr()) }
}
fn proof_as_fields(&self, proof: &[u8], public_inputs: Assignments) -> Vec<u8> {
let mut proof_fields_addr: *mut u8 = std::ptr::null_mut();
let p_proof_fields = &mut proof_fields_addr as *mut *mut u8;
let proof = prepend_public_inputs(proof.to_vec(), public_inputs);
let proof_fields_size;
unsafe {
proof_fields_size = barretenberg_sys::composer::serialize_proof_into_field_elements(
&proof,
p_proof_fields,
proof.len(),
)
}
std::mem::forget(proof);
let result;
unsafe {
result = Vec::from_raw_parts(proof_fields_addr, proof_fields_size, proof_fields_size);
}
result
}
fn verification_key_as_fields(&self, verification_key: &[u8]) -> (Vec<u8>, Vec<u8>) {
let g2_data = G2::new().data;
let mut vk_fields_addr: *mut u8 = std::ptr::null_mut();
let p_vk_fields = &mut vk_fields_addr as *mut *mut u8;
let mut vk_hash_fields_addr: *mut u8 = std::ptr::null_mut();
let p_vk_hash_fields = &mut vk_hash_fields_addr as *mut *mut u8;
let vk_fields_size;
unsafe {
vk_fields_size =
barretenberg_sys::composer::serialize_verification_key_into_field_elements(
&g2_data,
verification_key,
p_vk_fields,
p_vk_hash_fields,
)
}
std::mem::forget(g2_data);
std::mem::forget(verification_key);
let vk_result;
let vk_hash_result;
unsafe {
vk_result = Vec::from_raw_parts(vk_fields_addr, vk_fields_size, vk_fields_size);
vk_hash_result = slice::from_raw_parts(vk_hash_fields_addr, 32);
}
(vk_result.to_vec(), vk_hash_result.to_vec())
}
fn compute_proving_key(&self, constraint_system: &ConstraintSystem) -> Vec<u8> {
let cs_buf = constraint_system.to_bytes();
let mut pk_addr: *mut u8 = std::ptr::null_mut();
let pk_ptr = &mut pk_addr as *mut *mut u8;
let pk_size;
unsafe {
pk_size = barretenberg_sys::composer::init_proving_key(&cs_buf, pk_ptr);
}
std::mem::forget(cs_buf);
let result;
unsafe {
result = Vec::from_raw_parts(pk_addr, pk_size, pk_size);
}
result
}
fn compute_verification_key(
&self,
constraint_system: &ConstraintSystem,
proving_key: &[u8],
) -> Vec<u8> {
let circuit_size = self.get_circuit_size(constraint_system);
let CRS {
g1_data, g2_data, ..
} = CRS::new(circuit_size as usize);
let pippenger_ptr = self.get_pippenger(&g1_data).pointer();
let mut vk_addr: *mut u8 = std::ptr::null_mut();
let vk_ptr = &mut vk_addr as *mut *mut u8;
let proving_key = proving_key.to_vec();
let vk_size;
unsafe {
vk_size = barretenberg_sys::composer::init_verification_key(
pippenger_ptr,
&g2_data,
&proving_key,
vk_ptr,
)
}
std::mem::forget(g2_data);
std::mem::forget(proving_key);
let result;
unsafe {
result = Vec::from_raw_parts(vk_addr, vk_size, vk_size);
}
result.to_vec()
}
// TODO: possibly change this interface depending on how we integrate with nargo
// the flag was initially made to quickly test our Rust Composer against the bberg changes, but might still be required
fn create_proof_with_pk(
&self,
constraint_system: &ConstraintSystem,
witness: Assignments,
proving_key: &[u8],
is_recursive: bool,
) -> Vec<u8> {
let circuit_size = self.get_circuit_size(constraint_system);
let CRS {
g1_data, g2_data, ..
} = CRS::new(circuit_size as usize);
let pippenger_ptr = self.get_pippenger(&g1_data).pointer();
let cs_buf: Vec<u8> = constraint_system.to_bytes();
let witness_buf = witness.to_bytes();
let mut proof_addr: *mut u8 = std::ptr::null_mut();
let p_proof = &mut proof_addr as *mut *mut u8;
let proving_key = proving_key.to_vec();
let proof_size;
unsafe {
proof_size = barretenberg_sys::composer::create_proof_with_pk(
pippenger_ptr,
&g2_data,
&proving_key,
&cs_buf,
&witness_buf,
p_proof,
is_recursive,
);
}
std::mem::forget(g2_data);
std::mem::forget(proving_key);
std::mem::forget(cs_buf);
std::mem::forget(witness_buf);
let result;
unsafe {
result = Vec::from_raw_parts(proof_addr, proof_size, proof_size);
}
// Barretenberg returns proofs which are prepended with the public inputs.
// This behavior is nonstandard so we strip the public inputs from the proof.
remove_public_inputs(constraint_system.public_inputs_size(), &result)
}
fn verify_with_vk(
&self,
constraint_system: &ConstraintSystem,
// XXX: Important: This assumes that the proof does not have the public inputs pre-pended to it
// This is not the case, if you take the proof directly from Barretenberg
proof: &[u8],
public_inputs: Assignments,
verification_key: &[u8],
is_recursive: bool,
) -> bool {
let g2_data = G2::new().data;
// Barretenberg expects public inputs to be prepended onto the proof
let proof = prepend_public_inputs(proof.to_vec(), public_inputs);
let cs_buf = constraint_system.to_bytes();
let verified;
unsafe {
verified = barretenberg_sys::composer::verify_with_vk(
&g2_data,
verification_key,
&cs_buf,
&proof,
is_recursive,
);
}
verified
}
}
#[cfg(not(feature = "native"))]
impl Composer for Barretenberg {
// XXX: There seems to be a bug in the C++ code
// where it causes a `HeapAccessOutOfBound` error
// for certain circuit sizes.
//
// This method calls the WASM for the circuit size
// if an error is returned, then the circuit size is defaulted to 2^19.
//
// This method is primarily used to determine how many group
// elements we need from the CRS. So using 2^19 on an error
// should be an overestimation.
fn get_circuit_size(&self, constraint_system: &ConstraintSystem) -> u32 {
let cs_buf = constraint_system.to_bytes();
let cs_ptr = self.allocate(&cs_buf);
let circuit_size = self
.call("acir_proofs_get_total_circuit_size", &cs_ptr)
.into_i32();
let circuit_size =
u32::try_from(circuit_size).expect("circuit cannot have negative number of gates");
self.free(cs_ptr);
pow2ceil(circuit_size + NUM_RESERVED_GATES)
}
fn get_exact_circuit_size(&self, constraint_system: &ConstraintSystem) -> u32 {
let cs_buf = constraint_system.to_bytes();
let cs_ptr = self.allocate(&cs_buf);
let circuit_size = self
.call("acir_proofs_get_exact_circuit_size", &cs_ptr)
.into_i32();
let circuit_size =
u32::try_from(circuit_size).expect("circuit cannot have negative number of gates");
self.free(cs_ptr);
circuit_size
}
fn compute_proving_key(&self, constraint_system: &ConstraintSystem) -> Vec<u8> {
use super::wasm::POINTER_BYTES;
use wasmer::Value;
let cs_buf = constraint_system.to_bytes();
let cs_ptr = self.allocate(&cs_buf);
// The proving key is not actually written to this pointer.
// `pk_ptr_ptr` is a pointer to a pointer which holds the proving key.
let pk_ptr_ptr: usize = 0;
let pk_size = self
.call_multiple(
"acir_proofs_init_proving_key",
vec![&cs_ptr, &Value::I32(pk_ptr_ptr as i32)],
)
.value();
let pk_size: usize = pk_size.unwrap_i32() as usize;
// We then need to read the pointer at `pk_ptr_ptr` to get the key's location
// and then slice memory again at `pk_ptr` to get the proving key.
let pk_ptr = self.slice_memory(pk_ptr_ptr, POINTER_BYTES);
let pk_ptr: usize =
u32::from_le_bytes(pk_ptr[0..POINTER_BYTES].try_into().unwrap()) as usize;
self.slice_memory(pk_ptr, pk_size)
}
fn compute_verification_key(
&self,
constraint_system: &ConstraintSystem,
proving_key: &[u8],
) -> Vec<u8> {
use super::wasm::POINTER_BYTES;
use wasmer::Value;
let circuit_size = self.get_circuit_size(constraint_system);
let CRS {
g1_data, g2_data, ..
} = CRS::new(circuit_size as usize);
let pippenger_ptr = self.get_pippenger(&g1_data).pointer();
let g2_ptr = self.allocate(&g2_data);
let pk_ptr = self.allocate(proving_key);
// The verification key is not actually written to this pointer.
// `vk_ptr_ptr` is a pointer to a pointer which holds the verification key.
let vk_ptr_ptr: usize = 0;
let vk_size = self
.call_multiple(
"acir_proofs_init_verification_key",
vec![
&pippenger_ptr,
&g2_ptr,
&pk_ptr,
&Value::I32(vk_ptr_ptr as i32),
],
)
.value();
let vk_size: usize = vk_size.unwrap_i32() as usize;
// We then need to read the pointer at `vk_ptr_ptr` to get the key's location
// and then slice memory again at `vk_ptr` to get the verification key.
let vk_ptr = self.slice_memory(vk_ptr_ptr, POINTER_BYTES);
let vk_ptr: usize =
u32::from_le_bytes(vk_ptr[0..POINTER_BYTES].try_into().unwrap()) as usize;
self.slice_memory(vk_ptr, vk_size)
}
fn create_proof_with_pk(
&self,
constraint_system: &ConstraintSystem,
witness: Assignments,
proving_key: &[u8],
) -> Vec<u8> {
use super::wasm::POINTER_BYTES;
use wasmer::Value;
let circuit_size = self.get_circuit_size(constraint_system);
let CRS {
g1_data, g2_data, ..
} = CRS::new(circuit_size as usize);
let pippenger_ptr = self.get_pippenger(&g1_data).pointer();
let cs_buf: Vec<u8> = constraint_system.to_bytes();
let witness_buf = witness.to_bytes();
let cs_ptr = self.allocate(&cs_buf);
let witness_ptr = self.allocate(&witness_buf);
let g2_ptr = self.allocate(&g2_data);
let pk_ptr = self.allocate(proving_key);
// The proof data is not actually written to this pointer.
// `proof_ptr_ptr` is a pointer to a pointer which holds the proof data.
let proof_ptr_ptr: usize = 0;
let proof_size = self
.call_multiple(
"acir_proofs_new_proof",
vec![
&pippenger_ptr,
&g2_ptr,
&pk_ptr,
&cs_ptr,
&witness_ptr,
&Value::I32(0),
],
)
.value();
let proof_size: usize = proof_size.unwrap_i32() as usize;
// We then need to read the pointer at `proof_ptr_ptr` to get the proof's location
// and then slice memory again at `proof_ptr` to get the proof data.
let proof_ptr = self.slice_memory(proof_ptr_ptr, POINTER_BYTES);
let proof_ptr: usize =
u32::from_le_bytes(proof_ptr[0..POINTER_BYTES].try_into().unwrap()) as usize;
let result = self.slice_memory(proof_ptr, proof_size);
// Barretenberg returns proofs which are prepended with the public inputs.
// This behavior is nonstandard so we strip the public inputs from the proof.
remove_public_inputs(constraint_system.public_inputs_size(), &result)
}
fn verify_with_vk(
&self,
constraint_system: &ConstraintSystem,
// XXX: Important: This assumes that the proof does not have the public inputs pre-pended to it
// This is not the case, if you take the proof directly from Barretenberg
proof: &[u8],
public_inputs: Assignments,
verification_key: &[u8],
) -> bool {
use wasmer::Value;
let g2_data = G2::new().data;
// Barretenberg expects public inputs to be prepended onto the proof
let proof = prepend_public_inputs(proof.to_vec(), public_inputs);
let cs_buf = constraint_system.to_bytes();
let cs_ptr = self.allocate(&cs_buf);
let proof_ptr = self.allocate(&proof);
let g2_ptr = self.allocate(&g2_data);
let vk_ptr = self.allocate(verification_key);
let verified = self
.call_multiple(
"acir_proofs_verify_proof",
vec![
&g2_ptr,
&vk_ptr,
&cs_ptr,
&proof_ptr,
&Value::I32(proof.len() as i32),
],
)
.value();
self.free(proof_ptr);
match verified.unwrap_i32() {
0 => false,
1 => true,
_ => panic!("Expected a 1 or a zero for the verification result"),
}
}
}
fn pow2ceil(v: u32) -> u32 {
if v > (u32::MAX >> 1) {
panic!("pow2ceil overflow");
}
let mut p = 1;
while p < v {
p <<= 1;
}
p
}
/// Removes the public inputs which are prepended to a proof by Barretenberg.
fn remove_public_inputs(num_pub_inputs: usize, proof: &[u8]) -> Vec<u8> {
// Barretenberg prepends the public inputs onto the proof so we need to remove
// the first `num_pub_inputs` field elements.
let num_bytes_to_remove = num_pub_inputs * FIELD_BYTES;
proof[num_bytes_to_remove..].to_vec()
}
/// Prepends a set of public inputs to a proof.
fn prepend_public_inputs(proof: Vec<u8>, public_inputs: Assignments) -> Vec<u8> {
if public_inputs.is_empty() {
return proof;
}
let public_inputs_bytes = public_inputs
.into_iter()
.flat_map(|assignment| assignment.to_be_bytes());
public_inputs_bytes.chain(proof.into_iter()).collect()
}
#[cfg(test)]
mod test {
use acvm::FieldElement;
use super::*;
use crate::{
barretenberg_structures::{
ComputeMerkleRootConstraint, Constraint, LogicConstraint, PedersenConstraint,
RangeConstraint, SchnorrConstraint, RecursionConstraint,
},
merkle::{MerkleTree, MessageHasher},
};
#[test]
fn test_no_constraints_no_pub_inputs() {
let constraint_system = ConstraintSystem::new();
let case_1 = WitnessResult {
witness: vec![].into(),
public_inputs: Assignments::default(),
result: true,
};
let test_cases = vec![case_1];
test_composer_with_pk_vk(constraint_system, test_cases);
}
#[test]
fn test_a_single_constraint_no_pub_inputs() {
let constraint = Constraint {
a: 1,
b: 2,
c: 3,
qm: FieldElement::zero(),
ql: FieldElement::one(),
qr: FieldElement::one(),
qo: -FieldElement::one(),
qc: FieldElement::zero(),
};
let constraint_system = ConstraintSystem::new()
.var_num(4)
.constraints(vec![constraint]);
let case_1 = WitnessResult {
witness: vec![(-1_i128).into(), 2_i128.into(), 1_i128.into()].into(),
public_inputs: Assignments::default(),
result: true,
};
let case_2 = WitnessResult {
witness: vec![
FieldElement::zero(),
FieldElement::zero(),
FieldElement::zero(),
]
.into(),
public_inputs: Assignments::default(),
result: true,
};
let case_3 = WitnessResult {
witness: vec![10_i128.into(), (-3_i128).into(), 7_i128.into()].into(),
public_inputs: Assignments::default(),
result: true,
};
let case_4 = WitnessResult {
witness: vec![
FieldElement::zero(),
FieldElement::zero(),
FieldElement::one(),
]
.into(),
public_inputs: Assignments::default(),
result: false,
};
let case_5 = WitnessResult {
witness: vec![FieldElement::one(), 2_i128.into(), 6_i128.into()].into(),
public_inputs: Assignments::default(),
result: false,
};
let test_cases = vec![case_1, case_2, case_3, case_4, case_5];
test_composer_with_pk_vk(constraint_system, test_cases);
}
#[test]
fn test_a_single_constraint_with_pub_inputs() {
let constraint = Constraint {
a: 1,
b: 2,
c: 3,
qm: FieldElement::zero(),
ql: FieldElement::one(),
qr: FieldElement::one(),
qo: -FieldElement::one(),
qc: FieldElement::zero(),
};
let constraint_system = ConstraintSystem::new()
.var_num(4)
.public_inputs(vec![1, 2])
.constraints(vec![constraint]);
// This fails because the constraint system requires public inputs,
// but none are supplied in public_inputs. So the verifier will not
// supply anything.
let _case_1 = WitnessResult {
witness: vec![(-1_i128).into(), 2_i128.into(), 1_i128.into()].into(),
public_inputs: Assignments::default(),
result: false,
};
let case_2 = WitnessResult {
witness: vec![
FieldElement::zero(),
FieldElement::zero(),
FieldElement::zero(),
]
.into(),
public_inputs: vec![FieldElement::zero(), FieldElement::zero()].into(),
result: true,
};
let case_3 = WitnessResult {
witness: vec![FieldElement::one(), 2_i128.into(), 6_i128.into()].into(),
public_inputs: vec![FieldElement::one(), 3_i128.into()].into(),
result: false,
};
// Not enough public inputs
let _case_4 = WitnessResult {
witness: vec![
FieldElement::one(),
FieldElement::from(2_i128),
FieldElement::from(6_i128),
]
.into(),
public_inputs: vec![FieldElement::one()].into(),
result: false,
};
let case_5 = WitnessResult {
witness: vec![FieldElement::one(), 2_i128.into(), 3_i128.into()].into(),
public_inputs: vec![FieldElement::one(), 2_i128.into()].into(),
result: true,
};
let case_6 = WitnessResult {
witness: vec![FieldElement::one(), 2_i128.into(), 3_i128.into()].into(),
public_inputs: vec![FieldElement::one(), 3_i128.into()].into(),
result: false,
};
let test_cases = vec![
/*case_1,*/ case_2, case_3, /*case_4,*/ case_5, case_6,
];
test_composer_with_pk_vk(constraint_system, test_cases);
}
#[test]
fn test_multiple_constraints() {
let constraint = Constraint {
a: 1,
b: 2,
c: 3,
qm: FieldElement::zero(),
ql: FieldElement::one(),
qr: FieldElement::one(),
qo: -FieldElement::one(),
qc: FieldElement::zero(),
};
let constraint2 = Constraint {
a: 2,
b: 3,
c: 4,
qm: FieldElement::one(),
ql: FieldElement::zero(),
qr: FieldElement::zero(),
qo: -FieldElement::one(),
qc: FieldElement::one(),
};
let constraint_system = ConstraintSystem::new()
.var_num(5)
.public_inputs(vec![1])
.constraints(vec![constraint, constraint2]);
let case_1 = WitnessResult {
witness: vec![1_i128.into(), 1_i128.into(), 2_i128.into(), 3_i128.into()].into(),
public_inputs: vec![FieldElement::one()].into(),
result: true,
};
let case_2 = WitnessResult {
witness: vec![1_i128.into(), 1_i128.into(), 2_i128.into(), 13_i128.into()].into(),
public_inputs: vec![FieldElement::one()].into(),
result: false,
};
test_composer_with_pk_vk(constraint_system, vec![case_1, case_2]);
}
#[test]
fn test_schnorr_constraints() {
let mut signature_indices = [0i32; 64];
for i in 13..(13 + 64) {
signature_indices[i - 13] = i as i32;
}
let result_index = signature_indices.last().unwrap() + 1;
let constraint = SchnorrConstraint {
message: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
public_key_x: 11,
public_key_y: 12,
signature: signature_indices,
result: result_index,
};
let arith_constraint = Constraint {
a: result_index,
b: result_index,
c: result_index,
qm: FieldElement::zero(),
ql: FieldElement::zero(),
qr: FieldElement::zero(),
qo: FieldElement::one(),
qc: -FieldElement::one(),
};
let constraint_system = ConstraintSystem::new()
.var_num(80)
.schnorr_constraints(vec![constraint])
.constraints(vec![arith_constraint]);
let pub_x = FieldElement::from_hex(
"0x17cbd3ed3151ccfd170efe1d54280a6a4822640bf5c369908ad74ea21518a9c5",
)
.unwrap();
let pub_y = FieldElement::from_hex(
"0x0e0456e3795c1a31f20035b741cd6158929eeccd320d299cfcac962865a6bc74",
)
.unwrap();
let sig: [i128; 64] = [
5, 202, 31, 146, 81, 242, 246, 69, 43, 107, 249, 153, 198, 44, 14, 111, 191, 121, 137,
166, 160, 103, 18, 181, 243, 233, 226, 95, 67, 16, 37, 128, 85, 76, 19, 253, 30, 77,
192, 53, 138, 205, 69, 33, 236, 163, 83, 194, 84, 137, 184, 221, 176, 121, 179, 27, 63,
70, 54, 16, 176, 250, 39, 239,
];
let sig_as_scalars: Vec<FieldElement> = sig.into_iter().map(FieldElement::from).collect();
let message: Vec<FieldElement> = vec![
0_i128.into(),
1_i128.into(),
2_i128.into(),
3_i128.into(),
4_i128.into(),
5_i128.into(),
6_i128.into(),
7_i128.into(),
8_i128.into(),
9_i128.into(),
];
let mut witness_values = Vec::new();
witness_values.extend(message);
witness_values.push(pub_x);
witness_values.push(pub_y);
witness_values.extend(sig_as_scalars);
witness_values.push(FieldElement::zero());
let case_1 = WitnessResult {
witness: witness_values.into(),
public_inputs: Assignments::default(),
result: true,
};
test_composer_with_pk_vk(constraint_system, vec![case_1]);
}
#[test]
fn test_ped_constraints() {
let constraint = PedersenConstraint {
inputs: vec![1, 2],
result_x: 3,
result_y: 4,
};
let x_constraint = Constraint {
a: 3,
b: 3,
c: 3,
qm: FieldElement::zero(),
ql: FieldElement::one(),
qr: FieldElement::zero(),
qo: FieldElement::zero(),
qc: -FieldElement::from_hex(
"0x11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef",
)
.unwrap(),
};
let y_constraint = Constraint {
a: 4,
b: 4,
c: 4,
qm: FieldElement::zero(),
ql: FieldElement::one(),
qr: FieldElement::zero(),
qo: FieldElement::zero(),
qc: -FieldElement::from_hex(
"0x0ecf9d98be4597a88c46a7e0fa8836b57a7dcb41ee30f8d8787b11cc259c83fa",
)
.unwrap(),
};
let constraint_system = ConstraintSystem::new()
.var_num(100)
.pedersen_constraints(vec![constraint])
.constraints(vec![x_constraint, y_constraint]);
let scalar_0 = FieldElement::from_hex("0x00").unwrap();
let scalar_1 = FieldElement::from_hex("0x01").unwrap();
let witness_values = vec![scalar_0, scalar_1];
let case_1 = WitnessResult {
witness: witness_values.into(),
public_inputs: Assignments::default(),
result: true,
};
test_composer_with_pk_vk(constraint_system, vec![case_1]);
}
#[test]
fn test_compute_merkle_root_constraint() {
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
let mut msg_hasher: blake2::Blake2s = MessageHasher::new();
let tree: MerkleTree<blake2::Blake2s, Barretenberg> = MerkleTree::new(3, &temp_dir);
let empty_leaf = vec![0; 64];
let index = FieldElement::zero();
let index_as_usize: usize = 0;
let mut index_bits = index.bits();
index_bits.reverse();
let leaf = msg_hasher.hash(&empty_leaf);
let root = tree.root();
let hash_path = tree.get_hash_path(index_as_usize);
let mut hash_path_ref = Vec::new();
for (i, path_pair) in hash_path.into_iter().enumerate() {
let path_bit = index_bits[i];
let hash = if !path_bit { path_pair.1 } else { path_pair.0 };
hash_path_ref.push(hash);
}
let hash_path_ref: Vec<FieldElement> = hash_path_ref.into_iter().collect();
let constraint = ComputeMerkleRootConstraint {
hash_path: (3..3 + hash_path_ref.len() as i32).collect(),
leaf: 0,
index: 1,
result: 2,
};
let constraint_system = ConstraintSystem::new()
.var_num(500)
.compute_merkle_root_constraints(vec![constraint]);
let mut witness_values = vec![leaf, index, root];
witness_values.extend(hash_path_ref);
let case_1 = WitnessResult {
witness: witness_values.into(),
public_inputs: vec![].into(),
result: true,
};
test_composer_with_pk_vk(constraint_system, vec![case_1]);
}
#[test]
fn test_logic_constraints() {
let (constraint_system, case_1) = create_logic_constraint_circuit();
test_composer_with_pk_vk(constraint_system, vec![case_1]);
}
fn create_logic_constraint_circuit() -> (ConstraintSystem, WitnessResult) {
/*
* constraints produced by Noir program:
* fn main(x : u32, y : pub u32) {
* let z = x ^ y;
*
* constrain z != 10;
* }
*/
let range_a = RangeConstraint { a: 1, num_bits: 32 };
let range_b = RangeConstraint { a: 2, num_bits: 32 };
let logic_constraint = LogicConstraint {
a: 1,
b: 2,
result: 3,
num_bits: 32,
is_xor_gate: true,
};
let expr_a = Constraint {
a: 3,
b: 4,
c: 0,
qm: FieldElement::zero(),
ql: FieldElement::one(),
qr: -FieldElement::one(),
qo: FieldElement::zero(),
qc: -FieldElement::from_hex("0x0a").unwrap(),
};
let expr_b = Constraint {
a: 4,
b: 5,
c: 6,
qm: FieldElement::one(),
ql: FieldElement::zero(),
qr: FieldElement::zero(),
qo: -FieldElement::one(),
qc: FieldElement::zero(),
};
let expr_c = Constraint {
a: 4,
b: 6,
c: 4,
qm: FieldElement::one(),
ql: FieldElement::zero(),
qr: FieldElement::zero(),
qo: -FieldElement::one(),
qc: FieldElement::zero(),
};
let expr_d = Constraint {
a: 6,
b: 0,
c: 0,
qm: FieldElement::zero(),
ql: -FieldElement::one(),
qr: FieldElement::zero(),
qo: FieldElement::zero(),
qc: FieldElement::one(),
};
let constraint_system = ConstraintSystem::new()
.var_num(7)
.public_inputs(vec![2])
.range_constraints(vec![range_a, range_b])
.logic_constraints(vec![logic_constraint])
.constraints(vec![expr_a, expr_b, expr_c, expr_d]);
let scalar_5 = FieldElement::from_hex("0x05").unwrap();
let scalar_10 = FieldElement::from_hex("0x0a").unwrap();
let scalar_15 = FieldElement::from_hex("0x0f").unwrap();
let scalar_5_inverse = scalar_5.inverse();
let witness_values = vec![
scalar_5,
scalar_10,
scalar_15,
scalar_5,
scalar_5_inverse,
FieldElement::one(),
];
let case = WitnessResult {
witness: witness_values.into(),
public_inputs: vec![scalar_10].into(),
result: true,
};
(constraint_system, case)
}
#[test]
fn test_recursion_constraint() {
let (inner_constraint_system, inner_witness_res) = create_logic_constraint_circuit();
let bb = Barretenberg::new();
let proving_key = bb.compute_proving_key(&inner_constraint_system);
let verification_key = bb.compute_verification_key(&inner_constraint_system, &proving_key);
let proof = bb.create_proof_with_pk(
&inner_constraint_system,
inner_witness_res.witness,
&proving_key,
true,
);
let verified = bb.verify_with_vk(
&inner_constraint_system,
&proof,