Skip to content

Commit 325fe11

Browse files
committed
bindings/go: make it work with upcoming go1.24.
In an unexpected turn of events go1.24 reclassified import "C" non-local, which rendered C.blst_* type aliases defunct. The only working solution appears to be to wrap them into structs. Since all the C.blst_* types were opaque, the wrapping shouldn't affect applications. In the sense that the shell struct won't obstruct anything, because nothing was visible.
1 parent bef14ca commit 325fe11

9 files changed

+415
-361
lines changed

bindings/go/blst.go

+240-206
Large diffs are not rendered by default.

bindings/go/blst.tgo

+43-35
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ const BLST_P1_SERIALIZE_BYTES = BLST_FP_BYTES * 2
158158
const BLST_P2_COMPRESS_BYTES = BLST_FP_BYTES * 2
159159
const BLST_P2_SERIALIZE_BYTES = BLST_FP_BYTES * 4
160160

161-
type Scalar = C.blst_scalar
162-
type Fp = C.blst_fp
163-
type Fp2 = C.blst_fp2
161+
type Scalar struct{ cgo C.blst_scalar }
162+
type Fp struct{ cgo C.blst_fp }
163+
type Fp2 struct{ cgo C.blst_fp2 }
164164
type Fp6 = C.blst_fp6
165-
type Fp12 = C.blst_fp12
166-
type P1 = C.blst_p1
167-
type P2 = C.blst_p2
168-
type P1Affine = C.blst_p1_affine
169-
type P2Affine = C.blst_p2_affine
165+
type Fp12 struct{ cgo C.blst_fp12 }
166+
type P1 struct{ cgo C.blst_p1 }
167+
type P2 struct{ cgo C.blst_p2 }
168+
type P1Affine struct{ cgo C.blst_p1_affine }
169+
type P2Affine struct{ cgo C.blst_p2_affine }
170170
type Message = []byte
171171
type Pairing = []C.blst_pairing
172172
type SecretKey = Scalar
@@ -219,9 +219,9 @@ func numThreads(maxThreads int) int {
219219
}
220220

221221
var cgo_pairingSizeOf = C.blst_pairing_sizeof()
222-
var cgo_p1Generator = *C.blst_p1_generator()
223-
var cgo_p2Generator = *C.blst_p2_generator()
224-
var cgo_fp12One = *C.blst_fp12_one()
222+
var cgo_p1Generator = P1{*C.blst_p1_generator()}
223+
var cgo_p2Generator = P2{*C.blst_p2_generator()}
224+
var cgo_fp12One = Fp12{*C.blst_fp12_one()}
225225

226226
//
227227
// Secret key
@@ -240,8 +240,8 @@ func KeyGen(ikm []byte, optional ...[]byte) *SecretKey {
240240
if len(ikm) < 32 {
241241
return nil
242242
}
243-
C.blst_keygen(&sk, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
244-
ptrOrNil(info), C.size_t(len(info)))
243+
C.blst_keygen(&sk.cgo, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
244+
ptrOrNil(info), C.size_t(len(info)))
245245
// Postponing secret key zeroing till garbage collection can be too
246246
// late to be effective, but every little bit helps...
247247
runtime.SetFinalizer(&sk, func(sk *SecretKey) { sk.Zeroize() })
@@ -257,8 +257,8 @@ func KeyGenV3(ikm []byte, optional ...[]byte) *SecretKey {
257257
if len(optional) > 0 {
258258
info = optional[0]
259259
}
260-
C.blst_keygen_v3(&sk, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
261-
ptrOrNil(info), C.size_t(len(info)))
260+
C.blst_keygen_v3(&sk.cgo, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
261+
ptrOrNil(info), C.size_t(len(info)))
262262
// Postponing secret key zeroing till garbage collection can be too
263263
// late to be effective, but every little bit helps...
264264
runtime.SetFinalizer(&sk, func(sk *SecretKey) { sk.Zeroize() })
@@ -274,9 +274,9 @@ func KeyGenV45(ikm []byte, salt []byte, optional ...[]byte) *SecretKey {
274274
if len(optional) > 0 {
275275
info = optional[0]
276276
}
277-
C.blst_keygen_v4_5(&sk, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
278-
(*C.byte)(&salt[0]), C.size_t(len(salt)),
279-
ptrOrNil(info), C.size_t(len(info)))
277+
C.blst_keygen_v4_5(&sk.cgo, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
278+
(*C.byte)(&salt[0]), C.size_t(len(salt)),
279+
ptrOrNil(info), C.size_t(len(info)))
280280
// Postponing secret key zeroing till garbage collection can be too
281281
// late to be effective, but every little bit helps...
282282
runtime.SetFinalizer(&sk, func(sk *SecretKey) { sk.Zeroize() })
@@ -292,9 +292,9 @@ func KeyGenV5(ikm []byte, salt []byte, optional ...[]byte) *SecretKey {
292292
if len(optional) > 0 {
293293
info = optional[0]
294294
}
295-
C.blst_keygen_v5(&sk, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
296-
(*C.byte)(&salt[0]), C.size_t(len(salt)),
297-
ptrOrNil(info), C.size_t(len(info)))
295+
C.blst_keygen_v5(&sk.cgo, (*C.byte)(&ikm[0]), C.size_t(len(ikm)),
296+
(*C.byte)(&salt[0]), C.size_t(len(salt)),
297+
ptrOrNil(info), C.size_t(len(info)))
298298
// Postponing secret key zeroing till garbage collection can be too
299299
// late to be effective, but every little bit helps...
300300
runtime.SetFinalizer(&sk, func(sk *SecretKey) { sk.Zeroize() })
@@ -306,7 +306,7 @@ func DeriveMasterEip2333(ikm []byte) *SecretKey {
306306
return nil
307307
}
308308
var sk SecretKey
309-
C.blst_derive_master_eip2333(&sk, (*C.byte)(&ikm[0]), C.size_t(len(ikm)))
309+
C.blst_derive_master_eip2333(&sk.cgo, (*C.byte)(&ikm[0]), C.size_t(len(ikm)))
310310
// Postponing secret key zeroing till garbage collection can be too
311311
// late to be effective, but every little bit helps...
312312
runtime.SetFinalizer(&sk, func(sk *SecretKey) { sk.Zeroize() })
@@ -315,7 +315,7 @@ func DeriveMasterEip2333(ikm []byte) *SecretKey {
315315

316316
func (master *SecretKey) DeriveChildEip2333(child_index uint32) *SecretKey {
317317
var sk SecretKey
318-
C.blst_derive_child_eip2333(&sk, master, C.uint(child_index))
318+
C.blst_derive_child_eip2333(&sk.cgo, &master.cgo, C.uint(child_index))
319319
// Postponing secret key zeroing till garbage collection can be too
320320
// late to be effective, but every little bit helps...
321321
runtime.SetFinalizer(&sk, func(sk *SecretKey) { sk.Zeroize() })
@@ -350,30 +350,38 @@ func PairingFinalVerify(ctx Pairing, optional ...*Fp12) bool {
350350
if len(optional) > 0 {
351351
gtsig = optional[0]
352352
}
353-
return bool(C.blst_pairing_finalverify(&ctx[0], gtsig))
353+
return bool(C.blst_pairing_finalverify(&ctx[0], gtsig.asPtr()))
354354
}
355355

356356
func PairingRawAggregate(ctx Pairing, q *P2Affine, p *P1Affine) {
357-
C.blst_pairing_raw_aggregate(&ctx[0], q, p)
357+
C.blst_pairing_raw_aggregate(&ctx[0], &q.cgo, &p.cgo)
358358
}
359359

360360
func PairingAsFp12(ctx Pairing) *Fp12 {
361361
var pt Fp12
362-
C.go_pairing_as_fp12(&pt, &ctx[0])
362+
C.go_pairing_as_fp12(&pt.cgo, &ctx[0])
363363
return &pt
364364
}
365365

366+
func (pt *Fp12) asPtr() *C.blst_fp12 {
367+
if (pt != nil) {
368+
return &pt.cgo
369+
}
370+
371+
return nil
372+
}
373+
366374
func Fp12One() Fp12 {
367375
return cgo_fp12One
368376
}
369377

370378
func Fp12FinalVerify(pt1 *Fp12, pt2 *Fp12) bool {
371-
return bool(C.blst_fp12_finalverify(pt1, pt2))
379+
return bool(C.blst_fp12_finalverify(&pt1.cgo, &pt2.cgo))
372380
}
373381

374382
func Fp12MillerLoop(q *P2Affine, p *P1Affine) *Fp12 {
375383
var pt Fp12
376-
C.blst_miller_loop(&pt, q, p)
384+
C.blst_miller_loop(&pt.cgo, &q.cgo, &p.cgo)
377385
return &pt
378386
}
379387

@@ -387,7 +395,7 @@ func Fp12MillerLoopN(qs []P2Affine, ps []P1Affine) *Fp12 {
387395

388396
if nThreads == 1 || nElems == 1 {
389397
var pt Fp12
390-
C.go_miller_loop_n(&pt, &qs[0], &ps[0], C.size_t(nElems), false)
398+
C.go_miller_loop_n(&pt.cgo, &qs[0].cgo, &ps[0].cgo, C.size_t(nElems), false)
391399
return &pt
392400
}
393401

@@ -417,7 +425,7 @@ func Fp12MillerLoopN(qs []P2Affine, ps []P1Affine) *Fp12 {
417425
if n > stride {
418426
n = stride
419427
}
420-
C.go_miller_loop_n(&acc, &qs[work], &ps[work], C.size_t(n),
428+
C.go_miller_loop_n(&acc.cgo, &qs[work].cgo, &ps[work].cgo, C.size_t(n),
421429
C.bool(!first))
422430
first = false
423431
}
@@ -431,25 +439,25 @@ func Fp12MillerLoopN(qs []P2Affine, ps []P1Affine) *Fp12 {
431439
}
432440

433441
var pt Fp12
434-
C.go_fp12slice_mul(&pt, &ret[0], C.size_t(nThreads))
442+
C.go_fp12slice_mul(&pt.cgo, &ret[0].cgo, C.size_t(nThreads))
435443
return &pt
436444
}
437445

438446
func (pt *Fp12) MulAssign(p *Fp12) {
439-
C.blst_fp12_mul(pt, pt, p)
447+
C.blst_fp12_mul(&pt.cgo, &pt.cgo, &p.cgo)
440448
}
441449

442450
func (pt *Fp12) FinalExp() {
443-
C.blst_final_exp(pt, pt)
451+
C.blst_final_exp(&pt.cgo, &pt.cgo)
444452
}
445453

446454
func (pt *Fp12) InGroup() bool {
447-
return bool(C.blst_fp12_in_group(pt))
455+
return bool(C.blst_fp12_in_group(&pt.cgo))
448456
}
449457

450458
func (pt *Fp12) ToBendian() []byte {
451459
var out [BLST_FP_BYTES*12]byte
452-
C.blst_bendian_from_fp12((*C.byte)(&out[0]), pt)
460+
C.blst_bendian_from_fp12((*C.byte)(&out[0]), &pt.cgo)
453461
return out[:]
454462
}
455463

bindings/go/blst_htoc_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ func decodeP1(m map[string]interface{}) *P1Affine {
2929
return nil
3030
}
3131
var p1 P1Affine
32-
p1.x.FromBEndian(x)
33-
p1.y.FromBEndian(y)
32+
p1.Deserialize(append(x, y...))
3433
return &p1
3534
}
3635

@@ -131,10 +130,7 @@ func decodeP2(m map[string]interface{}) *P2Affine {
131130
return nil
132131
}
133132
var p2 P2Affine
134-
p2.x.fp[0].FromBEndian(x0)
135-
p2.x.fp[1].FromBEndian(x1)
136-
p2.y.fp[0].FromBEndian(y0)
137-
p2.y.fp[1].FromBEndian(y1)
133+
p2.Deserialize(append(x1, append(x0, append(y1, y0...)...)...))
138134
return &p2
139135
}
140136

bindings/go/blst_minpk.tgo

+22-14
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,29 @@ import (
99
// PublicKey
1010
//
1111

12+
func (pt *P1Affine) asPtr() *C.blst_p1_affine {
13+
if (pt != nil) {
14+
return &pt.cgo
15+
}
16+
17+
return nil
18+
}
19+
1220
func (pk *P1Affine) From(s *Scalar) *P1Affine {
13-
C.blst_sk_to_pk2_in_g1(nil, pk, s)
21+
C.blst_sk_to_pk2_in_g1(nil, &pk.cgo, &s.cgo)
1422
return pk
1523
}
1624

1725
func (pk *P1Affine) KeyValidate() bool {
18-
return bool(C.go_p1_affine_validate(pk, true))
26+
return bool(C.go_p1_affine_validate(&pk.cgo, true))
1927
}
2028

2129
// sigInfcheck, check for infinity, is a way to avoid going
2230
// into resource-consuming verification. Passing 'false' is
2331
// always cryptographically safe, but application might want
2432
// to guard against obviously bogus individual[!] signatures.
2533
func (sig *P2Affine) SigValidate(sigInfcheck bool) bool {
26-
return bool(C.go_p2_affine_validate(sig, C.bool(sigInfcheck)))
34+
return bool(C.go_p2_affine_validate(&sig.cgo, C.bool(sigInfcheck)))
2735
}
2836

2937
//
@@ -43,7 +51,7 @@ func (sig *P2Affine) Sign(sk *SecretKey, msg []byte, dst []byte,
4351
} else {
4452
q = EncodeToG2(msg, dst, augSingle)
4553
}
46-
C.blst_sign_pk2_in_g1(nil, sig, q, sk)
54+
C.blst_sign_pk2_in_g1(nil, &sig.cgo, &q.cgo, &sk.cgo)
4755
return sig
4856
}
4957

@@ -247,7 +255,7 @@ func coreAggregateVerifyPkInG1(sigFn sigGetterP2, sigGroupcheck bool,
247255
atomic.StoreInt32(&valid, 0)
248256
}
249257
if atomic.LoadInt32(&valid) > 0 {
250-
C.blst_aggregated_in_g2(&gtsig, sig)
258+
C.blst_aggregated_in_g2(&gtsig.cgo, &sig.cgo)
251259
}
252260
mutex.Unlock()
253261

@@ -295,7 +303,7 @@ func CoreVerifyPkInG1(pk *P1Affine, sig *P2Affine, hash_or_encode bool,
295303
return C.BLST_SUCCESS
296304
}
297305

298-
return int(C.blst_core_verify_pk_in_g1(pk, sig, C.bool(hash_or_encode),
306+
return int(C.blst_core_verify_pk_in_g1(&pk.cgo, &sig.cgo, C.bool(hash_or_encode),
299307
ptrOrNil(msg), C.size_t(len(msg)),
300308
ptrOrNil(dst), C.size_t(len(dst)),
301309
ptrOrNil(aug), C.size_t(len(aug))))
@@ -489,19 +497,19 @@ func (agg *P2Aggregate) AddAggregate(other *P2Aggregate) {
489497
} else if agg.v == nil {
490498
agg.v = other.v
491499
} else {
492-
C.blst_p2_add_or_double(agg.v, agg.v, other.v)
500+
C.blst_p2_add_or_double(&agg.v.cgo, &agg.v.cgo, &other.v.cgo)
493501
}
494502
}
495503

496504
func (agg *P2Aggregate) Add(elmt *P2Affine, groupcheck bool) bool {
497-
if groupcheck && !bool(C.blst_p2_affine_in_g2(elmt)) {
505+
if groupcheck && !bool(C.blst_p2_affine_in_g2(&elmt.cgo)) {
498506
return false
499507
}
500508
if agg.v == nil {
501509
agg.v = new(P2)
502-
C.blst_p2_from_affine(agg.v, elmt)
510+
C.blst_p2_from_affine(&agg.v.cgo, &elmt.cgo)
503511
} else {
504-
C.blst_p2_add_or_double_affine(agg.v, agg.v, elmt)
512+
C.blst_p2_add_or_double_affine(&agg.v.cgo, &agg.v.cgo, &elmt.cgo)
505513
}
506514
return true
507515
}
@@ -551,15 +559,15 @@ func (agg *P2Aggregate) coreAggregate(getter aggGetterP2, groupcheck bool,
551559
atomic.StoreInt32(&valid, 0)
552560
break
553561
}
554-
if groupcheck && !bool(C.blst_p2_affine_in_g2(curElmt)) {
562+
if groupcheck && !bool(C.blst_p2_affine_in_g2(&curElmt.cgo)) {
555563
atomic.StoreInt32(&valid, 0)
556564
break
557565
}
558566
if first {
559-
C.blst_p2_from_affine(&agg, curElmt)
567+
C.blst_p2_from_affine(&agg.cgo, &curElmt.cgo)
560568
first = false
561569
} else {
562-
C.blst_p2_add_or_double_affine(&agg, &agg, curElmt)
570+
C.blst_p2_add_or_double_affine(&agg.cgo, &agg.cgo, &curElmt.cgo)
563571
}
564572
// application might have some async work to do
565573
runtime.Gosched()
@@ -590,7 +598,7 @@ func (agg *P2Aggregate) coreAggregate(getter aggGetterP2, groupcheck bool,
590598
agg.v = msg.agg
591599
first = false
592600
} else {
593-
C.blst_p2_add_or_double(agg.v, agg.v, msg.agg)
601+
C.blst_p2_add_or_double(&agg.v.cgo, &agg.v.cgo, &msg.agg.cgo)
594602
}
595603
}
596604
}

bindings/go/blst_minpk_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestSerdesMinPk(t *testing.T) {
5454
}
5555

5656
// Negative test equals
57-
sk.b[0]++
57+
sk.cgo.b[0]++
5858
if sk.Equals(sk2) {
5959
t.Error("sk2 == sk")
6060
}

bindings/go/blst_minsig_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestSerdesMinSig(t *testing.T) {
5858
}
5959

6060
// Negative test equals
61-
sk.b[0]++
61+
sk.cgo.b[0]++
6262
if sk.Equals(sk2) {
6363
t.Error("sk2 == sk")
6464
}

0 commit comments

Comments
 (0)