diff --git a/.gitignore b/.gitignore index 7def77d..1ea749b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ coverage.out *.wasm + +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..a7a0180 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,39 @@ +version: 2 + +before: + hooks: + - go generate ./... + - make fmt + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 + id: goic + binary: goic + main: ./cmd/goic + +archives: + - format: tar.gz + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end }} + format_overrides: + - goos: windows + format: zip + +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" diff --git a/certification/bls/bls.go b/certification/bls/bls.go index 2a9e3e2..d255c9a 100644 --- a/certification/bls/bls.go +++ b/certification/bls/bls.go @@ -7,21 +7,24 @@ import ( "math/big" ) -const ( - dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_" -) +const dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_" + +var g2, g2Gen bls.G2Affine + +func init() { + _, _, _, g2Gen = bls.Generators() + g2.Neg(&g2Gen) +} type PublicKey bls.G2Affine // PublicKeyFromBytes returns a PublicKey from a byte slice. func PublicKeyFromBytes(b []byte) (*PublicKey, error) { - var pub bls.G2Affine - err := pub.Unmarshal(b) - if err != nil { + var publicKey bls.G2Affine + if err := publicKey.Unmarshal(b); err != nil { return nil, err } - res := PublicKey(pub) - return &res, err + return (*PublicKey)(&publicKey), nil } // PublicKeyFromHexString returns a PublicKey from a hex string. @@ -37,44 +40,42 @@ type SecretKey fr.Element // NewSecretKeyByCSPRNG returns a new SecretKey generated by CSPRNG. func NewSecretKeyByCSPRNG() *SecretKey { - var sk fr.Element - _, err := sk.SetRandom() - if err != nil { + var secretKey fr.Element + if _, err := secretKey.SetRandom(); err != nil { return nil } - res := SecretKey(sk) - return &res + return (*SecretKey)(&secretKey) } 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) + element := fr.Element(*sk) + return (*PublicKey)(g2Gen.ScalarMultiplication( + &g2Gen, + element.BigInt(big.NewInt(0)), + )) } -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) +func (sk *SecretKey) Sign(msg []byte) (*Signature, error) { + element := fr.Element(*sk) + g1, err := bls.HashToG1(msg, []byte(dstG1)) + if err != nil { + return nil, err + } + return (*Signature)(g1.ScalarMultiplication( + &g1, + element.BigInt(big.NewInt(0)), + )), nil } type Signature bls.G1Affine // SignatureFromBytes returns a Signature from a byte slice. func SignatureFromBytes(b []byte) (*Signature, error) { - var sig bls.G1Affine - _, err := sig.SetBytes(b) - if err != nil { + var signature bls.G1Affine + if _, err := signature.SetBytes(b); err != nil { return nil, err } - res := Signature(sig) - return &res, err + return (*Signature)(&signature), nil } // SignatureFromHexString returns a Signature from a hex string. @@ -87,12 +88,14 @@ func SignatureFromHexString(s string) (*Signature, error) { } 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)}) + g1, err := bls.HashToG1(msg, []byte(dstG1)) + if err != nil { + return false + } + valid, err := bls.PairingCheck( + []bls.G1Affine{bls.G1Affine(*sig), g1}, + []bls.G2Affine{g2, bls.G2Affine(*pk)}, + ) if err != nil { return false } diff --git a/certification/bls/bls_test.go b/certification/bls/bls_test.go index 6c3d041..707bb17 100644 --- a/certification/bls/bls_test.go +++ b/certification/bls/bls_test.go @@ -2,15 +2,21 @@ package bls import ( "encoding/hex" - "github.com/stretchr/testify/assert" "testing" ) func TestSecretKey(t *testing.T) { sk := NewSecretKeyByCSPRNG() - s := sk.Sign([]byte("hello")) - assert.True(t, s.Verify(sk.PublicKey(), []byte("hello"))) - assert.False(t, s.Verify(sk.PublicKey(), []byte("world"))) + s, err := sk.Sign([]byte("hello")) + if err != nil { + t.Fatal(err) + } + if s.Verify(sk.PublicKey(), []byte("hello")) != true { + t.Error() + } + if s.Verify(sk.PublicKey(), []byte("world")) != false { + t.Error() + } } func TestVerify(t *testing.T) { diff --git a/go.mod b/go.mod index 91608b5..80948d5 100644 --- a/go.mod +++ b/go.mod @@ -8,21 +8,16 @@ require ( 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/stretchr/testify v1.8.4 google.golang.org/protobuf v1.34.1 ) 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/stretchr/testify v1.8.4 // 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 ) diff --git a/go.sum b/go.sum index 0aa90d2..00f2ab7 100644 --- a/go.sum +++ b/go.sum @@ -8,7 +8,6 @@ github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/Yj 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= @@ -18,10 +17,6 @@ github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXE 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= @@ -29,8 +24,6 @@ github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqky 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= @@ -39,9 +32,6 @@ 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=