Skip to content

Commit

Permalink
Lint fix for pairing package
Browse files Browse the repository at this point in the history
  • Loading branch information
K1li4nL committed May 31, 2024
1 parent 19ca0d2 commit 44e1918
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 57 deletions.
2 changes: 2 additions & 0 deletions pairing/bn254/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var p2 = [4]uint64{0x3c208c16d87cfd47, 0x97816a916871ca8d, 0xb85045b68181585d, 0
var curveB = newGFp(3)

// np is the negative inverse of p, mod 2^256.
//
//nolint:unused // maybe useful
var np = [4]uint64{0x87d20782e4866389, 0x9ede7d651eca6ac9, 0xd8afcbd01833da80, 0xf57a22b791888c6b}

// rN1 is R^-1 where R = 2^256 mod p.
Expand Down
2 changes: 1 addition & 1 deletion pairing/bn254/gfp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func newGFpFromBase10(x string) *gfP {
bx, _ := new(big.Int).SetString(x, 10)
bx = bx.Mod(bx, p)
out := &gfP{}
out.Unmarshal(zeroPadBytes(bx.Bytes(), 32))
_ = out.Unmarshal(zeroPadBytes(bx.Bytes(), 32))
montEncode(out, out)
return out
}
Expand Down
8 changes: 4 additions & 4 deletions pairing/bn254/gfp12.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func (e *gfP12) Mul(a, b *gfP12) *gfP12 {
return e
}

func (e *gfP12) MulScalar(a *gfP12, b *gfP6) *gfP12 {
e.x.Mul(&e.x, b)
e.y.Mul(&e.y, b)
func (e *gfP12) MulScalar(a *gfP6) *gfP12 {
e.x.Mul(&e.x, a)
e.y.Mul(&e.y, a)
return e
}

Expand Down Expand Up @@ -155,7 +155,7 @@ func (e *gfP12) Invert(a *gfP12) *gfP12 {

e.x.Neg(&a.x)
e.y.Set(&a.y)
e.MulScalar(e, t2)
e.MulScalar(t2)
return e
}

Expand Down
1 change: 1 addition & 0 deletions pairing/bn254/gfp_decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"golang.org/x/sys/cpu"
)

//nolint:unused // maybe useful
var hasBMI2 = cpu.X86.HasBMI2

// go:noescape
Expand Down
1 change: 1 addition & 0 deletions pairing/bn254/lattice.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var curveLattice = &lattice{
det: bigFromBase10("43776485743678550444492811490514550177096728800832068687396408373151616991234"),
}

//nolint:lll,unused // maybe useful
var targetLattice = &lattice{
vectors: [][]*big.Int{
{bigFromBase10("9931322734385697761"), bigFromBase10("9931322734385697761"), bigFromBase10("9931322734385697763"), bigFromBase10("9931322734385697764")},
Expand Down
4 changes: 2 additions & 2 deletions pairing/bn254/optate.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func lineFunctionAdd(r, p *twistPoint, q *curvePoint, r2 *gfP2) (a, b, c *gfP2,
b = (&gfP2{}).Neg(L1)
b.MulScalar(b, &q.x).Add(b, b)

return
return a, b, c, rOut
}

func lineFunctionDouble(r *twistPoint, q *curvePoint) (a, b, c *gfP2, rOut *twistPoint) {
Expand Down Expand Up @@ -88,7 +88,7 @@ func lineFunctionDouble(r *twistPoint, q *curvePoint) (a, b, c *gfP2, rOut *twis
c = (&gfP2{}).Mul(&rOut.z, &r.t)
c.Add(c, c).MulScalar(c, &q.y)

return
return a, b, c, rOut
}

func mulLine(ret *gfP12, a, b, c *gfP2) {
Expand Down
120 changes: 90 additions & 30 deletions pairing/bn254/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *pointG1) EmbedLen() int {
panic("bn254.G1: unsupported operation")
}

func (p *pointG1) Embed(data []byte, rand cipher.Stream) kyber.Point {
func (p *pointG1) Embed(_ []byte, _ cipher.Stream) kyber.Point {
// XXX: An approach to implement this is:
// - Encode data as the x-coordinate of a point on y²=x³+3 where len(data)
// is stored in the least significant byte of x and the rest is being
Expand Down Expand Up @@ -152,8 +152,15 @@ func (p *pointG1) UnmarshalBinary(buf []byte) error {
p.g.x, p.g.y = gfP{0}, gfP{0}
}

p.g.x.Unmarshal(buf)
p.g.y.Unmarshal(buf[n:])
err := p.g.x.Unmarshal(buf)
if err != nil {
return err
}
err = p.g.y.Unmarshal(buf[n:])
if err != nil {
return err
}

montEncode(&p.g.x, &p.g.x)
montEncode(&p.g.y, &p.g.y)

Expand Down Expand Up @@ -215,8 +222,8 @@ func hashToField(domain, m []byte) (*gfP, *gfP) {
x.SetBytes(_msg[0:48]).Mod(x, p)
y.SetBytes(_msg[48:96]).Mod(y, p)
gx, gy := &gfP{}, &gfP{}
gx.Unmarshal(zeroPadBytes(x.Bytes(), 32))
gy.Unmarshal(zeroPadBytes(y.Bytes(), 32))
_ = gx.Unmarshal(zeroPadBytes(x.Bytes(), 32))
_ = gy.Unmarshal(zeroPadBytes(y.Bytes(), 32))
montEncode(gx, gx)
montEncode(gy, gy)
return gx, gy
Expand Down Expand Up @@ -254,17 +261,20 @@ func mapToPoint(domain []byte, u *gfP) kyber.Point {
gfpMul(x3, c4, x3)
gfpAdd(x3, newGFp(1), x3)

x, y := &gfP{}, &gfP{}
if legendre(g(x1)) == 1 {
var x *gfP
y := &gfP{}
switch {
case legendre(g(x1)) == 1:
x = x1
y.Sqrt(g(x1))
} else if legendre(g(x2)) == 1 {
case legendre(g(x2)) == 1:
x = x2
y.Sqrt(g(x2))
} else {
default:
x = x3
y.Sqrt(g(x3))
}

if sgn0(u) != sgn0(y) {
gfpNeg(y, y)
}
Expand Down Expand Up @@ -318,11 +328,11 @@ func expandMsgXmdKeccak256(domain, msg []byte, outLen int) []byte {
_, _ = h.Write([]byte{domainLen})

// b_1 || ... || b_(ell - 1)
copy(out[(i-1)*h.Size():i*h.Size()], bi[:])
copy(out[(i-1)*h.Size():i*h.Size()], bi)
bi = h.Sum(nil)
}
// b_ell
copy(out[(ell-1)*h.Size():], bi[:])
copy(out[(ell-1)*h.Size():], bi)
return out[:outLen]
}

Expand Down Expand Up @@ -376,7 +386,7 @@ func (p *pointG2) EmbedLen() int {
panic("bn254.G2: unsupported operation")
}

func (p *pointG2) Embed(data []byte, rand cipher.Stream) kyber.Point {
func (p *pointG2) Embed(_ []byte, _ cipher.Stream) kyber.Point {
panic("bn254.G2: unsupported operation")
}

Expand Down Expand Up @@ -463,10 +473,23 @@ func (p *pointG2) UnmarshalBinary(buf []byte) error {
return errors.New("bn254.G2: not enough data")
}

p.g.x.x.Unmarshal(buf[0*n:])
p.g.x.y.Unmarshal(buf[1*n:])
p.g.y.x.Unmarshal(buf[2*n:])
p.g.y.y.Unmarshal(buf[3*n:])
err := p.g.x.x.Unmarshal(buf[0*n:])
if err != nil {
return err
}
err = p.g.x.y.Unmarshal(buf[1*n:])
if err != nil {
return err
}
err = p.g.y.x.Unmarshal(buf[2*n:])
if err != nil {
return err
}
err = p.g.y.y.Unmarshal(buf[3*n:])
if err != nil {
return err
}

montEncode(&p.g.x.x, &p.g.x.x)
montEncode(&p.g.x.y, &p.g.x.y)
montEncode(&p.g.y.x, &p.g.y.x)
Expand Down Expand Up @@ -560,7 +583,7 @@ func (p *pointGT) EmbedLen() int {
panic("bn254.GT: unsupported operation")
}

func (p *pointGT) Embed(data []byte, rand cipher.Stream) kyber.Point {
func (p *pointGT) Embed(_ []byte, _ cipher.Stream) kyber.Point {
panic("bn254.GT: unsupported operation")
}

Expand Down Expand Up @@ -641,6 +664,7 @@ func (p *pointGT) MarshalTo(w io.Writer) (int, error) {
return w.Write(buf)
}

//nolint:funlen
func (p *pointGT) UnmarshalBinary(buf []byte) error {
n := p.ElementSize()
if len(buf) < p.MarshalSize() {
Expand All @@ -651,18 +675,55 @@ func (p *pointGT) UnmarshalBinary(buf []byte) error {
p.g = &gfP12{}
}

p.g.x.x.x.Unmarshal(buf[0*n:])
p.g.x.x.y.Unmarshal(buf[1*n:])
p.g.x.y.x.Unmarshal(buf[2*n:])
p.g.x.y.y.Unmarshal(buf[3*n:])
p.g.x.z.x.Unmarshal(buf[4*n:])
p.g.x.z.y.Unmarshal(buf[5*n:])
p.g.y.x.x.Unmarshal(buf[6*n:])
p.g.y.x.y.Unmarshal(buf[7*n:])
p.g.y.y.x.Unmarshal(buf[8*n:])
p.g.y.y.y.Unmarshal(buf[9*n:])
p.g.y.z.x.Unmarshal(buf[10*n:])
p.g.y.z.y.Unmarshal(buf[11*n:])
err := p.g.x.x.x.Unmarshal(buf[0*n:])
if err != nil {
return err
}
err = p.g.x.x.y.Unmarshal(buf[1*n:])
if err != nil {
return err
}
err = p.g.x.y.x.Unmarshal(buf[2*n:])
if err != nil {
return err
}
err = p.g.x.y.y.Unmarshal(buf[3*n:])
if err != nil {
return err
}
err = p.g.x.z.x.Unmarshal(buf[4*n:])
if err != nil {
return err
}
err = p.g.x.z.y.Unmarshal(buf[5*n:])
if err != nil {
return err
}
err = p.g.y.x.x.Unmarshal(buf[6*n:])
if err != nil {
return err
}
err = p.g.y.x.y.Unmarshal(buf[7*n:])
if err != nil {
return err
}
err = p.g.y.y.x.Unmarshal(buf[8*n:])
if err != nil {
return err
}
err = p.g.y.y.y.Unmarshal(buf[9*n:])
if err != nil {
return err
}
err = p.g.y.z.x.Unmarshal(buf[10*n:])
if err != nil {
return err
}
err = p.g.y.z.y.Unmarshal(buf[11*n:])
if err != nil {
return err
}

montEncode(&p.g.x.x.x, &p.g.x.x.x)
montEncode(&p.g.x.x.y, &p.g.x.x.y)
montEncode(&p.g.x.y.x, &p.g.x.y.x)
Expand All @@ -677,7 +738,6 @@ func (p *pointGT) UnmarshalBinary(buf []byte) error {
montEncode(&p.g.y.z.y, &p.g.y.z.y)

// TODO: check if point is on curve

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pairing/bn254/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (c *commonSuite) Read(r io.Reader, objs ...interface{}) error {

// Write is the default implementation of kyber.Encoding interface Write.
func (c *commonSuite) Write(w io.Writer, objs ...interface{}) error {
return fixbuf.Write(w, objs)
return fixbuf.Write(w, objs...)
}

// Hash returns a newly instantiated keccak256 hash function.
Expand Down
1 change: 0 additions & 1 deletion pairing/bn254/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ type tsrPoint struct {
}

func TestSuiteProtobuf(t *testing.T) {
//bn := suites.MustFind("bn254.adapter")
bn1 := NewSuiteG1()
bn2 := NewSuiteG2()
bnT := NewSuiteGT()
Expand Down
2 changes: 1 addition & 1 deletion pairing/circl_bls12381/g1.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *G1Elt) EmbedLen() int {
panic("bls12-381: unsupported operation")
}

func (p *G1Elt) Embed(data []byte, r cipher.Stream) kyber.Point {
func (p *G1Elt) Embed(_ []byte, _ cipher.Stream) kyber.Point {
panic("bls12-381: unsupported operation")
}

Expand Down
2 changes: 1 addition & 1 deletion pairing/circl_bls12381/g2.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *G2Elt) EmbedLen() int {
panic("bls12-381: unsupported operation")
}

func (p *G2Elt) Embed(data []byte, r cipher.Stream) kyber.Point {
func (p *G2Elt) Embed(_ []byte, _ cipher.Stream) kyber.Point {
panic("bls12-381: unsupported operation")
}

Expand Down
4 changes: 2 additions & 2 deletions pairing/circl_bls12381/gt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *GTElt) Null() kyber.Point { p.inner.SetIdentity(); return p }

func (p *GTElt) Base() kyber.Point { p.inner = *gtBase; return p }

func (p *GTElt) Pick(rand cipher.Stream) kyber.Point {
func (p *GTElt) Pick(_ cipher.Stream) kyber.Point {
panic("bls12-381: unsupported operation")
}

Expand All @@ -61,7 +61,7 @@ func (p *GTElt) EmbedLen() int {
panic("bls12-381: unsupported operation")
}

func (p *GTElt) Embed(data []byte, r cipher.Stream) kyber.Point {
func (p *GTElt) Embed(_ []byte, _ cipher.Stream) kyber.Point {
panic("bls12-381: unsupported operation")
}

Expand Down
4 changes: 2 additions & 2 deletions pairing/circl_bls12381/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func (s Suite) ValidatePairing(p1, p2, p3, p4 kyber.Point) bool {
return out.IsIdentity()
}

func (s Suite) Read(r io.Reader, objs ...interface{}) error {
func (s Suite) Read(_ io.Reader, _ ...interface{}) error {
panic("Suite.Read(): deprecated in drand")
}

func (s Suite) Write(w io.Writer, objs ...interface{}) error {
func (s Suite) Write(_ io.Writer, _ ...interface{}) error {
panic("Suite.Write(): deprecated in drand")
}

Expand Down
Loading

0 comments on commit 44e1918

Please sign in to comment.