-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathbls_c_impl.hpp
1165 lines (1062 loc) · 28.2 KB
/
bls_c_impl.hpp
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
#ifndef MCLBN_FORCE_EXPORT
#define MCLBN_DONT_EXPORT
#endif
#define BLS_DLL_EXPORT
#include <bls/bls.h>
#include "mcl/impl/bn_c_impl.hpp"
#if (CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11) && !defined(__EMSCRIPTEN__) && !defined(__wasm__)
#include <thread>
#define BLS_MULTI_VERIFY_THREAD
#endif
inline void Gmul(G1& z, const G1& x, const Fr& y) { G1::mul(z, x, y); }
inline void Gmul(G2& z, const G2& x, const Fr& y) { G2::mul(z, x, y); }
inline void GmulCT(G1& z, const G1& x, const Fr& y) { G1::mulCT(z, x, y); }
inline void GmulCT(G2& z, const G2& x, const Fr& y) { G2::mulCT(z, x, y); }
inline void Gneg(G1& y, const G1& x) { G1::neg(y, x); }
inline void Gneg(G2& y, const G2& x) { G2::neg(y, x); }
inline void GmulVec(G1& z, G1* x, const Fr *y, mclSize n) { G1::mulVec(z, x, y, n); }
inline void GmulVec(G2& z, G2* x, const Fr *y, mclSize n) { G2::mulVec(z, x, y, n); }
inline void hashAndMapToG(G1& z, const void *m, mclSize size) { hashAndMapToG1(z, m, size); }
inline void hashAndMapToG(G2& z, const void *m, mclSize size) { hashAndMapToG2(z, m, size); }
/*
BLS signature
e : G1 x G2 -> GT
Q in G2 ; fixed global parameter
H : {str} -> G1
s : secret key
sQ ; public key
s H(m) ; signature of m
verify ; e(sQ, H(m)) = e(Q, s H(m))
swap G1 and G2 if BLS_ETH is defined
@note the current implementation does not support precomputed miller loop
*/
/*
ETH2 spec requires the original G2cofactor
original cofactor = mulByCofactorBLS12fast * adj
adj H(m) ; original version
H(m) ; fast version
PublicKey = s P
Signature = s (adj H(m)) ; slow
= (s adj) H(m) ; fast
Verify original ; e(P, s adj H(m)) == e(s P, adj H(m))
fast version ; e((1/adj) P, s H(m)) == e(s P, H(m))
*/
static int g_curveType;
static bool g_irtfHashAndMap;
#ifdef BLS_ETH
typedef G2 G;
typedef G1 Gother;
static G1 g_P;
inline const G1& getBasePoint() { return g_P; }
#else
typedef G1 G;
typedef G2 Gother;
static G2 g_Q;
const size_t maxQcoeffN = 128;
static mcl::FixedArray<Fp6, maxQcoeffN> g_Qcoeff; // precomputed Q
inline const G2& getBasePoint() { return g_Q; }
inline const mcl::FixedArray<Fp6, maxQcoeffN>& getQcoeff() { return g_Qcoeff; }
#endif
int blsSetETHmode(int mode)
{
if (g_curveType != MCL_BLS12_381) return -1;
switch (mode) {
case BLS_ETH_MODE_OLD:
g_irtfHashAndMap = false;
break;
case BLS_ETH_MODE_DRAFT_07:
g_irtfHashAndMap = true;
break;
default:
return -1;
}
return 0;
}
int blsSetMapToMode(int mode)
{
return mclBn_setMapToMode(mode);
}
int blsInit(int curve, int compiledTimeVar)
{
if (compiledTimeVar != MCLBN_COMPILED_TIME_VAR) {
return -(compiledTimeVar + (MCLBN_COMPILED_TIME_VAR * 1000));
}
const mcl::CurveParam* cp = mcl::getCurveParam(curve);
if (cp == 0) return -1;
bool b;
initPairing(&b, *cp);
#ifdef __wasm__
// G2::setMulArrayGLV(0);
#endif
if (!b) return -1;
g_curveType = curve;
#ifdef BLS_ETH
if (curve == MCL_BLS12_381) {
mclBn_setETHserialization(1);
g_P.setStr(&b, "1 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569", 10);
mclBn_setMapToMode(MCL_MAP_TO_MODE_HASH_TO_CURVE_07);
blsSetETHmode(BLS_ETH_MODE_LATEST);
} else
{
mapToG1(&b, g_P, 1);
}
#else
if (curve == MCL_BN254) {
const char *Qx_BN254 = "11ccb44e77ac2c5dc32a6009594dbe331ec85a61290d6bbac8cc7ebb2dceb128 f204a14bbdac4a05be9a25176de827f2e60085668becdd4fc5fa914c9ee0d9a";
const char *Qy_BN254 = "7c13d8487903ee3c1c5ea327a3a52b6cc74796b1760d5ba20ed802624ed19c8 8f9642bbaacb73d8c89492528f58932f2de9ac3e80c7b0e41f1a84f1c40182";
g_Q.x.setStr(&b, Qx_BN254, 16);
g_Q.y.setStr(&b, Qy_BN254, 16);
g_Q.z = 1;
} else {
mapToG2(&b, g_Q, 1);
}
if (!b) return -100;
#if MCL_SIZEOF_UNIT == 8
if (curve == MCL_BN254) {
#include "./qcoeff-bn254.hpp"
g_Qcoeff.resize(BN::param.precomputedQcoeffSize);
assert(g_Qcoeff.size() == CYBOZU_NUM_OF_ARRAY(QcoeffTblBN254));
for (size_t i = 0; i < g_Qcoeff.size(); i++) {
Fp6& x6 = g_Qcoeff[i];
for (size_t j = 0; j < 6; j++) {
Fp& x = x6.getFp0()[j];
mcl::fp::Unit *p = const_cast<mcl::fp::Unit*>(x.getUnit());
for (size_t k = 0; k < 4; k++) {
p[k] = QcoeffTblBN254[i][j][k];
}
}
}
} else
#endif
{
precomputeG2(&b, g_Qcoeff, getBasePoint());
}
#endif
if (!b) return -101;
verifyOrderG1(true);
verifyOrderG2(true);
return 0;
}
void blsSetETHserialization(int ETHserialization)
{
mclBn_setETHserialization(ETHserialization);
}
#ifdef __EMSCRIPTEN__
extern "C" BLS_DLL_API void *blsMalloc(size_t n)
{
return malloc(n);
}
extern "C" BLS_DLL_API void blsFree(void *p)
{
free(p);
}
#endif
static inline const mclBnG1 *cast(const G1* x) { return (const mclBnG1*)x; }
static inline const mclBnG2 *cast(const G2* x) { return (const mclBnG2*)x; }
void blsIdSetInt(blsId *id, int x)
{
*cast(&id->v) = x;
}
int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufSize)
{
cast(&sec->v)->setArrayMask((const uint8_t *)buf, bufSize);
return 0;
}
int blsSecretKeySetLittleEndianMod(blsSecretKey *sec, const void *buf, mclSize bufSize)
{
bool b;
cast(&sec->v)->setArrayMod(&b, (const uint8_t *)buf, bufSize);
return b ? 0 : -1;
}
void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec)
{
Gmul(*cast(&pub->v), getBasePoint(), *cast(&sec->v));
}
int blsHashToSignature(blsSignature *sig, const void *buf, mclSize bufSize)
{
hashAndMapToG(*cast(&sig->v), buf, bufSize);
return 0;
}
void blsSign(blsSignature *sig, const blsSecretKey *sec, const void *m, mclSize size)
{
blsHashToSignature(sig, m, size);
Fr s = *cast(&sec->v);
GmulCT(*cast(&sig->v), *cast(&sig->v), s);
}
#ifdef BLS_ETH
/*
e(P, sHm) == e(sP, Hm)
<=> finalExp(ML(P, sHm) * e(-sP, Hm)) == 1
*/
bool isEqualTwoPairings(const G2& sHm, const G1& sP, const G2& Hm)
{
GT e;
G1 v1[2];
G2 v2[2] = { sHm, Hm };
v1[0] = getBasePoint();
G1::neg(v1[1], sP);
millerLoopVec(e, v1, v2, 2);
finalExp(e, e);
return e.isOne();
}
#else
/*
e(P1, Q1) == e(P2, Q2)
<=> finalExp(ML(P1, Q1)) == finalExp(ML(P2, Q2))
<=> finalExp(ML(P1, Q1) / ML(P2, Q2)) == 1
<=> finalExp(ML(P1, Q1) * ML(-P2, Q2)) == 1
Q1 is precomputed
*/
bool isEqualTwoPairings(const G1& P1, const Fp6* Q1coeff, const G1& P2, const G2& Q2)
{
GT e;
precomputedMillerLoop2mixed(e, P2, Q2, -P1, Q1coeff);
finalExp(e, e);
return e.isOne();
}
#endif
int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const void *m, mclSize size)
{
if (cast(&pub->v)->isZero()) return 0;
G Hm;
hashAndMapToG(Hm, m, size);
#ifdef BLS_ETH
return isEqualTwoPairings(*cast(&sig->v), *cast(&pub->v), Hm);
#else
/*
e(sHm, Q) = e(Hm, sQ)
e(sig, Q) = e(Hm, pub)
*/
return isEqualTwoPairings(*cast(&sig->v), getQcoeff().data(), Hm, *cast(&pub->v));
#endif
}
void blsMultiVerifySub(mclBnGT *e, blsSignature *aggSig, blsSignature *sigVec, const blsPublicKey *pubVec, const char *msg, mclSize msgSize, const char *randVec, mclSize randSize, mclSize n)
{
#ifdef BLS_ETH
const size_t N = 16;
Fr rand[N];
G1 g1Vec[N];
G2 g2Vec[N];
bool initE = true;
while (n > 0) {
size_t m = mcl::fp::min_<size_t>(n, N);
for (size_t i = 0; i < m; i++) {
bool b;
rand[i].setArray(&b, (const uint8_t *)&randVec[i * randSize], randSize);
(void)b;
const G1& pub = *cast(&pubVec[i].v);
if (pub.isZero()) {
cast(e)->clear();
return;
}
G1::mul(g1Vec[i], pub, rand[i]);
hashAndMapToG(g2Vec[i], &msg[i * msgSize], msgSize);
}
if (initE) {
G2::mulVec(*cast(&aggSig->v), cast(&sigVec->v), rand, m);
} else {
G2 t;
G2::mulVec(t, cast(&sigVec->v), rand, m);
*cast(&aggSig->v) += t;
}
sigVec += m;
pubVec += m;
msg += m * msgSize;
randVec += m * randSize;
n -= m;
millerLoopVec(*cast(e), g1Vec, g2Vec, m, initE);
initE = false;
}
#else
(void)e;
(void)aggSig;
(void)sigVec;
(void)pubVec;
(void)msg;
(void)msgSize;
(void)randVec;
(void)randSize;;
(void)n;
#endif
}
int blsMultiVerifyFinal(const mclBnGT *e, const blsSignature *aggSig)
{
#ifdef BLS_ETH
if (cast(e)->isZero()) return false;
GT e2;
millerLoop(e2, -getBasePoint(), *cast(&aggSig->v));
e2 *= *cast(e);
finalExp(e2, e2);
return e2.isOne();
#else
(void)e;
(void)aggSig;
return 0;
#endif
}
/*
sig = sum_i sigVec[i] * randVec[i]
pubVec[i] *= randVec[i]
verify prod e(H(pubVec[i], msgToG2[i]) == e(P, sig)
@remark return 0 if some pubVec[i] is zero
*/
int blsMultiVerify(blsSignature *sigVec, const blsPublicKey *pubVec, const void *msgVec, mclSize msgSize, const void *randVec, mclSize randSize, mclSize n, int threadN)
{
#ifdef BLS_ETH
if (n == 0) return 0;
const char *msg = (const char*)msgVec;
const char *rp = (const char*)randVec;
GT e;
G2 aggSig;
const int maxThreadNum = 32;
if (threadN < 1) threadN = 1;
if (threadN > maxThreadNum) threadN = maxThreadNum;
#ifdef BLS_MULTI_VERIFY_THREAD
const size_t minN = 16;
if (threadN > 1 && n >= minN) {
GT et[maxThreadNum];
G2 aggSigt[maxThreadNum];
std::thread th[maxThreadNum];
size_t blockN = n / minN;
assert(blockN > 0);
size_t q = blockN / threadN;
size_t r = blockN % threadN;
for (int i = 0; i < threadN; i++) {
size_t m = q;
if (r > 0) {
m++;
r--;
}
if (m == 0) {
threadN = i; // n is too small for threadN
break;
}
m *= minN;
if (i == threadN - 1) {
m = n;
}
th[i] = std::thread(blsMultiVerifySub, (mclBnGT*)&et[i], (blsSignature*)&aggSigt[i], sigVec, pubVec, msg, msgSize, rp, randSize, m);
sigVec += m;
pubVec += m;
msg += msgSize * m;
rp += randSize * m;
n -= m;
}
for (int i = 0; i < threadN; i++) {
th[i].join();
}
e = et[0];
aggSig = aggSigt[0];
for (int i = 1; i < threadN; i++) {
e *= et[i];
aggSig += aggSigt[i];
}
} else
#endif
{
blsMultiVerifySub((mclBnGT*)&e, (blsSignature*)&aggSig, sigVec, pubVec, msg, msgSize, rp, randSize, n);
}
return blsMultiVerifyFinal((const mclBnGT*)&e, (const blsSignature*)&aggSig);
#else
(void)sigVec;
(void)pubVec;
(void)msgVec;
(void)msgSize;
(void)randVec;
(void)randSize;
(void)n;
(void)threadN;
return 0;
#endif
}
void blsAggregateSignature(blsSignature *aggSig, const blsSignature *sigVec, mclSize n)
{
if (n == 0) {
memset(aggSig, 0, sizeof(*aggSig));
return;
}
*aggSig = sigVec[0];
for (mclSize i = 1; i < n; i++) {
blsSignatureAdd(aggSig, &sigVec[i]);
}
}
// return -1 if some pubVec[i] is zero else 0
int blsAggregatePublicKey(blsPublicKey *aggPub, const blsPublicKey *pubVec, mclSize n)
{
if (n == 0) {
memset(aggPub, 0, sizeof(*aggPub));
return 0;
}
int ret = 0;
*aggPub = pubVec[0];
if (cast(&pubVec[0].v)->isZero()) ret = -1;
for (mclSize i = 1; i < n; i++) {
if (cast(&pubVec[i].v)->isZero()) ret = -1;
blsPublicKeyAdd(aggPub, &pubVec[i]);
}
return ret;
}
int blsFastAggregateVerify(const blsSignature *sig, const blsPublicKey *pubVec, mclSize n, const void *msg, mclSize msgSize)
{
if (n == 0) return 0;
blsPublicKey aggPub;
int ret = blsAggregatePublicKey(&aggPub, pubVec, n);
if (ret < 0) return 0;
return blsVerify(sig, &aggPub, msg, msgSize);
}
int blsAggregateVerifyNoCheck(const blsSignature *sig, const blsPublicKey *pubVec, const void *msgVec, mclSize msgSize, mclSize n)
{
#ifdef BLS_ETH
if (n == 0) return 0;
#if 1 // 1.1 times faster
GT e;
const char *msg = (const char*)msgVec;
const size_t N = 16;
G1 g1Vec[N+1];
G2 g2Vec[N+1];
bool initE = true;
while (n > 0) {
size_t m = mcl::fp::min_<size_t>(n, N);
for (size_t i = 0; i < m; i++) {
g1Vec[i] = *cast(&pubVec[i].v);
if (g1Vec[i].isZero()) return 0;
hashAndMapToG(g2Vec[i], &msg[i * msgSize], msgSize);
}
pubVec += m;
msg += m * msgSize;
n -= m;
if (n == 0) {
g1Vec[m] = getBasePoint();
G2::neg(g2Vec[m], *cast(&sig->v));
m++;
}
millerLoopVec(e, g1Vec, g2Vec, m, initE);
initE = false;
}
BN::finalExp(e, e);
return e.isOne();
#else
const char *p = (const char *)msgVec;
GT s(1), t;
for (mclSize i = 0; i < n; i++) {
G2 Q;
hashAndMapToG(Q, &p[msgSize * i], msgSize);
if (cast(&pubVec[i].v)->isZero()) return 0;
millerLoop(t, *cast(&pubVec[i].v), Q);
s *= t;
}
millerLoop(t, -getBasePoint(), *cast(&sig->v));
s *= t;
finalExp(s, s);
return s.isOne() ? 1 : 0;
#endif
#else
(void)sig;
(void)pubVec;
(void)msgVec;
(void)msgSize;
(void)n;
return 0;
#endif
}
mclSize blsIdSerialize(void *buf, mclSize maxBufSize, const blsId *id)
{
return cast(&id->v)->serialize(buf, maxBufSize);
}
mclSize blsSecretKeySerialize(void *buf, mclSize maxBufSize, const blsSecretKey *sec)
{
return cast(&sec->v)->serialize(buf, maxBufSize);
}
mclSize blsPublicKeySerialize(void *buf, mclSize maxBufSize, const blsPublicKey *pub)
{
return cast(&pub->v)->serialize(buf, maxBufSize);
}
mclSize blsSignatureSerialize(void *buf, mclSize maxBufSize, const blsSignature *sig)
{
return cast(&sig->v)->serialize(buf, maxBufSize);
}
mclSize blsIdDeserialize(blsId *id, const void *buf, mclSize bufSize)
{
return cast(&id->v)->deserialize(buf, bufSize);
}
mclSize blsSecretKeyDeserialize(blsSecretKey *sec, const void *buf, mclSize bufSize)
{
return cast(&sec->v)->deserialize(buf, bufSize);
}
mclSize blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, mclSize bufSize)
{
return cast(&pub->v)->deserialize(buf, bufSize);
}
mclSize blsSignatureDeserialize(blsSignature *sig, const void *buf, mclSize bufSize)
{
return cast(&sig->v)->deserialize(buf, bufSize);
}
int blsIdIsEqual(const blsId *lhs, const blsId *rhs)
{
return *cast(&lhs->v) == *cast(&rhs->v);
}
int blsSecretKeyIsEqual(const blsSecretKey *lhs, const blsSecretKey *rhs)
{
return *cast(&lhs->v) == *cast(&rhs->v);
}
int blsPublicKeyIsEqual(const blsPublicKey *lhs, const blsPublicKey *rhs)
{
return *cast(&lhs->v) == *cast(&rhs->v);
}
int blsSignatureIsEqual(const blsSignature *lhs, const blsSignature *rhs)
{
return *cast(&lhs->v) == *cast(&rhs->v);
}
int blsIdIsZero(const blsId *x)
{
return cast(&x->v)->isZero() ? 1 : 0;
}
int blsSecretKeyIsZero(const blsSecretKey *x)
{
return cast(&x->v)->isZero() ? 1 : 0;
}
int blsPublicKeyIsZero(const blsPublicKey *x)
{
return cast(&x->v)->isZero() ? 1 : 0;
}
int blsSignatureIsZero(const blsSignature *x)
{
return cast(&x->v)->isZero() ? 1 : 0;
}
int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, mclSize k, const blsId *id)
{
bool b;
mcl::evaluatePolynomial(&b, *cast(&sec->v), cast(&msk->v), k, *cast(&id->v));
return b ? 0 : -1;
}
int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, mclSize k, const blsId *id)
{
bool b;
mcl::evaluatePolynomial(&b, *cast(&pub->v), cast(&mpk->v), k, *cast(&id->v));
return b ? 0 : -1;
}
int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, mclSize n)
{
bool b;
mcl::LagrangeInterpolation(&b, *cast(&sec->v), cast(&idVec->v), cast(&secVec->v), n);
return b ? 0 : -1;
}
int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, mclSize n)
{
bool b;
mcl::LagrangeInterpolation(&b, *cast(&pub->v), cast(&idVec->v), cast(&pubVec->v), n);
return b ? 0 : -1;
}
int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, mclSize n)
{
bool b;
mcl::LagrangeInterpolation(&b, *cast(&sig->v), cast(&idVec->v), cast(&sigVec->v), n);
return b ? 0 : -1;
}
void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs)
{
*cast(&sec->v) += *cast(&rhs->v);
}
void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs)
{
*cast(&pub->v) += *cast(&rhs->v);
}
void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs)
{
*cast(&sig->v) += *cast(&rhs->v);
}
void blsSignatureVerifyOrder(int doVerify)
{
#ifdef BLS_ETH
verifyOrderG2(doVerify != 0);
#else
verifyOrderG1(doVerify != 0);
#endif
}
void blsPublicKeyVerifyOrder(int doVerify)
{
#ifdef BLS_ETH
verifyOrderG1(doVerify != 0);
#else
verifyOrderG2(doVerify != 0);
#endif
}
int blsSignatureIsValidOrder(const blsSignature *sig)
{
return cast(&sig->v)->isValidOrder();
}
int blsPublicKeyIsValidOrder(const blsPublicKey *pub)
{
return cast(&pub->v)->isValidOrder();
}
#ifndef BLS_MINIMUM_API
template<class G>
inline bool toG(G& Hm, const void *h, mclSize size)
{
if (g_irtfHashAndMap) {
hashAndMapToG(Hm, h, size);
return true;
}
// backward compatibility
Fp t;
t.setArrayMask((const uint8_t *)h, size);
bool b;
#ifdef BLS_ETH
BN::mapToG2(&b, Hm, Fp2(t, 0));
#else
BN::mapToG1(&b, Hm, t);
#endif
return b;
}
int blsVerifyAggregatedHashes(const blsSignature *aggSig, const blsPublicKey *pubVec, const void *hVec, size_t sizeofHash, mclSize n)
{
if (n == 0) return 0;
GT e;
const char *ph = (const char*)hVec;
const size_t N = 16;
G1 g1Vec[N+1]; // +1 is for the last appending element
G2 g2Vec[N+1];
#ifdef BLS_ETH
bool initE = true;
while (n > 0) {
size_t m = mcl::fp::min_<size_t>(n, N);
for (size_t i = 0; i < m; i++) {
g1Vec[i] = *cast(&pubVec[i].v);
if (g1Vec[i].isZero()) return 0;
if (!toG(g2Vec[i], &ph[i * sizeofHash], sizeofHash)) return 0;
}
pubVec += m;
ph += m * sizeofHash;
n -= m;
if (n == 0) {
g1Vec[m] = getBasePoint();
G2::neg(g2Vec[m], *cast(&aggSig->v));
m++;
}
millerLoopVec(e, g1Vec, g2Vec, m, initE);
initE = false;
}
#else
/*
e(aggSig, Q) = prod_i e(hVec[i], pubVec[i])
<=> finalExp(ML(-aggSig, Q) * prod_i ML(hVec[i], pubVec[i])) == 1
*/
BN::precomputedMillerLoop(e, -*cast(&aggSig->v), g_Qcoeff.data());
while (n > 0) {
size_t m = N;
if (n < m) m = n;
for (size_t i = 0; i < m; i++) {
if (!toG(g1Vec[i], &ph[i * sizeofHash], sizeofHash)) return 0;
g2Vec[i] = *cast(&pubVec[i].v);
if (g2Vec[i].isZero()) return 0;
}
millerLoopVec(e, g1Vec, g2Vec, m, false);
pubVec += m;
ph += m * sizeofHash;
n -= m;
}
#endif
BN::finalExp(e, e);
return e.isOne();
}
int blsSignHash(blsSignature *sig, const blsSecretKey *sec, const void *h, mclSize size)
{
G Hm;
if (!toG(Hm, h, size)) return -1;
Fr s = *cast(&sec->v);
GmulCT(*cast(&sig->v), Hm, s);
return 0;
}
#ifdef BLS_ETH
const uint8_t ZERO_HEADER = 1 << 6;
const mclSize serializedPublicKeySize = 48;
const mclSize serializedSignatureSize = 48 * 2;
inline bool isZeroFormat(const uint8_t *buf, mclSize n)
{
if (buf[0] != ZERO_HEADER) return false;
for (mclSize i = 1; i < n; i++) {
if (buf[i] != 0) return false;
}
return true;
}
#endif
mclSize blsPublicKeySerializeUncompressed(void *buf, mclSize maxBufSize, const blsPublicKey *pub)
{
#ifdef BLS_ETH
if (g_curveType != MCL_BLS12_381) return 0;
const mclSize retSize = serializedPublicKeySize * 2;
if (maxBufSize < retSize) return 0;
uint8_t *dst = (uint8_t*)buf;
if (cast(&pub->v)->isZero()) {
dst[0] = ZERO_HEADER;
memset(dst + 1, 0, retSize - 1);
} else {
G1 x;
G1::normalize(x, *cast(&pub->v));
if (x.x.serialize(dst, serializedPublicKeySize) == 0) return 0;
if (x.y.serialize(dst + serializedPublicKeySize, serializedPublicKeySize) == 0) return 0;
}
return retSize;
#else
(void)buf;
(void)maxBufSize;
(void)pub;
return 0;
#endif
}
mclSize blsSignatureSerializeUncompressed(void *buf, mclSize maxBufSize, const blsSignature *sig)
{
#ifdef BLS_ETH
if (g_curveType != MCL_BLS12_381) return 0;
const mclSize retSize = serializedSignatureSize * 2;
if (maxBufSize < retSize) return 0;
uint8_t *dst = (uint8_t*)buf;
if (cast(&sig->v)->isZero()) {
dst[0] = ZERO_HEADER;
memset(dst + 1, 0, retSize - 1);
} else {
G2 x;
G2::normalize(x, *cast(&sig->v));
if (x.x.serialize(dst, serializedSignatureSize) == 0) return 0;
if (x.y.serialize(dst + serializedSignatureSize, serializedSignatureSize) == 0) return 0;
}
return retSize;
#else
(void)buf;
(void)maxBufSize;
(void)sig;
return 0;
#endif
}
mclSize blsPublicKeyDeserializeUncompressed(blsPublicKey *pub, const void *buf, mclSize bufSize)
{
#ifdef BLS_ETH
if (g_curveType != MCL_BLS12_381) return 0;
const mclSize retSize = serializedPublicKeySize * 2;
if (bufSize < retSize) return 0;
const uint8_t *src = (const uint8_t*)buf;
G1& x = *cast(&pub->v);
if (isZeroFormat(src, retSize)) {
x.clear();
} else {
if (x.x.deserialize(src, serializedPublicKeySize) == 0) return 0;
if (x.y.deserialize(src + serializedPublicKeySize, serializedPublicKeySize) == 0) return 0;
x.z = 1;
}
if (!x.isValid()) return 0;
return retSize;
#else
(void)pub;
(void)buf;
(void)bufSize;
return 0;
#endif
}
mclSize blsSignatureDeserializeUncompressed(blsSignature *sig, const void *buf, mclSize bufSize)
{
#ifdef BLS_ETH
if (g_curveType != MCL_BLS12_381) return 0;
const mclSize retSize = serializedSignatureSize * 2;
if (bufSize < retSize) return 0;
const uint8_t *src = (const uint8_t*)buf;
G2& x = *cast(&sig->v);
if (isZeroFormat(src, retSize)) {
x.clear();
} else {
if (x.x.deserialize(src, serializedSignatureSize) == 0) return 0;
if (x.y.deserialize(src + serializedSignatureSize, serializedSignatureSize) == 0) return 0;
x.z = 1;
}
if (!x.isValid()) return 0;
return retSize;
#else
(void)sig;
(void)buf;
(void)bufSize;
return 0;
#endif
}
int blsVerifyPairing(const blsSignature *X, const blsSignature *Y, const blsPublicKey *pub)
{
#ifdef BLS_ETH
return isEqualTwoPairings(*cast(&X->v), *cast(&pub->v), *cast(&Y->v));
#else
return isEqualTwoPairings(*cast(&X->v), getQcoeff().data(), *cast(&Y->v), *cast(&pub->v));
#endif
}
int blsVerifyHash(const blsSignature *sig, const blsPublicKey *pub, const void *h, mclSize size)
{
blsSignature Hm;
if (!toG(*cast(&Hm.v), h, size)) return 0;
if (cast(&pub->v)->isZero()) return 0;
return blsVerifyPairing(sig, &Hm, pub);
}
void blsSecretKeySub(blsSecretKey *sec, const blsSecretKey *rhs)
{
*cast(&sec->v) -= *cast(&rhs->v);
}
void blsPublicKeySub(blsPublicKey *pub, const blsPublicKey *rhs)
{
*cast(&pub->v) -= *cast(&rhs->v);
}
void blsSignatureSub(blsSignature *sig, const blsSignature *rhs)
{
*cast(&sig->v) -= *cast(&rhs->v);
}
void blsSecretKeyNeg(blsSecretKey *x)
{
Fr::neg(*cast(&x->v), *cast(&x->v));
}
void blsPublicKeyNeg(blsPublicKey *x)
{
Gneg(*cast(&x->v), *cast(&x->v));
}
void blsSignatureNeg(blsSignature *x)
{
Gneg(*cast(&x->v), *cast(&x->v));
}
void blsSecretKeyMul(blsSecretKey *y, const blsSecretKey *x)
{
*cast(&y->v) *= *cast(&x->v);
}
void blsPublicKeyMul(blsPublicKey *y, const blsSecretKey *x)
{
*cast(&y->v) *= *cast(&x->v);
}
void blsSignatureMul(blsSignature *y, const blsSecretKey *x)
{
*cast(&y->v) *= *cast(&x->v);
}
void blsPublicKeyMulVec(blsPublicKey *z, blsPublicKey *x, const blsSecretKey *y, mclSize n)
{
GmulVec(*cast(&z->v), cast(&x->v), cast(&y->v), n);
}
void blsSignatureMulVec(blsSignature *z, blsSignature *x, const blsSecretKey *y, mclSize n)
{
GmulVec(*cast(&z->v), cast(&x->v), cast(&y->v), n);
}
mclSize blsGetOpUnitSize() // FpUint64Size
{
return Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t);
}
int blsGetCurveOrder(char *buf, mclSize maxBufSize)
{
return (int)Fr::getModulo(buf, maxBufSize);
}
int blsGetFieldOrder(char *buf, mclSize maxBufSize)
{
return (int)Fp::getModulo(buf, maxBufSize);
}
int blsGetSerializedSecretKeyByteSize()
{
return blsGetFrByteSize();
}
int blsGetSerializedPublicKeyByteSize()
{
#ifdef BLS_ETH
return blsGetG1ByteSize();
#else
return blsGetG1ByteSize() * 2;
#endif
}
int blsGetSerializedSignatureByteSize()
{
#ifdef BLS_ETH
return blsGetG1ByteSize() * 2;
#else
return blsGetG1ByteSize();
#endif
}
int blsGetG1ByteSize()
{
return (int)Fp::getByteSize();
}
int blsGetFrByteSize()
{
return (int)Fr::getByteSize();
}
void blsGetGeneratorOfPublicKey(blsPublicKey *pub)
{
*cast(&pub->v) = getBasePoint();
}
int blsSetGeneratorOfPublicKey(const blsPublicKey *pub)
{
#ifdef BLS_ETH
g_P = *cast(&pub->v);
return 0;
#else
g_Q = *cast(&pub->v);
bool b;
precomputeG2(&b, g_Qcoeff, getBasePoint());
return b ? 0 : -1;
#endif
}
int blsIdSetDecStr(blsId *id, const char *buf, mclSize bufSize)
{
return cast(&id->v)->deserialize(buf, bufSize, 10) > 0 ? 0 : -1;
}
int blsIdSetHexStr(blsId *id, const char *buf, mclSize bufSize)
{
return cast(&id->v)->deserialize(buf, bufSize, 16) > 0 ? 0 : -1;
}
int blsIdSetLittleEndian(blsId *id, const void *buf, mclSize bufSize)
{
cast(&id->v)->setArrayMask((const uint8_t *)buf, bufSize);
return 0;
}
mclSize blsIdGetDecStr(char *buf, mclSize maxBufSize, const blsId *id)
{
return cast(&id->v)->getStr(buf, maxBufSize, 10);
}