mirror of
https://github.com/arnaucube/go-iden3-crypto.git
synced 2026-02-07 11:36:41 +01:00
Extend Poseidon to 16 inputs (#37)
* Extend poseidon to 16 inputs. Better initialization of Poseidon constants * Update deps * Fix linter warnings * Test vectors for poseidon with 14 inputs
This commit is contained in:
committed by
GitHub
parent
933c28a8d7
commit
ef9f86210a
49618
poseidon/constants.go
49618
poseidon/constants.go
File diff suppressed because it is too large
Load Diff
@@ -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()
|
||||
@@ -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
|
||||
@@ -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[:])
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -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,
|
||||
@@ -58,13 +68,23 @@ 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) {
|
||||
@@ -72,16 +92,16 @@ func TestErrorInputs(t *testing.T) {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user