mirror of
https://github.com/arnaucube/go-iden3-crypto.git
synced 2026-02-07 11:36:41 +01:00
update babyjub B8, clean unused funcs & errs in mimc7, small update in mimc7 tests
This commit is contained in:
@@ -26,9 +26,6 @@ type constantsData struct {
|
||||
cts []*big.Int
|
||||
}
|
||||
|
||||
func getIV(seed string) {
|
||||
}
|
||||
|
||||
func generateConstantsData() constantsData {
|
||||
var constants constantsData
|
||||
|
||||
@@ -43,10 +40,7 @@ func generateConstantsData() constantsData {
|
||||
constants.iv = new(big.Int).Mod(c, constants.maxFieldVal)
|
||||
|
||||
constants.nRounds = 91
|
||||
cts, err := getConstants(constants.fqR, SEED, constants.nRounds)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cts := getConstants(constants.fqR, SEED, constants.nRounds)
|
||||
constants.cts = cts
|
||||
return constants
|
||||
}
|
||||
@@ -81,7 +75,7 @@ func RElemsToBigInts(arr []RElem) []*big.Int {
|
||||
return o
|
||||
}
|
||||
|
||||
func getConstants(fqR field.Fq, seed string, nRounds int) ([]*big.Int, error) {
|
||||
func getConstants(fqR field.Fq, seed string, nRounds int) []*big.Int {
|
||||
cts := make([]*big.Int, nRounds)
|
||||
cts[0] = big.NewInt(int64(0))
|
||||
c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
|
||||
@@ -91,15 +85,12 @@ func getConstants(fqR field.Fq, seed string, nRounds int) ([]*big.Int, error) {
|
||||
n := fqR.Affine(c)
|
||||
cts[i] = n
|
||||
}
|
||||
return cts, nil
|
||||
return cts
|
||||
}
|
||||
|
||||
// MIMC7HashGeneric performs the MIMC7 hash over a RElem, in a generic way, where it can be specified the Finite Field over R, and the number of rounds
|
||||
func MIMC7HashGeneric(fqR field.Fq, xIn, k *big.Int, nRounds int) (*big.Int, error) {
|
||||
cts, err := getConstants(fqR, SEED, nRounds)
|
||||
if err != nil {
|
||||
return &big.Int{}, err
|
||||
}
|
||||
func MIMC7HashGeneric(fqR field.Fq, xIn, k *big.Int, nRounds int) *big.Int {
|
||||
cts := getConstants(fqR, SEED, nRounds)
|
||||
var r *big.Int
|
||||
for i := 0; i < nRounds; i++ {
|
||||
var t *big.Int
|
||||
@@ -112,7 +103,7 @@ func MIMC7HashGeneric(fqR field.Fq, xIn, k *big.Int, nRounds int) (*big.Int, err
|
||||
t4 := fqR.Square(t2)
|
||||
r = fqR.Mul(fqR.Mul(t4, t2), t)
|
||||
}
|
||||
return fqR.Affine(fqR.Add(r, k)), nil
|
||||
return fqR.Affine(fqR.Add(r, k))
|
||||
}
|
||||
|
||||
// HashGeneric performs the MIMC7 hash over a RElem array, in a generic way, where it can be specified the Finite Field over R, and the number of rounds
|
||||
@@ -121,7 +112,7 @@ func HashGeneric(iv *big.Int, arrEl []RElem, fqR field.Fq, nRounds int) (RElem,
|
||||
r := iv
|
||||
var err error
|
||||
for i := 0; i < len(arr); i++ {
|
||||
r, err = MIMC7HashGeneric(fqR, r, arr[i], nRounds)
|
||||
r = MIMC7HashGeneric(fqR, r, arr[i], nRounds)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/iden3/go-iden3-crypto/field"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -43,6 +44,13 @@ func TestUtils(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestKeccak256(t *testing.T) {
|
||||
res := crypto.Keccak256([]byte(SEED))
|
||||
assert.Equal(t, "b6e489e6b37224a50bebfddbe7d89fa8fdcaa84304a70bd13f79b5d9f7951e9e", hex.EncodeToString(res))
|
||||
c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
|
||||
assert.Equal(t, "82724731331859054037315113496710413141112897654334566532528783843265082629790", c.String())
|
||||
}
|
||||
|
||||
func TestMIMC7Generic(t *testing.T) {
|
||||
b1 := big.NewInt(int64(1))
|
||||
b2 := big.NewInt(int64(2))
|
||||
@@ -57,7 +65,7 @@ func TestMIMC7Generic(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
// Generic Hash
|
||||
mhg, err := MIMC7HashGeneric(fqR, b1, b2, 91)
|
||||
mhg := MIMC7HashGeneric(fqR, b1, b2, 91)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "10594780656576967754230020536574539122676596303354946869887184401991294982664", mhg.String())
|
||||
hg, err := HashGeneric(fqR.Zero(), elementsArray, fqR, 91)
|
||||
@@ -86,10 +94,6 @@ func TestMIMC7(t *testing.T) {
|
||||
elementsArray2a, err := BigIntsToRElems(bigArray2a)
|
||||
assert.Nil(t, err)
|
||||
|
||||
mh2a := MIMC7Hash(b12, b45)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(mh2a).Bytes()), "0x2ba7ebad3c6b6f5a20bdecba2333c63173ca1a5f2f49d958081d9fa7179c44e4")
|
||||
|
||||
h2a := Hash(elementsArray2a, nil)
|
||||
assert.Nil(t, err)
|
||||
// same hash value than the iden3js and circomlib tests:
|
||||
|
||||
Reference in New Issue
Block a user