Skip to content

Commit

Permalink
use go version bls implementation (#39)
Browse files Browse the repository at this point in the history
* replace github.com/herumi/bls-go-binary with github.com/consensys/gnark-crypto for bls
  • Loading branch information
readygo67 authored Aug 18, 2024
1 parent 8c7d650 commit e2b11c1
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 60 deletions.
114 changes: 72 additions & 42 deletions certification/bls/bls.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,100 @@
package bls

import "github.com/herumi/bls-go-binary/bls"
import (
"encoding/hex"
bls "github.com/consensys/gnark-crypto/ecc/bls12-381"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"math/big"
)

const (
dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_"
publicKeyGenerator = "1 0x24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8 0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e 0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801 0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be"
dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_"
)

func init() {
if err := bls.Init(bls.BLS12_381); err != nil {
panic(err)
}

// Set Ethereum serialization format.
bls.SetETHserialization(true)
if err := bls.SetMapToMode(bls.IRTF); err != nil {
panic(err)
}

// Set the generator of G2. see https://www.ietf.org/archive/id/draft-irtf-cfrg-pairing-friendly-curves-11.html#section-4.2.1
var gen bls.PublicKey
if err := gen.SetHexString(publicKeyGenerator); err != nil {
panic(err)
}
if err := bls.SetGeneratorOfPublicKey(&gen); err != nil {
panic(err)
}

if err := bls.SetDstG1(dstG1); err != nil {
panic(err)
}
}

type PublicKey = bls.PublicKey
type PublicKey bls.G2Affine

// PublicKeyFromBytes returns a PublicKey from a byte slice.
func PublicKeyFromBytes(b []byte) (*PublicKey, error) {
var pub bls.PublicKey
return &pub, pub.Deserialize(b)
var pub bls.G2Affine
err := pub.Unmarshal(b)
if err != nil {
return nil, err
}
res := PublicKey(pub)
return &res, err
}

// PublicKeyFromHexString returns a PublicKey from a hex string.
func PublicKeyFromHexString(s string) (*PublicKey, error) {
var pub bls.PublicKey
return &pub, pub.DeserializeHexStr(s)
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return PublicKeyFromBytes(b)
}

type SecretKey = bls.SecretKey
type SecretKey fr.Element

// NewSecretKeyByCSPRNG returns a new SecretKey generated by CSPRNG.
func NewSecretKeyByCSPRNG() *SecretKey {
var sk bls.SecretKey
sk.SetByCSPRNG()
return &sk
var sk fr.Element
_, err := sk.SetRandom()
if err != nil {
return nil
}
res := SecretKey(sk)
return &res
}

func (sk *SecretKey) PublicKey() *PublicKey {
_, _, _, g2Gen := bls.Generators()

el := fr.Element(*sk)
v := el.BigInt(big.NewInt(0))

pk := g2Gen.ScalarMultiplication(&g2Gen, v)
return (*PublicKey)(pk)
}

func (sk *SecretKey) Sign(msg []byte) *Signature {
el := fr.Element(*sk)
v := el.BigInt(big.NewInt(0))
g1, _ := bls.HashToG1(msg, []byte(dstG1))
sig := g1.ScalarMultiplication(&g1, v)
return (*Signature)(sig)
}

type Signature = bls.Sign
type Signature bls.G1Affine

// SignatureFromBytes returns a Signature from a byte slice.
func SignatureFromBytes(b []byte) (*Signature, error) {
var sig bls.Sign
return &sig, sig.Deserialize(b)
var sig bls.G1Affine
_, err := sig.SetBytes(b)
if err != nil {
return nil, err
}
res := Signature(sig)
return &res, err
}

// SignatureFromHexString returns a Signature from a hex string.
func SignatureFromHexString(s string) (*Signature, error) {
var sig bls.Sign
return &sig, sig.DeserializeHexStr(s)
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return SignatureFromBytes(b)
}

func (sig *Signature) Verify(pk *PublicKey, msg []byte) bool {
g1, _ := bls.HashToG1(msg, []byte(dstG1))
_, _, _, g2 := bls.Generators()
var g2n bls.G2Affine
g2n.Neg(&g2)

valid, err := bls.PairingCheck([]bls.G1Affine{bls.G1Affine(*sig), g1}, []bls.G2Affine{g2n, bls.G2Affine(*pk)})
if err != nil {
return false
}
return valid
}
16 changes: 8 additions & 8 deletions certification/bls/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package bls

import (
"encoding/hex"
"github.com/stretchr/testify/assert"
"testing"
)

func TestSecretKey(t *testing.T) {
sk := NewSecretKeyByCSPRNG()
s := sk.Sign("hello")
if !s.Verify(sk.GetPublicKey(), "hello") {
t.Error()
}
s := sk.Sign([]byte("hello"))
assert.True(t, s.Verify(sk.PublicKey(), []byte("hello")))
assert.False(t, s.Verify(sk.PublicKey(), []byte("world")))
}

func TestVerify(t *testing.T) {
Expand All @@ -29,10 +29,10 @@ func TestVerify(t *testing.T) {
t.Fatal(err)
}

if signature.Verify(publicKey, "bye") {
if signature.Verify(publicKey, []byte("bye")) {
t.Error()
}
if !signature.Verify(publicKey, "hello") {
if !signature.Verify(publicKey, []byte("hello")) {
t.Error()
}
}
Expand All @@ -51,10 +51,10 @@ func TestVerify_hex(t *testing.T) {
t.Fatal(err)
}

if signature.Verify(publicKey, "bye") {
if signature.Verify(publicKey, []byte("bye")) {
t.Error()
}
if !signature.Verify(publicKey, "hello") {
if !signature.Verify(publicKey, []byte("hello")) {
t.Error()
}
}
5 changes: 3 additions & 2 deletions certification/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func PublicBLSKeyFromDER(der []byte) (*bls.PublicKey, error) {
if !curveID.Equal(asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 44668, 5, 3, 2, 1}) {
return nil, fmt.Errorf("invalid curve identifier: %v", curveID)
}
return bls.PublicKeyFromBytes(bs.Bytes)
pub, err := bls.PublicKeyFromBytes(bs.Bytes)
return pub, err
}

func PublicBLSKeyToDER(publicKey []byte) ([]byte, error) {
Expand Down Expand Up @@ -163,7 +164,7 @@ func verifyCertificateSignature(certificate Certificate, publicKey *bls.PublicKe
if err != nil {
return err
}
if !signature.VerifyByte(publicKey, message) {
if !signature.Verify(publicKey, message) {
return fmt.Errorf("signature verification failed")
}
return nil
Expand Down
18 changes: 16 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ go 1.22.1
require (
github.com/aviate-labs/leb128 v0.3.0
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a
github.com/consensys/gnark-crypto v0.12.2-0.20240215234832-d72fcb379d3e
github.com/di-wu/parser v0.3.0
github.com/fxamacker/cbor/v2 v2.6.0
github.com/herumi/bls-go-binary v1.34.0
github.com/stretchr/testify v1.8.4
google.golang.org/protobuf v1.34.1
)

require github.com/x448/float16 v0.8.4 // indirect
require (
github.com/bits-and-blooms/bitset v1.7.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
44 changes: 38 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,49 @@ github.com/aviate-labs/leb128 v0.3.0 h1:s9htRv3OYk8nuHqJu9PiVFJxv1jIUTIcpEeiURa9
github.com/aviate-labs/leb128 v0.3.0/go.mod h1:GclhBOjhIKmcDlgHKhj0AEZollzERfZUbcRUKiQVqgY=
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a h1:aQkG/D+l8Y7tr809l8pN+KebH2jzacWReSFQmeEKFgM=
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a/go.mod h1:C/lr3F9TimrVkdZckG5mz+VU0TrmpeyVKUjzv2YyGwA=
github.com/bits-and-blooms/bitset v1.7.0 h1:YjAGVd3XmtK9ktAbX8Zg2g2PwLIMjGREZJHlV4j7NEo=
github.com/bits-and-blooms/bitset v1.7.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ=
github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
github.com/consensys/gnark-crypto v0.12.2-0.20240215234832-d72fcb379d3e h1:MKdOuCiy2DAX1tMp2YsmtNDaqdigpY6B5cZQDJ9BvEo=
github.com/consensys/gnark-crypto v0.12.2-0.20240215234832-d72fcb379d3e/go.mod h1:wKqwsieaKPThcFkHe0d0zMsbHEUWFmZcG7KBCse210o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/di-wu/parser v0.3.0 h1:NMOvy5ifswgt4gsdhySVcKOQtvjC43cHZIfViWctqQY=
github.com/di-wu/parser v0.3.0/go.mod h1:SLp58pW6WamdmznrVRrw2NTyn4wAvT9rrEFynKX7nYo=
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/herumi/bls-go-binary v1.34.0 h1:x1sKp8zzx+alvlifB+vbA0KkY0Pz4Br31cZ/saDkiFE=
github.com/herumi/bls-go-binary v1.34.0/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=

0 comments on commit e2b11c1

Please sign in to comment.