-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
constantine.go
868 lines (786 loc) · 24.6 KB
/
constantine.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
/** Constantine
* Copyright (c) 2018-2019 Status Research & Development GmbH
* Copyright (c) 2020-Present Mamy André-Ratsimbazafy
* Licensed and distributed under either of
* * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
* * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
* at your option. This file may not be copied, modified, or distributed except according to those terms.
*/
package constantine
/*
#cgo CFLAGS: -I"${SRCDIR}/../include"
#cgo !windows LDFLAGS: "${SRCDIR}/../lib/libconstantine.a"
// The ending in .lib is rejected, so we can't use the direct linking syntax:
// https://github.com/golang/go/blob/46ea4ab/src/cmd/go/internal/work/security.go#L216
// #cgo windows LDFLAGS: "${SRCDIR}/../lib/constantine.lib"
#cgo windows LDFLAGS: -L"${SRCDIR}/../lib" -Wl,-Bstatic -lconstantine -Wl,-Bdynamic
#include <stdlib.h>
#include <constantine.h>
*/
import "C"
import (
"errors"
"unsafe"
)
// Threadpool API
// -----------------------------------------------------
type Threadpool struct {
ctx *C.ctt_threadpool
}
func ThreadpoolNew(numThreads int) Threadpool {
return Threadpool{
ctx: C.ctt_threadpool_new(C.int(numThreads)),
}
}
func (tp Threadpool) Shutdown() {
C.ctt_threadpool_shutdown(tp.ctx)
}
// Ethereum EIP-4844 KZG API
// -----------------------------------------------------
type (
EthKzgCommitment [48]byte
EthKzgProof [48]byte
EthBlob [4096 * 32]byte
EthKzgChallenge [32]byte
EthKzgEvalAtChallenge [32]byte
)
type EthKzgContext struct {
cCtx *C.ctt_eth_kzg_context
threadpool Threadpool
}
func EthKzgContextNew(trustedSetupFile string) (ctx EthKzgContext, err error) {
cFile := C.CString(trustedSetupFile)
defer C.free(unsafe.Pointer(cFile))
status := C.ctt_eth_trusted_setup_load(
&ctx.cCtx,
cFile,
C.cttEthTSFormat_ckzg4844,
)
if status != C.cttEthTS_Success {
err = errors.New(
C.GoString(C.ctt_eth_trusted_setup_status_to_string(status)),
)
}
ctx.threadpool.ctx = nil
return ctx, err
}
func (ctx *EthKzgContext) SetThreadpool(tp Threadpool) {
ctx.threadpool = tp
}
func (ctx EthKzgContext) Delete() {
C.ctt_eth_trusted_setup_delete(ctx.cCtx)
}
func (ctx EthKzgContext) BlobToKzgCommitment(blob EthBlob) (commitment EthKzgCommitment, err error) {
status := C.ctt_eth_kzg_blob_to_kzg_commitment(
ctx.cCtx,
(*C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitment)),
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
)
if status != C.cttEthKzg_Success {
err = errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
}
return commitment, err
}
func (ctx EthKzgContext) ComputeKzgProof(blob EthBlob, z EthKzgChallenge) (proof EthKzgProof, y EthKzgEvalAtChallenge, err error) {
status := C.ctt_eth_kzg_compute_kzg_proof(
ctx.cCtx,
(*C.ctt_eth_kzg_proof)(unsafe.Pointer(&proof)),
(*C.ctt_eth_kzg_eval_at_challenge)(unsafe.Pointer(&y)),
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
(*C.ctt_eth_kzg_opening_challenge)(unsafe.Pointer(&z)),
)
if status != C.cttEthKzg_Success {
err = errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
}
return proof, y, err
}
func (ctx EthKzgContext) VerifyKzgProof(commitment EthKzgCommitment, z EthKzgChallenge, y EthKzgEvalAtChallenge, proof EthKzgProof) (bool, error) {
status := C.ctt_eth_kzg_verify_kzg_proof(
ctx.cCtx,
(*C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitment)),
(*C.ctt_eth_kzg_opening_challenge)(unsafe.Pointer(&z)),
(*C.ctt_eth_kzg_eval_at_challenge)(unsafe.Pointer(&y)),
(*C.ctt_eth_kzg_proof)(unsafe.Pointer(&proof)),
)
if status != C.cttEthKzg_Success {
err := errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (ctx EthKzgContext) ComputeBlobKzgProof(blob EthBlob, commitment EthKzgCommitment) (proof EthKzgProof, err error) {
status := C.ctt_eth_kzg_compute_blob_kzg_proof(
ctx.cCtx,
(*C.ctt_eth_kzg_proof)(unsafe.Pointer(&proof)),
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
(*C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitment)),
)
if status != C.cttEthKzg_Success {
err = errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
}
return proof, err
}
func (ctx EthKzgContext) VerifyBlobKzgProof(blob EthBlob, commitment EthKzgCommitment, proof EthKzgProof) (bool, error) {
status := C.ctt_eth_kzg_verify_blob_kzg_proof(
ctx.cCtx,
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
(*C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitment)),
(*C.ctt_eth_kzg_proof)(unsafe.Pointer(&proof)),
)
if status != C.cttEthKzg_Success {
err := errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (ctx EthKzgContext) VerifyBlobKzgProofBatch(blobs []EthBlob, commitments []EthKzgCommitment, proofs []EthKzgProof, secureRandomBytes [32]byte) (bool, error) {
if len(blobs) != len(commitments) || len(blobs) != len(proofs) {
return false, errors.New("VerifyBlobKzgProofBatch: Lengths of inputs do not match.")
}
status := C.ctt_eth_kzg_verify_blob_kzg_proof_batch(
ctx.cCtx,
*(**C.ctt_eth_kzg_blob)(unsafe.Pointer(&blobs)),
*(**C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitments)),
*(**C.ctt_eth_kzg_proof)(unsafe.Pointer(&proofs)),
(C.size_t)(len(blobs)),
(*C.uint8_t)(unsafe.Pointer(&secureRandomBytes)),
)
if status != C.cttEthKzg_Success {
err := errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
return false, err
}
return true, nil
}
// Ethereum EIP-4844 KZG API - Parallel
// -----------------------------------------------------
func (ctx EthKzgContext) BlobToKzgCommitmentParallel(blob EthBlob) (commitment EthKzgCommitment, err error) {
if ctx.threadpool.ctx == nil {
return commitment, errors.New("BlobToKzgCommitmentParallel: The threadpool is not configured.")
}
status := C.ctt_eth_kzg_blob_to_kzg_commitment_parallel(
ctx.threadpool.ctx, ctx.cCtx,
(*C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitment)),
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
)
if status != C.cttEthKzg_Success {
err = errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
}
return commitment, err
}
func (ctx EthKzgContext) ComputeKzgProofParallel(blob EthBlob, z EthKzgChallenge) (proof EthKzgProof, y EthKzgEvalAtChallenge, err error) {
if ctx.threadpool.ctx == nil {
return proof, y, errors.New("ComputeKzgProofParallel: The Constantine's threadpool is not configured.")
}
status := C.ctt_eth_kzg_compute_kzg_proof_parallel(
ctx.threadpool.ctx, ctx.cCtx,
(*C.ctt_eth_kzg_proof)(unsafe.Pointer(&proof)),
(*C.ctt_eth_kzg_eval_at_challenge)(unsafe.Pointer(&y)),
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
(*C.ctt_eth_kzg_opening_challenge)(unsafe.Pointer(&z)),
)
if status != C.cttEthKzg_Success {
err = errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
}
return proof, y, err
}
func (ctx EthKzgContext) ComputeBlobKzgProofParallel(blob EthBlob, commitment EthKzgCommitment) (proof EthKzgProof, err error) {
if ctx.threadpool.ctx == nil {
return proof, errors.New("ComputeBlobKzgProofParallel: The threadpool is not configured.")
}
status := C.ctt_eth_kzg_compute_blob_kzg_proof_parallel(
ctx.threadpool.ctx, ctx.cCtx,
(*C.ctt_eth_kzg_proof)(unsafe.Pointer(&proof)),
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
(*C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitment)),
)
if status != C.cttEthKzg_Success {
err = errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
}
return proof, err
}
func (ctx EthKzgContext) VerifyBlobKzgProofParallel(blob EthBlob, commitment EthKzgCommitment, proof EthKzgProof) (bool, error) {
if ctx.threadpool.ctx == nil {
return false, errors.New("VerifyBlobKzgProofParallel: The threadpool is not configured.")
}
status := C.ctt_eth_kzg_verify_blob_kzg_proof_parallel(
ctx.threadpool.ctx, ctx.cCtx,
(*C.ctt_eth_kzg_blob)(unsafe.Pointer(&blob)),
(*C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitment)),
(*C.ctt_eth_kzg_proof)(unsafe.Pointer(&proof)),
)
if status != C.cttEthKzg_Success {
err := errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (ctx EthKzgContext) VerifyBlobKzgProofBatchParallel(blobs []EthBlob, commitments []EthKzgCommitment, proofs []EthKzgProof, secureRandomBytes [32]byte) (bool, error) {
if len(blobs) != len(commitments) || len(blobs) != len(proofs) {
return false, errors.New("VerifyBlobKzgProofBatch: Lengths of inputs do not match.")
}
if ctx.threadpool.ctx == nil {
return false, errors.New("VerifyBlobKzgProofBatch: The threadpool is not configured.")
}
status := C.ctt_eth_kzg_verify_blob_kzg_proof_batch_parallel(
ctx.threadpool.ctx, ctx.cCtx,
*(**C.ctt_eth_kzg_blob)(unsafe.Pointer(&blobs)),
*(**C.ctt_eth_kzg_commitment)(unsafe.Pointer(&commitments)),
*(**C.ctt_eth_kzg_proof)(unsafe.Pointer(&proofs)),
(C.size_t)(len(blobs)),
(*C.uint8_t)(unsafe.Pointer(&secureRandomBytes)),
)
if status != C.cttEthKzg_Success {
err := errors.New(
C.GoString(C.ctt_eth_kzg_status_to_string(status)),
)
return false, err
}
return true, nil
}
// Ethereum BLS signatures
// -----------------------------------------------------
func getAddr[T any](arg []T) (unsafe.Pointer) {
// Makes sure to not access a non existant 0 element if the slice is empty
if len(arg) > 0 {
return unsafe.Pointer(&arg[0])
} else {
return nil
}
}
type (
EthBlsSecKey C.ctt_eth_bls_seckey
EthBlsPubKey C.ctt_eth_bls_pubkey
EthBlsSignature C.ctt_eth_bls_signature
)
func (pub EthBlsPubKey) IsZero() bool {
status := C.ctt_eth_bls_pubkey_is_zero((*C.ctt_eth_bls_pubkey)(&pub))
return bool(status)
}
func (sec *EthBlsSecKey) Deserialize(src [32]byte) (bool, error) {
status := C.ctt_eth_bls_deserialize_seckey((*C.ctt_eth_bls_seckey)(sec),
(*C.byte)(&src[0]),
)
if status != C.cttCodecScalar_Success {
err := errors.New(
C.GoString(C.ctt_codec_scalar_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (pub *EthBlsPubKey) DerivePubKey(sec EthBlsSecKey) {
C.ctt_eth_bls_derive_pubkey((*C.ctt_eth_bls_pubkey)(pub), (*C.ctt_eth_bls_seckey)(&sec))
}
func (pub *EthBlsPubKey) Verify(message []byte, sig EthBlsSignature) (bool, error) {
status := C.ctt_eth_bls_verify((*C.ctt_eth_bls_pubkey)(pub),
(*C.byte)(getAddr(message)),
(C.ptrdiff_t)(len(message)),
(*C.ctt_eth_bls_signature)(&sig),
)
if status != C.cttEthBls_Success {
err := errors.New(
C.GoString(C.ctt_eth_bls_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (sig EthBlsSignature) IsZero() bool {
status := C.ctt_eth_bls_signature_is_zero((*C.ctt_eth_bls_signature)(&sig))
return bool(status)
}
func (pub1 EthBlsPubKey) AreEqual(pub2 EthBlsPubKey) bool {
status := C.ctt_eth_bls_pubkeys_are_equal((*C.ctt_eth_bls_pubkey)(&pub1), (*C.ctt_eth_bls_pubkey)(&pub2))
return bool(status)
}
func (sig1 EthBlsSignature) AreEqual(sig2 EthBlsSignature) bool {
status := C.ctt_eth_bls_signatures_are_equal((*C.ctt_eth_bls_signature)(&sig1), (*C.ctt_eth_bls_signature)(&sig2))
return bool(status)
}
func (sec *EthBlsSecKey) Validate() (bool, error) {
status := C.ctt_eth_bls_validate_seckey((*C.ctt_eth_bls_seckey)(sec))
if status != C.cttCodecScalar_Success {
err := errors.New(
C.GoString(C.ctt_codec_scalar_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (pub *EthBlsPubKey) Validate() (bool, error) {
status := C.ctt_eth_bls_validate_pubkey((*C.ctt_eth_bls_pubkey)(pub))
if status != C.cttCodecEcc_Success {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (sig *EthBlsSignature) Validate() (bool, error) {
status := C.ctt_eth_bls_validate_signature((*C.ctt_eth_bls_signature)(sig))
if status != C.cttCodecEcc_Success {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (sec *EthBlsSecKey) Serialize(dst *[32]byte) (bool, error) {
status := C.ctt_eth_bls_serialize_seckey((*C.byte)(unsafe.Pointer(dst)),
(*C.ctt_eth_bls_seckey)(sec),
)
if status != C.cttCodecScalar_Success {
err := errors.New(
C.GoString(C.ctt_codec_scalar_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (pub *EthBlsPubKey) SerializeCompressed(dst *[48]byte) (bool, error) {
status := C.ctt_eth_bls_serialize_pubkey_compressed((*C.byte)(unsafe.Pointer(dst)),
(*C.ctt_eth_bls_pubkey)(pub),
)
if status != C.cttCodecEcc_Success {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (sig *EthBlsSignature) SerializeCompressed(dst *[96]byte) (bool, error) {
status := C.ctt_eth_bls_serialize_signature_compressed((*C.byte)(unsafe.Pointer(dst)),
(*C.ctt_eth_bls_signature)(sig),
)
if status != C.cttCodecEcc_Success {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (pub *EthBlsPubKey) DeserializeCompressedUnchecked(src [48]byte) (bool, error) {
status := C.ctt_eth_bls_deserialize_pubkey_compressed_unchecked((*C.ctt_eth_bls_pubkey)(pub),
(*C.byte)(&src[0]),
)
if status != C.cttCodecEcc_Success {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (sig *EthBlsSignature) DeserializeCompressedUnchecked(src [96]byte) (bool, error) {
status := C.ctt_eth_bls_deserialize_signature_compressed_unchecked((*C.ctt_eth_bls_signature)(sig),
(*C.byte)(&src[0]),
)
if status != C.cttCodecEcc_Success {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (pub *EthBlsPubKey) DeserializeCompressed(src [48]byte) (bool, error) {
status := C.ctt_eth_bls_deserialize_pubkey_compressed((*C.ctt_eth_bls_pubkey)(pub),
(*C.byte)(&src[0]),
)
if status != C.cttCodecEcc_Success && status != C.cttCodecEcc_PointAtInfinity {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (sig *EthBlsSignature) DeserializeCompressed(src [96]byte) (bool, error) {
status := C.ctt_eth_bls_deserialize_signature_compressed((*C.ctt_eth_bls_signature)(sig),
(*C.byte)(&src[0]),
)
if status != C.cttCodecEcc_Success && status != C.cttCodecEcc_PointAtInfinity {
err := errors.New(
C.GoString(C.ctt_codec_ecc_status_to_string(status)),
)
return false, err
}
return true, nil
}
func (sig *EthBlsSignature) Sign(sec EthBlsSecKey, message []byte) {
C.ctt_eth_bls_sign((*C.ctt_eth_bls_signature)(sig), (*C.ctt_eth_bls_seckey)(&sec),
(*C.byte)(getAddr(message)),
(C.ptrdiff_t)(len(message)),
)
}
func FastAggregateVerify(pubkeys []EthBlsPubKey, message []byte, aggregate_sig EthBlsSignature) (bool, error) {
if len(pubkeys) == 0 {
err := errors.New(
"No public keys given.",
)
return false, err
}
status := C.ctt_eth_bls_fast_aggregate_verify((*C.ctt_eth_bls_pubkey)(getAddr(pubkeys)),
(C.ptrdiff_t)(len(pubkeys)),
(*C.byte)(getAddr(message)),
(C.ptrdiff_t)(len(message)),
(*C.ctt_eth_bls_signature)(&aggregate_sig),
)
if status != C.cttEthBls_Success {
err := errors.New(
C.GoString(C.ctt_eth_bls_status_to_string(status)),
)
return false, err
}
return true, nil
}
// NOTE: C.ctt_eth_bls_batch_sig_accumulator is an incomplete struct. Therefore
// we use 2 functions on the Nim side to (de)allocate storage for the struct.
type ethBlsBatchSigAccumulator struct {
ctx *C.ctt_eth_bls_batch_sig_accumulator
}
func ethBlsBatchSigAccumulatorAlloc() (accum ethBlsBatchSigAccumulator) {
accum.ctx = C.ctt_eth_bls_alloc_batch_sig_accumulator()
return accum
}
func ethBlsBatchSigAccumulatorFree(accum ethBlsBatchSigAccumulator) {
C.ctt_eth_bls_free_batch_sig_accumulator(accum.ctx)
}
func (accum ethBlsBatchSigAccumulator) init(secureRandomBytes [32]byte, accumSepTag []byte) {
C.ctt_eth_bls_init_batch_sig_accumulator((*C.ctt_eth_bls_batch_sig_accumulator)(accum.ctx),
(*C.byte)(&secureRandomBytes[0]),
(*C.byte)(getAddr(accumSepTag)),
(C.ptrdiff_t)(len(accumSepTag)),
)
}
func (accum ethBlsBatchSigAccumulator) update(pub EthBlsPubKey, message []byte, sig EthBlsSignature) bool {
status := C.ctt_eth_bls_update_batch_sig_accumulator((*C.ctt_eth_bls_batch_sig_accumulator)(accum.ctx),
(*C.ctt_eth_bls_pubkey)(&pub),
(*C.byte)(getAddr(message)),
(C.ptrdiff_t)(len(message)),
(*C.ctt_eth_bls_signature)(&sig),
)
return bool(status)
}
func (accum ethBlsBatchSigAccumulator) finalVerify() bool {
status := C.ctt_eth_bls_final_verify_batch_sig_accumulator(
(*C.ctt_eth_bls_batch_sig_accumulator)(accum.ctx),
)
return bool(status)
}
func BatchVerifySoA(pubkeys []EthBlsPubKey, messages [][]byte, signatures []EthBlsSignature, secureRandomBytes [32]byte) (bool, error) {
if len(pubkeys) == 0 {
err := errors.New(
C.GoString(
C.ctt_eth_bls_status_to_string(C.cttEthBls_ZeroLengthAggregation),
),
)
return false, err
} else if len(pubkeys) != len(messages) {
err := errors.New("Number of public keys must match number of messages.")
return false, err
} else if len(pubkeys) != len(signatures) {
err := errors.New("Number of public keys must match number of signatures.")
return false, err
}
// Deal with cases were pubkey or signature were mistakenly zero-init, due to a generic aggregation tentative for example
for _, pub := range pubkeys {
if pub.IsZero() {
err := errors.New(
C.GoString(
C.ctt_eth_bls_status_to_string(C.cttEthBls_PointAtInfinity),
),
)
return false, err
}
}
for _, sig := range signatures {
if sig.IsZero() {
err := errors.New(
C.GoString(
C.ctt_eth_bls_status_to_string(C.cttEthBls_PointAtInfinity),
),
)
return false, err
}
}
// NOTE: We *must* use the New / Free functions!
accum := ethBlsBatchSigAccumulatorAlloc()
defer ethBlsBatchSigAccumulatorFree(accum)
accum.init(secureRandomBytes, []byte("serial"))
for i, pub := range pubkeys {
if !accum.update(pub, messages[i], signatures[i]) {
err := errors.New(
C.GoString(
C.ctt_eth_bls_status_to_string(C.cttEthBls_VerificationFailure),
),
)
return false, err
}
}
return accum.finalVerify(), nil
}
type BatchVerifyTriplet struct {
pub EthBlsPubKey
message []byte
sig EthBlsSignature
}
func BatchVerifyAoS(triplets []BatchVerifyTriplet, secureRandomBytes [32]byte) (bool, error) {
if len(triplets) == 0 {
err := errors.New(
C.GoString(
C.ctt_eth_bls_status_to_string(C.cttEthBls_ZeroLengthAggregation),
),
)
return false, err
}
// Deal with cases were pubkey or signature were mistakenly zero-init, due to a generic aggregation tentative for example
for _, trp := range triplets {
if trp.pub.IsZero() || trp.sig.IsZero() {
err := errors.New(
C.GoString(
C.ctt_eth_bls_status_to_string(C.cttEthBls_PointAtInfinity),
),
)
return false, err
}
}
// NOTE: We *must* use the New / Free functions!
accum := ethBlsBatchSigAccumulatorAlloc()
defer ethBlsBatchSigAccumulatorFree(accum)
accum.init(secureRandomBytes, []byte("serial"))
for _, trp := range triplets {
if !accum.update(trp.pub, trp.message, trp.sig) {
err := errors.New(
C.GoString(
C.ctt_eth_bls_status_to_string(C.cttEthBls_VerificationFailure),
),
)
return false, err
}
}
return accum.finalVerify(), nil
}
// --------------------------------
// ------- EVM precompiles --------
// --------------------------------
func EvmSha256(result *[32]byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_sha256((*C.byte)(&result[0]),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmModexp(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_modexp((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBn254G1Add(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bn254_g1add((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBn254G1Mul(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bn254_g1mul((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBn254G1EcPairingCheck(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bn254_ecpairingcheck((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381G1Add(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_g1add((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381G1Mul(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_g1mul((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381G1Msm(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_g1msm((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381G2Add(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_g2add((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381G2Mul(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_g2mul((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381G2Msm(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_g2msm((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381PairingCheck(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_pairingcheck((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381MapFpToG1(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_map_fp_to_g1((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}
func EvmBls12381MapFp2ToG2(result []byte, inputs []byte) (bool, error) {
status := C.ctt_eth_evm_bls12381_map_fp2_to_g2((*C.byte)(getAddr(result)),
(C.ptrdiff_t)(len(result)),
(*C.byte)(getAddr(inputs)),
(C.ptrdiff_t)(len(inputs)),
)
if status != C.cttEVM_Success {
err := errors.New(
C.GoString(C.ctt_evm_status_to_string(status)),
)
return false, err
}
return true, nil
}