Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gnark as bls-12-381 backend #551

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ issues:
- path: "group/edwards25519/scalar.go"
linters:
- ineffassign
- path: "pairing/(circl_bls12381|bn254)/."
- path: "pairing/(bls12381|bn254)/."
linters:
- errcheck #TODO: proper error handling
text: "Error return value is not checked"
Expand Down
41 changes: 40 additions & 1 deletion pairing/bls12381/bls12381_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"go.dedis.ch/kyber/v4/internal/test"
"go.dedis.ch/kyber/v4/pairing"
circl "go.dedis.ch/kyber/v4/pairing/bls12381/circl"
"go.dedis.ch/kyber/v4/pairing/bls12381/gnark"
kilic "go.dedis.ch/kyber/v4/pairing/bls12381/kilic"
"go.dedis.ch/kyber/v4/sign/bdn"
"go.dedis.ch/kyber/v4/sign/bls"
Expand All @@ -39,6 +40,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

seed := "TestScalarEndianess"
Expand Down Expand Up @@ -110,6 +112,16 @@
if err != nil && testCaseValid {
panic("Circl: err should be nil")
}

// Test gnark
g3 := gnark.G1Elt{}
err = g3.UnmarshalBinary(byts)
if err == nil && !testCaseValid {
panic("Gnark: err should not be nil")
}
if err != nil && testCaseValid {
panic("Gnark: err should be nil")
}
})
}
}
Expand Down Expand Up @@ -157,6 +169,16 @@
if err != nil && testCaseValid {
panic("Circl: err should be nil")
}

// Test gnark
g3 := gnark.G2Elt{}
err = g3.UnmarshalBinary(byts)
if err == nil && !testCaseValid {
panic("Gnark: err should not be nil")
}
if err != nil && testCaseValid {
panic("Gnark: err should be nil")
}
})
}
}
Expand Down Expand Up @@ -401,6 +423,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand All @@ -412,6 +435,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand All @@ -423,6 +447,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, s := range suites {
Expand Down Expand Up @@ -450,6 +475,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, s := range suites {
Expand All @@ -474,6 +500,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand All @@ -486,6 +513,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand All @@ -498,6 +526,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand All @@ -510,6 +539,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand All @@ -522,6 +552,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand Down Expand Up @@ -550,6 +581,7 @@
suites := []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, suite := range suites {
Expand Down Expand Up @@ -602,6 +634,7 @@
var suites = []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, s := range suites {
Expand Down Expand Up @@ -631,6 +664,7 @@
var suites = []pairing.Suite{
kilic.NewBLS12381Suite(),
circl.NewSuiteBLS12381(),
gnark.NewSuiteBLS12381(),
}

for _, s := range suites {
Expand Down Expand Up @@ -658,7 +692,7 @@
var (
dataSize = 32
numSigs = []int{1, 10, 100, 1000, 10000}
curveOptions = []string{"kilic", "circl"}
curveOptions = []string{"kilic", "circl", "gnark"}
)

// Used to avoid compiler optimizations
Expand All @@ -672,6 +706,9 @@
func BenchmarkCircl(b *testing.B) {
BDNBenchmark(b, "circl")
}
func BenchmarkGnark(b *testing.B) {
BLSBenchmark(b, "gnark")

Check failure on line 710 in pairing/bls12381/bls12381_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 64b, 1.21.10)

undefined: BLSBenchmark

Check failure on line 710 in pairing/bls12381/bls12381_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: BLSBenchmark (typecheck)

Check failure on line 710 in pairing/bls12381/bls12381_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 64b, 1.22.3)

undefined: BLSBenchmark

Check failure on line 710 in pairing/bls12381/bls12381_test.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 64b, 1.21.10)

undefined: BLSBenchmark

Check failure on line 710 in pairing/bls12381/bls12381_test.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 64b, 1.22.3)

undefined: BLSBenchmark
}

//nolint: gocyclo,cyclop // breaking this down doesn't make sense
func BDNBenchmark(b *testing.B, curveOption string) {
Expand All @@ -698,6 +735,8 @@
suite = kilic.NewBLS12381Suite()
case "circl":
suite = circl.NewSuiteBLS12381()
case "gnark":
suite = gnark.NewSuiteBLS12381()
default:
panic(fmt.Errorf("invalid curve option: %s", curveOption))
}
Expand Down
48 changes: 48 additions & 0 deletions pairing/bls12381/gnark/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gnark

import (
"go.dedis.ch/kyber/v4"
)

// SuiteBLS12381 is an adapter that implements the suites.Suite interface so that
// bls12381 can be used as a common suite to generate key pairs for instance but
// still preserves the properties of the pairing (e.g. the Pair function).
//
// It's important to note that the Point function will generate a point
// compatible with public keys only (group G2) where the signature must be
// used as a point from the group G1.
type SuiteBLS12381 struct {
Suite
kyber.Group
}

// NewSuiteBLS12381 makes a new BN256 suite
func NewSuiteBLS12381() *SuiteBLS12381 {
return &SuiteBLS12381{}
}

// Point generates a point from the G2 group that can only be used
// for public keys
func (s *SuiteBLS12381) Point() kyber.Point {
return s.G2().Point()
}

// PointLen returns the length of a G2 point
func (s *SuiteBLS12381) PointLen() int {
return s.G2().PointLen()
}

// Scalar generates a scalar
func (s *SuiteBLS12381) Scalar() kyber.Scalar {
return s.G1().Scalar()
}

// ScalarLen returns the length of a scalar
func (s *SuiteBLS12381) ScalarLen() int {
return s.G1().ScalarLen()
}

// String returns the name of the suite
func (s *SuiteBLS12381) String() string {
return "gnark.adapter"
}
30 changes: 30 additions & 0 deletions pairing/bls12381/gnark/adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gnark

import (
"testing"

"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v4/util/key"
)

func TestAdapter_SuiteBLS12381(t *testing.T) {
suite := NewSuiteBLS12381()

pair := key.NewKeyPair(suite)
pubkey, err := pair.Public.MarshalBinary()
require.Nil(t, err)
privkey, err := pair.Private.MarshalBinary()
require.Nil(t, err)

pubhex := suite.Point()
err = pubhex.UnmarshalBinary(pubkey)
require.Nil(t, err)
require.True(t, pair.Public.Equal(pubhex))

privhex := suite.Scalar()
err = privhex.UnmarshalBinary(privkey)
require.Nil(t, err)
require.True(t, pair.Private.Equal(privhex))

AnomalRoil marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, "gnark.adapter", suite.String())
}
Loading
Loading