-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsampleCalls.cpp
2028 lines (1738 loc) · 74.7 KB
/
sampleCalls.cpp
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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "zendoo_mc.h"
#include "doctest.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cassert>
#include <vector>
#include <time.h>
static const uint32_t MAX_SEGMENT_SIZE = 1 << 9;
static bool dlog_keys_init_result([]{
CctpErrorCode ret_code = CctpErrorCode::OK;
// Bootstrap keys
bool init_result = zendoo_init_dlog_keys(
MAX_SEGMENT_SIZE,
&ret_code
);
assert(init_result == true);
assert(ret_code == CctpErrorCode::OK);
return init_result;
}());
static bool init_zendoo([]{
CctpErrorCode ret_code = CctpErrorCode::OK;
const char* log_config_path = "../src/tests/log/sample_log_config.yaml";
zendoo_init_logger(
(path_char_t const*) log_config_path,
strlen(log_config_path),
&ret_code
);
assert(ret_code == CctpErrorCode::OK);
return true;
}());
TEST_SUITE("Field Element") {
TEST_CASE("Field Size") {
int field_len = zendoo_get_field_size_in_bytes();
CHECK(field_len == FIELD_SIZE);
}
TEST_CASE("Positive serialize/deserialize"){
CctpErrorCode ret_code = CctpErrorCode::OK;
// Check correct serialization
auto field = zendoo_get_random_field();
//Serialize and deserialize and check equality
unsigned char field_bytes[FIELD_SIZE];
zendoo_serialize_field(field, field_bytes, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Check correct deserialization
auto field_deserialized = zendoo_deserialize_field(field_bytes, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Check equality
CHECK(zendoo_field_assert_eq(field, field_deserialized));
zendoo_field_free(field);
zendoo_field_free(field_deserialized);
}
TEST_CASE("Negative serialize/deserialize"){
CctpErrorCode ret_code = CctpErrorCode::OK;
//Serialize and deserialize and check equality
unsigned char field_bytes[FIELD_SIZE] = {
64, 192, 222, 36, 97, 22, 129, 41, 101, 218, 34, 193, 41, 200, 74, 248,
126, 226, 209, 85, 85, 50, 64, 27, 23, 69, 240, 210, 79, 85, 196, 3
};
// Check correct deserialization
auto correct_field_deserialized = zendoo_deserialize_field(field_bytes, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Modify a byte of field_bytes and deserialize
field_bytes[0] = 0;
auto wrong_field_deserialized = zendoo_deserialize_field(field_bytes, &ret_code);
// Check equality
CHECK_FALSE(zendoo_field_assert_eq(correct_field_deserialized, wrong_field_deserialized));
// Free memory
zendoo_field_free(correct_field_deserialized);
zendoo_field_free(wrong_field_deserialized);
}
TEST_CASE("Edge cases serialize/deserialize"){
CctpErrorCode ret_code = CctpErrorCode::OK;
// Attempt to deserialize a field element over the modulus
unsigned char over_the_modulus_fe[FIELD_SIZE] = {
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255
};
auto field_deserialized = zendoo_deserialize_field(over_the_modulus_fe, &ret_code);
CHECK(field_deserialized == NULL);
CHECK(ret_code == CctpErrorCode::InvalidValue);
zendoo_field_free(field_deserialized);
}
}
TEST_SUITE("Poseidon Hash") {
static const unsigned char expected_result_bytes[FIELD_SIZE] = {
254, 126, 175, 176, 130, 2, 161, 183, 90, 48, 41, 150, 100, 148, 142, 37,
122, 246, 6, 134, 190, 158, 5, 195, 112, 148, 148, 144, 106, 91, 234, 5
};
TEST_CASE("Constant Length Poseidon Hash") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Init digest
auto digest = ZendooPoseidonHashConstantLength(2, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
//Update with 1 field element
auto lhs = zendoo_get_field_from_long(1);
digest.update(lhs, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Trying to finalize without having reached the
// specified input size will cause an error
auto result_before = digest.finalize(&ret_code);
CHECK(result_before == NULL);
CHECK(ret_code == CctpErrorCode::HashingError);
// Update with 1 field element
auto rhs = zendoo_get_field_from_long(2);
digest.update(rhs, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Finalize hash
auto result = digest.finalize(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Check result is equal to the expected one
auto expected_result = zendoo_deserialize_field(expected_result_bytes, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(result, expected_result));
// Finalize is idempotent
auto result_copy = digest.finalize(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(result, result_copy));
// Update once and more and assert that trying to finalize with more
// inputs than the ones specified at creation will result in an error.
auto additional_input = zendoo_get_field_from_long(3);
digest.update(additional_input, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
auto result_after = digest.finalize(&ret_code);
CHECK(result_after == NULL);
CHECK(ret_code == CctpErrorCode::HashingError);
// Free memory
zendoo_field_free(lhs);
zendoo_field_free(rhs);
zendoo_field_free(result);
zendoo_field_free(expected_result);
zendoo_field_free(result_copy);
zendoo_field_free(additional_input);
// Once out of scope the destructor of ZendooPoseidonHash will automatically free the memory Rust-side
// for digest
}
TEST_CASE("Variable Length Poseidon Hash mod rate") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Init digest
auto digest = ZendooPoseidonHashVariableLength(true, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
//Update with 1 field element
auto lhs = zendoo_get_field_from_long(1);
digest.update(lhs, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Trying to finalize with an input size non mod rate
// will result in an error
auto result_before = digest.finalize(&ret_code);
CHECK(result_before == NULL);
CHECK(ret_code == CctpErrorCode::HashingError);
// Update with 1 field element
auto rhs = zendoo_get_field_from_long(2);
digest.update(rhs, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Finalize hash
auto result = digest.finalize(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Check result is equal to the expected one.
// Result is also the same of the constant length poseidon hash
// (no unnecessary padding is added)
auto expected_result = zendoo_deserialize_field(expected_result_bytes, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(result, expected_result));
// Finalize is idempotent
auto result_copy = digest.finalize(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(result, result_copy));
// Update once and more and assert that trying to finalize
// with an input non mod rate will result in an error
auto additional_input = zendoo_get_field_from_long(3);
digest.update(additional_input, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
auto result_after = digest.finalize(&ret_code);
CHECK(result_after == NULL);
CHECK(ret_code == CctpErrorCode::HashingError);
// Free memory
zendoo_field_free(lhs);
zendoo_field_free(rhs);
zendoo_field_free(result);
zendoo_field_free(expected_result);
zendoo_field_free(result_copy);
zendoo_field_free(additional_input);
// Once out of scope the destructor of ZendooPoseidonHash will automatically free the memory Rust-side
// for digest
}
TEST_CASE("Variable Length Poseidon Hash NON mod rate") {
CctpErrorCode ret_code = CctpErrorCode::OK;
unsigned char expected_result_bytes_variable_length[FIELD_SIZE] = {
212, 129, 183, 174, 117, 46, 61, 128, 124, 74, 158, 233, 177, 251, 225, 0,
99, 148, 140, 105, 239, 1, 217, 66, 106, 133, 62, 197, 131, 215, 206, 28
};
// Init digest
auto digest = ZendooPoseidonHashVariableLength(false, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
//Update with 1 field element
auto lhs = zendoo_get_field_from_long(1);
digest.update(lhs, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// It's possible to finalize in any moment (padding will be performed)
auto result_before = digest.finalize(&ret_code);
CHECK(result_before != NULL);
CHECK(ret_code == CctpErrorCode::OK);
// Update with 1 field element
auto rhs = zendoo_get_field_from_long(2);
digest.update(rhs, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Finalize hash
auto result = digest.finalize(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Check result is equal to the expected one.
auto expected_result = zendoo_deserialize_field(expected_result_bytes_variable_length, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(result, expected_result));
// Finalize is idempotent
auto result_copy = digest.finalize(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(result, result_copy));
// It's possible to finalize in any moment (padding will be performed)
auto additional_input = zendoo_get_field_from_long(3);
digest.update(additional_input, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
auto result_after = digest.finalize(&ret_code);
CHECK(result_after != NULL);
CHECK(ret_code == CctpErrorCode::OK);
// Free memory
zendoo_field_free(lhs);
zendoo_field_free(rhs);
zendoo_field_free(result_before);
zendoo_field_free(result);
zendoo_field_free(expected_result);
zendoo_field_free(result_copy);
zendoo_field_free(result_after);
zendoo_field_free(additional_input);
// Once out of scope the destructor of ZendooPoseidonHash will automatically free the memory Rust-side
// for digest
}
}
TEST_CASE("Merkle Tree") {
size_t height = 5;
CctpErrorCode ret_code = CctpErrorCode::OK;
// Deserialize root
unsigned char expected_root_bytes[FIELD_SIZE] = {
113, 174, 41, 1, 227, 14, 47, 27, 44, 172, 21, 18, 63, 182, 174, 162, 239, 251,
93, 88, 43, 221, 235, 253, 30, 110, 180, 114, 134, 192, 15, 20
};
auto expected_root = zendoo_deserialize_field(expected_root_bytes, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
//Generate leaves
int leaves_len = 32;
const field_t* leaves[leaves_len];
for (int i = 0; i < leaves_len; i++){
leaves[i] = zendoo_get_field_from_long(i);
}
// Initialize tree
auto tree = ZendooGingerMerkleTree(height, leaves_len, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Add leaves to tree
for (int i = 0; i < leaves_len; i++){
tree.append(leaves[i], &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
}
// Adding more leaves than the tree size should result in an error
tree.append(leaves[0], &ret_code);
CHECK(ret_code == CctpErrorCode::MerkleTreeError);
// Asking for the root of a non-finalized tree should result in an error
auto null_root = tree.root(&ret_code);
CHECK(null_root == NULL);
CHECK(ret_code == CctpErrorCode::MerkleRootBuildError);
// Asking for a merkle path of a non-finalized tree should result in an error
auto path = tree.get_merkle_path(0, &ret_code);
CHECK(path == NULL);
CHECK(ret_code == CctpErrorCode::MerkleTreeError);
// Finalize tree
tree.finalize_in_place(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Compute root and assert equality with expected one
auto root = tree.root(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(root, expected_root));
// It is the same by calling finalize()
auto tree_copy = tree.finalize(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
auto root_copy = tree_copy.root(&ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(root_copy, root));
auto wrong_root = zendoo_get_random_field();
// Test Merkle Paths
for (int i = 0; i < leaves_len; i++) {
// Get Merkle Path
auto path = tree.get_merkle_path(i, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
// Verify Merkle Path
CHECK(zendoo_verify_ginger_merkle_path(path, height, (field_t*)leaves[i], root, &ret_code));
CHECK(ret_code == CctpErrorCode::OK);
// Negative test: verify MerklePath for a wrong root and assert failure
CHECK_FALSE(zendoo_verify_ginger_merkle_path(path, height, (field_t*)leaves[i], wrong_root, &ret_code));
CHECK(ret_code == CctpErrorCode::OK);
zendoo_free_ginger_merkle_path(path);
}
// Free memory
for (int i = 0; i < leaves_len; i++){
zendoo_field_free((field_t*)leaves[i]);
}
zendoo_field_free(root);
zendoo_field_free(root_copy);
zendoo_field_free(expected_root);
zendoo_field_free(wrong_root);
// Once out of scope the destructor of ZendooGingerMerkleTree will automatically free
// the memory Rust-side for tree and tree_copy
}
void check_root(unsigned char expected_root_bytes[], field_t* actual_root) {
CctpErrorCode ret_code = CctpErrorCode::OK;
auto expected_root = zendoo_deserialize_field(expected_root_bytes, &ret_code);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(zendoo_field_assert_eq(expected_root, actual_root));
zendoo_field_free(expected_root);
zendoo_field_free(actual_root);
}
TEST_CASE("Commitment Tree") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Get commitment tree instance
auto cmt = zendoo_commitment_tree_create();
// Test empty tree root
auto empty_root = zendoo_commitment_tree_get_commitment(cmt, &ret_code);
CHECK(empty_root != NULL);
CHECK(ret_code == CctpErrorCode::OK);
unsigned char expected_empty_root_bytes[FIELD_SIZE] = {
102, 212, 1, 47, 102, 212, 117, 139, 51, 210, 40, 137, 149, 110, 212, 157,
149, 193, 18, 216, 145, 99, 127, 83, 230, 240, 0, 196, 108, 233, 101, 13
};
check_root(expected_empty_root_bytes, empty_root);
// Add SCC with random data
auto sc_id = zendoo_get_field_from_long(1);
uint64_t amount = 100;
uint64_t btr_fee = 1000;
uint64_t ft_min_amount = 5000;
uint32_t out_idx = 2;
uint32_t withdrawal_epoch_length = 10;
uint8_t mc_btr_request_data_length = 255;
std::vector<unsigned char> pub_key_vec(FIELD_SIZE, 255);
auto pub_key = BufferWithSize(pub_key_vec.data(), pub_key_vec.size());
std::vector<unsigned char> tx_hash_vec(FIELD_SIZE, 255);
auto tx_hash = BufferWithSize(tx_hash_vec.data(), tx_hash_vec.size());
std::vector<unsigned char> custom_field_elements_config_vec(5, 1);
auto custom_field_elements_config = BufferWithSize(custom_field_elements_config_vec.data(), custom_field_elements_config_vec.size());
size_t custom_bv_elements_config_len = 10;
const BitVectorElementsConfig custom_bv_elements_config[custom_bv_elements_config_len] = { {2000, 1500} };
std::vector<unsigned char> custom_creation_data_vec(7, 10);
auto custom_creation_data = BufferWithSize(custom_creation_data_vec.data(), custom_creation_data_vec.size());
std::vector<unsigned char> cert_vk_vec(2000, 222);
auto cert_vk = BufferWithSize(cert_vk_vec.data(), cert_vk_vec.size());
CHECK(zendoo_commitment_tree_add_scc(
cmt, sc_id, amount, &pub_key, &tx_hash, out_idx, withdrawal_epoch_length,
mc_btr_request_data_length, &custom_field_elements_config,
custom_bv_elements_config, custom_bv_elements_config_len,
btr_fee, ft_min_amount, &custom_creation_data, NULL,
&cert_vk, NULL, &ret_code
) == true);
CHECK(ret_code == CctpErrorCode::OK);
// Test root after add scc
auto root_after_scc = zendoo_commitment_tree_get_commitment(cmt, &ret_code);
CHECK(root_after_scc != NULL);
CHECK(ret_code == CctpErrorCode::OK);
unsigned char expected_root_after_scc_bytes[FIELD_SIZE] = {
166, 173, 139, 78, 105, 234, 68, 33, 65, 9, 233, 183, 187, 254, 31, 32,
108, 89, 112, 235, 163, 14, 114, 28, 58, 169, 97, 183, 11, 168, 106, 63
};
check_root(expected_root_after_scc_bytes, root_after_scc);
// Add fwt with random data
std::vector<unsigned char> mc_return_address_vec(MC_PK_SIZE, 222);
auto mc_return_address = BufferWithSize(mc_return_address_vec.data(), mc_return_address_vec.size());
CHECK(zendoo_commitment_tree_add_fwt(cmt, sc_id, amount, &pub_key, &mc_return_address, &tx_hash, out_idx, &ret_code) == true);
CHECK(ret_code == CctpErrorCode::OK);
// Test root after add fwt
auto root_after_fwt = zendoo_commitment_tree_get_commitment(cmt, &ret_code);
CHECK(root_after_fwt != NULL);
CHECK(ret_code == CctpErrorCode::OK);
unsigned char expected_root_after_fwt_bytes[FIELD_SIZE] = {
149, 250, 105, 188, 215, 88, 246, 3, 5, 100, 189, 8, 248, 94, 208, 254,
158, 166, 97, 10, 162, 131, 153, 14, 26, 90, 101, 54, 11, 23, 9, 14
};
check_root(expected_root_after_fwt_bytes, root_after_fwt);
// Add bwtr with random data
uint64_t sc_fee = 3333;
auto nullifier = zendoo_get_field_from_long(2);
auto end_cum_comm_tree_root = zendoo_get_field_from_long(3);
std::vector<unsigned char> mc_pk_hash_vec(MC_PK_SIZE, 200);
auto mc_pk_hash = BufferWithSize(mc_pk_hash_vec.data(), mc_pk_hash_vec.size());
const field_t* sc_req_data[] = {sc_id, nullifier, end_cum_comm_tree_root};
CHECK(zendoo_commitment_tree_add_bwtr(cmt, sc_id, sc_fee, sc_req_data, 3, &mc_pk_hash, &tx_hash, out_idx, &ret_code) == true);
CHECK(ret_code == CctpErrorCode::OK);
// Test root after add bwtr
auto root_after_bwtr = zendoo_commitment_tree_get_commitment(cmt, &ret_code);
CHECK(root_after_bwtr != NULL);
CHECK(ret_code == CctpErrorCode::OK);
unsigned char expected_root_after_bwtr_bytes[FIELD_SIZE] = {
227, 174, 46, 213, 92, 144, 79, 234, 218, 194, 210, 247, 229, 226, 142, 7,
40, 248, 93, 132, 155, 183, 167, 61, 72, 203, 63, 176, 173, 82, 251, 10
};
check_root(expected_root_after_bwtr_bytes, root_after_bwtr);
// Add csw with random data
auto new_sc_id = zendoo_get_field_from_long(5); //use new sc_id for csw (it's part of sc_tree_ceased)
CHECK(zendoo_commitment_tree_add_csw(cmt, new_sc_id, amount, nullifier, &mc_pk_hash, &ret_code) == true);
CHECK(ret_code == CctpErrorCode::OK);
// Test root after add csw
auto root_after_csw = zendoo_commitment_tree_get_commitment(cmt, &ret_code);
CHECK(root_after_csw != NULL);
CHECK(ret_code == CctpErrorCode::OK);
unsigned char expected_root_after_csw_bytes[FIELD_SIZE] = {
221, 172, 13, 226, 58, 169, 19, 248, 21, 100, 252, 4, 175, 180, 92, 142,
242, 145, 59, 175, 118, 152, 238, 224, 151, 26, 220, 243, 37, 12, 97, 23
};
check_root(expected_root_after_csw_bytes, root_after_csw);
// Add cert with random data
uint32_t epoch_number = 10;
uint64_t quality = 4444;
size_t bt_list_len = 10;
std::vector<backward_transfer_t> bt_list;
if (bt_list_len != 0) {
for(int i = 0; i < bt_list_len; i++){
bt_list.push_back({{255}, 10});
}
}
CHECK(zendoo_commitment_tree_add_cert(
cmt, sc_id, epoch_number, quality, bt_list.data(), bt_list_len, NULL, 0,
end_cum_comm_tree_root, btr_fee, ft_min_amount, &ret_code
) == true);
CHECK(ret_code == CctpErrorCode::OK);
// Test root after add cert
auto root_after_cert = zendoo_commitment_tree_get_commitment(cmt, &ret_code);
CHECK(root_after_cert != NULL);
CHECK(ret_code == CctpErrorCode::OK);
unsigned char expected_root_after_cert_bytes[FIELD_SIZE] = {
51, 180, 196, 214, 89, 68, 232, 156, 104, 198, 180, 202, 189, 126, 41, 26,
91, 187, 146, 36, 9, 8, 197, 236, 228, 15, 106, 111, 50, 183, 194, 19
};
check_root(expected_root_after_cert_bytes, root_after_cert);
zendoo_field_free(sc_id);
zendoo_field_free(new_sc_id);
zendoo_field_free(nullifier);
zendoo_field_free(end_cum_comm_tree_root);
zendoo_commitment_tree_delete(cmt);
}
TEST_SUITE("Bit Vector") {
void compress_decompress(CompressionAlgorithm algo) {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate test data
//2^12 * 254 = 130048 bytes
int bit_vec_dim = 130048;
std::vector<unsigned char> bit_vec(bit_vec_dim , 111);
auto bit_vec_bwt = BufferWithSize(bit_vec.data(), bit_vec.size());
// Compress bit vec
auto compressed_bit_vec_bwt = zendoo_compress_bit_vector(&bit_vec_bwt, algo, &ret_code);
CHECK(compressed_bit_vec_bwt != NULL);
CHECK(ret_code == CctpErrorCode::OK);
// Get root from compressed bytes
auto root = zendoo_merkle_root_from_compressed_bytes(compressed_bit_vec_bwt, bit_vec_dim, &ret_code);
unsigned char expected_root[FIELD_SIZE] = {
108, 210, 228, 91, 128, 218, 226, 40, 27, 129, 78, 6, 2, 4, 217,
120, 17, 147, 56, 236, 6, 120, 85, 112, 229, 38, 56, 227, 56, 16, 109, 34
};
check_root(expected_root, root);
// Decompress bit vec
auto decompressed_bit_vec_bwt = zendoo_decompress_bit_vector(compressed_bit_vec_bwt, bit_vec_dim, &ret_code);
CHECK(decompressed_bit_vec_bwt != NULL);
CHECK(ret_code == CctpErrorCode::OK);
// Check equality
for (int i = 0; i < bit_vec_dim; i++){
if (bit_vec_bwt.data[i] != decompressed_bit_vec_bwt->data[i]) {
CHECK(false); // Fail
}
}
// Free memory
zendoo_free_bws(compressed_bit_vec_bwt);
zendoo_free_bws(decompressed_bit_vec_bwt);
}
TEST_CASE("Compress/Decompress") {
compress_decompress(CompressionAlgorithm::Gzip);
compress_decompress(CompressionAlgorithm::Bzip2);
}
}
TEST_SUITE("Single Proof Verifier") {
static std::string params_dir = std::string("../examples");
static size_t params_dir_len = params_dir.size();
static const uint32_t NUM_CONSTRAINTS = 1 << 10;
void create_verify_cert_proof(
size_t numBt,
bool zk,
std::string proof_path,
std::string pk_path,
std::string vk_path,
bool constant_present,
bool prev_hash_present = false
) {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate random data
auto sc_id = zendoo_get_field_from_long(10);
field_t* constant = NULL;
if (constant_present) {
constant = zendoo_get_field_from_long(1);
}
auto end_cum_comm_tree_root = zendoo_get_field_from_long(2);
uint32_t epoch_number = 10;
uint64_t quality = 100;
uint64_t btr_fee = 1000;
uint64_t ft_min_amount = 5000;
field_t* prev_hash = prev_hash_present ? zendoo_get_field_from_long(7) : nullptr;
//Create dummy bt list
size_t bt_list_len = numBt;
std::vector<backward_transfer_t> bt_list;
backward_transfer_t* bt_list_ptr = NULL;
if (bt_list_len != 0) {
for(int i = 0; i < bt_list_len; i++){
bt_list.push_back({{255}, 10});
}
bt_list_ptr = bt_list.data();
}
// Create dummy custom_fields re-using fields we already have
const field_t* custom_fields[] = {sc_id, end_cum_comm_tree_root};
// Specify paths
auto pk_ps_type = zendoo_get_sc_pk_proving_system_type_from_file(
(path_char_t*)pk_path.c_str(),
pk_path.size(),
&ret_code
);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(pk_ps_type != ProvingSystem::Undefined);
auto sc_pk = zendoo_deserialize_sc_pk_from_file(
(path_char_t*)pk_path.c_str(),
pk_path.size(),
true,
&ret_code
);
CHECK(sc_pk != NULL);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(
zendoo_create_cert_test_proof(
zk, constant, sc_id, epoch_number, quality, bt_list_ptr, bt_list_len,
custom_fields, 2, end_cum_comm_tree_root, btr_fee, ft_min_amount,
sc_pk, (path_char_t*)proof_path.c_str(), proof_path.size(), NUM_CONSTRAINTS, prev_hash, &ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
// Verify proof with correct data
auto proof_ps_type = zendoo_get_sc_proof_proving_system_type_from_file(
(path_char_t*)proof_path.c_str(),
proof_path.size(),
&ret_code
);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(proof_ps_type != ProvingSystem::Undefined);
CHECK(proof_ps_type == pk_ps_type);
auto sc_proof = zendoo_deserialize_sc_proof_from_file(
(path_char_t*)proof_path.c_str(),
proof_path.size(),
true,
&ret_code
);
CHECK(sc_proof != NULL);
CHECK(ret_code == CctpErrorCode::OK);
auto vk_ps_type = zendoo_get_sc_vk_proving_system_type_from_file(
(path_char_t*)vk_path.c_str(),
vk_path.size(),
&ret_code
);
CHECK(ret_code == CctpErrorCode::OK);
CHECK(vk_ps_type != ProvingSystem::Undefined);
CHECK(proof_ps_type == vk_ps_type);
auto sc_vk = zendoo_deserialize_sc_vk_from_file(
(path_char_t*)vk_path.c_str(),
vk_path.size(),
true,
&ret_code
);
CHECK(sc_vk != NULL);
CHECK(ret_code == CctpErrorCode::OK);
// Positive verification
CHECK(
zendoo_verify_certificate_proof(
constant, sc_id, epoch_number, quality, bt_list_ptr, bt_list_len,
custom_fields, 2, end_cum_comm_tree_root,
btr_fee, ft_min_amount, sc_proof, sc_vk, prev_hash, &ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
// Negative verification
auto wrong_sc_id = zendoo_get_field_from_long(2);
CHECK(
zendoo_verify_certificate_proof(
constant, wrong_sc_id, epoch_number, quality, bt_list_ptr, bt_list_len,
custom_fields, 2, end_cum_comm_tree_root, btr_fee, ft_min_amount,
sc_proof, sc_vk, prev_hash, &ret_code
) == false
);
CHECK(ret_code == CctpErrorCode::OK);
// Free memory
zendoo_field_free(sc_id);
zendoo_field_free(constant);
zendoo_field_free(wrong_sc_id);
zendoo_field_free(end_cum_comm_tree_root);
zendoo_sc_pk_free(sc_pk);
zendoo_sc_vk_free(sc_vk);
zendoo_sc_proof_free(sc_proof);
// Destroy proof file
remove(proof_path.c_str());
}
TEST_CASE("Proof Verifier: Cert - Coboundary Marlin") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::Certificate,
ProvingSystem::CoboundaryMarlin,
NUM_CONSTRAINTS,
false,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/cob_marlin_cert_test_proof");
auto pk_path = params_dir + std::string("/cob_marlin_cert_test_pk");
auto vk_path = params_dir + std::string("/cob_marlin_cert_test_vk");
// Test all cases
create_verify_cert_proof(10, true, proof_path, pk_path, vk_path, true);
create_verify_cert_proof(0, true, proof_path, pk_path, vk_path, true);
create_verify_cert_proof(10, false, proof_path, pk_path, vk_path, true);
create_verify_cert_proof(0, false, proof_path, pk_path, vk_path, true);
// Delete files
remove(pk_path.c_str());
remove(vk_path.c_str());
}
TEST_CASE("Proof Verifier: Cert - Coboundary Marlin - Keyrot") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::Certificate,
ProvingSystem::CoboundaryMarlin,
NUM_CONSTRAINTS,
true,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/cob_marlin_cert_test_proof");
auto pk_path = params_dir + std::string("/cob_marlin_cert_test_pk");
auto vk_path = params_dir + std::string("/cob_marlin_cert_test_vk");
// Test all cases
create_verify_cert_proof(10, true, proof_path, pk_path, vk_path, true, true);
create_verify_cert_proof(0, true, proof_path, pk_path, vk_path, true, true);
create_verify_cert_proof(10, false, proof_path, pk_path, vk_path, true, true);
create_verify_cert_proof(0, false, proof_path, pk_path, vk_path, true, true);
// Delete files
remove(pk_path.c_str());
remove(vk_path.c_str());
}
TEST_CASE("Proof Verifier: CertNoConst - Coboundary Marlin") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::CertificateNoConstant,
ProvingSystem::CoboundaryMarlin,
NUM_CONSTRAINTS,
false,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/cob_marlin_cert_no_const_test_proof");
auto pk_path = params_dir + std::string("/cob_marlin_cert_no_const_test_pk");
auto vk_path = params_dir + std::string("/cob_marlin_cert_no_const_test_vk");
// Test all cases
create_verify_cert_proof(10, true, proof_path, pk_path, vk_path, false);
create_verify_cert_proof(0, true, proof_path, pk_path, vk_path, false);
create_verify_cert_proof(10, false, proof_path, pk_path, vk_path, false);
create_verify_cert_proof(0, false, proof_path, pk_path, vk_path, false);
// Delete files
remove(pk_path.c_str());
remove(vk_path.c_str());
}
TEST_CASE("Proof Verifier: CertNoConst - Coboundary Marlin - Keyrot") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::CertificateNoConstant,
ProvingSystem::CoboundaryMarlin,
NUM_CONSTRAINTS,
true,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/cob_marlin_cert_no_const_test_proof");
auto pk_path = params_dir + std::string("/cob_marlin_cert_no_const_test_pk");
auto vk_path = params_dir + std::string("/cob_marlin_cert_no_const_test_vk");
// Test all cases
create_verify_cert_proof(10, true, proof_path, pk_path, vk_path, false, true);
create_verify_cert_proof(0, true, proof_path, pk_path, vk_path, false, true);
create_verify_cert_proof(10, false, proof_path, pk_path, vk_path, false, true);
create_verify_cert_proof(0, false, proof_path, pk_path, vk_path, false, true);
// Delete files
remove(pk_path.c_str());
remove(vk_path.c_str());
}
TEST_CASE("Proof Verifier: Cert - Darlin") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::Certificate,
ProvingSystem::Darlin,
NUM_CONSTRAINTS,
false,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/darlin_cert_test_proof");
auto pk_path = params_dir + std::string("/darlin_cert_test_pk");
auto vk_path = params_dir + std::string("/darlin_cert_test_vk");
// Test all cases
create_verify_cert_proof(10, true, proof_path, pk_path, vk_path, true);
create_verify_cert_proof(0, true, proof_path, pk_path, vk_path, true);
create_verify_cert_proof(10, false, proof_path, pk_path, vk_path, true);
create_verify_cert_proof(0, false, proof_path, pk_path, vk_path, true);
// Delete files
remove(pk_path.c_str());
remove(vk_path.c_str());
}
TEST_CASE("Proof Verifier: Cert - Darlin - Keyrot") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::Certificate,
ProvingSystem::Darlin,
NUM_CONSTRAINTS,
true,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/darlin_cert_test_proof");
auto pk_path = params_dir + std::string("/darlin_cert_test_pk");
auto vk_path = params_dir + std::string("/darlin_cert_test_vk");
// Test all cases
create_verify_cert_proof(10, true, proof_path, pk_path, vk_path, true, true);
create_verify_cert_proof(0, true, proof_path, pk_path, vk_path, true, true);
create_verify_cert_proof(10, false, proof_path, pk_path, vk_path, true, true);
create_verify_cert_proof(0, false, proof_path, pk_path, vk_path, true, true);
// Delete files
remove(pk_path.c_str());
remove(vk_path.c_str());
}
TEST_CASE("Proof Verifier: CertNoConst - Darlin") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::CertificateNoConstant,
ProvingSystem::Darlin,
NUM_CONSTRAINTS,
false,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/darlin_cert_no_const_test_proof");
auto pk_path = params_dir + std::string("/darlin_cert_no_const_test_pk");
auto vk_path = params_dir + std::string("/darlin_cert_no_const_test_vk");
// Test all cases
create_verify_cert_proof(10, true, proof_path, pk_path, vk_path, false);
create_verify_cert_proof(0, true, proof_path, pk_path, vk_path, false);
create_verify_cert_proof(10, false, proof_path, pk_path, vk_path, false);
create_verify_cert_proof(0, false, proof_path, pk_path, vk_path, false);
// Delete files
remove(pk_path.c_str());
remove(vk_path.c_str());
}
TEST_CASE("Proof Verifier: CertNoConst - Darlin - Keyrot") {
CctpErrorCode ret_code = CctpErrorCode::OK;
// Generate cert test circuit pk and vk
CHECK(
zendoo_generate_mc_test_params(
TestCircuitType::CertificateNoConstant,
ProvingSystem::Darlin,
NUM_CONSTRAINTS,
true,
(path_char_t*)params_dir.c_str(),
params_dir_len,
&ret_code
) == true
);
CHECK(ret_code == CctpErrorCode::OK);
auto proof_path = params_dir + std::string("/darlin_cert_no_const_test_proof");
auto pk_path = params_dir + std::string("/darlin_cert_no_const_test_pk");
auto vk_path = params_dir + std::string("/darlin_cert_no_const_test_vk");
// Test all cases