arnaucube
arnaucube/go-iden3-cryptoPublic Self-hosted repository
go-iden3-crypto/mimc7/mimc7.go
mimc7/mimc7.goRaw
  1package mimc7
  2
  3import (
  4	"errors"
  5	"math/big"
  6
  7	"github.com/ethereum/go-ethereum/crypto"
  8	_constants "github.com/iden3/go-iden3-crypto/constants"
  9	"github.com/iden3/go-iden3-crypto/ff"
 10	"github.com/iden3/go-iden3-crypto/utils"
 11)
 12
 13// SEED defines the seed used to constants
 14const SEED = "mimc"
 15
 16var constants = generateConstantsData()
 17
 18type constantsData struct {
 19	seedHash *big.Int
 20	iv       *big.Int
 21	nRounds  int
 22	cts      []*ff.Element
 23}
 24
 25func generateConstantsData() constantsData {
 26	var constants constantsData
 27
 28	constants.seedHash = new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
 29	c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED + "_iv")))
 30	constants.iv = new(big.Int).Mod(c, _constants.Q)
 31
 32	constants.nRounds = 91
 33	cts := getConstants(SEED, constants.nRounds)
 34	constants.cts = cts
 35	return constants
 36}
 37
 38func getConstants(seed string, nRounds int) []*ff.Element {
 39	cts := make([]*ff.Element, nRounds)
 40	cts[0] = ff.NewElement()
 41	c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
 42	for i := 1; i < nRounds; i++ {
 43		c = new(big.Int).SetBytes(crypto.Keccak256(c.Bytes()))
 44
 45		n := new(big.Int).Mod(c, _constants.Q)
 46		cts[i] = ff.NewElement().SetBigInt(n)
 47	}
 48	return cts
 49}
 50
 51// MIMC7HashGeneric performs the MIMC7 hash over a *big.Int, in a generic way,
 52// where it can be specified the Finite Field over R, and the number of rounds
 53func MIMC7HashGeneric(xInBI, kBI *big.Int, nRounds int) *big.Int { //nolint:golint
 54	xIn := ff.NewElement().SetBigInt(xInBI)
 55	k := ff.NewElement().SetBigInt(kBI)
 56
 57	cts := getConstants(SEED, nRounds)
 58	var r *ff.Element
 59	for i := 0; i < nRounds; i++ {
 60		var t *ff.Element
 61		if i == 0 {
 62			t = ff.NewElement().Add(xIn, k)
 63		} else {
 64			t = ff.NewElement().Add(ff.NewElement().Add(r, k), cts[i])
 65		}
 66		t2 := ff.NewElement().Square(t)
 67		t4 := ff.NewElement().Square(t2)
 68		r = ff.NewElement().Mul(ff.NewElement().Mul(t4, t2), t)
 69	}
 70	rE := ff.NewElement().Add(r, k)
 71
 72	res := big.NewInt(0)
 73	rE.ToBigIntRegular(res)
 74	return res
 75}
 76
 77// HashGeneric performs the MIMC7 hash over a *big.Int array, in a generic way,
 78// where it can be specified the Finite Field over R, and the number of rounds
 79func HashGeneric(iv *big.Int, arr []*big.Int, nRounds int) (*big.Int, error) {
 80	if !utils.CheckBigIntArrayInField(arr) {
 81		return nil, errors.New("inputs values not inside Finite Field")
 82	}
 83	r := iv
 84	var err error
 85	for i := 0; i < len(arr); i++ {
 86		r = MIMC7HashGeneric(r, arr[i], nRounds)
 87		if err != nil {
 88			return r, err
 89		}
 90	}
 91	return r, nil
 92}
 93
 94// MIMC7Hash performs the MIMC7 hash over a *big.Int, using the Finite Field
 95// over R and the number of rounds setted in the `constants` variable
 96func MIMC7Hash(xInBI, kBI *big.Int) *big.Int { //nolint:golint
 97	xIn := ff.NewElement().SetBigInt(xInBI)
 98	k := ff.NewElement().SetBigInt(kBI)
 99
100	var r *ff.Element
101	for i := 0; i < constants.nRounds; i++ {
102		var t *ff.Element
103		if i == 0 {
104			t = ff.NewElement().Add(xIn, k)
105		} else {
106			t = ff.NewElement().Add(ff.NewElement().Add(r, k), constants.cts[i])
107		}
108		t2 := ff.NewElement().Square(t)
109		t4 := ff.NewElement().Square(t2)
110		r = ff.NewElement().Mul(ff.NewElement().Mul(t4, t2), t)
111	}
112	rE := ff.NewElement().Add(r, k)
113
114	res := big.NewInt(0)
115	rE.ToBigIntRegular(res)
116	return res
117}
118
119// Hash performs the MIMC7 hash over a *big.Int array
120func Hash(arr []*big.Int, key *big.Int) (*big.Int, error) {
121	if !utils.CheckBigIntArrayInField(arr) {
122		return nil, errors.New("inputs values not inside Finite Field")
123	}
124	var r *big.Int
125	if key == nil {
126		r = big.NewInt(0)
127	} else {
128		r = key
129	}
130	for i := 0; i < len(arr); i++ {
131		r = new(big.Int).Add(
132			new(big.Int).Add(
133				r,
134				arr[i],
135			),
136			MIMC7Hash(arr[i], r))
137		r = new(big.Int).Mod(r, _constants.Q)
138	}
139	return r, nil
140}
141
142// HashBytes hashes a msg byte slice by blocks of 31 bytes encoded as
143// little-endian
144func HashBytes(b []byte) *big.Int {
145	n := 31
146	bElems := make([]*big.Int, 0, len(b)/n+1)
147	for i := 0; i < len(b)/n; i++ {
148		v := new(big.Int)
149		utils.SetBigIntFromLEBytes(v, b[n*i:n*(i+1)])
150		bElems = append(bElems, v)
151	}
152	if len(b)%n != 0 {
153		v := new(big.Int)
154		utils.SetBigIntFromLEBytes(v, b[(len(b)/n)*n:])
155		bElems = append(bElems, v)
156	}
157	h, err := Hash(bElems, nil)
158	if err != nil {
159		panic(err)
160	}
161	return h
162}