-
Notifications
You must be signed in to change notification settings - Fork 150
/
pairing.go
367 lines (327 loc) · 8.67 KB
/
pairing.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
// Copyright 2020 ConsenSys AG
//
// 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 bw6761
import (
"errors"
"github.com/consensys/gnark-crypto/ecc/bw6-761/fp"
"github.com/consensys/gnark-crypto/ecc/bw6-761/internal/fptower"
)
// GT target group of the pairing
type GT = fptower.E6
type lineEvaluation struct {
r0 fp.Element
r1 fp.Element
r2 fp.Element
}
// Pair calculates the reduced pairing for a set of points
func Pair(P []G1Affine, Q []G2Affine) (GT, error) {
f, err := MillerLoop(P, Q)
if err != nil {
return GT{}, err
}
return FinalExponentiation(&f), nil
}
// PairingCheck calculates the reduced pairing for a set of points and returns True if the result is One
func PairingCheck(P []G1Affine, Q []G2Affine) (bool, error) {
f, err := Pair(P, Q)
if err != nil {
return false, err
}
var one GT
one.SetOne()
return f.Equal(&one), nil
}
// FinalExponentiation computes the final expo x**(c*(p**3-1)(p+1)(p**2-p+1)/r)
func FinalExponentiation(z *GT, _z ...*GT) GT {
var result GT
result.Set(z)
for _, e := range _z {
result.Mul(&result, e)
}
var buf GT
// easy part exponent: (p**3 - 1)*(p+1)
buf.Conjugate(&result)
result.Inverse(&result)
buf.Mul(&buf, &result)
result.Frobenius(&buf).
Mul(&result, &buf)
// hard part exponent: 12(u+1)(p**2 - p + 1)/r
var m1, _m1, m2, _m2, m3, f0, f0_36, g0, g1, _g1, g2, g3, _g3, g4, _g4, g5, _g5, g6, gA, gB, g034, _g1g2, gC, h1, h2, h2g2C, h4 GT
m1.Expt(&result)
_m1.Conjugate(&m1)
m2.Expt(&m1)
_m2.Conjugate(&m2)
m3.Expt(&m2)
f0.Frobenius(&result).
Mul(&f0, &result).
Mul(&f0, &m2)
m2.CyclotomicSquare(&_m1)
f0.Mul(&f0, &m2)
f0_36.CyclotomicSquare(&f0).
CyclotomicSquare(&f0_36).
CyclotomicSquare(&f0_36).
Mul(&f0_36, &f0).
CyclotomicSquare(&f0_36).
CyclotomicSquare(&f0_36)
g0.Mul(&result, &m1).
Frobenius(&g0).
Mul(&g0, &m3).
Mul(&g0, &_m2).
Mul(&g0, &_m1)
g1.Expt(&g0)
_g1.Conjugate(&g1)
g2.Expt(&g1)
g3.Expt(&g2)
_g3.Conjugate(&g3)
g4.Expt(&g3)
_g4.Conjugate(&g4)
g5.Expt(&g4)
_g5.Conjugate(&g5)
g6.Expt(&g5)
gA.Mul(&g3, &_g5).
CyclotomicSquare(&gA).
Mul(&gA, &g6).
Mul(&gA, &g1).
Mul(&gA, &g0)
g034.Mul(&g0, &g3).
Mul(&g034, &_g4)
gB.CyclotomicSquare(&g034).
Mul(&gB, &g034).
Mul(&gB, &g5).
Mul(&gB, &_g1)
_g1g2.Mul(&_g1, &g2)
gC.Mul(&_g3, &_g1g2).
CyclotomicSquare(&gC).
Mul(&gC, &_g1g2).
Mul(&gC, &g0).
CyclotomicSquare(&gC).
Mul(&gC, &g2).
Mul(&gC, &g0).
Mul(&gC, &g4)
// ht, hy = 13, 9
// c1 = ht**2+3*hy**2 = 412
h1.Expc1(&gA)
// c2 = ht+hy = 22
h2.Expc2(&gB)
h2g2C.CyclotomicSquare(&gC).
Mul(&h2g2C, &h2)
h4.CyclotomicSquare(&h2g2C).
Mul(&h4, &h2g2C).
CyclotomicSquare(&h4)
result.Mul(&h1, &h4).
Mul(&result, &f0_36)
return result
}
// MillerLoop Optimal Tate alternative (or twisted ate or Eta revisited)
// Alg.2 in https://eprint.iacr.org/2021/1359.pdf
// Eq. (6) in https://hackmd.io/@yelhousni/BW6-761-changes
func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
// check input size match
n := len(P)
if n == 0 || n != len(Q) {
return GT{}, errors.New("invalid inputs sizes")
}
// filter infinity points
p0 := make([]G1Affine, 0, n)
q := make([]G2Affine, 0, n)
for k := 0; k < n; k++ {
if P[k].IsInfinity() || Q[k].IsInfinity() {
continue
}
p0 = append(p0, P[k])
q = append(q, Q[k])
}
n = len(q)
// precomputations
pProj1 := make([]g1Proj, n)
p1 := make([]G1Affine, n)
p01 := make([]G1Affine, n)
p10 := make([]G1Affine, n)
pProj01 := make([]g1Proj, n) // P0+P1
pProj10 := make([]g1Proj, n) // P0-P1
l01 := make([]lineEvaluation, n)
l10 := make([]lineEvaluation, n)
for k := 0; k < n; k++ {
p1[k].Y.Neg(&p0[k].Y)
p1[k].X.Mul(&p0[k].X, &thirdRootOneG2)
pProj1[k].FromAffine(&p1[k])
// l_{p0,p1}(q)
pProj01[k].Set(&pProj1[k])
pProj01[k].AddMixedStep(&l01[k], &p0[k])
l01[k].r1.Mul(&l01[k].r1, &q[k].X)
l01[k].r0.Mul(&l01[k].r0, &q[k].Y)
// l_{p0,-p1}(q)
pProj10[k].Neg(&pProj1[k])
pProj10[k].AddMixedStep(&l10[k], &p0[k])
l10[k].r1.Mul(&l10[k].r1, &q[k].X)
l10[k].r0.Mul(&l10[k].r0, &q[k].Y)
}
BatchProjectiveToAffineG1(pProj01, p01)
BatchProjectiveToAffineG1(pProj10, p10)
// f_{a0+lambda*a1,P}(Q)
var result, ss GT
result.SetOne()
var l, l0 lineEvaluation
var j int8
// i = 188
for k := 0; k < n; k++ {
pProj1[k].DoubleStep(&l0)
l0.r1.Mul(&l0.r1, &q[k].X)
l0.r0.Mul(&l0.r0, &q[k].Y)
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
}
var tmp G1Affine
for i := 187; i >= 0; i-- {
result.Square(&result)
j = loopCounter1[i]*3 + loopCounter0[i]
for k := 0; k < n; k++ {
pProj1[k].DoubleStep(&l0)
l0.r1.Mul(&l0.r1, &q[k].X)
l0.r0.Mul(&l0.r0, &q[k].Y)
switch j {
case -4:
tmp.Neg(&p01[k])
pProj1[k].AddMixedStep(&l, &tmp)
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
result.MulBy034(&l0.r0, &l0.r1, &l0.r2).
Mul(&result, &ss)
case -3:
tmp.Neg(&p1[k])
pProj1[k].AddMixedStep(&l, &tmp)
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
result.Mul(&result, &ss)
case -2:
pProj1[k].AddMixedStep(&l, &p10[k])
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
result.MulBy034(&l0.r0, &l0.r1, &l0.r2).
Mul(&result, &ss)
case -1:
tmp.Neg(&p0[k])
pProj1[k].AddMixedStep(&l, &tmp)
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
result.Mul(&result, &ss)
case 0:
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
case 1:
pProj1[k].AddMixedStep(&l, &p0[k])
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
result.Mul(&result, &ss)
case 2:
tmp.Neg(&p10[k])
pProj1[k].AddMixedStep(&l, &tmp)
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
result.MulBy034(&l0.r0, &l0.r1, &l0.r2).
Mul(&result, &ss)
case 3:
pProj1[k].AddMixedStep(&l, &p1[k])
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
result.Mul(&result, &ss)
case 4:
pProj1[k].AddMixedStep(&l, &p01[k])
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
ss.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
result.MulBy034(&l0.r0, &l0.r1, &l0.r2).
Mul(&result, &ss)
default:
return GT{}, errors.New("invalid loopCounter")
}
}
}
return result, nil
}
// DoubleStep doubles a point in Homogenous projective coordinates, and evaluates the line in Miller loop
// https://eprint.iacr.org/2013/722.pdf (Section 4.3)
func (p *g1Proj) DoubleStep(evaluations *lineEvaluation) {
// get some Element from our pool
var t1, A, B, C, D, E, EE, F, G, H, I, J, K fp.Element
A.Mul(&p.x, &p.y)
A.Halve()
B.Square(&p.y)
C.Square(&p.z)
D.Double(&C).
Add(&D, &C)
// E.Mul(&D, &bCurveCoeff)
E.Neg(&D)
F.Double(&E).
Add(&F, &E)
G.Add(&B, &F)
G.Halve()
H.Add(&p.y, &p.z).
Square(&H)
t1.Add(&B, &C)
H.Sub(&H, &t1)
I.Sub(&E, &B)
J.Square(&p.x)
EE.Square(&E)
K.Double(&EE).
Add(&K, &EE)
// X, Y, Z
p.x.Sub(&B, &F).
Mul(&p.x, &A)
p.y.Square(&G).
Sub(&p.y, &K)
p.z.Mul(&B, &H)
// Line evaluation
evaluations.r0.Neg(&H)
evaluations.r1.Double(&J).
Add(&evaluations.r1, &J)
evaluations.r2.Set(&I)
}
// AddMixedStep point addition in Mixed Homogenous projective and Affine coordinates
// https://eprint.iacr.org/2013/722.pdf (Section 4.3)
func (p *g1Proj) AddMixedStep(evaluations *lineEvaluation, a *G1Affine) {
// get some Element from our pool
var Y2Z1, X2Z1, O, L, C, D, E, F, G, H, t0, t1, t2, J fp.Element
Y2Z1.Mul(&a.Y, &p.z)
O.Sub(&p.y, &Y2Z1)
X2Z1.Mul(&a.X, &p.z)
L.Sub(&p.x, &X2Z1)
C.Square(&O)
D.Square(&L)
E.Mul(&L, &D)
F.Mul(&p.z, &C)
G.Mul(&p.x, &D)
t0.Double(&G)
H.Add(&E, &F).
Sub(&H, &t0)
t1.Mul(&p.y, &E)
// X, Y, Z
p.x.Mul(&L, &H)
p.y.Sub(&G, &H).
Mul(&p.y, &O).
Sub(&p.y, &t1)
p.z.Mul(&E, &p.z)
t2.Mul(&L, &a.Y)
J.Mul(&a.X, &O).
Sub(&J, &t2)
// Line evaluation
evaluations.r0.Set(&L)
evaluations.r1.Neg(&O)
evaluations.r2.Set(&J)
}