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