/
encrypt.go
175 lines (150 loc) · 4.53 KB
/
encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright © 2020 ConsenSys
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mimc
import (
"github.com/consensys/gnark-crypto/ecc"
bls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/mimc"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/mimc"
bls24315 "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/mimc"
bls24317 "github.com/consensys/gnark-crypto/ecc/bls24-317/fr/mimc"
bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
bw6633 "github.com/consensys/gnark-crypto/ecc/bw6-633/fr/mimc"
bw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/mimc"
"github.com/consensys/gnark/frontend"
)
var encryptFuncs map[ecc.ID]func(MiMC, frontend.Variable) frontend.Variable
var newMimc map[ecc.ID]func(frontend.API) MiMC
func init() {
encryptFuncs = make(map[ecc.ID]func(MiMC, frontend.Variable) frontend.Variable)
encryptFuncs[ecc.BN254] = encryptPow5
encryptFuncs[ecc.BLS12_381] = encryptPow5
encryptFuncs[ecc.BLS12_377] = encryptPow17
encryptFuncs[ecc.BW6_761] = encryptPow5
encryptFuncs[ecc.BW6_633] = encryptPow5
encryptFuncs[ecc.BLS24_315] = encryptPow5
encryptFuncs[ecc.BLS24_317] = encryptPow7
newMimc = make(map[ecc.ID]func(frontend.API) MiMC)
newMimc[ecc.BN254] = newMimcBN254
newMimc[ecc.BLS12_381] = newMimcBLS381
newMimc[ecc.BLS12_377] = newMimcBLS377
newMimc[ecc.BW6_761] = newMimcBW761
newMimc[ecc.BW6_633] = newMimcBW633
newMimc[ecc.BLS24_315] = newMimcBLS315
newMimc[ecc.BLS24_317] = newMimcBLS317
}
// -------------------------------------------------------------------------------------------------
// constructors
func newMimcBLS377(api frontend.API) MiMC {
res := MiMC{}
res.params = bls12377.GetConstants()
res.id = ecc.BLS12_377
res.h = 0
res.api = api
return res
}
func newMimcBLS381(api frontend.API) MiMC {
res := MiMC{}
res.params = bls12381.GetConstants()
res.id = ecc.BLS12_381
res.h = 0
res.api = api
return res
}
func newMimcBN254(api frontend.API) MiMC {
res := MiMC{}
res.params = bn254.GetConstants()
res.id = ecc.BN254
res.h = 0
res.api = api
return res
}
func newMimcBW761(api frontend.API) MiMC {
res := MiMC{}
res.params = bw6761.GetConstants()
res.id = ecc.BW6_761
res.h = 0
res.api = api
return res
}
func newMimcBLS317(api frontend.API) MiMC {
res := MiMC{}
res.params = bls24317.GetConstants()
res.id = ecc.BLS24_317
res.h = 0
res.api = api
return res
}
func newMimcBLS315(api frontend.API) MiMC {
res := MiMC{}
res.params = bls24315.GetConstants()
res.id = ecc.BLS24_315
res.h = 0
res.api = api
return res
}
func newMimcBW633(api frontend.API) MiMC {
res := MiMC{}
res.params = bw6633.GetConstants()
res.id = ecc.BW6_633
res.h = 0
res.api = api
return res
}
// -------------------------------------------------------------------------------------------------
// encryptions functions
func pow5(api frontend.API, x frontend.Variable) frontend.Variable {
r := api.Mul(x, x)
r = api.Mul(r, r)
return api.Mul(r, x)
}
func pow7(api frontend.API, x frontend.Variable) frontend.Variable {
t := api.Mul(x, x)
r := api.Mul(t, t)
r = api.Mul(r, t)
return api.Mul(r, x)
}
func pow17(api frontend.API, x frontend.Variable) frontend.Variable {
r := api.Mul(x, x)
r = api.Mul(r, r)
r = api.Mul(r, r)
r = api.Mul(r, r)
return api.Mul(r, x)
}
// encryptBn256 of a mimc run expressed as r1cs
// m is the message, k the key
func encryptPow5(h MiMC, m frontend.Variable) frontend.Variable {
x := m
for i := 0; i < len(h.params); i++ {
x = pow5(h.api, h.api.Add(x, h.h, h.params[i]))
}
return h.api.Add(x, h.h)
}
// encryptBLS24317 of a mimc run expressed as r1cs
// m is the message, k the key
func encryptPow7(h MiMC, m frontend.Variable) frontend.Variable {
x := m
for i := 0; i < len(h.params); i++ {
x = pow7(h.api, h.api.Add(x, h.h, h.params[i]))
}
return h.api.Add(x, h.h)
}
// encryptBLS377 of a mimc run expressed as r1cs
// m is the message, k the key
func encryptPow17(h MiMC, m frontend.Variable) frontend.Variable {
x := m
for i := 0; i < len(h.params); i++ {
// res = (res+key+c)**17
x = pow17(h.api, h.api.Add(x, h.h, h.params[i]))
}
return h.api.Add(x, h.h)
}