Skip to content

Commit

Permalink
Extend Poseidon to 16 inputs (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
OBrezhniev authored Oct 6, 2021
1 parent ef9f862 commit c544ba0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions poseidon/poseidon.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func mix(state []*ff.Element, t int, m [][]*ff.Element) []*ff.Element {
// Hash computes the Poseidon hash for the given inputs
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)-2) //nolint:gomnd,lll
if len(inpBI) == 0 || len(inpBI) > len(NROUNDSP) {
return nil, fmt.Errorf("invalid inputs length %d, max %d", len(inpBI), len(NROUNDSP)) //nolint:gomnd,lll
}
inp := utils.BigIntArrayToElementArray(inpBI[:])

Expand Down
33 changes: 25 additions & 8 deletions poseidon/poseidon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/iden3/go-iden3-crypto/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/blake2b"
)

Expand All @@ -33,6 +34,8 @@ func TestPoseidonHash(t *testing.T) {
b12 := big.NewInt(12)
b13 := big.NewInt(13)
b14 := big.NewInt(14)
b15 := big.NewInt(15)
b16 := big.NewInt(16)

h, err := Hash([]*big.Int{b1})
assert.Nil(t, err)
Expand Down Expand Up @@ -85,23 +88,37 @@ func TestPoseidonHash(t *testing.T) {
assert.Equal(t,
"5540388656744764564518487011617040650780060800286365721923524861648744699539",
h.String())

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

h, err = Hash([]*big.Int{b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16})
assert.Nil(t, err)
assert.Equal(t,
"9989051620750914585850546081941653841776809718687451684622678807385399211877",
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, b0, b0, b0, b0, b0, b0, b0, b0})
assert.Nil(t, err)

_, 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 15, max 14", err.Error())
var err error

_, 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 16, max 14", err.Error())
require.Nil(t, err)

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

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

func BenchmarkPoseidonHash(b *testing.B) {
Expand Down

0 comments on commit c544ba0

Please sign in to comment.