forked from Consensys/gnark
/
e6.go
377 lines (296 loc) · 7.88 KB
/
e6.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
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 fields_bls12377
import (
"math/big"
bls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377"
"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
)
// E6 element in a quadratic extension
type E6 struct {
B0, B1, B2 E2
}
// SetZero returns a newly allocated element equal to 0
func (e *E6) SetZero() *E6 {
e.B0.SetZero()
e.B1.SetZero()
e.B2.SetZero()
return e
}
// SetOne returns a newly allocated element equal to 1
func (e *E6) SetOne() *E6 {
e.B0.SetOne()
e.B1.SetZero()
e.B2.SetZero()
return e
}
func (e *E6) assign(e1 []frontend.Variable) {
e.B0.A0 = e1[0]
e.B0.A1 = e1[1]
e.B1.A0 = e1[2]
e.B1.A1 = e1[3]
e.B2.A0 = e1[4]
e.B2.A1 = e1[5]
}
// Add creates a fp6elmt from fp elmts
func (e *E6) Add(api frontend.API, e1, e2 E6) *E6 {
e.B0.Add(api, e1.B0, e2.B0)
e.B1.Add(api, e1.B1, e2.B1)
e.B2.Add(api, e1.B2, e2.B2)
return e
}
// NewFp6Zero creates a new
func NewFp6Zero(api frontend.API) *E6 {
return &E6{
B0: E2{0, 0},
B1: E2{0, 0},
B2: E2{0, 0},
}
}
// Sub creates a fp6elmt from fp elmts
func (e *E6) Sub(api frontend.API, e1, e2 E6) *E6 {
e.B0.Sub(api, e1.B0, e2.B0)
e.B1.Sub(api, e1.B1, e2.B1)
e.B2.Sub(api, e1.B2, e2.B2)
return e
}
// Neg negates an Fp6 elmt
func (e *E6) Neg(api frontend.API, e1 E6) *E6 {
e.B0.Neg(api, e1.B0)
e.B1.Neg(api, e1.B1)
e.B2.Neg(api, e1.B2)
return e
}
// Mul creates a fp6elmt from fp elmts
// icube is the imaginary elmt to the cube
func (e *E6) Mul(api frontend.API, e1, e2 E6) *E6 {
// Algorithm 13 from https://eprint.iacr.org/2010/354.pdf
var t0, t1, t2, c0, c1, c2, tmp E2
t0.Mul(api, e1.B0, e2.B0)
t1.Mul(api, e1.B1, e2.B1)
t2.Mul(api, e1.B2, e2.B2)
c0.Add(api, e1.B1, e1.B2)
tmp.Add(api, e2.B1, e2.B2)
c0.Mul(api, c0, tmp).Sub(api, c0, t1).Sub(api, c0, t2).MulByNonResidue(api, c0).Add(api, c0, t0)
c1.Add(api, e1.B0, e1.B1)
tmp.Add(api, e2.B0, e2.B1)
c1.Mul(api, c1, tmp).Sub(api, c1, t0).Sub(api, c1, t1)
tmp.MulByNonResidue(api, t2)
c1.Add(api, c1, tmp)
tmp.Add(api, e1.B0, e1.B2)
c2.Add(api, e2.B0, e2.B2).Mul(api, c2, tmp).Sub(api, c2, t0).Sub(api, c2, t2).Add(api, c2, t1)
e.B0 = c0
e.B1 = c1
e.B2 = c2
return e
}
func (e *E6) Mul0By01(api frontend.API, a0, b0, b1 E2) *E6 {
var t0, c1 E2
t0.Mul(api, a0, b0)
c1.Add(api, b0, b1)
c1.Mul(api, c1, a0).Sub(api, c1, t0)
e.B0 = t0
e.B1 = c1
e.B2 = E2{0, 0}
return e
}
// MulByFp2 creates a fp6elmt from fp elmts
// icube is the imaginary elmt to the cube
func (e *E6) MulByFp2(api frontend.API, e1 E6, e2 E2) *E6 {
res := E6{}
res.B0.Mul(api, e1.B0, e2)
res.B1.Mul(api, e1.B1, e2)
res.B2.Mul(api, e1.B2, e2)
e.B0 = res.B0
e.B1 = res.B1
e.B2 = res.B2
return e
}
// MulByNonResidue multiplies e by the imaginary elmt of Fp6 (noted a+bV+cV where V**3 in F²)
func (e *E6) MulByNonResidue(api frontend.API, e1 E6) *E6 {
res := E6{}
res.B0.MulByNonResidue(api, e1.B2)
e.B1 = e1.B0
e.B2 = e1.B1
e.B0 = res.B0
return e
}
// Square sets z to the E6 product of x,x, returns e
func (e *E6) Square(api frontend.API, x E6) *E6 {
// Algorithm 16 from https://eprint.iacr.org/2010/354.pdf
var c4, c5, c1, c2, c3, c0 E2
c4.Mul(api, x.B0, x.B1).Double(api, c4)
c5.Square(api, x.B2)
c1.MulByNonResidue(api, c5).Add(api, c1, c4)
c2.Sub(api, c4, c5)
c3.Square(api, x.B0)
c4.Sub(api, x.B0, x.B1).Add(api, c4, x.B2)
c5.Mul(api, x.B1, x.B2).Double(api, c5)
c4.Square(api, c4)
c0.MulByNonResidue(api, c5).Add(api, c0, c3)
e.B2.Add(api, c2, c4).Add(api, e.B2, c5).Sub(api, e.B2, c3)
e.B0 = c0
e.B1 = c1
return e
}
var DivE6Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, b, c bls12377.E6
a.B0.A0.SetBigInt(inputs[0])
a.B0.A1.SetBigInt(inputs[1])
a.B1.A0.SetBigInt(inputs[2])
a.B1.A1.SetBigInt(inputs[3])
a.B2.A0.SetBigInt(inputs[4])
a.B2.A1.SetBigInt(inputs[5])
b.B0.A0.SetBigInt(inputs[6])
b.B0.A1.SetBigInt(inputs[7])
b.B1.A0.SetBigInt(inputs[8])
b.B1.A1.SetBigInt(inputs[9])
b.B2.A0.SetBigInt(inputs[10])
b.B2.A1.SetBigInt(inputs[11])
c.Inverse(&b).Mul(&c, &a)
c.B0.A0.BigInt(res[0])
c.B0.A1.BigInt(res[1])
c.B1.A0.BigInt(res[2])
c.B1.A1.BigInt(res[3])
c.B2.A0.BigInt(res[4])
c.B2.A1.BigInt(res[5])
return nil
}
func init() {
solver.RegisterHint(DivE6Hint)
}
// DivUnchecked e6 elmts
func (e *E6) DivUnchecked(api frontend.API, e1, e2 E6) *E6 {
res, err := api.NewHint(DivE6Hint, 6, e1.B0.A0, e1.B0.A1, e1.B1.A0, e1.B1.A1, e1.B2.A0, e1.B2.A1, e2.B0.A0, e2.B0.A1, e2.B1.A0, e2.B1.A1, e2.B2.A0, e2.B2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
}
var e3, one E6
e3.assign(res[:6])
one.SetOne()
// e1 == e3 * e2
e3.Mul(api, e3, e2)
e3.AssertIsEqual(api, e1)
e.assign(res[:6])
return e
}
var InverseE6Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, c bls12377.E6
a.B0.A0.SetBigInt(inputs[0])
a.B0.A1.SetBigInt(inputs[1])
a.B1.A0.SetBigInt(inputs[2])
a.B1.A1.SetBigInt(inputs[3])
a.B2.A0.SetBigInt(inputs[4])
a.B2.A1.SetBigInt(inputs[5])
c.Inverse(&a)
c.B0.A0.BigInt(res[0])
c.B0.A1.BigInt(res[1])
c.B1.A0.BigInt(res[2])
c.B1.A1.BigInt(res[3])
c.B2.A0.BigInt(res[4])
c.B2.A1.BigInt(res[5])
return nil
}
func init() {
solver.RegisterHint(InverseE6Hint)
}
// Inverse e6 elmts
func (e *E6) Inverse(api frontend.API, e1 E6) *E6 {
res, err := api.NewHint(InverseE6Hint, 6, e1.B0.A0, e1.B0.A1, e1.B1.A0, e1.B1.A1, e1.B2.A0, e1.B2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
}
var e3, one E6
e3.assign(res[:6])
one.SetOne()
// 1 == e3 * e1
e3.Mul(api, e3, e1)
e3.AssertIsEqual(api, one)
e.assign(res[:6])
return e
}
// Assign a value to self (witness assignment)
func (e *E6) Assign(a *bls12377.E6) {
e.B0.Assign(&a.B0)
e.B1.Assign(&a.B1)
e.B2.Assign(&a.B2)
}
// AssertIsEqual constraint self to be equal to other into the given constraint system
func (e *E6) AssertIsEqual(api frontend.API, other E6) {
e.B0.AssertIsEqual(api, other.B0)
e.B1.AssertIsEqual(api, other.B1)
e.B2.AssertIsEqual(api, other.B2)
}
// MulByE2 multiplies an element in E6 by an element in E2
func (e *E6) MulByE2(api frontend.API, e1 E6, e2 E2) *E6 {
e.B0.Mul(api, e1.B0, e2)
e.B1.Mul(api, e1.B1, e2)
e.B2.Mul(api, e1.B2, e2)
return e
}
// MulBy01 multiplication by sparse element (c0,c1,0)
func (e *E6) MulBy01(api frontend.API, c0, c1 E2) *E6 {
var a, b, tmp, t0, t1, t2 E2
a.Mul(api, e.B0, c0)
b.Mul(api, e.B1, c1)
tmp.Add(api, e.B1, e.B2)
t0.Mul(api, c1, tmp)
t0.Sub(api, t0, b)
t0.MulByNonResidue(api, t0)
t0.Add(api, t0, a)
tmp.Add(api, e.B0, e.B2)
t2.Mul(api, c0, tmp)
t2.Sub(api, t2, a)
t2.Add(api, t2, b)
t1.Add(api, c0, c1)
tmp.Add(api, e.B0, e.B1)
t1.Mul(api, t1, tmp)
t1.Sub(api, t1, a)
t1.Sub(api, t1, b)
e.B0 = t0
e.B1 = t1
e.B2 = t2
return e
}
func Mul01By01(api frontend.API, c0, c1, d0, d1 E2) *E6 {
var a, b, t0, t1, t2, tmp E2
a.Mul(api, d0, c0)
b.Mul(api, d1, c1)
t0.Mul(api, c1, d1)
t0.Sub(api, t0, b)
t0.MulByNonResidue(api, t0)
t0.Add(api, t0, a)
t2.Mul(api, c0, d0)
t2.Sub(api, t2, a)
t2.Add(api, t2, b)
t1.Add(api, c0, c1)
tmp.Add(api, d0, d1)
t1.Mul(api, t1, tmp)
t1.Sub(api, t1, a)
t1.Sub(api, t1, b)
return &E6{
B0: t0,
B1: t1,
B2: t2,
}
}
// Select sets e to r1 if b=1, r2 otherwise
func (e *E6) Select(api frontend.API, b frontend.Variable, r1, r2 E6) *E6 {
e.B0.Select(api, b, r1.B0, r2.B0)
e.B1.Select(api, b, r1.B1, r2.B1)
e.B2.Select(api, b, r1.B2, r2.B2)
return e
}