diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 30ba85664..b3b200f65 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,12 +16,12 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v5 - name: Install Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v6 with: - go-version: '1.18' + go-version: '1.25' - name: Run tests run: make test diff --git a/Makefile b/Makefile index 40d7a70cc..3a4667281 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ .PHONY: fetch-dependencies fetch-dependencies: - go get -v -t -d ./... + go get -v -t ./... .PHONY: test test: fetch-dependencies - go test -v ./... + go test -race -v ./... diff --git a/README.md b/README.md index 2fc163305..f207ccee1 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ main package is. Installing ---------- -First make sure you have [Go](https://golang.org) version 1.11 or newer installed. +First make sure you have [Go](https://golang.org) version 1.24 or newer installed. The basic crypto library requires only Go and a few third-party Go-language dependencies that can be installed automatically @@ -47,7 +47,7 @@ as follows: You can recursively test all the packages in the library as follows: - go test -v ./... + go test -race -v ./... A note on deriving shared secrets --------------------------------- diff --git a/encrypt/ibe/ibe.go b/encrypt/ibe/ibe.go index 4e688153b..e768cbf0b 100644 --- a/encrypt/ibe/ibe.go +++ b/encrypt/ibe/ibe.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "errors" "fmt" + "github.com/drand/kyber" "github.com/drand/kyber/group/mod" "github.com/drand/kyber/pairing" @@ -70,7 +71,7 @@ func EncryptCCAonG1(s pairing.Suite, master kyber.Point, ID, msg []byte) (*Ciphe return nil, err } // 4. Compute U = rP - U := s.G1().Point().Mul(r, s.G1().Point().Base()) + U := s.G1().Point().Mul(r, nil) // 5. Compute V = sigma XOR H2(rGid) rGid := Gid.Mul(r, Gid) // even in Gt, it's additive notation @@ -124,7 +125,7 @@ func DecryptCCAonG1(s pairing.Suite, private kyber.Point, c *Ciphertext) ([]byte if err != nil { return nil, err } - rP := s.G1().Point().Mul(r, s.G1().Point().Base()) + rP := s.G1().Point().Mul(r, nil) if !rP.Equal(c.U) { return nil, fmt.Errorf("invalid proof: rP check failed") } @@ -165,7 +166,7 @@ func EncryptCCAonG2(s pairing.Suite, master kyber.Point, ID, msg []byte) (*Ciphe return nil, err } // 4. Compute U = rP - U := s.G2().Point().Mul(r, s.G2().Point().Base()) + U := s.G2().Point().Mul(r, nil) // 5. Compute V = sigma XOR H2(rGid) rGid := Gid.Mul(r, Gid) // even in Gt, it's additive notation @@ -219,9 +220,9 @@ func DecryptCCAonG2(s pairing.Suite, private kyber.Point, c *Ciphertext) ([]byte if err != nil { return nil, err } - rP := s.G2().Point().Mul(r, s.G2().Point().Base()) + rP := s.G2().Point().Mul(r, nil) if !rP.Equal(c.U) { - return nil, fmt.Errorf("invalid proof: rP check failed") + return nil, fmt.Errorf("invalid proof: rP check failed on msg %s, r %x", msg, r) } return msg, nil } @@ -230,6 +231,10 @@ func DecryptCCAonG2(s pairing.Suite, private kyber.Point, c *Ciphertext) ([]byte func h3(s pairing.Suite, sigma, msg []byte) (kyber.Scalar, error) { h := s.Hash() + if h.Size() != s.G1().ScalarLen() { + return nil, fmt.Errorf("hash size mismatch with scalar length %d != %d", h.Size(), s.G1().ScalarLen()) + } + if _, err := h.Write(H3Tag()); err != nil { return nil, fmt.Errorf("err hashing h3 tag: %v", err) } @@ -339,13 +344,15 @@ type CiphertextCPA struct { // H1: {0,1}^n -> G1 // H2: GT -> {0,1}^n // ID: Qid = H1(ID) = xP \in G2 -// secret did = s*Qid \in G2 +// +// secret did = s*Qid \in G2 +// // Encrypt: -// - random r scalar -// - Gid = e(Ppub, r*Qid) == e(P, P)^(x*s*r) \in GT -// = GidT -// - U = rP \in G1, -// - V = M XOR H2(Gid)) = M XOR H2(GidT) \in {0,1}^n +// - random r scalar +// - Gid = e(Ppub, r*Qid) == e(P, P)^(x*s*r) \in GT +// = GidT +// - U = rP \in G1, +// - V = M XOR H2(Gid)) = M XOR H2(GidT) \in {0,1}^n func EncryptCPAonG1(s pairing.Suite, basePoint, public kyber.Point, ID, msg []byte) (*CiphertextCPA, error) { if len(msg)>>16 > 0 { // we're using blake2 as XOF which only outputs 2^16-1 length @@ -382,9 +389,9 @@ func EncryptCPAonG1(s pairing.Suite, basePoint, public kyber.Point, ID, msg []by // SigGroup = G2 (large secret identities) // KeyGroup = G1 (short master public keys) // Decrypt: -// - V XOR H2(e(U, did)) = V XOR H2(e(rP, s*Qid)) -// = V XOR H2(e(P, P)^(r*s*x)) -// = V XOR H2(GidT) = M +// - V XOR H2(e(U, did)) = V XOR H2(e(rP, s*Qid)) +// = V XOR H2(e(P, P)^(r*s*x)) +// = V XOR H2(GidT) = M func DecryptCPAonG1(s pairing.Suite, private kyber.Point, c *CiphertextCPA) ([]byte, error) { GidT := s.Pair(c.RP, private) hGidT, err := gtToHash(s, GidT, len(c.C)) diff --git a/go.mod b/go.mod index b4505e7a2..62341f782 100644 --- a/go.mod +++ b/go.mod @@ -1,28 +1,25 @@ module github.com/drand/kyber -go 1.18 +go 1.24.0 require ( - github.com/cloudflare/circl v1.3.7 - github.com/consensys/gnark-crypto v0.12.1 - github.com/drand/kyber-bls12381 v0.3.1 - github.com/jonboulle/clockwork v0.4.0 - github.com/stretchr/testify v1.9.0 + github.com/cloudflare/circl v1.6.1 + github.com/consensys/gnark-crypto v0.19.2 + github.com/drand/kyber-bls12381 v0.3.3 + github.com/jonboulle/clockwork v0.5.0 + github.com/stretchr/testify v1.11.1 go.dedis.ch/fixbuf v1.0.3 go.dedis.ch/protobuf v1.0.11 - golang.org/x/crypto v0.21.0 - golang.org/x/sys v0.18.0 + golang.org/x/crypto v0.43.0 ) require ( - github.com/bits-and-blooms/bitset v1.13.0 // indirect - github.com/consensys/bavard v0.1.13 // indirect + github.com/bits-and-blooms/bitset v1.24.3 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/kilic/bls12-381 v0.1.0 // 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/rogpeppe/go-internal v1.14.1 // indirect + golang.org/x/sys v0.38.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 d300c7cab..5c7bd5de7 100644 --- a/go.sum +++ b/go.sum @@ -1,37 +1,37 @@ -github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= -github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= -github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= -github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= -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.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M= -github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY= +github.com/bits-and-blooms/bitset v1.24.2 h1:M7/NzVbsytmtfHbumG+K2bremQPMJuqv1JD3vOaFxp0= +github.com/bits-and-blooms/bitset v1.24.2/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= +github.com/bits-and-blooms/bitset v1.24.3 h1:Bte86SlO3lwPQqww+7BE9ZuUCKIjfqnG5jtEyqA9y9Y= +github.com/bits-and-blooms/bitset v1.24.3/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= +github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0= +github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs= +github.com/consensys/gnark-crypto v0.19.0 h1:zXCqeY2txSaMl6G5wFpZzMWJU9HPNh8qxPnYJ1BL9vA= +github.com/consensys/gnark-crypto v0.19.0/go.mod h1:rT23F0XSZqE0mUA0+pRtnL56IbPxs6gp4CeRsBk4XS0= +github.com/consensys/gnark-crypto v0.19.2 h1:qrEAIXq3T4egxqiliFFoNrepkIWVEeIYwt3UL0fvS80= +github.com/consensys/gnark-crypto v0.19.2/go.mod h1:rT23F0XSZqE0mUA0+pRtnL56IbPxs6gp4CeRsBk4XS0= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/drand/kyber-bls12381 v0.3.1 h1:KWb8l/zYTP5yrvKTgvhOrk2eNPscbMiUOIeWBnmUxGo= -github.com/drand/kyber-bls12381 v0.3.1/go.mod h1:H4y9bLPu7KZA/1efDg+jtJ7emKx+ro3PU7/jWUVt140= -github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= -github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= -github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= +github.com/drand/kyber-bls12381 v0.3.3 h1:sLl0ILJtB4+POHAKq6tdnWyg+iXADE0LjVKN91RI8JI= +github.com/drand/kyber-bls12381 v0.3.3/go.mod h1:uVRWtcZDAApOWFMwoJVcTfC4csVxXmpkdoSCUZJ5QOY= +github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbdFz6I= +github.com/jonboulle/clockwork v0.5.0/go.mod h1:3mZlmanh0g2NDKO5TWZVJAfofYk64M7XN3SzBPjZF60= github.com/kilic/bls12-381 v0.1.0 h1:encrdjqKMEvabVQ7qYOKu1OvhqpK4s47wDYtNiPtlp4= github.com/kilic/bls12-381 v0.1.0/go.mod h1:vDTTHJONJ6G+P2R74EhnyotQDTliQDnFEwhdmfzw1ig= 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/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/leanovate/gopter v0.2.11 h1:vRjThO1EKPb/1NsDXuDrzldR28RLkBflWYcU9CvzWu4= +github.com/leanovate/gopter v0.2.11/go.mod h1:aK3tzZP/C+p1m3SPRE4SYZFGP7jjkuSI4f7Xvpt0S9c= 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/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= +github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= go.dedis.ch/fixbuf v1.0.3 h1:hGcV9Cd/znUxlusJ64eAlExS+5cJDIyTyEG+otu5wQs= go.dedis.ch/fixbuf v1.0.3/go.mod h1:yzJMt34Wa5xD37V5RTdmp38cz3QhMagdGoem9anUalw= go.dedis.ch/kyber/v3 v3.0.4/go.mod h1:OzvaEnPvKlyrWyp3kGXlFdp7ap1VC6RkZDTaPikqhsQ= @@ -42,15 +42,16 @@ go.dedis.ch/protobuf v1.0.7/go.mod h1:pv5ysfkDX/EawiPqcW3ikOxsL5t+BqnV6xHSmE79KI go.dedis.ch/protobuf v1.0.11 h1:FTYVIEzY/bfl37lu3pR4lIj+F9Vp1jE8oh91VmxKgLo= go.dedis.ch/protobuf v1.0.11/go.mod h1:97QR256dnkimeNdfmURz0wAMNVbd1VmLXhG1CrTYrJ4= golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04= +golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0= golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= +golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= +golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= 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.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= diff --git a/group/curve25519/ext.go b/group/curve25519/ext.go index e9f25603b..5e85cc550 100644 --- a/group/curve25519/ext.go +++ b/group/curve25519/ext.go @@ -68,7 +68,6 @@ func (P *extPoint) UnmarshalFrom(r io.Reader) (int, error) { // (X1/Z1,Y1/Z1) == (X2/Z2,Y2/Z2) // iff // (X1*Z2,Y1*Z2) == (X2*Z1,Y2*Z1) -// func (P *extPoint) Equal(CP2 kyber.Point) bool { P2 := CP2.(*extPoint) var t1, t2 mod.Int @@ -120,14 +119,6 @@ func (P *extPoint) normalize() { P.T.Mul(&P.X, &P.Y) } -// Check the validity of the T coordinate -func (P *extPoint) checkT() { - var t1, t2 mod.Int - if !t1.Mul(&P.X, &P.Y).Equal(t2.Mul(&P.Z, &P.T)) { - panic("oops") - } -} - func (P *extPoint) Embed(data []byte, rand cipher.Stream) kyber.Point { P.c.embed(P, data, rand) return P @@ -230,7 +221,6 @@ func (P *extPoint) double() { // Currently doesn't implement the optimization of // switching between projective and extended coordinates during // scalar multiplication. -// func (P *extPoint) Mul(s kyber.Scalar, G kyber.Point) kyber.Point { v := s.(*mod.Int).V if G == nil { @@ -272,7 +262,6 @@ func (P *extPoint) Mul(s kyber.Scalar, G kyber.Point) kyber.Point { // special case with curve parameter a=-1. // We leave the task of hyperoptimization to curve-specific implementations // such as the ed25519 package. -// type ExtendedCurve struct { curve // generic Edwards curve functionality null extPoint // Constant identity/null point (0,1) diff --git a/group/edwards25519/const.go b/group/edwards25519/const.go index 2d212d1c2..f9fba2759 100644 --- a/group/edwards25519/const.go +++ b/group/edwards25519/const.go @@ -8,9 +8,6 @@ import ( "math/big" ) -// prime modulus of underlying field = 2^255 - 19 -var prime, _ = new(big.Int).SetString("57896044618658097711785492504343953926634992332820282019728792003956564819949", 10) - // prime order of base point = 2^252 + 27742317777372353535851937790883648493 var primeOrder, _ = new(big.Int).SetString("7237005577332262213973186563042994240857116359379907606001950938285454250989", 10) @@ -44,10 +41,6 @@ var sqrtM1 = fieldElement{ -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482, } -var paramA = fieldElement{ - 486662, 0, 0, 0, 0, 0, 0, 0, 0, 0, -} - var baseext = extendedGroupElement{ fieldElement{25485296, 5318399, 8791791, -8299916, -14349720, 6939349, -3324311, -7717049, 7287234, -6577708}, fieldElement{-758052, -1832720, 13046421, -4857925, 6576754, 14371947, -13139572, 6845540, -2198883, -4003719}, @@ -55,49 +48,6 @@ var baseext = extendedGroupElement{ fieldElement{6966464, -2456167, 7033433, 6781840, 28785542, 12262365, -2659449, 13959020, -21013759, -5262166}, } -var bi = [8]preComputedGroupElement{ - { - fieldElement{25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605}, - fieldElement{-12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378}, - fieldElement{-8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546}, - }, - { - fieldElement{15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024}, - fieldElement{16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574}, - fieldElement{30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357}, - }, - { - fieldElement{10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380}, - fieldElement{4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306}, - fieldElement{19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942}, - }, - { - fieldElement{5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766}, - fieldElement{-30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701}, - fieldElement{28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300}, - }, - { - fieldElement{-22518993, -6692182, 14201702, -8745502, -23510406, 8844726, 18474211, -1361450, -13062696, 13821877}, - fieldElement{-6455177, -7839871, 3374702, -4740862, -27098617, -10571707, 31655028, -7212327, 18853322, -14220951}, - fieldElement{4566830, -12963868, -28974889, -12240689, -7602672, -2830569, -8514358, -10431137, 2207753, -3209784}, - }, - { - fieldElement{-25154831, -4185821, 29681144, 7868801, -6854661, -9423865, -12437364, -663000, -31111463, -16132436}, - fieldElement{25576264, -2703214, 7349804, -11814844, 16472782, 9300885, 3844789, 15725684, 171356, 6466918}, - fieldElement{23103977, 13316479, 9739013, -16149481, 817875, -15038942, 8965339, -14088058, -30714912, 16193877}, - }, - { - fieldElement{-33521811, 3180713, -2394130, 14003687, -16903474, -16270840, 17238398, 4729455, -18074513, 9256800}, - fieldElement{-25182317, -4174131, 32336398, 5036987, -21236817, 11360617, 22616405, 9761698, -19827198, 630305}, - fieldElement{-13720693, 2639453, -24237460, -7406481, 9494427, -5774029, -6554551, -15960994, -2449256, -14291300}, - }, - { - fieldElement{-3151181, -5046075, 9282714, 6866145, -31907062, -863023, -18940575, 15033784, 25105118, -7894876}, - fieldElement{-24326370, 15950226, -31801215, -14592823, -11662737, -5090925, 1573892, -2625887, 2198790, -15804619}, - fieldElement{-3099351, 10324967, -2241613, 7453183, -5446979, -2735503, -13812022, -16236442, -32461234, -12290683}, - }, -} - var base = [32][8]preComputedGroupElement{ { { diff --git a/group/edwards25519/scalar.go b/group/edwards25519/scalar.go index 12f729625..f205ff1ca 100644 --- a/group/edwards25519/scalar.go +++ b/group/edwards25519/scalar.go @@ -195,13 +195,15 @@ func newScalarInt(i *big.Int) *scalar { } // Input: -// a[0]+256*a[1]+...+256^31*a[31] = a -// b[0]+256*b[1]+...+256^31*b[31] = b -// c[0]+256*c[1]+...+256^31*c[31] = c +// +// a[0]+256*a[1]+...+256^31*a[31] = a +// b[0]+256*b[1]+...+256^31*b[31] = b +// c[0]+256*c[1]+...+256^31*c[31] = c // // Output: -// s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l -// where l = 2^252 + 27742317777372353535851937790883648493. +// +// s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l +// where l = 2^252 + 27742317777372353535851937790883648493. func scMulAdd(s, a, b, c *[32]byte) { a0 := 2097151 & load3(a[:]) a1 := 2097151 & (load4(a[2:]) >> 5) @@ -630,13 +632,14 @@ func scMulAdd(s, a, b, c *[32]byte) { // Hacky scAdd cobbled together rather sub-optimally from scMulAdd. // // Input: -// a[0]+256*a[1]+...+256^31*a[31] = a -// c[0]+256*c[1]+...+256^31*c[31] = c +// +// a[0]+256*a[1]+...+256^31*a[31] = a +// c[0]+256*c[1]+...+256^31*c[31] = c // // Output: -// s[0]+256*s[1]+...+256^31*s[31] = (a+c) mod l -// where l = 2^252 + 27742317777372353535851937790883648493. // +// s[0]+256*s[1]+...+256^31*s[31] = (a+c) mod l +// where l = 2^252 + 27742317777372353535851937790883648493. func scAdd(s, a, c *[32]byte) { a0 := 2097151 & load3(a[:]) a1 := 2097151 & (load4(a[2:]) >> 5) @@ -1053,13 +1056,14 @@ func scAdd(s, a, c *[32]byte) { // Hacky scSub cobbled together rather sub-optimally from scMulAdd. // // Input: -// a[0]+256*a[1]+...+256^31*a[31] = a -// c[0]+256*c[1]+...+256^31*c[31] = c +// +// a[0]+256*a[1]+...+256^31*a[31] = a +// c[0]+256*c[1]+...+256^31*c[31] = c // // Output: -// s[0]+256*s[1]+...+256^31*s[31] = (a-c) mod l -// where l = 2^252 + 27742317777372353535851937790883648493. // +// s[0]+256*s[1]+...+256^31*s[31] = (a-c) mod l +// where l = 2^252 + 27742317777372353535851937790883648493. func scSub(s, a, c *[32]byte) { a0 := 2097151 & load3(a[:]) a1 := 2097151 & (load4(a[2:]) >> 5) @@ -1476,12 +1480,14 @@ func scSub(s, a, c *[32]byte) { // Hacky scMul cobbled together rather sub-optimally from scMulAdd. // // Input: -// a[0]+256*a[1]+...+256^31*a[31] = a -// b[0]+256*b[1]+...+256^31*b[31] = b +// +// a[0]+256*a[1]+...+256^31*a[31] = a +// b[0]+256*b[1]+...+256^31*b[31] = b // // Output: -// s[0]+256*s[1]+...+256^31*s[31] = (ab) mod l -// where l = 2^252 + 27742317777372353535851937790883648493. +// +// s[0]+256*s[1]+...+256^31*s[31] = (ab) mod l +// where l = 2^252 + 27742317777372353535851937790883648493. func scMul(s, a, b *[32]byte) { a0 := 2097151 & load3(a[:]) a1 := 2097151 & (load4(a[2:]) >> 5) @@ -1906,327 +1912,3 @@ func scMul(s, a, b *[32]byte) { s[30] = byte(s11 >> 9) s[31] = byte(s11 >> 17) } - -// Input: -// s[0]+256*s[1]+...+256^63*s[63] = s -// -// Output: -// s[0]+256*s[1]+...+256^31*s[31] = s mod l -// where l = 2^252 + 27742317777372353535851937790883648493. -func scReduce(out *[32]byte, s *[64]byte) { - s0 := 2097151 & load3(s[:]) - s1 := 2097151 & (load4(s[2:]) >> 5) - s2 := 2097151 & (load3(s[5:]) >> 2) - s3 := 2097151 & (load4(s[7:]) >> 7) - s4 := 2097151 & (load4(s[10:]) >> 4) - s5 := 2097151 & (load3(s[13:]) >> 1) - s6 := 2097151 & (load4(s[15:]) >> 6) - s7 := 2097151 & (load3(s[18:]) >> 3) - s8 := 2097151 & load3(s[21:]) - s9 := 2097151 & (load4(s[23:]) >> 5) - s10 := 2097151 & (load3(s[26:]) >> 2) - s11 := 2097151 & (load4(s[28:]) >> 7) - s12 := 2097151 & (load4(s[31:]) >> 4) - s13 := 2097151 & (load3(s[34:]) >> 1) - s14 := 2097151 & (load4(s[36:]) >> 6) - s15 := 2097151 & (load3(s[39:]) >> 3) - s16 := 2097151 & load3(s[42:]) - s17 := 2097151 & (load4(s[44:]) >> 5) - s18 := 2097151 & (load3(s[47:]) >> 2) - s19 := 2097151 & (load4(s[49:]) >> 7) - s20 := 2097151 & (load4(s[52:]) >> 4) - s21 := 2097151 & (load3(s[55:]) >> 1) - s22 := 2097151 & (load4(s[57:]) >> 6) - s23 := (load4(s[60:]) >> 3) - - s11 += s23 * 666643 - s12 += s23 * 470296 - s13 += s23 * 654183 - s14 -= s23 * 997805 - s15 += s23 * 136657 - s16 -= s23 * 683901 - s23 = 0 - - s10 += s22 * 666643 - s11 += s22 * 470296 - s12 += s22 * 654183 - s13 -= s22 * 997805 - s14 += s22 * 136657 - s15 -= s22 * 683901 - s22 = 0 - - s9 += s21 * 666643 - s10 += s21 * 470296 - s11 += s21 * 654183 - s12 -= s21 * 997805 - s13 += s21 * 136657 - s14 -= s21 * 683901 - s21 = 0 - - s8 += s20 * 666643 - s9 += s20 * 470296 - s10 += s20 * 654183 - s11 -= s20 * 997805 - s12 += s20 * 136657 - s13 -= s20 * 683901 - s20 = 0 - - s7 += s19 * 666643 - s8 += s19 * 470296 - s9 += s19 * 654183 - s10 -= s19 * 997805 - s11 += s19 * 136657 - s12 -= s19 * 683901 - s19 = 0 - - s6 += s18 * 666643 - s7 += s18 * 470296 - s8 += s18 * 654183 - s9 -= s18 * 997805 - s10 += s18 * 136657 - s11 -= s18 * 683901 - s18 = 0 - - var carry [17]int64 - - carry[6] = (s6 + (1 << 20)) >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[8] = (s8 + (1 << 20)) >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[10] = (s10 + (1 << 20)) >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - carry[12] = (s12 + (1 << 20)) >> 21 - s13 += carry[12] - s12 -= carry[12] << 21 - carry[14] = (s14 + (1 << 20)) >> 21 - s15 += carry[14] - s14 -= carry[14] << 21 - carry[16] = (s16 + (1 << 20)) >> 21 - s17 += carry[16] - s16 -= carry[16] << 21 - - carry[7] = (s7 + (1 << 20)) >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[9] = (s9 + (1 << 20)) >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[11] = (s11 + (1 << 20)) >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - carry[13] = (s13 + (1 << 20)) >> 21 - s14 += carry[13] - s13 -= carry[13] << 21 - carry[15] = (s15 + (1 << 20)) >> 21 - s16 += carry[15] - s15 -= carry[15] << 21 - - s5 += s17 * 666643 - s6 += s17 * 470296 - s7 += s17 * 654183 - s8 -= s17 * 997805 - s9 += s17 * 136657 - s10 -= s17 * 683901 - s17 = 0 - - s4 += s16 * 666643 - s5 += s16 * 470296 - s6 += s16 * 654183 - s7 -= s16 * 997805 - s8 += s16 * 136657 - s9 -= s16 * 683901 - s16 = 0 - - s3 += s15 * 666643 - s4 += s15 * 470296 - s5 += s15 * 654183 - s6 -= s15 * 997805 - s7 += s15 * 136657 - s8 -= s15 * 683901 - s15 = 0 - - s2 += s14 * 666643 - s3 += s14 * 470296 - s4 += s14 * 654183 - s5 -= s14 * 997805 - s6 += s14 * 136657 - s7 -= s14 * 683901 - s14 = 0 - - s1 += s13 * 666643 - s2 += s13 * 470296 - s3 += s13 * 654183 - s4 -= s13 * 997805 - s5 += s13 * 136657 - s6 -= s13 * 683901 - s13 = 0 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = (s0 + (1 << 20)) >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[2] = (s2 + (1 << 20)) >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[4] = (s4 + (1 << 20)) >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[6] = (s6 + (1 << 20)) >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[8] = (s8 + (1 << 20)) >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[10] = (s10 + (1 << 20)) >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - - carry[1] = (s1 + (1 << 20)) >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[3] = (s3 + (1 << 20)) >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[5] = (s5 + (1 << 20)) >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[7] = (s7 + (1 << 20)) >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[9] = (s9 + (1 << 20)) >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[11] = (s11 + (1 << 20)) >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = s0 >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[1] = s1 >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[2] = s2 >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[3] = s3 >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[4] = s4 >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[5] = s5 >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[6] = s6 >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[7] = s7 >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[8] = s8 >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[9] = s9 >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[10] = s10 >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - carry[11] = s11 >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = s0 >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[1] = s1 >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[2] = s2 >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[3] = s3 >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[4] = s4 >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[5] = s5 >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[6] = s6 >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[7] = s7 >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[8] = s8 >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[9] = s9 >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[10] = s10 >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - - out[0] = byte(s0 >> 0) - out[1] = byte(s0 >> 8) - out[2] = byte((s0 >> 16) | (s1 << 5)) - out[3] = byte(s1 >> 3) - out[4] = byte(s1 >> 11) - out[5] = byte((s1 >> 19) | (s2 << 2)) - out[6] = byte(s2 >> 6) - out[7] = byte((s2 >> 14) | (s3 << 7)) - out[8] = byte(s3 >> 1) - out[9] = byte(s3 >> 9) - out[10] = byte((s3 >> 17) | (s4 << 4)) - out[11] = byte(s4 >> 4) - out[12] = byte(s4 >> 12) - out[13] = byte((s4 >> 20) | (s5 << 1)) - out[14] = byte(s5 >> 7) - out[15] = byte((s5 >> 15) | (s6 << 6)) - out[16] = byte(s6 >> 2) - out[17] = byte(s6 >> 10) - out[18] = byte((s6 >> 18) | (s7 << 3)) - out[19] = byte(s7 >> 5) - out[20] = byte(s7 >> 13) - out[21] = byte(s8 >> 0) - out[22] = byte(s8 >> 8) - out[23] = byte((s8 >> 16) | (s9 << 5)) - out[24] = byte(s9 >> 3) - out[25] = byte(s9 >> 11) - out[26] = byte((s9 >> 19) | (s10 << 2)) - out[27] = byte(s10 >> 6) - out[28] = byte((s10 >> 14) | (s11 << 7)) - out[29] = byte(s11 >> 1) - out[30] = byte(s11 >> 9) - out[31] = byte(s11 >> 17) -} diff --git a/group/mod/int.go b/group/mod/int.go index 598fe51b7..e8276d3b2 100644 --- a/group/mod/int.go +++ b/group/mod/int.go @@ -14,8 +14,6 @@ import ( "github.com/drand/kyber/util/random" ) -var one = big.NewInt(1) -var two = big.NewInt(2) var marshalScalarID = [8]byte{'m', 'o', 'd', '.', 'i', 'n', 't', ' '} // ByteOrder denotes the endianness of the operation. diff --git a/group/nist/curve.go b/group/nist/curve.go index 062337bb5..3c34a160e 100644 --- a/group/nist/curve.go +++ b/group/nist/curve.go @@ -175,6 +175,7 @@ func (p *curvePoint) MarshalSize() int { } func (p *curvePoint) MarshalBinary() ([]byte, error) { + //nolint:staticcheck // Using elliptic.Marshal for compatibility with existing kyber interface return elliptic.Marshal(p.c, p.x, p.y), nil } @@ -187,6 +188,7 @@ func (p *curvePoint) UnmarshalBinary(buf []byte) error { c |= b } if c != 0 { + //nolint:staticcheck // Using elliptic.Unmarshal for compatibility with existing kyber interface p.x, p.y = elliptic.Unmarshal(p.c, buf) if p.x == nil || !p.Valid() { return errors.New("invalid elliptic curve point") diff --git a/group/nist/residue.go b/group/nist/residue.go index 04a8db882..30c54307c 100644 --- a/group/nist/residue.go +++ b/group/nist/residue.go @@ -2,7 +2,7 @@ package nist import ( "crypto/cipher" - "crypto/dsa" + "crypto/dsa" //nolint:staticcheck // Only using Parameters struct for DSA parameter compatibility "errors" "fmt" "io" diff --git a/pairing/bn254/bls_test.go b/pairing/bn254/bls_test.go index a47398fcf..2c9ee8897 100644 --- a/pairing/bn254/bls_test.go +++ b/pairing/bn254/bls_test.go @@ -3,7 +3,7 @@ package bn254 import ( "testing" - "github.com/drand/kyber/sign/bls" + "github.com/drand/kyber/sign/bls" //nolint:staticcheck // Testing deprecated but still functional BLS package "github.com/drand/kyber/sign/test" ) diff --git a/pairing/bn254/gfp_decl.go b/pairing/bn254/gfp_decl.go index 8c5429c52..ca56b6c4f 100644 --- a/pairing/bn254/gfp_decl.go +++ b/pairing/bn254/gfp_decl.go @@ -12,7 +12,7 @@ import ( var hasBMI2 = cpu.X86.HasBMI2 -// go:noescape +//go:noescape func gfpNeg(c, a *gfP) //go:noescape diff --git a/pairing/bn254/lattice.go b/pairing/bn254/lattice.go index f457cd30f..6c91a4e82 100644 --- a/pairing/bn254/lattice.go +++ b/pairing/bn254/lattice.go @@ -18,22 +18,6 @@ var curveLattice = &lattice{ det: bigFromBase10("43776485743678550444492811490514550177096728800832068687396408373151616991234"), } -var targetLattice = &lattice{ - vectors: [][]*big.Int{ - {bigFromBase10("9931322734385697761"), bigFromBase10("9931322734385697761"), bigFromBase10("9931322734385697763"), bigFromBase10("9931322734385697764")}, - {bigFromBase10("4965661367192848881"), bigFromBase10("4965661367192848881"), bigFromBase10("4965661367192848882"), bigFromBase10("-9931322734385697762")}, - {bigFromBase10("-9931322734385697762"), bigFromBase10("-4965661367192848881"), bigFromBase10("4965661367192848881"), bigFromBase10("-4965661367192848882")}, - {bigFromBase10("9931322734385697763"), bigFromBase10("-4965661367192848881"), bigFromBase10("-4965661367192848881"), bigFromBase10("-4965661367192848881")}, - }, - inverse: []*big.Int{ - bigFromBase10("734653495049373973658254490726798021314063399421879442165"), - bigFromBase10("147946756881789319000765030803803410728"), - bigFromBase10("-147946756881789319005730692170996259609"), - bigFromBase10("1469306990098747947464455738335385361643788813749140841702"), - }, - det: new(big.Int).Set(Order), -} - type lattice struct { vectors [][]*big.Int inverse []*big.Int diff --git a/pairing/bn254/point.go b/pairing/bn254/point.go index b50d50906..9c60e218b 100644 --- a/pairing/bn254/point.go +++ b/pairing/bn254/point.go @@ -193,7 +193,7 @@ func (p *pointG1) ElementSize() int { } func (p *pointG1) String() string { - return "bn254.G1" + p.g.String() + return "bn254.G1(DST: " + string(p.dst) + ")" + p.g.String() } func (p *pointG1) Hash(m []byte) kyber.Point { @@ -342,16 +342,21 @@ func (p *pointG2) Equal(q kyber.Point) bool { return subtle.ConstantTimeCompare(x, y) == 1 } +// Null returns the point p set to Infinity on G2. Be careful: it mutates p. +// Consider using Clone if you're using this in a comparison. func (p *pointG2) Null() kyber.Point { p.g.SetInfinity() return p } +// Base returns the point p set to the generator on G2. Be careful: it mutates p. +// Consider using Clone first if you're using this in a comparison. func (p *pointG2) Base() kyber.Point { p.g.Set(twistGen) return p } +// Pick returns the point p set to a random point on G2. Be careful: it mutates p. func (p *pointG2) Pick(rand cipher.Stream) kyber.Point { s := mod.NewInt64(0, Order).Pick(rand) p.Base() @@ -524,16 +529,16 @@ func (p *pointGT) Equal(q kyber.Point) bool { return subtle.ConstantTimeCompare(x, y) == 1 } +var nullGT = newPointGT().Pair(newPointG1(nil).Null(), newPointG2(nil).Null()) + func (p *pointGT) Null() kyber.Point { - // TODO: This can be a precomputed constant - p.Pair(newPointG1([]byte{}).Null(), newPointG2([]byte{}).Null()) - return p + return nullGT.Clone() } +var baseGT = newPointGT().Pair(newPointG1(nil).Base(), newPointG2(nil).Base()) + func (p *pointGT) Base() kyber.Point { - // TODO: This can be a precomputed constant - p.Pair(newPointG1([]byte{}).Base(), newPointG2([]byte{}).Base()) - return p + return baseGT.Clone() } func (p *pointGT) Pick(rand cipher.Stream) kyber.Point { diff --git a/pairing/bn254/point_test.go b/pairing/bn254/point_test.go index 2275b996d..d8d975128 100644 --- a/pairing/bn254/point_test.go +++ b/pairing/bn254/point_test.go @@ -6,6 +6,7 @@ import ( "errors" "testing" + "github.com/drand/kyber" "golang.org/x/crypto/sha3" ) @@ -206,3 +207,66 @@ func min(a, b int) int { } return b } + +func Test_pointG1_Equal(t *testing.T) { + tests := []struct { + name string + p1 kyber.Point + p2 kyber.Point + want bool + }{ + { + "g1 inf", + newPointG1(nil).Null(), + newPointG1(nil).Null(), + true, + }, { + "g1 base", + newPointG1(nil).Base(), + newPointG1(nil).Base(), + true, + }, { + "g1 base!=inf", + newPointG1(nil).Base(), + newPointG1(nil).Null(), + false, + }, { + "g2 base!=inf", + newPointG2(nil).Base(), + newPointG2(nil).Null(), + false, + }, { + "g2 inf", + newPointG2(nil).Null(), + newPointG2(nil).Null(), + true, + }, { + "g2 base", + newPointG2(nil).Base(), + newPointG2(nil).Base(), + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := tt.p1 + if got := p.Equal(tt.p2); got != tt.want { + t.Errorf("Equal() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_pointGT_Null(t *testing.T) { + inf := newPointGT().Pair(newPointG1([]byte{}).Null(), newPointG2([]byte{}).Null()) + if !inf.Equal(newPointGT().Null()) { + t.Fatal("Null isn't the same null") + } +} + +func Test_pointGT_Base(t *testing.T) { + base := newPointGT().Pair(newPointG1([]byte{}).Base(), newPointG2([]byte{}).Base()) + if !base.Equal(newPointGT().Base()) { + t.Fatal("Null isn't the same null") + } +} diff --git a/pairing/bn254/twist.go b/pairing/bn254/twist.go index d1da036c7..15102bf4d 100644 --- a/pairing/bn254/twist.go +++ b/pairing/bn254/twist.go @@ -186,6 +186,7 @@ func (c *twistPoint) MakeAffine() { g.x.SetZero() g.y.SetOne() g.t.SetZero() + c.Set(g) return } diff --git a/pairing/bn256/bls_test.go b/pairing/bn256/bls_test.go index 2d1c8c6bc..87bb503d7 100644 --- a/pairing/bn256/bls_test.go +++ b/pairing/bn256/bls_test.go @@ -3,7 +3,7 @@ package bn256 import ( "testing" - "github.com/drand/kyber/sign/bls" + "github.com/drand/kyber/sign/bls" //nolint:staticcheck // Testing deprecated but still functional BLS package "github.com/drand/kyber/sign/test" "github.com/drand/kyber/util/random" "github.com/stretchr/testify/require" diff --git a/pairing/bn256/gfp_decl.go b/pairing/bn256/gfp_decl.go index be1b80906..bdb6a8915 100644 --- a/pairing/bn256/gfp_decl.go +++ b/pairing/bn256/gfp_decl.go @@ -1,3 +1,4 @@ +//go:build (amd64 && !generic) || (arm64 && !generic) // +build amd64,!generic arm64,!generic package bn256 @@ -11,7 +12,7 @@ import ( var hasBMI2 = cpu.X86.HasBMI2 -// go:noescape +//go:noescape func gfpNeg(c, a *gfP) //go:noescape diff --git a/pairing/bn256/suite_test.go b/pairing/bn256/suite_test.go index 032a9dd7a..6d8b91013 100644 --- a/pairing/bn256/suite_test.go +++ b/pairing/bn256/suite_test.go @@ -5,12 +5,12 @@ import ( "fmt" "testing" - "github.com/stretchr/testify/require" "github.com/drand/kyber" "github.com/drand/kyber/group/mod" "github.com/drand/kyber/util/random" + "github.com/stretchr/testify/require" "go.dedis.ch/protobuf" - "golang.org/x/crypto/bn256" + "golang.org/x/crypto/bn256" //nolint:staticcheck // Testing interoperability with x/crypto/bn256 ) func TestScalarMarshal(t *testing.T) { diff --git a/pairing/circl_bls12381/suite_test.go b/pairing/circl_bls12381/suite_test.go index 99597be09..2ec66321b 100644 --- a/pairing/circl_bls12381/suite_test.go +++ b/pairing/circl_bls12381/suite_test.go @@ -8,7 +8,7 @@ import ( "github.com/drand/kyber" this "github.com/drand/kyber/pairing/circl_bls12381" - "github.com/drand/kyber/sign/bls" + "github.com/drand/kyber/sign/bls" //nolint:staticcheck // Testing deprecated but still functional BLS package "github.com/drand/kyber/sign/tbls" "github.com/drand/kyber/sign/test" "github.com/drand/kyber/util/random" @@ -223,10 +223,10 @@ func testGroup(t *testing.T, g kyber.Group, rand cipher.Stream) []kyber.Point { buf.Reset() s := g.Scalar().Pick(rand) if _, err := s.MarshalTo(buf); err != nil { - t.Fatalf("encoding of secret fails: " + err.Error()) + t.Fatalf("encoding of secret fails: %v", err) } if _, err := stmp.UnmarshalFrom(buf); err != nil { - t.Fatalf("decoding of secret fails: " + err.Error()) + t.Fatalf("decoding of secret fails: %v", err) } if !stmp.Equal(s) { t.Fatalf("decoding produces different secret than encoded") @@ -235,10 +235,10 @@ func testGroup(t *testing.T, g kyber.Group, rand cipher.Stream) []kyber.Point { buf.Reset() p := pick(rand) if _, err := p.MarshalTo(buf); err != nil { - t.Fatalf("encoding of point fails: " + err.Error()) + t.Fatalf("encoding of point fails: %v", err) } if _, err := ptmp.UnmarshalFrom(buf); err != nil { - t.Fatalf("decoding of point fails: " + err.Error()) + t.Fatalf("decoding of point fails: %v", err) } if !ptmp.Equal(p) { diff --git a/share/dkg/dkg.go b/share/dkg/dkg.go index 19135c104..a48f3ed47 100644 --- a/share/dkg/dkg.go +++ b/share/dkg/dkg.go @@ -183,8 +183,6 @@ type DistKeyGenerator struct { newPresent bool // indicates whether the node is present in the old list oldPresent bool - // already processed our own deal - processed bool // public polynomial of the old group olddpub *share.PubPoly } diff --git a/share/dkg/dkg_test.go b/share/dkg/dkg_test.go index 840f8ab46..15acea833 100644 --- a/share/dkg/dkg_test.go +++ b/share/dkg/dkg_test.go @@ -26,7 +26,7 @@ type TestNode struct { proto *Protocol phaser *TimePhaser board *TestBoard - clock clock.FakeClock + clock *clock.FakeClock } func NewTestNode(s Suite, index int) *TestNode { diff --git a/share/dkg/protocol.go b/share/dkg/protocol.go index 364d58b00..9b0d73102 100644 --- a/share/dkg/protocol.go +++ b/share/dkg/protocol.go @@ -3,7 +3,6 @@ package dkg import ( "bytes" "fmt" - "strings" "time" ) @@ -79,14 +78,7 @@ type Protocol struct { skipVerif bool } -// XXX TO DELETE -func printNodes(list []Node) string { - var arr []string - for _, node := range list { - arr = append(arr, fmt.Sprintf("[%d : %s]", node.Index, node.Public)) - } - return strings.Join(arr, "\n") -} +// Protocol is the interface that represents the protocol. It is implemented by func NewProtocol(c *Config, b Board, phaser Phaser, skipVerification bool) (*Protocol, error) { dkg, err := NewDistKeyHandler(c) diff --git a/share/dkg/status.go b/share/dkg/status.go index 5f3851ea4..8a727a375 100644 --- a/share/dkg/status.go +++ b/share/dkg/status.go @@ -108,16 +108,6 @@ func (s *StatusMatrix) String() string { return str } -func findMaxIndex(list []Node) int { - m := 0 - for _, n := range list { - if n.Index > uint32(m) { - m = int(n.Index) - } - } - return m -} - func (b BitSet) LengthComplaints() int { var count = 0 for _, status := range b { diff --git a/share/vss/pedersen/vss.go b/share/vss/pedersen/vss.go index c441aacca..83142a2d6 100644 --- a/share/vss/pedersen/vss.go +++ b/share/vss/pedersen/vss.go @@ -6,7 +6,6 @@ package vss import ( "bytes" - "crypto/cipher" "encoding/binary" "errors" "fmt" @@ -29,8 +28,7 @@ type Suite interface { // Dealer encapsulates for creating and distributing the shares and for // replying to any Responses. type Dealer struct { - suite Suite - reader cipher.Stream + suite Suite // long is the longterm key of the Dealer long kyber.Scalar pub kyber.Point @@ -311,6 +309,7 @@ type Verifier struct { // - its longterm secret key // - the longterm dealer public key // - the list of public key of verifiers. The list MUST include the public key of this Verifier also. +// // The security parameter t of the secret sharing scheme is automatically set to // a default safe value. If a different t value is required, it is possible to set // it with `verifier.SetT()`. @@ -726,15 +725,6 @@ func validT(t int, verifiers []kyber.Point) bool { return t >= 2 && t <= len(verifiers) && int(uint32(t)) == t } -func deriveH(suite Suite, verifiers []kyber.Point) kyber.Point { - var b bytes.Buffer - for _, v := range verifiers { - _, _ = v.MarshalTo(&b) - } - base := suite.Point().Pick(suite.XOF(b.Bytes())) - return base -} - func findPub(verifiers []kyber.Point, idx uint32) (kyber.Point, bool) { iidx := int(idx) if iidx >= len(verifiers) { diff --git a/share/vss/pedersen/vss_test.go b/share/vss/pedersen/vss_test.go index f3cc5233c..b3f17ffe0 100644 --- a/share/vss/pedersen/vss_test.go +++ b/share/vss/pedersen/vss_test.go @@ -1,8 +1,9 @@ package vss import ( + "crypto/rand" "fmt" - "math/rand" + mathrand "math/rand" "testing" "github.com/drand/kyber" @@ -126,7 +127,7 @@ func TestVSSDealerNew(t *testing.T) { } func TestVSSVerifierNew(t *testing.T) { - randIdx := rand.Int() % len(verifiersPub) + randIdx := mathrand.Int() % len(verifiersPub) v, err := NewVerifier(suite, verifiersSec[randIdx], dealerPub, verifiersPub) assert.NoError(t, err) assert.Equal(t, randIdx, v.index) diff --git a/shuffle/pair.go b/shuffle/pair.go index 8422d9699..1d39ecea7 100644 --- a/shuffle/pair.go +++ b/shuffle/pair.go @@ -67,11 +67,6 @@ type ega5 struct { Ztau kyber.Scalar } -// P and V, step 5: simple k-shuffle proof -type ega6 struct { - SimpleShuffle -} - // PairShuffle creates a proof of the correctness of a shuffle // of a series of ElGamal pairs. // diff --git a/sign/bdn/bdn.go b/sign/bdn/bdn.go index 4b1ab1b9c..d87e4c6de 100644 --- a/sign/bdn/bdn.go +++ b/sign/bdn/bdn.go @@ -18,7 +18,7 @@ import ( "github.com/drand/kyber/group/mod" "github.com/drand/kyber/pairing" "github.com/drand/kyber/sign" - "github.com/drand/kyber/sign/bls" + "github.com/drand/kyber/sign/bls" //nolint:staticcheck // BDN extends BLS with rogue key attack protection "golang.org/x/crypto/blake2s" ) diff --git a/sign/eddsa/eddsa_test.go b/sign/eddsa/eddsa_test.go index cb332cf18..88ca2757d 100644 --- a/sign/eddsa/eddsa_test.go +++ b/sign/eddsa/eddsa_test.go @@ -5,8 +5,8 @@ import ( "bytes" "compress/gzip" "crypto/cipher" + "crypto/rand" "encoding/hex" - "math/rand" "os" "strings" "testing" diff --git a/sign/tbls/tbls.go b/sign/tbls/tbls.go index 2f1030ba6..21a80de08 100644 --- a/sign/tbls/tbls.go +++ b/sign/tbls/tbls.go @@ -19,7 +19,7 @@ import ( "github.com/drand/kyber/pairing" "github.com/drand/kyber/share" "github.com/drand/kyber/sign" - "github.com/drand/kyber/sign/bls" + "github.com/drand/kyber/sign/bls" //nolint:staticcheck // TBLS extends BLS for threshold signatures ) // SigShare encodes a threshold BLS signature share Si = i || v where the 2-byte diff --git a/sign/test/bls_test.go b/sign/test/bls_test.go index a6dff9554..6692e9b7a 100644 --- a/sign/test/bls_test.go +++ b/sign/test/bls_test.go @@ -4,7 +4,7 @@ import ( "testing" bls "github.com/drand/kyber-bls12381" - sign "github.com/drand/kyber/sign/bls" + sign "github.com/drand/kyber/sign/bls" //nolint:staticcheck // Testing deprecated but still functional BLS package ) func TestBLS12381(t *testing.T) { diff --git a/util/test/test.go b/util/test/test.go index cf4811603..c817d46e8 100644 --- a/util/test/test.go +++ b/util/test/test.go @@ -138,7 +138,6 @@ func testScalarClone(t *testing.T, g kyber.Group, rand cipher.Stream) { // Returns a log of the pseudorandom Points produced in the test, // for comparison across alternative implementations // that are supposed to be equivalent. -// func testGroup(t *testing.T, g kyber.Group, rand cipher.Stream) []kyber.Point { t.Logf("\nTesting group '%s': %d-byte Point, %d-byte Scalar\n", g.String(), g.PointLen(), g.ScalarLen()) @@ -325,10 +324,10 @@ func testGroup(t *testing.T, g kyber.Group, rand cipher.Stream) []kyber.Point { buf.Reset() s := g.Scalar().Pick(rand) if _, err := s.MarshalTo(buf); err != nil { - t.Errorf("encoding of secret fails: " + err.Error()) + t.Errorf("encoding of secret fails: %v", err) } if _, err := stmp.UnmarshalFrom(buf); err != nil { - t.Errorf("decoding of secret fails: " + err.Error()) + t.Errorf("decoding of secret fails: %v", err) } if !stmp.Equal(s) { t.Errorf("decoding produces different secret than encoded") @@ -337,10 +336,10 @@ func testGroup(t *testing.T, g kyber.Group, rand cipher.Stream) []kyber.Point { buf.Reset() p := g.Point().Pick(rand) if _, err := p.MarshalTo(buf); err != nil { - t.Errorf("encoding of point fails: " + err.Error()) + t.Errorf("encoding of point fails: %v", err) } if _, err := ptmp.UnmarshalFrom(buf); err != nil { - t.Errorf("decoding of point fails: " + err.Error()) + t.Errorf("decoding of point fails: %v", err) } if !ptmp.Equal(p) { t.Errorf("decoding produces different point than encoded")