-
Notifications
You must be signed in to change notification settings - Fork 160
/
blst.go
1958 lines (1729 loc) · 40.5 KB
/
blst.go
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
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// DO NOT EDIT THIS FILE!!
// The file is generated from *.tgo by generate.py
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/*
* Copyright Supranational LLC
* Licensed under the Apache License, Version 2.0, see LICENSE for details.
* SPDX-License-Identifier: Apache-2.0
*/
package blst
// #cgo CFLAGS: -I${SRCDIR}/.. -I${SRCDIR}/../../build -I${SRCDIR}/../../src -D__BLST_CGO__
// #cgo amd64 CFLAGS: -D__ADX__ -mno-avx
// #include "blst.h"
import "C"
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
)
const BLST_SCALAR_BYTES = 256 / 8
const BLST_FP_BYTES = 384 / 8
const BLST_P1_COMPRESS_BYTES = BLST_FP_BYTES
const BLST_P1_SERIALIZE_BYTES = BLST_FP_BYTES * 2
const BLST_P2_COMPRESS_BYTES = BLST_FP_BYTES * 2
const BLST_P2_SERIALIZE_BYTES = BLST_FP_BYTES * 4
type Scalar = C.blst_scalar
type Fp = C.blst_fp
type Fp2 = C.blst_fp2
type Fp6 = C.blst_fp6
type Fp12 = C.blst_fp12
type P1 = C.blst_p1
type P2 = C.blst_p2
type P1Affine = C.blst_p1_affine
type P2Affine = C.blst_p2_affine
type Message = []byte
type Pairing = []uint64
type SecretKey = Scalar
//
// Configuration
//
var maxProcs = initMaxProcs()
func initMaxProcs() int {
maxProcs := runtime.GOMAXPROCS(0) - 1
if maxProcs <= 0 {
maxProcs = 1
}
return maxProcs
}
func SetMaxProcs(max int) {
if max <= 0 {
max = 1
}
maxProcs = max
}
//
// Secret key
//
func KeyGen(ikm []byte, optional ...[]byte) *SecretKey {
var sk SecretKey
var info []byte
var infoP *C.byte
if len(optional) > 0 {
info = optional[0]
infoP = (*C.byte)(&info[0])
}
if len(ikm) < 32 {
return nil
}
C.blst_keygen(&sk, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
infoP, C.size_t(len(info)))
return &sk
}
//
// Pairing
//
func PairingCtx(hash_or_encode bool, DST []byte) Pairing {
ctx := make([]uint64, C.blst_pairing_sizeof()/8)
var uDST *C.byte
if DST != nil {
uDST = (*C.byte)(&DST[0])
}
C.blst_pairing_init((*C.blst_pairing)(&ctx[0]), C.bool(hash_or_encode),
uDST, C.size_t(len(DST)))
return ctx
}
func PairingAggregatePkInG1(ctx Pairing, PK *P1Affine, sig *P2Affine,
msg []byte,
optional ...[]byte) int { // aug
var aug []byte
var uaug *C.byte
if len(optional) > 0 {
aug = optional[0]
if aug != nil {
uaug = (*C.byte)(&aug[0])
}
}
var umsg *C.byte
if msg != nil {
umsg = (*C.byte)(&msg[0])
}
r := C.blst_pairing_aggregate_pk_in_g1((*C.blst_pairing)(&ctx[0]), PK, sig,
umsg, C.size_t(len(msg)),
uaug, C.size_t(len(aug)))
return int(r)
}
func PairingAggregatePkInG2(ctx Pairing, PK *P2Affine, sig *P1Affine,
msg []byte,
optional ...[]byte) int { // aug
var aug []byte
var uaug *C.byte
if len(optional) > 0 {
aug = optional[0]
if aug != nil {
uaug = (*C.byte)(&aug[0])
}
}
var umsg *C.byte
if msg != nil {
umsg = (*C.byte)(&msg[0])
}
r := C.blst_pairing_aggregate_pk_in_g2((*C.blst_pairing)(&ctx[0]), PK, sig,
umsg, C.size_t(len(msg)),
uaug, C.size_t(len(aug)))
return int(r)
}
func PairingMulNAggregatePkInG1(ctx Pairing, PK *P1Affine, sig *P2Affine,
rand *Scalar, randBits int, msg []byte,
optional ...[]byte) int { // aug
var aug []byte
var uaug *C.byte
if len(optional) > 0 {
aug = optional[0]
if aug != nil {
uaug = (*C.byte)(&aug[0])
}
}
var umsg *C.byte
if msg != nil {
umsg = (*C.byte)(&msg[0])
}
r := C.blst_pairing_mul_n_aggregate_pk_in_g1((*C.blst_pairing)(&ctx[0]),
PK, sig,
&rand.b[0], C.size_t(randBits),
umsg, C.size_t(len(msg)),
uaug, C.size_t(len(aug)))
return int(r)
}
func PairingMulNAggregatePkInG2(ctx Pairing, PK *P2Affine, sig *P1Affine,
rand *Scalar, randBits int, msg []byte,
optional ...[]byte) int { // aug
var aug []byte
var uaug *C.byte
if len(optional) > 0 {
aug = optional[0]
if aug != nil {
uaug = (*C.byte)(&aug[0])
}
}
var umsg *C.byte
if msg != nil {
umsg = (*C.byte)(&msg[0])
}
r := C.blst_pairing_mul_n_aggregate_pk_in_g2((*C.blst_pairing)(&ctx[0]),
PK, sig,
&rand.b[0], C.size_t(randBits),
umsg, C.size_t(len(msg)),
uaug, C.size_t(len(aug)))
return int(r)
}
func PairingCommit(ctx Pairing) {
C.blst_pairing_commit((*C.blst_pairing)(&ctx[0]))
}
func PairingMerge(ctx Pairing, ctx1 Pairing) int {
r := C.blst_pairing_merge((*C.blst_pairing)(&ctx[0]),
(*C.blst_pairing)(&ctx1[0]))
return int(r)
}
func PairingFinalVerify(ctx Pairing, optional ...*Fp12) bool {
var gtsig *Fp12 = nil
if len(optional) > 0 {
gtsig = optional[0]
}
return bool(C.blst_pairing_finalverify((*C.blst_pairing)(&ctx[0]), gtsig))
}
func Fp12One() Fp12 {
return *C.blst_fp12_one()
}
//
// MIN-PK
//
//
// PublicKey
//
func (pk *P1Affine) From(s *Scalar) *P1Affine {
C.blst_sk_to_pk2_in_g1(nil, pk, s)
return pk
}
//
// Sign
//
func (sig *P2Affine) Sign(sk *SecretKey, msg []byte, dst []byte,
optional ...interface{}) *P2Affine {
augSingle, aug, useHash, ok := parseOpts(optional...)
if !ok || len(aug) != 0 {
return nil
}
var q *P2
if useHash {
q = HashToG2(msg, dst, augSingle)
} else {
q = EncodeToG2(msg, dst, augSingle)
}
C.blst_sign_pk2_in_g1(nil, sig, q, sk)
return sig
}
//
// Signature
//
// Functions to return a signature and public key+augmentation tuple.
// This enables point decompression (if needed) to happen in parallel.
type sigGetterP2 func() *P2Affine
type pkGetterP1 func(i uint32, temp *P1Affine) (*P1Affine, []byte)
// Single verify with decompressed pk
func (sig *P2Affine) Verify(pk *P1Affine, msg Message, dst []byte,
optional ...interface{}) bool { // useHash bool, aug []byte
// CLEANUP!!
// Check for infinities (eth spec)
var zeroSig P2Affine
var zeroPk P1Affine
if pk.Equals(&zeroPk) && sig.Equals(&zeroSig) {
return true
}
// CLEANUP!!
aug, _, useHash, ok := parseOpts(optional...)
if !ok {
return false
}
return sig.AggregateVerify([]*P1Affine{pk}, []Message{msg}, dst,
useHash, [][]byte{aug})
}
// Single verify with compressed pk
// Uses a dummy signature to get the correct type
func (dummy *P2Affine) VerifyCompressed(sig []byte, pk []byte,
msg Message, dst []byte,
optional ...bool) bool { // useHash bool, usePksAsAugs bool
// CLEANUP!!
// Check for infinities (eth spec)
// Need to support serialized points here?
if len(sig) == BLST_P2_COMPRESS_BYTES && sig[0] == 0xc0 &&
len(pk) == BLST_P1_COMPRESS_BYTES && pk[0] == 0xc0 &&
bytesAllZero(sig[1:]) && bytesAllZero(pk[1:]) {
return true
}
// CLEANUP!!
return dummy.AggregateVerifyCompressed(sig, [][]byte{pk},
[]Message{msg}, dst, optional...)
}
// Aggregate verify with uncompressed signature and public keys
func (sig *P2Affine) AggregateVerify(pks []*P1Affine, msgs []Message,
dst []byte,
optional ...interface{}) bool { // useHash bool, augs [][]byte
// sanity checks and argument parsing
if len(pks) != len(msgs) {
return false
}
_, augs, useHash, ok := parseOpts(optional...)
useAugs := len(augs) != 0
if !ok || (useAugs && len(augs) != len(msgs)) {
return false
}
sigFn := func() *P2Affine {
return sig
}
pkFn := func(i uint32, _ *P1Affine) (*P1Affine, []byte) {
if useAugs {
return pks[i], augs[i]
} else {
return pks[i], nil
}
}
return coreAggregateVerifyPkInG1(sigFn, pkFn, msgs, dst, useHash)
}
// Aggregate verify with compressed signature and public keys
// Uses a dummy signature to get the correct type
func (dummy *P2Affine) AggregateVerifyCompressed(sig []byte, pks [][]byte,
msgs []Message, dst []byte,
optional ...bool) bool { // useHash bool, usePksAsAugs bool
// sanity checks and argument parsing
if len(pks) != len(msgs) {
return false
}
useHash := true
if len(optional) > 0 {
useHash = optional[0]
}
usePksAsAugs := false
if len(optional) > 1 {
usePksAsAugs = optional[1]
}
sigFn := func() *P2Affine {
sigP := new(P2Affine)
if sig[0]&0x80 == 0 {
// Not compressed
if sigP.Deserialize(sig) == nil {
return nil
}
} else {
if sigP.Uncompress(sig) == nil {
return nil
}
}
return sigP
}
pkFn := func(i uint32, pk *P1Affine) (*P1Affine, []byte) {
bytes := pks[i]
if len(bytes) == 0 {
return nil, nil
}
if bytes[0]&0x80 == 0 {
// Not compressed
if pk.Deserialize(bytes) == nil {
return nil, nil
}
} else {
if pk.Uncompress(bytes) == nil {
return nil, nil
}
}
if usePksAsAugs {
return pk, bytes
}
return pk, nil
}
return coreAggregateVerifyPkInG1(sigFn, pkFn, msgs, dst, useHash)
}
// TODO: check message uniqueness
func coreAggregateVerifyPkInG1(sigFn sigGetterP2, pkFn pkGetterP1,
msgs []Message, dst []byte,
optional ...bool) bool { // useHash
n := len(msgs)
if n == 0 {
return true
}
useHash := true
if len(optional) > 0 {
useHash = optional[0]
}
numCores := runtime.GOMAXPROCS(0)
numThreads := maxProcs
if numThreads > numCores {
numThreads = numCores
}
if numThreads > n {
numThreads = n
}
// Each thread will determine next message to process by atomically
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
// repeat until n is exceeded. The resulting accumulations will be
// fed into the msgsCh channel.
msgsCh := make(chan Pairing, numThreads)
valid := int32(1)
curItem := uint32(0)
mutex := sync.Mutex{}
mutex.Lock()
for tid := 0; tid < numThreads; tid++ {
go func() {
pairing := PairingCtx(useHash, dst)
var temp P1Affine
for atomic.LoadInt32(&valid) > 0 {
// Get a work item
work := atomic.AddUint32(&curItem, 1) - 1
if work >= uint32(n) {
break
} else if work == 0 && maxProcs == numCores-1 &&
numThreads == maxProcs {
// Avoid consuming all cores by waiting until the
// main thread has completed its miller loop before
// proceeding.
mutex.Lock()
mutex.Unlock()
}
// Pull Public Key and augmentation blob
curPk, aug := pkFn(work, &temp)
if curPk == nil {
atomic.StoreInt32(&valid, 0)
break
}
// Pairing and accumulate
PairingAggregatePkInG1(pairing, curPk, nil, msgs[work], aug)
// application might have some async work to do
runtime.Gosched()
}
if atomic.LoadInt32(&valid) > 0 {
PairingCommit(pairing)
msgsCh <- pairing
} else {
msgsCh <- nil
}
}()
}
// Uncompress and check signature
var gtsig Fp12
sig := sigFn()
if sig == nil {
atomic.StoreInt32(&valid, 0)
} else {
C.blst_aggregated_in_g2(>sig, sig)
}
mutex.Unlock()
// Accumulate the thread results
var pairings Pairing
for i := 0; i < numThreads; i++ {
msg := <-msgsCh
if msg != nil {
if pairings == nil {
pairings = msg
} else {
PairingMerge(pairings, msg)
}
}
}
if atomic.LoadInt32(&valid) == 0 || pairings == nil {
return false
}
return PairingFinalVerify(pairings, >sig)
}
func (sig *P2Affine) FastAggregateVerify(pks []*P1Affine, msg Message,
dst []byte,
optional ...interface{}) bool { // pass-through to Verify
n := len(pks)
// TODO: return value for length zero?
if n == 0 {
return false
}
aggregator := new(P1Aggregate).Aggregate(pks)
if aggregator == nil {
return false
}
pkAff := aggregator.ToAffine()
// Verify
return sig.Verify(pkAff, msg, dst, optional...)
}
func (dummy *P2Affine) MultipleAggregateVerify(sigs []*P2Affine,
pks []*P1Affine, msgs []Message, dst []byte, randFn func(*Scalar),
randBits int,
optional ...interface{}) bool { // useHash
// Sanity checks and argument parsing
if len(pks) != len(msgs) || len(pks) != len(sigs) {
return false
}
_, augs, useHash, ok := parseOpts(optional...)
useAugs := len(augs) != 0
if !ok || (useAugs && len(augs) != len(msgs)) {
return false
}
paramsFn :=
func(work uint32, sig *P2Affine, pk *P1Affine, rand *Scalar) (
*P2Affine, *P1Affine, *Scalar, []byte) {
randFn(rand)
var aug []byte
if useAugs {
aug = augs[work]
}
return sigs[work], pks[work], rand, aug
}
return multipleAggregateVerifyPkInG1(paramsFn, msgs, dst,
randBits, useHash)
}
type mulAggGetterPkInG1 func(work uint32, sig *P2Affine, pk *P1Affine,
rand *Scalar) (*P2Affine, *P1Affine, *Scalar, []byte)
func multipleAggregateVerifyPkInG1(paramsFn mulAggGetterPkInG1, msgs []Message,
dst []byte, randBits int,
optional ...bool) bool { // useHash
n := len(msgs)
if n == 0 {
return true
}
useHash := true
if len(optional) > 0 {
useHash = optional[0]
}
numCores := runtime.GOMAXPROCS(0)
numThreads := maxProcs
if numThreads > numCores {
numThreads = numCores
}
if numThreads > n {
numThreads = n
}
// Each thread will determine next message to process by atomically
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
// repeat until n is exceeded. The resulting accumulations will be
// fed into the msgsCh channel.
msgsCh := make(chan Pairing, numThreads)
valid := int32(1)
curItem := uint32(0)
for tid := 0; tid < numThreads; tid++ {
go func() {
pairing := PairingCtx(useHash, dst)
var tempRand Scalar
var tempPk P1Affine
var tempSig P2Affine
for atomic.LoadInt32(&valid) > 0 {
// Get a work item
work := atomic.AddUint32(&curItem, 1) - 1
if work >= uint32(n) {
break
}
curSig, curPk, curRand, aug := paramsFn(work, &tempSig,
&tempPk, &tempRand)
if PairingMulNAggregatePkInG1(pairing, curPk, curSig, curRand,
randBits, msgs[work], aug) !=
C.BLST_SUCCESS {
atomic.StoreInt32(&valid, 0)
break
}
// application might have some async work to do
runtime.Gosched()
}
if atomic.LoadInt32(&valid) > 0 {
PairingCommit(pairing)
msgsCh <- pairing
} else {
msgsCh <- nil
}
}()
}
// Accumulate the thread results
var pairings Pairing
for i := 0; i < numThreads; i++ {
msg := <-msgsCh
if msg != nil {
if pairings == nil {
pairings = msg
} else {
PairingMerge(pairings, msg)
}
}
}
if atomic.LoadInt32(&valid) == 0 || pairings == nil {
return false
}
return PairingFinalVerify(pairings, nil)
}
//
// Aggregate P2
//
type aggGetterP2 func(i uint32, temp *P2Affine) *P2Affine
type P2Aggregate struct {
v *P2
}
// Aggregate uncompressed elements
func (agg *P2Aggregate) Aggregate(elmts []*P2Affine) *P2Aggregate {
if len(elmts) == 0 {
return agg
}
getter := func(i uint32, _ *P2Affine) *P2Affine { return elmts[i] }
if !agg.aggregate(getter, len(elmts)) {
return nil
}
return agg
}
// Aggregate compressed elements
func (agg *P2Aggregate) AggregateCompressed(elmts [][]byte) *P2Aggregate {
if len(elmts) == 0 {
return agg
}
getter := func(i uint32, p *P2Affine) *P2Affine {
bytes := elmts[i]
if len(bytes) == 0 {
return nil
}
if bytes[0]&0x80 == 0 {
// Not compressed
if p.Deserialize(bytes) == nil {
return nil
}
} else {
if p.Uncompress(bytes) == nil {
return nil
}
}
return p
}
if !agg.aggregate(getter, len(elmts)) {
return nil
}
return agg
}
func (agg *P2Aggregate) AddAggregate(other *P2Aggregate) *P2Aggregate {
if other.v == nil {
// do nothing
} else if agg.v == nil {
agg.v = other.v
} else {
C.blst_p2_add(agg.v, agg.v, other.v)
}
return agg
}
func (agg *P2Aggregate) Add(elmt *P2Affine) *P2Aggregate {
if agg.v == nil {
agg.v = new(P2)
C.blst_p2_from_affine(agg.v, elmt)
} else {
C.blst_p2_add_or_double_affine(agg.v, agg.v, elmt)
}
return agg
}
func (agg *P2Aggregate) ToAffine() *P2Affine {
if agg.v == nil {
return new(P2Affine)
}
return agg.v.ToAffine()
}
func (agg *P2Aggregate) aggregate(getter aggGetterP2, n int) bool {
if n == 0 {
return true
}
// operations are considered short enough for not to care about
// keeping one core free...
numThreads := runtime.GOMAXPROCS(0)
if numThreads > n {
numThreads = n
}
valid := int32(1)
type result struct {
agg *P2
empty bool
}
msgs := make(chan result, numThreads)
curItem := uint32(0)
for tid := 0; tid < numThreads; tid++ {
go func() {
first := true
var agg P2
var temp P2Affine
for atomic.LoadInt32(&valid) > 0 {
// Get a work item
work := atomic.AddUint32(&curItem, 1) - 1
if work >= uint32(n) {
break
}
// Signature validate
curElmt := getter(work, &temp)
if curElmt == nil {
atomic.StoreInt32(&valid, 0)
break
}
if first {
C.blst_p2_from_affine(&agg, curElmt)
first = false
} else {
C.blst_p2_add_or_double_affine(&agg, &agg, curElmt)
}
// application might have some async work to do
runtime.Gosched()
}
if first {
msgs <- result{nil, true}
} else if atomic.LoadInt32(&valid) > 0 {
msgs <- result{&agg, false}
} else {
msgs <- result{nil, false}
}
}()
}
// Accumulate the thread results
first := agg.v == nil
validLocal := true
for i := 0; i < numThreads; i++ {
msg := <-msgs
if !validLocal || msg.empty {
// do nothing
} else if msg.agg == nil {
validLocal = false
// This should be unnecessary but seems safer
atomic.StoreInt32(&valid, 0)
} else {
if first {
agg.v = msg.agg
first = false
} else {
C.blst_p2_add(agg.v, agg.v, msg.agg)
}
}
}
if atomic.LoadInt32(&valid) == 0 {
agg.v = nil
return false
}
return true
}
//
// MIN-SIG
//
//
// PublicKey
//
func (pk *P2Affine) From(s *Scalar) *P2Affine {
C.blst_sk_to_pk2_in_g2(nil, pk, s)
return pk
}
//
// Sign
//
func (sig *P1Affine) Sign(sk *SecretKey, msg []byte, dst []byte,
optional ...interface{}) *P1Affine {
augSingle, aug, useHash, ok := parseOpts(optional...)
if !ok || len(aug) != 0 {
return nil
}
var q *P1
if useHash {
q = HashToG1(msg, dst, augSingle)
} else {
q = EncodeToG1(msg, dst, augSingle)
}
C.blst_sign_pk2_in_g2(nil, sig, q, sk)
return sig
}
//
// Signature
//
// Functions to return a signature and public key+augmentation tuple.
// This enables point decompression (if needed) to happen in parallel.
type sigGetterP1 func() *P1Affine
type pkGetterP2 func(i uint32, temp *P2Affine) (*P2Affine, []byte)
// Single verify with decompressed pk
func (sig *P1Affine) Verify(pk *P2Affine, msg Message, dst []byte,
optional ...interface{}) bool { // useHash bool, aug []byte
// CLEANUP!!
// Check for infinities (eth spec)
var zeroSig P1Affine
var zeroPk P2Affine
if pk.Equals(&zeroPk) && sig.Equals(&zeroSig) {
return true
}
// CLEANUP!!
aug, _, useHash, ok := parseOpts(optional...)
if !ok {
return false
}
return sig.AggregateVerify([]*P2Affine{pk}, []Message{msg}, dst,
useHash, [][]byte{aug})
}
// Single verify with compressed pk
// Uses a dummy signature to get the correct type
func (dummy *P1Affine) VerifyCompressed(sig []byte, pk []byte,
msg Message, dst []byte,
optional ...bool) bool { // useHash bool, usePksAsAugs bool
// CLEANUP!!
// Check for infinities (eth spec)
// Need to support serialized points here?
if len(sig) == BLST_P1_COMPRESS_BYTES && sig[0] == 0xc0 &&
len(pk) == BLST_P2_COMPRESS_BYTES && pk[0] == 0xc0 &&
bytesAllZero(sig[1:]) && bytesAllZero(pk[1:]) {
return true
}
// CLEANUP!!
return dummy.AggregateVerifyCompressed(sig, [][]byte{pk},
[]Message{msg}, dst, optional...)
}
// Aggregate verify with uncompressed signature and public keys
func (sig *P1Affine) AggregateVerify(pks []*P2Affine, msgs []Message,
dst []byte,
optional ...interface{}) bool { // useHash bool, augs [][]byte
// sanity checks and argument parsing
if len(pks) != len(msgs) {
return false
}
_, augs, useHash, ok := parseOpts(optional...)
useAugs := len(augs) != 0
if !ok || (useAugs && len(augs) != len(msgs)) {
return false
}
sigFn := func() *P1Affine {
return sig
}
pkFn := func(i uint32, _ *P2Affine) (*P2Affine, []byte) {
if useAugs {
return pks[i], augs[i]
} else {
return pks[i], nil
}
}
return coreAggregateVerifyPkInG2(sigFn, pkFn, msgs, dst, useHash)
}
// Aggregate verify with compressed signature and public keys
// Uses a dummy signature to get the correct type
func (dummy *P1Affine) AggregateVerifyCompressed(sig []byte, pks [][]byte,
msgs []Message, dst []byte,
optional ...bool) bool { // useHash bool, usePksAsAugs bool
// sanity checks and argument parsing
if len(pks) != len(msgs) {
return false
}
useHash := true
if len(optional) > 0 {
useHash = optional[0]
}
usePksAsAugs := false
if len(optional) > 1 {
usePksAsAugs = optional[1]
}
sigFn := func() *P1Affine {
sigP := new(P1Affine)
if sig[0]&0x80 == 0 {
// Not compressed
if sigP.Deserialize(sig) == nil {
return nil
}
} else {
if sigP.Uncompress(sig) == nil {
return nil
}
}
return sigP
}
pkFn := func(i uint32, pk *P2Affine) (*P2Affine, []byte) {
bytes := pks[i]
if len(bytes) == 0 {
return nil, nil
}
if bytes[0]&0x80 == 0 {
// Not compressed
if pk.Deserialize(bytes) == nil {
return nil, nil
}
} else {
if pk.Uncompress(bytes) == nil {
return nil, nil
}
}
if usePksAsAugs {
return pk, bytes
}
return pk, nil
}
return coreAggregateVerifyPkInG2(sigFn, pkFn, msgs, dst, useHash)
}
// TODO: check message uniqueness
func coreAggregateVerifyPkInG2(sigFn sigGetterP1, pkFn pkGetterP2,
msgs []Message, dst []byte,
optional ...bool) bool { // useHash
n := len(msgs)
if n == 0 {
return true
}
useHash := true
if len(optional) > 0 {
useHash = optional[0]
}
numCores := runtime.GOMAXPROCS(0)
numThreads := maxProcs
if numThreads > numCores {
numThreads = numCores
}
if numThreads > n {
numThreads = n
}
// Each thread will determine next message to process by atomically
// incrementing curItem, process corresponding pk,msg[,aug] tuple and
// repeat until n is exceeded. The resulting accumulations will be
// fed into the msgsCh channel.
msgsCh := make(chan Pairing, numThreads)
valid := int32(1)
curItem := uint32(0)
mutex := sync.Mutex{}
mutex.Lock()
for tid := 0; tid < numThreads; tid++ {
go func() {
pairing := PairingCtx(useHash, dst)
var temp P2Affine
for atomic.LoadInt32(&valid) > 0 {
// Get a work item
work := atomic.AddUint32(&curItem, 1) - 1
if work >= uint32(n) {
break
} else if work == 0 && maxProcs == numCores-1 &&
numThreads == maxProcs {
// Avoid consuming all cores by waiting until the
// main thread has completed its miller loop before
// proceeding.
mutex.Lock()