Skip to content

Commit

Permalink
Merge pull request #32 from iden3/feature/upgrade-linters
Browse files Browse the repository at this point in the history
Upgrade linters
  • Loading branch information
ed255 authored Dec 18, 2020
2 parents 821a601 + 6d75396 commit a2015ad
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 112 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
on: [ push, pull_request ]
name: Lint
on: [ push, pull_request ]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: 1.14.x
- name: Checkout code
uses: actions/checkout@v2
- name: Lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0
$(go env GOPATH)/bin/golangci-lint run
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: 1.14.x
- name: Checkout code
uses: actions/checkout@v2
- name: Lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0
$(go env GOPATH)/bin/golangci-lint run --timeout=5m -c .golangci.yml
17 changes: 17 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
issues:
max-same-issues: 0
exclude-use-default: false
linters:
enable:
- whitespace
- gosec
- gci
- misspell
- gomnd
- gofmt
- goimports
- lll
- golint
linters-settings:
lll:
line-length: 100
46 changes: 25 additions & 21 deletions babyjub/babyjub.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ type PointProjective struct {

// NewPointProjective creates a new Point in projective coordinates.
func NewPointProjective() *PointProjective {
return &PointProjective{X: ff.NewElement().SetZero(), Y: ff.NewElement().SetOne(), Z: ff.NewElement().SetOne()}
return &PointProjective{X: ff.NewElement().SetZero(),
Y: ff.NewElement().SetOne(), Z: ff.NewElement().SetOne()}
}

// Affine returns the Point from the projective representation
Expand All @@ -84,19 +85,21 @@ func (p *PointProjective) Affine() *Point {
}
}

// Add computes the addition of two points in projective coordinates representation
func (res *PointProjective) Add(p *PointProjective, q *PointProjective) *PointProjective {
// add-2008-bbjlp https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#doubling-dbl-2008-bbjlp
a := ff.NewElement().Mul(p.Z, q.Z)
// Add computes the addition of two points in projective coordinates
// representation
func (p *PointProjective) Add(q *PointProjective, o *PointProjective) *PointProjective {
// add-2008-bbjlp
// https://hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#doubling-dbl-2008-bbjlp
a := ff.NewElement().Mul(q.Z, o.Z)
b := ff.NewElement().Square(a)
c := ff.NewElement().Mul(p.X, q.X)
d := ff.NewElement().Mul(p.Y, q.Y)
c := ff.NewElement().Mul(q.X, o.X)
d := ff.NewElement().Mul(q.Y, o.Y)
e := ff.NewElement().Mul(Dff, c)
e.MulAssign(d)
f := ff.NewElement().Sub(b, e)
g := ff.NewElement().Add(b, e)
x1y1 := ff.NewElement().Add(p.X, p.Y)
x2y2 := ff.NewElement().Add(q.X, q.Y)
x1y1 := ff.NewElement().Add(q.X, q.Y)
x2y2 := ff.NewElement().Add(o.X, o.Y)
x3 := ff.NewElement().Mul(x1y1, x2y2)
x3.SubAssign(c)
x3.SubAssign(d)
Expand All @@ -108,10 +111,10 @@ func (res *PointProjective) Add(p *PointProjective, q *PointProjective) *PointPr
y3.MulAssign(g)
z3 := ff.NewElement().Mul(f, g)

res.X = x3
res.Y = y3
res.Z = z3
return res
p.X = x3
p.Y = y3
p.Z = z3
return p
}

// Point represents a point of the babyjub curve.
Expand Down Expand Up @@ -141,24 +144,24 @@ func (p *Point) Projective() *PointProjective {
}
}

// Mul multiplies the Point p by the scalar s and stores the result in res,
// Mul multiplies the Point q by the scalar s and stores the result in p,
// which is also returned.
func (res *Point) Mul(s *big.Int, p *Point) *Point {
func (p *Point) Mul(s *big.Int, q *Point) *Point {
resProj := &PointProjective{
X: ff.NewElement().SetZero(),
Y: ff.NewElement().SetOne(),
Z: ff.NewElement().SetOne(),
}
exp := p.Projective()
exp := q.Projective()

for i := 0; i < s.BitLen(); i++ {
if s.Bit(i) == 1 {
resProj.Add(resProj, exp)
}
exp = exp.Add(exp, exp)
}
res = resProj.Affine()
return res
p = resProj.Affine()
return p
}

// InCurve returns true when the Point p is in the babyjub curve.
Expand Down Expand Up @@ -200,10 +203,11 @@ func PointCoordSign(c *big.Int) bool {
return c.Cmp(new(big.Int).Rsh(constants.Q, 1)) == 1
}

// PackPoint packs a point into a 32 byte array
func PackPoint(ay *big.Int, sign bool) [32]byte {
leBuf := utils.BigIntLEBytes(ay)
if sign {
leBuf[31] = leBuf[31] | 0x80
leBuf[31] = leBuf[31] | 0x80 //nolint:gomnd
}
return leBuf
}
Expand All @@ -219,9 +223,9 @@ func (p *Point) Compress() [32]byte {
// Point. Returns error if the compressed Point is invalid.
func (p *Point) Decompress(leBuf [32]byte) (*Point, error) {
sign := false
if (leBuf[31] & 0x80) != 0x00 {
if (leBuf[31] & 0x80) != 0x00 { //nolint:gomnd
sign = true
leBuf[31] = leBuf[31] & 0x7F
leBuf[31] = leBuf[31] & 0x7F //nolint:gomnd
}
utils.SetBigIntFromLEBytes(p.Y, leBuf[:])
return PointFromSignAndY(sign, p.Y)
Expand Down
15 changes: 10 additions & 5 deletions babyjub/babyjub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ func TestAdd2(t *testing.T) {
c.Y.String())

d := NewPointProjective().Add(c.Projective(), c.Projective()).Affine()
assert.Equal(t, "2f6458832049e917c95867185a96621336df33e13c98e81d1ef4928cdbb77772", hex.EncodeToString(d.X.Bytes()))
assert.Equal(t,
"2f6458832049e917c95867185a96621336df33e13c98e81d1ef4928cdbb77772",
hex.EncodeToString(d.X.Bytes()))

// Projective
aP := a.Projective()
bP := b.Projective()
cP := NewPointProjective().Add(aP, bP)
c2 := cP.Affine()
assert.Equal(t, c, c2)

}

func TestAdd3(t *testing.T) {
Expand Down Expand Up @@ -225,7 +226,9 @@ func TestCompressDecompress1(t *testing.T) {
p := &Point{X: x, Y: y}

buf := p.Compress()
assert.Equal(t, "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85", hex.EncodeToString(buf[:]))
assert.Equal(t,
"53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85",
hex.EncodeToString(buf[:]))

p2, err := NewPoint().Decompress(buf)
assert.Equal(t, nil, err)
Expand All @@ -241,7 +244,9 @@ func TestCompressDecompress2(t *testing.T) {
p := &Point{X: x, Y: y}

buf := p.Compress()
assert.Equal(t, "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709", hex.EncodeToString(buf[:]))
assert.Equal(t,
"e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709",
hex.EncodeToString(buf[:]))

p2, err := NewPoint().Decompress(buf)
assert.Equal(t, nil, err)
Expand All @@ -263,7 +268,7 @@ func TestCompressDecompressRnd(t *testing.T) {
func BenchmarkBabyjub(b *testing.B) {
const n = 256

rnd := rand.New(rand.NewSource(42))
rnd := rand.New(rand.NewSource(42)) //nolint:gosec

var badpoints [n]*Point
for i := 0; i < n; i++ {
Expand Down
Loading

0 comments on commit a2015ad

Please sign in to comment.