go-iden3-crypto/mimc7/mimc7.go
mimc7/mimc7.goRaw
1package mimc7
2
3import (
4 "errors"
5 "fmt"
6 "math/big"
7
8 "github.com/ethereum/go-ethereum/crypto"
9 _constants "github.com/iden3/go-iden3-crypto/constants"
10 "github.com/iden3/go-iden3-crypto/field"
11)
12
13const SEED = "mimc"
14
15// RElem is a big.Int of maximum 253 bits
16type RElem *big.Int
17
18var constants = generateConstantsData()
19
20type constantsData struct {
21 maxFieldVal *big.Int
22 seedHash *big.Int
23 iv *big.Int
24 fqR field.Fq
25 nRounds int
26 cts []*big.Int
27}
28
29func generateConstantsData() constantsData {
30 var constants constantsData
31
32 fqR := field.NewFq(_constants.Q)
33 constants.fqR = fqR
34
35 // maxFieldVal is the R value of the Finite Field
36 constants.maxFieldVal = constants.fqR.Q
37
38 constants.seedHash = new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
39 c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED + "_iv")))
40 constants.iv = new(big.Int).Mod(c, constants.maxFieldVal)
41
42 constants.nRounds = 91
43 cts := getConstants(constants.fqR, SEED, constants.nRounds)
44 constants.cts = cts
45 return constants
46}
47
48// BigIntToRElem checks if given big.Int fits in a Field R element, and returns the RElem type
49func BigIntToRElem(a *big.Int) (RElem, error) {
50 if a.Cmp(constants.maxFieldVal) != -1 {
51 return RElem(a), errors.New("Given big.Int don't fits in the Finite Field over R")
52 }
53 return RElem(a), nil
54}
55
56//BigIntsToRElems converts from array of *big.Int to array of RElem
57func BigIntsToRElems(arr []*big.Int) ([]RElem, error) {
58 o := make([]RElem, len(arr))
59 for i, a := range arr {
60 e, err := BigIntToRElem(a)
61 if err != nil {
62 return o, fmt.Errorf("element in position %v don't fits in Finite Field over R", i)
63 }
64 o[i] = e
65 }
66 return o, nil
67}
68
69// RElemsToBigInts converts from array of RElem to array of *big.Int
70func RElemsToBigInts(arr []RElem) []*big.Int {
71 o := make([]*big.Int, len(arr))
72 for i, a := range arr {
73 o[i] = a
74 }
75 return o
76}
77
78func getConstants(fqR field.Fq, seed string, nRounds int) []*big.Int {
79 cts := make([]*big.Int, nRounds)
80 cts[0] = big.NewInt(int64(0))
81 c := new(big.Int).SetBytes(crypto.Keccak256([]byte(SEED)))
82 for i := 1; i < nRounds; i++ {
83 c = new(big.Int).SetBytes(crypto.Keccak256(c.Bytes()))
84
85 n := fqR.Affine(c)
86 cts[i] = n
87 }
88 return cts
89}
90
91// MIMC7HashGeneric performs the MIMC7 hash over a RElem, in a generic way, where it can be specified the Finite Field over R, and the number of rounds
92func MIMC7HashGeneric(fqR field.Fq, xIn, k *big.Int, nRounds int) *big.Int {
93 cts := getConstants(fqR, SEED, nRounds)
94 var r *big.Int
95 for i := 0; i < nRounds; i++ {
96 var t *big.Int
97 if i == 0 {
98 t = fqR.Add(xIn, k)
99 } else {
100 t = fqR.Add(fqR.Add(r, k), cts[i])
101 }
102 t2 := fqR.Square(t)
103 t4 := fqR.Square(t2)
104 r = fqR.Mul(fqR.Mul(t4, t2), t)
105 }
106 return fqR.Affine(fqR.Add(r, k))
107}
108
109// HashGeneric performs the MIMC7 hash over a RElem array, in a generic way, where it can be specified the Finite Field over R, and the number of rounds
110func HashGeneric(iv *big.Int, arrEl []RElem, fqR field.Fq, nRounds int) (RElem, error) {
111 arr := RElemsToBigInts(arrEl)
112 r := iv
113 var err error
114 for i := 0; i < len(arr); i++ {
115 r = MIMC7HashGeneric(fqR, r, arr[i], nRounds)
116 if err != nil {
117 return r, err
118 }
119 }
120 return RElem(r), nil
121}
122
123// MIMC7Hash performs the MIMC7 hash over a RElem, using the Finite Field over R and the number of rounds setted in the `constants` variable
124func MIMC7Hash(xIn, k *big.Int) *big.Int {
125 var r *big.Int
126 for i := 0; i < constants.nRounds; i++ {
127 var t *big.Int
128 if i == 0 {
129 t = constants.fqR.Add(xIn, k)
130 } else {
131 t = constants.fqR.Add(constants.fqR.Add(r, k), constants.cts[i])
132 }
133 t2 := constants.fqR.Square(t)
134 t4 := constants.fqR.Square(t2)
135 r = constants.fqR.Mul(constants.fqR.Mul(t4, t2), t)
136 }
137 return constants.fqR.Affine(constants.fqR.Add(r, k))
138}
139
140// Hash performs the MIMC7 hash over a RElem array
141func Hash(arrEl []RElem, key *big.Int) RElem {
142 arr := RElemsToBigInts(arrEl)
143 var r *big.Int
144 if key == nil {
145 r = constants.fqR.Zero()
146 } else {
147 r = key
148 }
149 // r := constants.iv
150 for i := 0; i < len(arr); i++ {
151 r = constants.fqR.Add(
152 constants.fqR.Add(
153 r,
154 arr[i],
155 ),
156 MIMC7Hash(arr[i], r))
157 }
158 return RElem(r)
159}