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