Skip to content

Commit

Permalink
Poseidon Sponge Hash with different frame sizes (#52)
Browse files Browse the repository at this point in the history
* Poseidon Sponge Hash with different frame sizes
* Update deps. Bump go version
* Update & fix linter.
* Refactor a bit.
* Reduce gc pressure
  • Loading branch information
OBrezhniev committed Mar 8, 2023
1 parent edc36bf commit e5cf066
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 163 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
name: Lint
on: [ push, pull_request ]
on:
push:
branches:
- main
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v1
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.16.x
- name: Checkout code
uses: actions/checkout@v2
- name: Lint
run: |
curl -sSfL https://github.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
go-version: 1.20.x
- uses: golangci/golangci-lint-action@v3
with:
version: v1.51.1
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [ 1.16.x, 1.17.x ]
go-version: [ 1.18.x, 1.19.x, 1.20.x ]
goarch: [ "amd64", "386" ]
runs-on: ubuntu-latest
steps:
Expand Down
90 changes: 75 additions & 15 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,77 @@
issues:
max-same-issues: 0
exclude-use-default: false
service:
golangci-lint-version: 1.51.x

run:
timeout: 2m
skip-dirs:
- vendor

linters-settings:
govet:
check-shadowing: true
revive:
min-confidence: 0.1
rules:
- name: package-comments
disabled: true
maligned:
suggest-new: true
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- hugeParam

linters:
enable:
- whitespace
enable:
- bodyclose
- megacheck
- revive
- govet
- unconvert
- megacheck
- gas
- gocyclo
- dupl
- misspell
- unparam
- typecheck
- ineffassign
- stylecheck
- exportloopref
- nakedret
- gosimple
- prealloc
- unused

## format - fill free to fix
# - errcheck
# - gofmt
# - goimports
fast: false
disable-all: true

issues:
exclude-rules:
# - Fix and remove
- text: "at least one file in a package should have a package comment"
linters:
- stylecheck
# - Fix and remove
- text: "should have a package comment, unless it's in another file for this package"
linters:
- revive
- path: _test\.go
linters:
- gosec
- gci
- misspell
- gomnd
- gofmt
- goimports
- lll
- golint
linters-settings:
lll:
line-length: 100
- dupl
exclude-use-default: false
6 changes: 3 additions & 3 deletions babyjub/babyjub.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (p *PointProjective) Affine() *Point {

// Add computes the addition of two points in projective coordinates
// representation
func (p *PointProjective) Add(q *PointProjective, o *PointProjective) *PointProjective {
func (p *PointProjective) Add(q, 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)
Expand Down Expand Up @@ -209,7 +209,7 @@ func PointCoordSign(c *big.Int) bool {
func PackSignY(sign bool, y *big.Int) [32]byte {
leBuf := utils.BigIntLEBytes(y)
if sign {
leBuf[31] = leBuf[31] | 0x80 //nolint:gomnd
leBuf[31] |= 0x80 //nolint:gomnd
}
return leBuf
}
Expand All @@ -225,7 +225,7 @@ func UnpackSignY(leBuf [32]byte) (bool, *big.Int) {
y := big.NewInt(0)
if (leBuf[31] & 0x80) != 0x00 { //nolint:gomnd
sign = true
leBuf[31] = leBuf[31] & 0x7F //nolint:gomnd
leBuf[31] &= 0x7F //nolint:gomnd
}
utils.SetBigIntFromLEBytes(y, leBuf[:])
return sign, y
Expand Down
11 changes: 6 additions & 5 deletions babyjub/eddsa.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package babyjub eddsa implements the EdDSA over the BabyJubJub curve
//
//nolint:gomnd
package babyjub

Expand All @@ -16,9 +17,9 @@ import (
// pruneBuffer prunes the buffer during key generation according to RFC 8032.
// https://tools.ietf.org/html/rfc8032#page-13
func pruneBuffer(buf *[32]byte) *[32]byte {
buf[0] = buf[0] & 0xF8
buf[31] = buf[31] & 0x7F
buf[31] = buf[31] | 0x40
buf[0] &= 0xF8
buf[31] &= 0x7F
buf[31] |= 0x40
return buf
}

Expand Down Expand Up @@ -210,7 +211,7 @@ func (sComp *SignatureComp) Scan(src interface{}) error {
if len(srcB) != 64 {
return fmt.Errorf("can't scan []byte of len %d into Signature, want %d", len(srcB), 64)
}
copy(sComp[:], srcB[:])
copy(sComp[:], srcB)
return nil
}

Expand All @@ -229,7 +230,7 @@ func (s *Signature) Scan(src interface{}) error {
return fmt.Errorf("can't scan []byte of len %d into Signature, want %d", len(srcB), 64)
}
buf := [64]byte{}
copy(buf[:], srcB[:])
copy(buf[:], srcB)
_, err := s.Decompress(buf)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion babyjub/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// the original blake from the SHA3 competition and not the new blake2 version.
func Blake512(m []byte) []byte {
h := blake512.New()
_, err := h.Write(m[:])
_, err := h.Write(m)
if err != nil {
panic(err)
}
Expand Down
24 changes: 6 additions & 18 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
package constants

import (
"fmt"
"math/big"
)

const qString = "21888242871839275222246405745257275088548364400416034343698204186575808495617"

// Q is the order of the integer field (Zq) that fits inside the SNARK.
var Q *big.Int
var Q, _ = new(big.Int).SetString(qString, 10)

// Zero is 0.
var Zero *big.Int
var Zero = big.NewInt(0)

// One is 1.
var One *big.Int
var One = big.NewInt(1)

// MinusOne is -1.
var MinusOne *big.Int

func init() {
Zero = big.NewInt(0)
One = big.NewInt(1)
MinusOne = big.NewInt(-1)

qString := "21888242871839275222246405745257275088548364400416034343698204186575808495617"
var ok bool
Q, ok = new(big.Int).SetString(qString, 10) //nolint:gomnd
if !ok {
panic(fmt.Sprintf("Bad base 10 string %s", qString))
}
}
var MinusOne = big.NewInt(-1)
17 changes: 10 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module github.com/iden3/go-iden3-crypto

go 1.16
go 1.18

require (
github.com/dchest/blake512 v1.0.0
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/leanovate/gopter v0.2.9 // indirect
github.com/leanovate/gopter v0.2.9
github.com/stretchr/testify v1.8.2
golang.org/x/crypto v0.7.0
golang.org/x/sys v0.6.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
30 changes: 14 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
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/dchest/blake512 v1.0.0 h1:oDFEQFIqFSeuA34xLtXZ/rWxCXdSjirjzPhey5EUvmA=
github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 h1:/pEO3GD/ABYAjuakUS6xSEmmlyVS4kxBNkeA9tLJiTI=
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12 changes: 8 additions & 4 deletions goldenposeidon/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package poseidon
import "github.com/iden3/go-iden3-crypto/ffg"

const (
NROUNDSF = 8 //nolint:golint
NROUNDSP = 22 //nolint:golint
CAPLEN = 4 //nolint:golint
mLen = 12
// NROUNDSF const from original paper
NROUNDSF = 8
// NROUNDSP const from original paper
NROUNDSP = 22
// CAPLEN const
CAPLEN = 4
// mLen const
mLen = 12
)

var (
Expand Down
4 changes: 3 additions & 1 deletion goldenposeidon/poseidon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ func zero() *ffg.Element {
return ffg.NewElement()
}

var big7 = big.NewInt(7)

// exp7 performs x^7 mod p
func exp7(a *ffg.Element) {
a.Exp(*a, big.NewInt(7)) //nolint:gomnd
a.Exp(*a, big7)
}

// exp7state perform exp7 for whole state
Expand Down
27 changes: 2 additions & 25 deletions goldenposeidon/poseidon_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package poseidon

import (
"math/big"
"testing"

"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -99,32 +97,11 @@ func TestPoseidonHashCompare(t *testing.T) {
)
}

func BenchmarkPoseidonHash12Inputs(b *testing.B) {
bigArray12 := []*big.Int{
big.NewInt(1),
big.NewInt(2),
big.NewInt(3),
big.NewInt(4),
big.NewInt(5),
big.NewInt(6),
big.NewInt(7),
big.NewInt(8),
big.NewInt(9),
big.NewInt(10),
big.NewInt(11),
big.NewInt(12),
}

for i := 0; i < b.N; i++ {
poseidon.Hash(bigArray12) //nolint:errcheck,gosec
}
}

func BenchmarkNeptuneHash(b *testing.B) {
inp := [NROUNDSF]uint64{1, 2, 3, 4, 5, 6, 7, 8}
cap := [CAPLEN]uint64{10, 11, 12, 13}
_cap := [CAPLEN]uint64{10, 11, 12, 13}

for i := 0; i < b.N; i++ {
Hash(inp, cap) //nolint:errcheck,gosec
_, _ = Hash(inp, _cap)
}
}
Loading

0 comments on commit e5cf066

Please sign in to comment.