Browse Source

update the input params of hash

fix/bbjj-err
Cool Developer 2 years ago
parent
commit
f0c462811a
3 changed files with 26 additions and 31 deletions
  1. +2
    -0
      goldenposeidon/constants.go
  2. +3
    -11
      goldenposeidon/poseidon.go
  3. +21
    -20
      goldenposeidon/poseidon_test.go

+ 2
- 0
goldenposeidon/constants.go

@ -106,7 +106,9 @@ var (
0x4543d9df5476d3cb, 0xf172d73e004fc90d, 0xdfd1c4febcc81238, 0xbc8dfb627fe558fc, 0x4543d9df5476d3cb, 0xf172d73e004fc90d, 0xdfd1c4febcc81238, 0xbc8dfb627fe558fc,
} }
// C is a constant array of element
C []*ffg.Element C []*ffg.Element
// M is a matrix
M [][]*ffg.Element M [][]*ffg.Element
) )

+ 3
- 11
goldenposeidon/poseidon.go

@ -1,7 +1,6 @@
package poseidon package poseidon
import ( import (
"fmt"
"math/big" "math/big"
"github.com/iden3/go-iden3-crypto/ffg" "github.com/iden3/go-iden3-crypto/ffg"
@ -51,20 +50,13 @@ func mix(state []*ffg.Element) []*ffg.Element {
} }
// Hash computes the Poseidon hash for the given inputs // 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) state := make([]*ffg.Element, mLen)
for i := 0; i < NROUNDSF; i++ { 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++ { 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++ { for r := 0; r < NROUNDSF+NROUNDSP; r++ {

+ 21
- 20
goldenposeidon/poseidon_test.go

@ -1,19 +1,20 @@
package poseidon package poseidon
import ( import (
"math/big"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const prime uint64 = 18446744069414584321
func TestPoseidonHash(t *testing.T) { 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.Nil(t, err)
assert.Equal(t, assert.Equal(t,
[CAPLEN]uint64{ [CAPLEN]uint64{
@ -24,7 +25,7 @@ func TestPoseidonHash(t *testing.T) {
}, h, }, 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.Nil(t, err)
assert.Equal(t, assert.Equal(t,
[CAPLEN]uint64{ [CAPLEN]uint64{
@ -35,7 +36,7 @@ func TestPoseidonHash(t *testing.T) {
}, h, }, 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.Nil(t, err)
assert.Equal(t, assert.Equal(t,
[CAPLEN]uint64{ [CAPLEN]uint64{
@ -46,7 +47,7 @@ func TestPoseidonHash(t *testing.T) {
}, h, }, 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.Nil(t, err)
assert.Equal(t, assert.Equal(t,
[CAPLEN]uint64{ [CAPLEN]uint64{
@ -57,7 +58,7 @@ func TestPoseidonHash(t *testing.T) {
}, h, }, 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.Nil(t, err)
assert.Equal(t, assert.Equal(t,
[CAPLEN]uint64{ [CAPLEN]uint64{
@ -68,16 +69,16 @@ func TestPoseidonHash(t *testing.T) {
}, h, }, 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.Nil(t, err)
assert.Equal(t, assert.Equal(t,
[CAPLEN]uint64{ [CAPLEN]uint64{

Loading…
Cancel
Save