Skip to content

Commit

Permalink
Extend Poseidon to 16 inputs (#37)
Browse files Browse the repository at this point in the history
* Extend poseidon to 16 inputs. Better initialization of Poseidon constants
* Update deps
* Fix linter warnings
* Test vectors for poseidon with 14 inputs
  • Loading branch information
OBrezhniev authored Oct 6, 2021
1 parent 933c28a commit ef9f862
Show file tree
Hide file tree
Showing 9 changed files with 25,299 additions and 24,896 deletions.
2 changes: 1 addition & 1 deletion babyjub/babyjub.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {

Order = utils.NewIntFromString(
"21888242871839275222246405745257275088614511777268538073601725287587578984328")
SubOrder = new(big.Int).Rsh(Order, 3)
SubOrder = new(big.Int).Rsh(Order, 3) //nolint:gomnd

B8 = NewPoint()
B8.X = utils.NewIntFromString(
Expand Down
2 changes: 1 addition & 1 deletion babyjub/eddsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestCompressDecompress(t *testing.T) {

func TestSignatureCompScannerValuer(t *testing.T) {
privK := NewRandPrivKey()
var value driver.Valuer //nolint:gosimple this is done to ensure interface compatibility
var value driver.Valuer //nolint:gosimple // this is done to ensure interface compatibility
value = privK.SignPoseidon(big.NewInt(674238462)).Compress()
scan := privK.SignPoseidon(big.NewInt(1)).Compress()
fromDB, err := value.Value()
Expand Down
2 changes: 1 addition & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {

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

go 1.12
go 1.14

require (
github.com/dchest/blake512 v1.0.0
github.com/ethereum/go-ethereum v1.9.12
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
github.com/ethereum/go-ethereum v1.10.8
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
)
516 changes: 453 additions & 63 deletions go.sum

Large diffs are not rendered by default.

49,618 changes: 24,806 additions & 24,812 deletions poseidon/constants.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions poseidon/poseidon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const NROUNDSF = 8 //nolint:golint

var NROUNDSP = []int{56, 57, 56, 60, 60, 63, 64, 63} //nolint:golint
var NROUNDSP = []int{56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68} //nolint:golint

func zero() *ff.Element {
return ff.NewElement()
Expand All @@ -19,7 +19,7 @@ func zero() *ff.Element {
// exp5 performs x^5 mod p
// https://eprint.iacr.org/2019/458.pdf page 8
func exp5(a *ff.Element) {
a.Exp(*a, 5)
a.Exp(*a, 5) //nolint:gomnd
}

// exp5state perform exp5 for whole state
Expand Down Expand Up @@ -57,7 +57,7 @@ func mix(state []*ff.Element, t int, m [][]*ff.Element) []*ff.Element {
func Hash(inpBI []*big.Int) (*big.Int, error) {
t := len(inpBI) + 1
if len(inpBI) == 0 || len(inpBI) >= len(NROUNDSP)-1 {
return nil, fmt.Errorf("invalid inputs length %d, max %d", len(inpBI), len(NROUNDSP)-1)
return nil, fmt.Errorf("invalid inputs length %d, max %d", len(inpBI), len(NROUNDSP)-2) //nolint:gomnd,lll
}
inp := utils.BigIntArrayToElementArray(inpBI[:])

Expand Down
38 changes: 29 additions & 9 deletions poseidon/poseidon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func TestPoseidonHash(t *testing.T) {
b0 := big.NewInt(0)
b1 := big.NewInt(1)
b2 := big.NewInt(2)
b3 := big.NewInt(3)
b4 := big.NewInt(4)
b5 := big.NewInt(5)
b6 := big.NewInt(6)
b7 := big.NewInt(7)
b8 := big.NewInt(8)
b9 := big.NewInt(9)
b10 := big.NewInt(10)
b11 := big.NewInt(11)
b12 := big.NewInt(12)
b13 := big.NewInt(13)
b14 := big.NewInt(14)

h, err := Hash([]*big.Int{b1})
assert.Nil(t, err)
Expand All @@ -45,8 +57,6 @@ func TestPoseidonHash(t *testing.T) {
"15336558801450556532856248569924170992202208561737609669134139141992924267169",
h.String())

b3 := big.NewInt(3)
b4 := big.NewInt(4)
h, err = Hash([]*big.Int{b3, b4, b0, b0, b0})
assert.Nil(t, err)
assert.Equal(t,
Expand All @@ -58,30 +68,40 @@ func TestPoseidonHash(t *testing.T) {
"12263118664590987767234828103155242843640892839966517009184493198782366909018",
h.String())

b5 := big.NewInt(5)
b6 := big.NewInt(6)
h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6})
assert.Nil(t, err)
assert.Equal(t,
"20400040500897583745843009878988256314335038853985262692600694741116813247201",
h.String())

h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14})
assert.Nil(t, err)
assert.Equal(t,
"8354478399926161176778659061636406690034081872658507739535256090879947077494",
h.String())

h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, b0, b0, b0, b0})
assert.Nil(t, err)
assert.Equal(t,
"5540388656744764564518487011617040650780060800286365721923524861648744699539",
h.String())
}

func TestErrorInputs(t *testing.T) {
b0 := big.NewInt(0)
b1 := big.NewInt(1)
b2 := big.NewInt(2)

_, err := Hash([]*big.Int{b1, b2, b0, b0, b0, b0})
_, err := Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0})
assert.Nil(t, err)

_, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0})
_, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0})
assert.NotNil(t, err)
assert.Equal(t, "invalid inputs length 7, max 7", err.Error())
assert.Equal(t, "invalid inputs length 15, max 14", err.Error())

_, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0})
_, err = Hash([]*big.Int{b1, b2, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0})
assert.NotNil(t, err)
assert.Equal(t, "invalid inputs length 8, max 7", err.Error())
assert.Equal(t, "invalid inputs length 16, max 14", err.Error())
}

func BenchmarkPoseidonHash(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// NewIntFromString creates a new big.Int from a decimal integer encoded as a
// string. It will panic if the string is not a decimal integer.
func NewIntFromString(s string) *big.Int {
v, ok := new(big.Int).SetString(s, 10)
v, ok := new(big.Int).SetString(s, 10) //nolint:gomnd
if !ok {
panic(fmt.Sprintf("Bad base 10 string %s", s))
}
Expand Down

0 comments on commit ef9f862

Please sign in to comment.