-
Notifications
You must be signed in to change notification settings - Fork 114
/
tests.c
2386 lines (1948 loc) · 73.2 KB
/
tests.c
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
/*
* This file contains unit tests for C-KZG-4844.
*/
#include "ckzg.c"
#include "tinytest.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#ifdef PROFILE
#include <gperftools/profiler.h>
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
// Globals
////////////////////////////////////////////////////////////////////////////////////////////////////
KZGSettings s;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Helper functions
////////////////////////////////////////////////////////////////////////////////////////////////////
static void get_rand_bytes32(Bytes32 *out) {
static uint64_t seed = 0;
blst_sha256(out->bytes, (uint8_t *)&seed, sizeof(seed));
seed++;
}
static void get_rand_field_element(Bytes32 *out) {
fr_t tmp_fr;
Bytes32 tmp_bytes;
/*
* Take 32 random bytes, make them an Fr, and then
* turn the Fr back to a bytes array.
*/
get_rand_bytes32(&tmp_bytes);
hash_to_bls_field(&tmp_fr, &tmp_bytes);
bytes_from_bls_field(out, &tmp_fr);
}
static void get_rand_fr(fr_t *out) {
Bytes32 tmp_bytes;
get_rand_bytes32(&tmp_bytes);
hash_to_bls_field(out, &tmp_bytes);
}
static void get_rand_blob(Blob *out) {
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
get_rand_field_element((Bytes32 *)&out->bytes[i * 32]);
}
}
static void get_rand_g1_bytes(Bytes48 *out) {
C_KZG_RET ret;
Blob blob;
/*
* Get the commitment to a random blob.
* This commitment is a valid g1 point.
*/
get_rand_blob(&blob);
ret = blob_to_kzg_commitment(out, &blob, &s);
ASSERT_EQUALS(ret, C_KZG_OK);
}
static void get_rand_g1(g1_t *out) {
Bytes32 tmp_bytes;
get_rand_bytes32(&tmp_bytes);
blst_hash_to_g1(out, tmp_bytes.bytes, 32, NULL, 0, NULL, 0);
}
static void get_rand_g2(g2_t *out) {
Bytes32 tmp_bytes;
get_rand_bytes32(&tmp_bytes);
blst_hash_to_g2(out, tmp_bytes.bytes, 32, NULL, 0, NULL, 0);
}
static void bytes32_from_hex(Bytes32 *out, const char *hex) {
int matches;
for (size_t i = 0; i < sizeof(Bytes32); i++) {
matches = sscanf(hex + i * 2, "%2hhx", &out->bytes[i]);
ASSERT_EQUALS(matches, 1);
}
}
static void bytes48_from_hex(Bytes48 *out, const char *hex) {
int matches;
for (size_t i = 0; i < sizeof(Bytes48); i++) {
matches = sscanf(hex + i * 2, "%2hhx", &out->bytes[i]);
ASSERT_EQUALS(matches, 1);
}
}
static void get_rand_uint64(uint64_t *out) {
Bytes32 b;
get_rand_bytes32(&b);
memcpy(out, b.bytes, sizeof(*out));
}
static void eval_poly(fr_t *out, fr_t *poly_coefficients, fr_t *x) {
*out = poly_coefficients[FIELD_ELEMENTS_PER_BLOB - 1];
for (size_t i = FIELD_ELEMENTS_PER_BLOB - 1; i > 0; i--) {
blst_fr_mul(out, out, x);
blst_fr_add(out, out, &poly_coefficients[i - 1]);
}
}
static void eval_extended_poly(fr_t *out, fr_t *poly_coefficients, fr_t *x) {
*out = poly_coefficients[FIELD_ELEMENTS_PER_EXT_BLOB - 1];
for (size_t i = FIELD_ELEMENTS_PER_EXT_BLOB - 1; i > 0; i--) {
blst_fr_mul(out, out, x);
blst_fr_add(out, out, &poly_coefficients[i - 1]);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for memory allocation functions
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_c_kzg_malloc__succeeds_size_greater_than_zero(void) {
void *ptr = NULL;
C_KZG_RET ret = c_kzg_malloc(&ptr, 123);
bool is_null = ptr == NULL;
c_kzg_free(ptr);
ASSERT_EQUALS(ret, C_KZG_OK);
ASSERT_EQUALS(is_null, false);
}
static void test_c_kzg_malloc__fails_size_equal_to_zero(void) {
void *ptr = NULL;
C_KZG_RET ret = c_kzg_malloc(&ptr, 0);
bool is_null = ptr == NULL;
c_kzg_free(ptr);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
ASSERT_EQUALS(is_null, true);
}
static void test_c_kzg_malloc__fails_too_big(void) {
void *ptr = NULL;
C_KZG_RET ret = c_kzg_malloc(&ptr, UINT64_MAX);
bool is_null = ptr == NULL;
c_kzg_free(ptr);
ASSERT_EQUALS(ret, C_KZG_MALLOC);
ASSERT_EQUALS(is_null, true);
}
static void test_c_kzg_calloc__succeeds_size_greater_than_zero(void) {
void *ptr = NULL;
C_KZG_RET ret = c_kzg_calloc(&ptr, 123, 456);
bool is_null = ptr == NULL;
c_kzg_free(ptr);
ASSERT_EQUALS(ret, C_KZG_OK);
ASSERT_EQUALS(is_null, false);
}
static void test_c_kzg_calloc__fails_count_equal_to_zero(void) {
void *ptr = (void *)0x123;
C_KZG_RET ret = c_kzg_calloc(&ptr, 0, 456);
bool is_null = ptr == NULL;
c_kzg_free(ptr);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
ASSERT_EQUALS(is_null, true);
}
static void test_c_kzg_calloc__fails_size_equal_to_zero(void) {
void *ptr = (void *)0x123;
C_KZG_RET ret = c_kzg_calloc(&ptr, 123, 0);
bool is_null = ptr == NULL;
c_kzg_free(ptr);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
ASSERT_EQUALS(is_null, true);
}
static void test_c_kzg_calloc__fails_too_big(void) {
void *ptr = NULL;
C_KZG_RET ret = c_kzg_calloc(&ptr, UINT64_MAX, UINT64_MAX);
bool is_null = ptr == NULL;
c_kzg_free(ptr);
ASSERT_EQUALS(ret, C_KZG_MALLOC);
ASSERT_EQUALS(is_null, true);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for fr_div
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_fr_div__by_one_is_equal(void) {
fr_t a, q;
get_rand_fr(&a);
fr_div(&q, &a, &FR_ONE);
bool ok = fr_equal(&q, &a);
ASSERT_EQUALS(ok, true);
}
static void test_fr_div__by_itself_is_one(void) {
fr_t a, q;
get_rand_fr(&a);
fr_div(&q, &a, &a);
bool ok = fr_equal(&q, &FR_ONE);
ASSERT_EQUALS(ok, true);
}
static void test_fr_div__specific_value(void) {
fr_t a, b, q, check;
fr_from_uint64(&a, 2345);
fr_from_uint64(&b, 54321);
blst_fr_from_hexascii(
&check,
(const byte *)("0x264d23155705ca938a1f22117681ea9759f348cb177a07ffe0813"
"de67e85c684")
);
fr_div(&q, &a, &b);
bool ok = fr_equal(&q, &check);
ASSERT_EQUALS(ok, true);
}
static void test_fr_div__succeeds_round_trip(void) {
fr_t a, b, q, r;
get_rand_fr(&a);
get_rand_fr(&b);
fr_div(&q, &a, &b);
blst_fr_mul(&r, &q, &b);
bool ok = fr_equal(&r, &a);
ASSERT_EQUALS(ok, true);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for fr_pow
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_fr_pow__test_power_of_two(void) {
fr_t a, r, check;
fr_from_uint64(&a, 2);
fr_from_uint64(&check, 0x100000000);
fr_pow(&r, &a, 32);
bool ok = fr_equal(&r, &check);
ASSERT_EQUALS(ok, true);
}
static void test_fr_pow__test_inverse_on_root_of_unity(void) {
fr_t r;
size_t order = log2_pow2(FIELD_ELEMENTS_PER_EXT_BLOB);
fr_pow(&r, &ROOT_OF_UNITY, 1ULL << order);
bool ok = fr_equal(&r, &FR_ONE);
ASSERT_EQUALS(ok, true);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for fr_batch_inv
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_fr_batch_inv__test_consistent(void) {
C_KZG_RET ret;
fr_t a[32], batch_inverses[32], check_inverses[32];
for (size_t i = 0; i < 32; i++) {
get_rand_fr(&a[i]);
blst_fr_eucl_inverse(&check_inverses[i], &a[i]);
}
ret = fr_batch_inv(batch_inverses, a, 32);
ASSERT_EQUALS(ret, C_KZG_OK);
for (size_t i = 0; i < 32; i++) {
bool ok = fr_equal(&check_inverses[i], &batch_inverses[i]);
ASSERT_EQUALS(ok, true);
}
}
/** Make sure that batch inverse doesn't support zeroes */
static void test_fr_batch_inv__test_zero(void) {
C_KZG_RET ret;
fr_t a[32], batch_inverses[32];
for (size_t i = 0; i < 32; i++) {
get_rand_fr(&a[i]);
}
a[5] = FR_ZERO;
ret = fr_batch_inv(batch_inverses, a, 32);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for g1_mul
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_g1_mul__test_consistent(void) {
blst_scalar scalar;
Bytes32 b;
fr_t f;
g1_t g, r, check;
get_rand_field_element(&b);
blst_scalar_from_lendian(&scalar, b.bytes);
blst_fr_from_scalar(&f, &scalar);
get_rand_g1(&g);
blst_p1_mult(&check, &g, (const byte *)&b, 256);
g1_mul(&r, &g, &f);
ASSERT("points are equal", blst_p1_is_equal(&check, &r));
}
static void test_g1_mul__test_scalar_is_zero(void) {
fr_t f;
g1_t g, r;
fr_from_uint64(&f, 0);
get_rand_g1(&g);
g1_mul(&r, &g, &f);
ASSERT("result is neutral element", blst_p1_is_inf(&r));
}
static void test_g1_mul__test_different_bit_lengths(void) {
Bytes32 b;
fr_t f, two;
g1_t g, r, check;
blst_scalar scalar;
fr_from_uint64(&f, 1);
fr_from_uint64(&two, 2);
blst_scalar_from_fr(&scalar, &f);
/* blst_p1_mult needs it to be little-endian */
blst_lendian_from_scalar(b.bytes, &scalar);
for (size_t i = 1; i < 255; i++) {
get_rand_g1(&g);
blst_p1_mult(&check, &g, b.bytes, 256);
g1_mul(&r, &g, &f);
ASSERT("points are equal", blst_p1_is_equal(&check, &r));
blst_fr_mul(&f, &f, &two);
blst_scalar_from_fr(&scalar, &f);
blst_lendian_from_scalar(b.bytes, &scalar);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for pairings_verify
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_pairings_verify__good_pairing(void) {
fr_t f;
g1_t g1, sg1;
g2_t g2, sg2;
get_rand_fr(&f);
get_rand_g1(&g1);
get_rand_g2(&g2);
g1_mul(&sg1, &g1, &f);
g2_mul(&sg2, &g2, &f);
ASSERT("pairings verify", pairings_verify(&g1, &sg2, &sg1, &g2));
}
static void test_pairings_verify__bad_pairing(void) {
fr_t f, splusone;
g1_t g1, sg1;
g2_t g2, s1g2;
get_rand_fr(&f);
blst_fr_add(&splusone, &f, &FR_ONE);
get_rand_g1(&g1);
get_rand_g2(&g2);
g1_mul(&sg1, &g1, &f);
g2_mul(&s1g2, &g2, &splusone);
ASSERT("pairings fail", !pairings_verify(&g1, &s1g2, &sg1, &g2));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for blob_to_kzg_commitment
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_blob_to_kzg_commitment__succeeds_x_less_than_modulus(void) {
C_KZG_RET ret;
KZGCommitment c;
Blob blob;
Bytes32 field_element;
/*
* A valid field element is x < BLS_MODULUS.
* Therefore, x = BLS_MODULUS - 1 should be valid.
*
* int(BLS_MODULUS - 1).to_bytes(32, 'big').hex()
*/
bytes32_from_hex(
&field_element, "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000"
);
memset(&blob, 0, sizeof(blob));
memcpy(blob.bytes, field_element.bytes, BYTES_PER_FIELD_ELEMENT);
ret = blob_to_kzg_commitment(&c, &blob, &s);
ASSERT_EQUALS(ret, C_KZG_OK);
}
static void test_blob_to_kzg_commitment__fails_x_equal_to_modulus(void) {
C_KZG_RET ret;
KZGCommitment c;
Blob blob;
Bytes32 field_element;
/*
* A valid field element is x < BLS_MODULUS.
* Therefore, x = BLS_MODULUS should be invalid.
*
* int(BLS_MODULUS).to_bytes(32, 'big').hex()
*/
bytes32_from_hex(
&field_element, "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
);
memset(&blob, 0, sizeof(blob));
memcpy(blob.bytes, field_element.bytes, BYTES_PER_FIELD_ELEMENT);
ret = blob_to_kzg_commitment(&c, &blob, &s);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_blob_to_kzg_commitment__fails_x_greater_than_modulus(void) {
C_KZG_RET ret;
KZGCommitment c;
Blob blob;
Bytes32 field_element;
/*
* A valid field element is x < BLS_MODULUS.
* Therefore, x = BLS_MODULUS + 1 should be invalid.
*
* int(BLS_MODULUS + 1).to_bytes(32, 'big').hex()
*/
bytes32_from_hex(
&field_element, "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000002"
);
memset(&blob, 0, sizeof(blob));
memcpy(blob.bytes, field_element.bytes, BYTES_PER_FIELD_ELEMENT);
ret = blob_to_kzg_commitment(&c, &blob, &s);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_blob_to_kzg_commitment__succeeds_point_at_infinity(void) {
C_KZG_RET ret;
KZGCommitment c;
Blob blob;
Bytes48 point_at_infinity;
int diff;
/* Get the commitment for a blob that's all zeros */
memset(&blob, 0, sizeof(blob));
ret = blob_to_kzg_commitment(&c, &blob, &s);
ASSERT_EQUALS(ret, C_KZG_OK);
/* The commitment should be the serialized point at infinity */
bytes48_from_hex(
&point_at_infinity,
"c00000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
);
diff = memcmp(c.bytes, point_at_infinity.bytes, BYTES_PER_COMMITMENT);
ASSERT_EQUALS(diff, 0);
}
static void test_blob_to_kzg_commitment__succeeds_expected_commitment(void) {
C_KZG_RET ret;
KZGCommitment c;
Blob blob;
Bytes32 field_element;
Bytes48 expected_commitment;
int diff;
bytes32_from_hex(
&field_element, "14629a3a39f7b854e6aa49aa2edb450267eac2c14bb2d4f97a0b81a3f57055ad"
);
/* Initialize the blob with a single field element */
memset(&blob, 0, sizeof(blob));
memcpy(blob.bytes, field_element.bytes, BYTES_PER_FIELD_ELEMENT);
/* Get a commitment to this particular blob */
ret = blob_to_kzg_commitment(&c, &blob, &s);
ASSERT_EQUALS(ret, C_KZG_OK);
/*
* We expect the commitment to match. If it doesn't
* match, something important has changed.
*/
bytes48_from_hex(
&expected_commitment,
"91a5e1c143820d2e7bec38a5404c5145807cb88c0abbbecb"
"cb4bccc83a4b417326e337574cff43303f8a6648ecbee7ac"
);
diff = memcmp(c.bytes, expected_commitment.bytes, BYTES_PER_COMMITMENT);
ASSERT_EQUALS(diff, 0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for validate_kzg_g1
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_validate_kzg_g1__succeeds_round_trip(void) {
C_KZG_RET ret;
Bytes48 a, b;
g1_t g1;
int diff;
get_rand_g1_bytes(&a);
ret = validate_kzg_g1(&g1, &a);
ASSERT_EQUALS(ret, C_KZG_OK);
bytes_from_g1(&b, &g1);
diff = memcmp(a.bytes, b.bytes, sizeof(Bytes48));
ASSERT_EQUALS(diff, 0);
}
static void test_validate_kzg_g1__succeeds_correct_point(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"a491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e264"
"4f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_OK);
}
static void test_validate_kzg_g1__fails_not_in_g1(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"8123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_not_in_curve(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"8123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcde0"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_x_equal_to_modulus(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf"
"6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_x_greater_than_modulus(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf"
"6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__succeeds_infinity_with_true_b_flag(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"c00000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_OK);
}
static void test_validate_kzg_g1__fails_infinity_with_true_b_flag(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"c01000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_infinity_with_false_b_flag(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"800000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_with_wrong_c_flag(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_with_b_flag_and_x_nonzero(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"c123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_with_b_flag_and_a_flag_true(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"e00000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_with_mask_bits_111(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"e491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e264"
"4f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_with_mask_bits_011(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"6491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e264"
"4f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_validate_kzg_g1__fails_with_mask_bits_001(void) {
C_KZG_RET ret;
Bytes48 g1_bytes;
g1_t g1;
bytes48_from_hex(
&g1_bytes,
"2491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e264"
"4f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a"
);
ret = validate_kzg_g1(&g1, &g1_bytes);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for reverse_bits
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_reverse_bits__succeeds_round_trip(void) {
uint64_t original;
uint64_t reversed;
uint64_t reversed_reversed;
get_rand_uint64(&original);
reversed = reverse_bits(original);
reversed_reversed = reverse_bits(reversed);
ASSERT_EQUALS(reversed_reversed, original);
}
static void test_reverse_bits__succeeds_all_bits_are_zero(void) {
uint64_t original = 0;
uint64_t reversed = 0;
ASSERT_EQUALS(reverse_bits(original), reversed);
}
static void test_reverse_bits__succeeds_some_bits_are_one(void) {
uint64_t original = 17004747765872328575ULL;
uint64_t reversed = 18374677679283584983ULL;
ASSERT_EQUALS(reverse_bits(original), reversed);
}
static void test_reverse_bits__succeeds_all_bits_are_one(void) {
uint64_t original = 18446744073709551615ULL;
uint64_t reversed = 18446744073709551615ULL;
ASSERT_EQUALS(reverse_bits(original), reversed);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for bit_reversal_permutation
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_bit_reversal_permutation__succeeds_round_trip(void) {
C_KZG_RET ret;
uint64_t original[128];
uint64_t reversed_reversed[128];
for (size_t i = 0; i < 128; i++) {
get_rand_uint64(&original[i]);
reversed_reversed[i] = original[i];
}
ret = bit_reversal_permutation(&reversed_reversed, sizeof(uint64_t), 128);
ASSERT_EQUALS(ret, C_KZG_OK);
ret = bit_reversal_permutation(&reversed_reversed, sizeof(uint64_t), 128);
ASSERT_EQUALS(ret, C_KZG_OK);
for (size_t i = 0; i < 128; i++) {
ASSERT_EQUALS(reversed_reversed[i], original[i]);
}
}
static void test_bit_reversal_permutation__specific_items(void) {
C_KZG_RET ret;
uint64_t original[128];
uint64_t reversed[128];
for (size_t i = 0; i < 128; i++) {
get_rand_uint64(&original[i]);
reversed[i] = original[i];
}
ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 128);
ASSERT_EQUALS(ret, C_KZG_OK);
// Test the first 8 elements of the bit reversal permutation
// This tests the ordering of the values, not the values themselves,
// so is independent of the randomness used to initialize original[]
ASSERT_EQUALS(reversed[0], original[0]);
ASSERT_EQUALS(reversed[1], original[64]);
ASSERT_EQUALS(reversed[2], original[32]);
ASSERT_EQUALS(reversed[3], original[96]);
ASSERT_EQUALS(reversed[4], original[16]);
ASSERT_EQUALS(reversed[5], original[80]);
ASSERT_EQUALS(reversed[6], original[48]);
ASSERT_EQUALS(reversed[7], original[112]);
}
static void test_bit_reversal_permutation__coset_structure(void) {
C_KZG_RET ret;
uint64_t original[256];
uint64_t reversed[256];
for (size_t i = 0; i < 256; i++) {
original[i] = i % 16;
reversed[i] = original[i];
}
ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 256);
ASSERT_EQUALS(ret, C_KZG_OK);
for (size_t i = 0; i < 16; i++) {
for (size_t j = 1; j < 16; j++) {
ASSERT_EQUALS(reversed[16 * i], reversed[16 * i + j]);
}
}
}
static void test_bit_reversal_permutation__fails_n_not_power_of_two(void) {
C_KZG_RET ret;
uint64_t reversed[256];
for (size_t i = 0; i < 256; i++) {
reversed[i] = 0;
}
ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 255);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
static void test_bit_reversal_permutation__fails_n_is_one(void) {
C_KZG_RET ret;
uint64_t reversed[1];
for (size_t i = 0; i < 1; i++) {
reversed[i] = 0;
}
ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 1);
ASSERT_EQUALS(ret, C_KZG_BADARGS);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for compute_powers
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_compute_powers__succeeds_expected_powers(void) {
C_KZG_RET ret;
Bytes32 field_element_bytes;
fr_t field_element_fr;
const size_t n = 3;
int diff;
fr_t powers[n];
Bytes32 powers_bytes[n];
Bytes32 expected_bytes[n];
/* Convert random field element to a fr_t */
bytes32_from_hex(
&field_element_bytes, "1bf5410da0468196b4e242ca17617331d238ba5e586198bd42ebd7252919c3e1"
);
ret = bytes_to_bls_field(&field_element_fr, &field_element_bytes);
ASSERT_EQUALS(ret, C_KZG_OK);
/* Compute three powers for the given field element */
compute_powers((fr_t *)&powers, &field_element_fr, n);
/*
* These are the expected results. Notable, the first element should always
* be 1 since x^0 is 1. The second element should be equivalent to the
* input field element. The third element can be verified with Python.
*/
bytes32_from_hex(
&expected_bytes[0], "0000000000000000000000000000000000000000000000000000000000000001"
);
bytes32_from_hex(
&expected_bytes[1], "1bf5410da0468196b4e242ca17617331d238ba5e586198bd42ebd7252919c3e1"
);
/*
* b = bytes.fromhex("1bf5410da0468196b...")
* i = (int.from_bytes(b, "big") ** 2) % BLS_MODULUS
* print(i.to_bytes(32, "big").hex())
*/
bytes32_from_hex(
&expected_bytes[2], "2f417bcb88693ff8bc5d61b6d44503f3a99e8c3df3891e0040dee96047458a0e"
);
for (size_t i = 0; i < n; i++) {
bytes_from_bls_field(&powers_bytes[i], &powers[i]);
diff = memcmp(powers_bytes[i].bytes, expected_bytes[i].bytes, sizeof(Bytes32));
ASSERT_EQUALS(diff, 0);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for g1_lincomb
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_g1_lincomb__verify_consistent(void) {
C_KZG_RET ret;
g1_t points[128], out, check;
fr_t scalars[128];
check = G1_IDENTITY;
for (size_t i = 0; i < 128; i++) {
get_rand_fr(&scalars[i]);
get_rand_g1(&points[i]);
}
g1_lincomb_naive(&check, points, scalars, 128);
ret = g1_lincomb_fast(&out, points, scalars, 128);
ASSERT_EQUALS(ret, C_KZG_OK);
ASSERT("pippenger matches naive MSM", blst_p1_is_equal(&out, &check));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests for evaluate_polynomial_in_evaluation_form
////////////////////////////////////////////////////////////////////////////////////////////////////
static void test_evaluate_polynomial_in_evaluation_form__constant_polynomial(void) {
C_KZG_RET ret;
Polynomial p;
fr_t x, y, c;
get_rand_fr(&c);
get_rand_fr(&x);
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
p.evals[i] = c;
}
ret = evaluate_polynomial_in_evaluation_form(&y, &p, &x, &s);
ASSERT_EQUALS(ret, C_KZG_OK);
ASSERT("evaluation matches constant", fr_equal(&y, &c));
}
static void test_evaluate_polynomial_in_evaluation_form__constant_polynomial_in_range(void) {
C_KZG_RET ret;
Polynomial p;
fr_t x, y, c;
get_rand_fr(&c);
x = s.brp_roots_of_unity[123];
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
p.evals[i] = c;
}
ret = evaluate_polynomial_in_evaluation_form(&y, &p, &x, &s);
ASSERT_EQUALS(ret, C_KZG_OK);
ASSERT("evaluation matches constant", fr_equal(&y, &c));
}
static void test_evaluate_polynomial_in_evaluation_form__random_polynomial(void) {
C_KZG_RET ret;
fr_t poly_coefficients[FIELD_ELEMENTS_PER_BLOB];
Polynomial p;
fr_t x, y, check;
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
get_rand_fr(&poly_coefficients[i]);
}
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
eval_poly(&p.evals[i], poly_coefficients, &s.brp_roots_of_unity[i]);
}
get_rand_fr(&x);
eval_poly(&check, poly_coefficients, &x);
ret = evaluate_polynomial_in_evaluation_form(&y, &p, &x, &s);
ASSERT_EQUALS(ret, C_KZG_OK);