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/stretchr/testify/assert"
 10)
 11
 12func TestKeccak256(t *testing.T) {
 13	res := crypto.Keccak256([]byte(SEED))
 14	assert.Equal(t,
 15		"b6e489e6b37224a50bebfddbe7d89fa8fdcaa84304a70bd13f79b5d9f7951e9e",
 16		hex.EncodeToString(res))
 17	c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
 18	assert.Equal(t,
 19		"82724731331859054037315113496710413141112897654334566532528783843265082629790",
 20		c.String())
 21}
 22
 23func TestMIMC7Generic(t *testing.T) {
 24	b1 := big.NewInt(int64(1))
 25	b2 := big.NewInt(int64(2))
 26	b3 := big.NewInt(int64(3))
 27
 28	bigArray := []*big.Int{b1, b2, b3}
 29
 30	// Generic Hash
 31	mhg := MIMC7HashGeneric(b1, b2, 91)
 32	assert.Equal(t,
 33		"10594780656576967754230020536574539122676596303354946869887184401991294982664",
 34		mhg.String())
 35	hg, err := HashGeneric(big.NewInt(0), bigArray, 91)
 36	assert.Nil(t, err)
 37	assert.Equal(t,
 38		"6464402164086696096195815557694604139393321133243036833927490113253119343397",
 39		(*big.Int)(hg).String())
 40}
 41
 42func TestMIMC7(t *testing.T) {
 43	b12 := big.NewInt(int64(12))
 44	b45 := big.NewInt(int64(45))
 45	b78 := big.NewInt(int64(78))
 46	b41 := big.NewInt(int64(41))
 47
 48	// h1, hash of 1 elements
 49	bigArray1 := []*big.Int{b12}
 50
 51	h1, err := Hash(bigArray1, nil)
 52	assert.Nil(t, err)
 53	// same hash value than the iden3js and circomlib tests:
 54	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h1).Bytes()),
 55		"0x237c92644dbddb86d8a259e0e923aaab65a93f1ec5758b8799988894ac0958fd")
 56
 57	// h2a, hash of 2 elements
 58	bigArray2a := []*big.Int{b78, b41}
 59
 60	h2a, err := Hash(bigArray2a, nil)
 61	assert.Nil(t, err)
 62	// same hash value than the iden3js and circomlib tests:
 63	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2a).Bytes()),
 64		"0x067f3202335ea256ae6e6aadcd2d5f7f4b06a00b2d1e0de903980d5ab552dc70")
 65
 66	// h2b, hash of 2 elements
 67	bigArray2b := []*big.Int{b12, b45}
 68
 69	mh2b := MIMC7Hash(b12, b45)
 70	assert.Nil(t, err)
 71	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(mh2b).Bytes()),
 72		"0x2ba7ebad3c6b6f5a20bdecba2333c63173ca1a5f2f49d958081d9fa7179c44e4")
 73
 74	h2b, err := Hash(bigArray2b, nil)
 75	assert.Nil(t, err)
 76	// same hash value than the iden3js and circomlib tests:
 77	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h2b).Bytes()),
 78		"0x15ff7fe9793346a17c3150804bcb36d161c8662b110c50f55ccb7113948d8879")
 79
 80	// h4, hash of 4 elements
 81	bigArray4 := []*big.Int{b12, b45, b78, b41}
 82
 83	h4, err := Hash(bigArray4, nil)
 84	assert.Nil(t, err)
 85	// same hash value than the iden3js and circomlib tests:
 86	assert.Equal(t, "0x"+hex.EncodeToString((*big.Int)(h4).Bytes()),
 87		"0x284bc1f34f335933a23a433b6ff3ee179d682cd5e5e2fcdd2d964afa85104beb")
 88
 89	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
 90	hmsg := HashBytes(msg)
 91	assert.Equal(t,
 92		"16855787120419064316734350414336285711017110414939748784029922801367685456065",
 93		hmsg.String())
 94}
 95
 96func BenchmarkMIMC7(b *testing.B) {
 97	b12 := big.NewInt(int64(12))
 98	b45 := big.NewInt(int64(45))
 99	b78 := big.NewInt(int64(78))
100	b41 := big.NewInt(int64(41))
101	bigArray4 := []*big.Int{b12, b45, b78, b41}
102
103	for i := 0; i < b.N; i++ {
104		Hash(bigArray4, nil) //nolint:errcheck,gosec
105	}
106}