arnaucube
arnaucube/go-iden3-cryptoPublic Self-hosted repository
go-iden3-crypto/mimc7/mimc7_test.go
mimc7/mimc7_test.goRaw
  1package mimc7
  2
  3import (
  4	"encoding/hex"
  5	"math/big"
  6	"testing"
  7
  8	"github.com/ethereum/go-ethereum/crypto"
  9	"github.com/iden3/go-iden3-crypto/field"
 10	"github.com/stretchr/testify/assert"
 11)
 12
 13func TestUtils(t *testing.T) {
 14	b1 := big.NewInt(int64(1))
 15	b2 := big.NewInt(int64(2))
 16	b3 := big.NewInt(int64(3))
 17	arrBigInt := []*big.Int{b1, b2, b3}
 18
 19	// *big.Int array to RElem array
 20	rElems, err := BigIntsToRElems(arrBigInt)
 21	assert.Nil(t, err)
 22
 23	// RElem array to *big.Int array
 24	bElems := RElemsToBigInts(rElems)
 25
 26	assert.Equal(t, arrBigInt, bElems)
 27
 28	r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
 29	assert.True(t, ok)
 30
 31	// greater or equal than R will give error when passing bigInts to RElems, to fit in the R Finite Field
 32	overR, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495618", 10)
 33	assert.True(t, ok)
 34	_, err = BigIntsToRElems([]*big.Int{b1, overR, b2})
 35	assert.True(t, err != nil)
 36
 37	_, err = BigIntsToRElems([]*big.Int{b1, r, b2})
 38	assert.True(t, err != nil)
 39
 40	// smaller than R will not give error when passing bigInts to RElems, to fit in the R Finite Field
 41	underR, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495616", 10)
 42	assert.True(t, ok)
 43	_, err = BigIntsToRElems([]*big.Int{b1, underR, b2})
 44	assert.Nil(t, err)
 45}
 46
 47func TestKeccak256(t *testing.T) {
 48	res := crypto.Keccak256([]byte(SEED))
 49	assert.Equal(t, "b6e489e6b37224a50bebfddbe7d89fa8fdcaa84304a70bd13f79b5d9f7951e9e", hex.EncodeToString(res))
 50	c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
 51	assert.Equal(t, "82724731331859054037315113496710413141112897654334566532528783843265082629790", c.String())
 52}
 53
 54func TestMIMC7Generic(t *testing.T) {
 55	b1 := big.NewInt(int64(1))
 56	b2 := big.NewInt(int64(2))
 57	b3 := big.NewInt(int64(3))
 58
 59	r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
 60	assert.True(t, ok)
 61	fqR := field.NewFq(r)
 62
 63	bigArray := []*big.Int{b1, b2, b3}
 64	elementsArray, err := BigIntsToRElems(bigArray)
 65	assert.Nil(t, err)
 66
 67	// Generic Hash
 68	mhg := MIMC7HashGeneric(fqR, b1, b2, 91)
 69	assert.Nil(t, err)
 70	assert.Equal(t, "10594780656576967754230020536574539122676596303354946869887184401991294982664", mhg.String())
 71	hg, err := HashGeneric(fqR.Zero(), elementsArray, fqR, 91)
 72	assert.Nil(t, err)
 73	assert.Equal(t, "6464402164086696096195815557694604139393321133243036833927490113253119343397", (*big.Int)(hg).String())
 74}
 75
 76func TestMIMC7(t *testing.T) {
 77	b12 := big.NewInt(int64(12))
 78	b45 := big.NewInt(int64(45))
 79	b78 := big.NewInt(int64(78))
 80	b41 := big.NewInt(int64(41))
 81
 82	// h1, hash of 1 elements
 83	bigArray1 := []*big.Int{b12}
 84	elementsArray1, err := BigIntsToRElems(bigArray1)
 85	assert.Nil(t, err)
 86
 87	h1 := Hash(elementsArray1, nil)
 88	assert.Nil(t, err)
 89	// same hash value than the iden3js and circomlib tests:
 90	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h1).Bytes()), "0x237c92644dbddb86d8a259e0e923aaab65a93f1ec5758b8799988894ac0958fd")
 91
 92	// h2a, hash of 2 elements
 93	bigArray2a := []*big.Int{b78, b41}
 94	elementsArray2a, err := BigIntsToRElems(bigArray2a)
 95	assert.Nil(t, err)
 96
 97	h2a := Hash(elementsArray2a, nil)
 98	assert.Nil(t, err)
 99	// same hash value than the iden3js and circomlib tests:
100	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2a).Bytes()), "0x067f3202335ea256ae6e6aadcd2d5f7f4b06a00b2d1e0de903980d5ab552dc70")
101
102	// h2b, hash of 2 elements
103	bigArray2b := []*big.Int{b12, b45}
104	elementsArray2b, err := BigIntsToRElems(bigArray2b)
105	assert.Nil(t, err)
106
107	mh2b := MIMC7Hash(b12, b45)
108	assert.Nil(t, err)
109	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(mh2b).Bytes()), "0x2ba7ebad3c6b6f5a20bdecba2333c63173ca1a5f2f49d958081d9fa7179c44e4")
110
111	h2b := Hash(elementsArray2b, nil)
112	assert.Nil(t, err)
113	// same hash value than the iden3js and circomlib tests:
114	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2b).Bytes()), "0x15ff7fe9793346a17c3150804bcb36d161c8662b110c50f55ccb7113948d8879")
115
116	// h4, hash of 4 elements
117	bigArray4 := []*big.Int{b12, b45, b78, b41}
118	elementsArray4, err := BigIntsToRElems(bigArray4)
119	assert.Nil(t, err)
120
121	h4 := Hash(elementsArray4, nil)
122	assert.Nil(t, err)
123	// same hash value than the iden3js and circomlib tests:
124	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h4).Bytes()), "0x284bc1f34f335933a23a433b6ff3ee179d682cd5e5e2fcdd2d964afa85104beb")
125}
126
127func BenchmarkMIMC7(b *testing.B) {
128	b12 := big.NewInt(int64(12))
129	b45 := big.NewInt(int64(45))
130	b78 := big.NewInt(int64(78))
131	b41 := big.NewInt(int64(41))
132	bigArray4 := []*big.Int{b12, b45, b78, b41}
133	elementsArray4, err := BigIntsToRElems(bigArray4)
134	assert.Nil(b, err)
135
136	var h4 RElem
137	for i := 0; i < b.N; i++ {
138		h4 = Hash(elementsArray4, nil)
139	}
140	println(h4)
141}