Poseidon Sponge Hash with different frame sizes (#52)

* Poseidon Sponge Hash with different frame sizes
* Update deps. Bump go version
* Update & fix linter.
* Refactor a bit.
* Reduce gc pressure
This commit is contained in:
Oleksandr Brezhniev
2023-03-08 13:18:55 -05:00
committed by GitHub
parent edc36bfa52
commit e5cf066b8b
19 changed files with 355 additions and 165 deletions

View File

@@ -23,22 +23,22 @@ type constantsData struct {
}
func generateConstantsData() constantsData {
var constants constantsData
var consts constantsData
constants.seedHash = new(big.Int).SetBytes(keccak256.Hash([]byte(SEED)))
consts.seedHash = new(big.Int).SetBytes(keccak256.Hash([]byte(SEED)))
c := new(big.Int).SetBytes(keccak256.Hash([]byte(SEED + "_iv")))
constants.iv = new(big.Int).Mod(c, _constants.Q)
consts.iv = new(big.Int).Mod(c, _constants.Q)
constants.nRounds = 91
cts := getConstants(SEED, constants.nRounds)
constants.cts = cts
return constants
consts.nRounds = 91
cts := getConstants(SEED, consts.nRounds)
consts.cts = cts
return consts
}
func getConstants(seed string, nRounds int) []*ff.Element {
cts := make([]*ff.Element, nRounds)
cts[0] = ff.NewElement()
c := new(big.Int).SetBytes(keccak256.Hash([]byte(SEED)))
c := new(big.Int).SetBytes(keccak256.Hash([]byte(seed)))
for i := 1; i < nRounds; i++ {
c = new(big.Int).SetBytes(keccak256.Hash(c.Bytes()))

View File

@@ -5,21 +5,9 @@ import (
"math/big"
"testing"
"github.com/iden3/go-iden3-crypto/keccak256"
"github.com/stretchr/testify/assert"
)
func TestKeccak256(t *testing.T) {
res := keccak256.Hash([]byte(SEED))
assert.Equal(t,
"b6e489e6b37224a50bebfddbe7d89fa8fdcaa84304a70bd13f79b5d9f7951e9e",
hex.EncodeToString(res))
c := new(big.Int).SetBytes(keccak256.Hash([]byte(SEED)))
assert.Equal(t,
"82724731331859054037315113496710413141112897654334566532528783843265082629790",
c.String())
}
func TestMIMC7Generic(t *testing.T) {
b1 := big.NewInt(int64(1))
b2 := big.NewInt(int64(2))
@@ -36,7 +24,7 @@ func TestMIMC7Generic(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t,
"6464402164086696096195815557694604139393321133243036833927490113253119343397",
(*big.Int)(hg).String())
hg.String())
}
func TestMIMC7(t *testing.T) {
@@ -51,7 +39,7 @@ func TestMIMC7(t *testing.T) {
h1, err := Hash(bigArray1, nil)
assert.Nil(t, err)
// same hash value than the iden3js and circomlib tests:
assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h1).Bytes()),
assert.Equal(t, "0x"+hex.EncodeToString(h1.Bytes()),
"0x237c92644dbddb86d8a259e0e923aaab65a93f1ec5758b8799988894ac0958fd")
// h2a, hash of 2 elements
@@ -60,7 +48,7 @@ func TestMIMC7(t *testing.T) {
h2a, err := Hash(bigArray2a, nil)
assert.Nil(t, err)
// same hash value than the iden3js and circomlib tests:
assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2a).Bytes()),
assert.Equal(t, "0x"+hex.EncodeToString(h2a.Bytes()),
"0x067f3202335ea256ae6e6aadcd2d5f7f4b06a00b2d1e0de903980d5ab552dc70")
// h2b, hash of 2 elements
@@ -68,13 +56,13 @@ func TestMIMC7(t *testing.T) {
mh2b := MIMC7Hash(b12, b45)
assert.Nil(t, err)
assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(mh2b).Bytes()),
assert.Equal(t, "0x"+hex.EncodeToString(mh2b.Bytes()),
"0x2ba7ebad3c6b6f5a20bdecba2333c63173ca1a5f2f49d958081d9fa7179c44e4")
h2b, err := Hash(bigArray2b, nil)
assert.Nil(t, err)
// same hash value than the iden3js and circomlib tests:
assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2b).Bytes()),
assert.Equal(t, "0x"+hex.EncodeToString(h2b.Bytes()),
"0x15ff7fe9793346a17c3150804bcb36d161c8662b110c50f55ccb7113948d8879")
// h4, hash of 4 elements
@@ -83,7 +71,7 @@ func TestMIMC7(t *testing.T) {
h4, err := Hash(bigArray4, nil)
assert.Nil(t, err)
// same hash value than the iden3js and circomlib tests:
assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h4).Bytes()),
assert.Equal(t, "0x"+hex.EncodeToString(h4.Bytes()),
"0x284bc1f34f335933a23a433b6ff3ee179d682cd5e5e2fcdd2d964afa85104beb")
msg := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.") //nolint:lll
@@ -101,6 +89,6 @@ func BenchmarkMIMC7(b *testing.B) {
bigArray4 := []*big.Int{b12, b45, b78, b41}
for i := 0; i < b.N; i++ {
Hash(bigArray4, nil) //nolint:errcheck,gosec
_, _ = Hash(bigArray4, nil)
}
}