Skip to content

Commit 9628c92

Browse files
committed
Add gnark as bls-12-381 backend
Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
1 parent b283c0c commit 9628c92

File tree

12 files changed

+790
-2
lines changed

12 files changed

+790
-2
lines changed

pairing/bls12381/bls12381_test.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.dedis.ch/kyber/v4/internal/test"
1919
"go.dedis.ch/kyber/v4/pairing"
2020
circl "go.dedis.ch/kyber/v4/pairing/bls12381/circl"
21+
"go.dedis.ch/kyber/v4/pairing/bls12381/gnark"
2122
kilic "go.dedis.ch/kyber/v4/pairing/bls12381/kilic"
2223
"go.dedis.ch/kyber/v4/sign/bls"
2324
"go.dedis.ch/kyber/v4/sign/tbls"
@@ -38,6 +39,7 @@ func TestScalarEndianess(t *testing.T) {
3839
suites := []pairing.Suite{
3940
kilic.NewBLS12381Suite(),
4041
circl.NewSuiteBLS12381(),
42+
gnark.NewSuiteBLS12381(),
4143
}
4244

4345
seed := "TestScalarEndianess"
@@ -109,6 +111,16 @@ func TestZKCryptoVectorsG1Compressed(t *testing.T) {
109111
if err != nil && testCaseValid {
110112
panic("Circl: err should be nil")
111113
}
114+
115+
// Test gnark
116+
g3 := gnark.G1Elt{}
117+
err = g3.UnmarshalBinary(byts)
118+
if err == nil && !testCaseValid {
119+
panic("Gnark: err should not be nil")
120+
}
121+
if err != nil && testCaseValid {
122+
panic("Gnark: err should be nil")
123+
}
112124
})
113125
}
114126
}
@@ -156,6 +168,16 @@ func TestZKCryptoVectorsG2Compressed(t *testing.T) {
156168
if err != nil && testCaseValid {
157169
panic("Circl: err should be nil")
158170
}
171+
172+
// Test gnark
173+
g3 := gnark.G2Elt{}
174+
err = g3.UnmarshalBinary(byts)
175+
if err == nil && !testCaseValid {
176+
panic("Gnark: err should not be nil")
177+
}
178+
if err != nil && testCaseValid {
179+
panic("Gnark: err should be nil")
180+
}
159181
})
160182
}
161183
}
@@ -400,6 +422,7 @@ func TestKyberG1(t *testing.T) {
400422
suites := []pairing.Suite{
401423
kilic.NewBLS12381Suite(),
402424
circl.NewSuiteBLS12381(),
425+
gnark.NewSuiteBLS12381(),
403426
}
404427

405428
for _, suite := range suites {
@@ -411,6 +434,7 @@ func TestKyberG2(t *testing.T) {
411434
suites := []pairing.Suite{
412435
kilic.NewBLS12381Suite(),
413436
circl.NewSuiteBLS12381(),
437+
gnark.NewSuiteBLS12381(),
414438
}
415439

416440
for _, suite := range suites {
@@ -422,6 +446,7 @@ func TestKyberPairingG2(t *testing.T) {
422446
suites := []pairing.Suite{
423447
kilic.NewBLS12381Suite(),
424448
circl.NewSuiteBLS12381(),
449+
gnark.NewSuiteBLS12381(),
425450
}
426451

427452
for _, s := range suites {
@@ -449,6 +474,7 @@ func TestRacePairings(_ *testing.T) {
449474
suites := []pairing.Suite{
450475
kilic.NewBLS12381Suite(),
451476
circl.NewSuiteBLS12381(),
477+
gnark.NewSuiteBLS12381(),
452478
}
453479

454480
for _, s := range suites {
@@ -473,6 +499,7 @@ func TestKyberBLSG2(t *testing.T) {
473499
suites := []pairing.Suite{
474500
kilic.NewBLS12381Suite(),
475501
circl.NewSuiteBLS12381(),
502+
gnark.NewSuiteBLS12381(),
476503
}
477504

478505
for _, suite := range suites {
@@ -485,6 +512,7 @@ func TestKyberBLSG1(t *testing.T) {
485512
suites := []pairing.Suite{
486513
kilic.NewBLS12381Suite(),
487514
circl.NewSuiteBLS12381(),
515+
gnark.NewSuiteBLS12381(),
488516
}
489517

490518
for _, suite := range suites {
@@ -497,6 +525,7 @@ func TestKyberThresholdG2(t *testing.T) {
497525
suites := []pairing.Suite{
498526
kilic.NewBLS12381Suite(),
499527
circl.NewSuiteBLS12381(),
528+
gnark.NewSuiteBLS12381(),
500529
}
501530

502531
for _, suite := range suites {
@@ -509,6 +538,7 @@ func TestKyberThresholdG1(t *testing.T) {
509538
suites := []pairing.Suite{
510539
kilic.NewBLS12381Suite(),
511540
circl.NewSuiteBLS12381(),
541+
gnark.NewSuiteBLS12381(),
512542
}
513543

514544
for _, suite := range suites {
@@ -521,6 +551,7 @@ func TestIsValidGroup(t *testing.T) {
521551
suites := []pairing.Suite{
522552
kilic.NewBLS12381Suite(),
523553
circl.NewSuiteBLS12381(),
554+
gnark.NewSuiteBLS12381(),
524555
}
525556

526557
for _, suite := range suites {
@@ -549,6 +580,7 @@ func TestBasicPairing(t *testing.T) {
549580
suites := []pairing.Suite{
550581
kilic.NewBLS12381Suite(),
551582
circl.NewSuiteBLS12381(),
583+
gnark.NewSuiteBLS12381(),
552584
}
553585

554586
for _, suite := range suites {
@@ -601,6 +633,7 @@ func BenchmarkPairingSeparate(bb *testing.B) {
601633
var suites = []pairing.Suite{
602634
kilic.NewBLS12381Suite(),
603635
circl.NewSuiteBLS12381(),
636+
gnark.NewSuiteBLS12381(),
604637
}
605638

606639
for _, s := range suites {
@@ -630,6 +663,7 @@ func BenchmarkPairingInv(bb *testing.B) {
630663
var suites = []pairing.Suite{
631664
kilic.NewBLS12381Suite(),
632665
circl.NewSuiteBLS12381(),
666+
gnark.NewSuiteBLS12381(),
633667
}
634668

635669
for _, s := range suites {
@@ -657,7 +691,7 @@ func BenchmarkPairingInv(bb *testing.B) {
657691
var (
658692
dataSize = 32
659693
numSigs = []int{1, 10, 100, 1000, 10000}
660-
curveOptions = []string{"kilic", "circl"}
694+
curveOptions = []string{"kilic", "circl", "gnark"}
661695
)
662696

663697
// Used to avoid compiler optimizations
@@ -671,6 +705,9 @@ func BenchmarkKilic(b *testing.B) {
671705
func BenchmarkCircl(b *testing.B) {
672706
BLSBenchmark(b, "circl")
673707
}
708+
func BenchmarkGnark(b *testing.B) {
709+
BLSBenchmark(b, "gnark")
710+
}
674711

675712
func BLSBenchmark(b *testing.B, curveOption string) {
676713
b.Logf("----------------------")
@@ -696,6 +733,8 @@ func BLSBenchmark(b *testing.B, curveOption string) {
696733
suite = kilic.NewBLS12381Suite()
697734
case "circl":
698735
suite = circl.NewSuiteBLS12381()
736+
case "gnark":
737+
suite = gnark.NewSuiteBLS12381()
699738
default:
700739
panic(fmt.Errorf("invalid curve option: %s", curveOption))
701740
}
@@ -752,7 +791,7 @@ func BLSBenchmark(b *testing.B, curveOption string) {
752791
}
753792
}
754793
})
755-
b.Run(fmt.Sprintf("AggregateSign-G1 on %d signs", n), func(bb *testing.B) {
794+
b.Run(fmt.Sprintf("AggregateSign-G2 on %d signs", n), func(bb *testing.B) {
756795
for j := 0; j < bb.N; j++ {
757796
result, err = schemeOnG2.AggregateSignatures(sigsOnG2[:n]...)
758797
if err != nil {

pairing/bls12381/gnark/adapter.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package gnark
2+
3+
import (
4+
"go.dedis.ch/kyber/v4"
5+
)
6+
7+
// SuiteBLS12381 is an adapter that implements the suites.Suite interface so that
8+
// bls12381 can be used as a common suite to generate key pairs for instance but
9+
// still preserves the properties of the pairing (e.g. the Pair function).
10+
//
11+
// It's important to note that the Point function will generate a point
12+
// compatible with public keys only (group G2) where the signature must be
13+
// used as a point from the group G1.
14+
type SuiteBLS12381 struct {
15+
Suite
16+
kyber.Group
17+
}
18+
19+
// NewSuiteBLS12381 makes a new BN256 suite
20+
func NewSuiteBLS12381() *SuiteBLS12381 {
21+
return &SuiteBLS12381{}
22+
}
23+
24+
// Point generates a point from the G2 group that can only be used
25+
// for public keys
26+
func (s *SuiteBLS12381) Point() kyber.Point {
27+
return s.G2().Point()
28+
}
29+
30+
// PointLen returns the length of a G2 point
31+
func (s *SuiteBLS12381) PointLen() int {
32+
return s.G2().PointLen()
33+
}
34+
35+
// Scalar generates a scalar
36+
func (s *SuiteBLS12381) Scalar() kyber.Scalar {
37+
return s.G1().Scalar()
38+
}
39+
40+
// ScalarLen returns the length of a scalar
41+
func (s *SuiteBLS12381) ScalarLen() int {
42+
return s.G1().ScalarLen()
43+
}
44+
45+
// String returns the name of the suite
46+
func (s *SuiteBLS12381) String() string {
47+
return "gnark.adapter"
48+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package gnark
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"go.dedis.ch/kyber/v4/util/key"
8+
)
9+
10+
func TestAdapter_SuiteBLS12381(t *testing.T) {
11+
suite := NewSuiteBLS12381()
12+
13+
pair := key.NewKeyPair(suite)
14+
pubkey, err := pair.Public.MarshalBinary()
15+
require.Nil(t, err)
16+
privkey, err := pair.Private.MarshalBinary()
17+
require.Nil(t, err)
18+
19+
pubhex := suite.Point()
20+
err = pubhex.UnmarshalBinary(pubkey)
21+
require.Nil(t, err)
22+
23+
privhex := suite.Scalar()
24+
err = privhex.UnmarshalBinary(privkey)
25+
require.Nil(t, err)
26+
27+
require.Equal(t, "gnark.adapter", suite.String())
28+
}

0 commit comments

Comments
 (0)