Skip to content

Commit

Permalink
update the input params of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope committed Mar 15, 2022
1 parent 26bfd10 commit f0c4628
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
2 changes: 2 additions & 0 deletions goldenposeidon/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ var (
0x4543d9df5476d3cb, 0xf172d73e004fc90d, 0xdfd1c4febcc81238, 0xbc8dfb627fe558fc,
}

// C is a constant array of element
C []*ffg.Element
// M is a matrix
M [][]*ffg.Element
)

Expand Down
14 changes: 3 additions & 11 deletions goldenposeidon/poseidon.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package poseidon

import (
"fmt"
"math/big"

"github.com/iden3/go-iden3-crypto/ffg"
Expand Down Expand Up @@ -51,20 +50,13 @@ func mix(state []*ffg.Element) []*ffg.Element {
}

// Hash computes the Poseidon hash for the given inputs
func Hash(inpBI []*big.Int, capBI []*big.Int) ([CAPLEN]uint64, error) {
if len(inpBI) != NROUNDSF {
return [CAPLEN]uint64{}, fmt.Errorf("invalid inputs length %d, must be 8", len(inpBI))
}
if len(capBI) != CAPLEN {
return [CAPLEN]uint64{}, fmt.Errorf("invalid capcity length %d, must be 4", len(capBI))
}

func Hash(inpBI [NROUNDSF]uint64, capBI [CAPLEN]uint64) ([CAPLEN]uint64, error) {
state := make([]*ffg.Element, mLen)
for i := 0; i < NROUNDSF; i++ {
state[i] = ffg.NewElement().SetBigInt(inpBI[i])
state[i] = ffg.NewElement().SetUint64(inpBI[i])
}
for i := 0; i < CAPLEN; i++ {
state[i+NROUNDSF] = ffg.NewElement().SetBigInt(capBI[i])
state[i+NROUNDSF] = ffg.NewElement().SetUint64(capBI[i])
}

for r := 0; r < NROUNDSF+NROUNDSP; r++ {
Expand Down
41 changes: 21 additions & 20 deletions goldenposeidon/poseidon_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package poseidon

import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
)

const prime uint64 = 18446744069414584321

func TestPoseidonHash(t *testing.T) {
b0 := big.NewInt(0)
b1 := big.NewInt(1)
b_1 := big.NewInt(-1)
bM := new(big.Int).SetUint64(18446744069414584321)
b0 := uint64(0)
b1 := uint64(1)
bm1 := prime - 1
bM := prime

h, err := Hash([]*big.Int{b0, b0, b0, b0, b0, b0, b0, b0}, []*big.Int{b0, b0, b0, b0})
h, err := Hash([NROUNDSF]uint64{b0, b0, b0, b0, b0, b0, b0, b0}, [CAPLEN]uint64{b0, b0, b0, b0})
assert.Nil(t, err)
assert.Equal(t,
[CAPLEN]uint64{
Expand All @@ -24,7 +25,7 @@ func TestPoseidonHash(t *testing.T) {
}, h,
)

h, err = Hash([]*big.Int{b1, b1, b1, b1, b1, b1, b1, b1}, []*big.Int{b1, b1, b1, b1})
h, err = Hash([NROUNDSF]uint64{b1, b1, b1, b1, b1, b1, b1, b1}, [CAPLEN]uint64{b1, b1, b1, b1})
assert.Nil(t, err)
assert.Equal(t,
[CAPLEN]uint64{
Expand All @@ -35,7 +36,7 @@ func TestPoseidonHash(t *testing.T) {
}, h,
)

h, err = Hash([]*big.Int{b1, b1, b1, b1, b1, b1, b1, b1}, []*big.Int{b1, b1, b1, b1})
h, err = Hash([NROUNDSF]uint64{b1, b1, b1, b1, b1, b1, b1, b1}, [CAPLEN]uint64{b1, b1, b1, b1})
assert.Nil(t, err)
assert.Equal(t,
[CAPLEN]uint64{
Expand All @@ -46,7 +47,7 @@ func TestPoseidonHash(t *testing.T) {
}, h,
)

h, err = Hash([]*big.Int{b_1, b_1, b_1, b_1, b_1, b_1, b_1, b_1}, []*big.Int{b_1, b_1, b_1, b_1})
h, err = Hash([NROUNDSF]uint64{bm1, bm1, bm1, bm1, bm1, bm1, bm1, bm1}, [CAPLEN]uint64{bm1, bm1, bm1, bm1})
assert.Nil(t, err)
assert.Equal(t,
[CAPLEN]uint64{
Expand All @@ -57,7 +58,7 @@ func TestPoseidonHash(t *testing.T) {
}, h,
)

h, err = Hash([]*big.Int{bM, bM, bM, bM, bM, bM, bM, bM}, []*big.Int{b0, b0, b0, b0})
h, err = Hash([NROUNDSF]uint64{bM, bM, bM, bM, bM, bM, bM, bM}, [CAPLEN]uint64{b0, b0, b0, b0})
assert.Nil(t, err)
assert.Equal(t,
[CAPLEN]uint64{
Expand All @@ -68,16 +69,16 @@ func TestPoseidonHash(t *testing.T) {
}, h,
)

h, err = Hash([]*big.Int{
new(big.Int).SetUint64(923978),
new(big.Int).SetUint64(235763497586),
new(big.Int).SetUint64(9827635653498),
new(big.Int).SetUint64(112870),
new(big.Int).SetUint64(289273673480943876),
new(big.Int).SetUint64(230295874986745876),
new(big.Int).SetUint64(6254867324987),
new(big.Int).SetUint64(2087),
}, []*big.Int{b0, b0, b0, b0})
h, err = Hash([NROUNDSF]uint64{
uint64(923978),
uint64(235763497586),
uint64(9827635653498),
uint64(112870),
uint64(289273673480943876),
uint64(230295874986745876),
uint64(6254867324987),
uint64(2087),
}, [CAPLEN]uint64{b0, b0, b0, b0})
assert.Nil(t, err)
assert.Equal(t,
[CAPLEN]uint64{
Expand Down

0 comments on commit f0c4628

Please sign in to comment.