forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairing.go
369 lines (296 loc) · 9.03 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
368
369
/*
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 sw_bls24315
import (
"errors"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/algebra/native/fields_bls24315"
)
// GT target group of the pairing
type GT = fields_bls24315.E24
var loopCounter = [33]int8{
-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1,
}
// MillerLoop computes the product of n miller loops (n can be 1)
// ∏ᵢ { fᵢ_{x₀,Q}(P) }
func MillerLoop(api frontend.API, 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")
}
lines := make([]lineEvaluations, len(Q))
for i := range Q {
if Q[i].Lines == nil {
Qlines := computeLines(api, Q[i].P)
Q[i].Lines = Qlines
}
lines[i] = *Q[i].Lines
}
return millerLoopLines(api, P, lines)
}
// millerLoopLines computes the multi-Miller loop from points in G1 and precomputed lines in G2
func millerLoopLines(api frontend.API, P []G1Affine, lines []lineEvaluations) (GT, error) {
// check input size match
n := len(P)
if n == 0 || n != len(lines) {
return GT{}, errors.New("invalid inputs sizes")
}
var res GT
res.SetOne()
var l1, l2 lineEvaluation
// precomputations
yInv := make([]frontend.Variable, n)
xNegOverY := make([]frontend.Variable, n)
for k := 0; k < n; k++ {
yInv[k] = api.DivUnchecked(1, P[k].Y)
xNegOverY[k] = api.Mul(P[k].X, yInv[k])
xNegOverY[k] = api.Neg(xNegOverY[k])
}
// Compute ∏ᵢ { fᵢ_{x₀,Q}(P) }
for i := 31; i >= 0; i-- {
// mutualize the square among n Miller loops
// (∏ᵢfᵢ)²
res.Square(api, res)
for k := 0; k < n; k++ {
if loopCounter[i] == 0 {
// line evaluation at P
l1.R0.MulByFp(api,
lines[k][0][i].R0,
xNegOverY[k])
l1.R1.MulByFp(api,
lines[k][0][i].R1,
yInv[k])
// ℓ × res
res.MulBy034(api, l1.R0, l1.R1)
} else {
// line evaluation at P
l1.R0.MulByFp(api,
lines[k][0][i].R0,
xNegOverY[k])
l1.R1.MulByFp(api,
lines[k][0][i].R1,
yInv[k])
// ℓ × res
res.MulBy034(api, l1.R0, l1.R1)
// line evaluation at P
l2.R0.MulByFp(api,
lines[k][1][i].R0,
xNegOverY[k])
l2.R1.MulByFp(api,
lines[k][1][i].R1,
yInv[k])
// ℓ × res
res.MulBy034(api, l2.R0, l2.R1)
}
}
}
res.Conjugate(api, res)
return res, nil
}
// FinalExponentiation computes the exponentiation e1ᵈ
// where d = (p²⁴-1)/r = (p²⁴-1)/Φ₂₄(p) ⋅ Φ₂₄(p)/r = (p¹²-1)(p⁴+1)(p⁸ - p⁴ +1)/r
// we use instead d=s ⋅ (p¹²-1)(p⁴+1)(p⁸ - p⁴ +1)/r
// where s is the cofactor 3 (Hayashida et al.)
func FinalExponentiation(api frontend.API, e1 GT) GT {
result := e1
// https://eprint.iacr.org/2012/232.pdf, section 7
var t [9]GT
// easy part
// (p¹²-1)(p⁴+1)
t[0].Conjugate(api, result)
t[0].DivUnchecked(api, t[0], result)
result.FrobeniusQuad(api, t[0]).
Mul(api, result, t[0])
// hard part (api, up to permutation)
// Daiki Hayashida and Kenichiro Hayasaka
// and Tadanori Teruya
// https://eprint.iacr.org/2020/875.pdf
// 3(p⁸ - p⁴ +1)/r = (x₀-1)² * (x₀+p) * (x₀²+p²) * (x₀⁴+p⁴-1) + 3
t[0].CyclotomicSquare(api, result)
t[1].Expt(api, result)
t[2].Conjugate(api, result)
t[1].Mul(api, t[1], t[2])
t[2].Expt(api, t[1])
t[1].Conjugate(api, t[1])
t[1].Mul(api, t[1], t[2])
t[2].Expt(api, t[1])
t[1].Frobenius(api, t[1])
t[1].Mul(api, t[1], t[2])
result.Mul(api, result, t[0])
t[0].Expt(api, t[1])
t[2].Expt(api, t[0])
t[0].FrobeniusSquare(api, t[1])
t[2].Mul(api, t[0], t[2])
t[1].Expt(api, t[2])
t[1].Expt(api, t[1])
t[1].Expt(api, t[1])
t[1].Expt(api, t[1])
t[0].FrobeniusQuad(api, t[2])
t[0].Mul(api, t[0], t[1])
t[2].Conjugate(api, t[2])
t[0].Mul(api, t[0], t[2])
result.Mul(api, result, t[0])
return result
}
// PairingCheck calculates the reduced pairing for a set of points and returns True if the result is One
// ∏ᵢ e(Pᵢ, Qᵢ) =? 1
//
// This function doesn't check that the inputs are in the correct subgroup. See IsInSubGroup.
func Pair(api frontend.API, P []G1Affine, Q []G2Affine) (GT, error) {
f, err := MillerLoop(api, P, Q)
if err != nil {
return GT{}, err
}
return FinalExponentiation(api, f), nil
}
// PairingCheck calculates the reduced pairing for a set of points and asserts if the result is One
// ∏ᵢ e(Pᵢ, Qᵢ) =? 1
//
// This function doesn't check that the inputs are in the correct subgroups. See AssertIsOnG1 and AssertIsOnG2.
func PairingCheck(api frontend.API, P []G1Affine, Q []G2Affine) error {
f, err := Pair(api, P, Q)
if err != nil {
return err
}
var one GT
one.SetOne()
f.AssertIsEqual(api, one)
return nil
}
// doubleAndAddStep doubles p1 and adds p2 to the result in affine coordinates, and evaluates the line in Miller loop
// https://eprint.iacr.org/2022/1162 (Section 6.1)
func doubleAndAddStep(api frontend.API, p1, p2 *g2AffP) (g2AffP, *lineEvaluation, *lineEvaluation) {
var n, d, l1, l2, x3, x4, y4 fields_bls24315.E4
var line1, line2 lineEvaluation
var p g2AffP
// compute lambda1 = (y2-y1)/(x2-x1)
n.Sub(api, p1.Y, p2.Y)
d.Sub(api, p1.X, p2.X)
l1.DivUnchecked(api, n, d)
// x3 =lambda1**2-p1.x-p2.x
x3.Square(api, l1).
Sub(api, x3, p1.X).
Sub(api, x3, p2.X)
// omit y3 computation
// compute line1
line1.R0 = l1
line1.R1.Mul(api, l1, p1.X).Sub(api, line1.R1, p1.Y)
// compute lambda2 = -lambda1-2*y1/(x3-x1)
n.Double(api, p1.Y)
d.Sub(api, x3, p1.X)
l2.DivUnchecked(api, n, d)
l2.Add(api, l2, l1).Neg(api, l2)
// compute x4 = lambda2**2-x1-x3
x4.Square(api, l2).
Sub(api, x4, p1.X).
Sub(api, x4, x3)
// compute y4 = lambda2*(x1 - x4)-y1
y4.Sub(api, p1.X, x4).
Mul(api, l2, y4).
Sub(api, y4, p1.Y)
p.X = x4
p.Y = y4
// compute line2
line2.R0 = l2
line2.R1.Mul(api, l2, p1.X).Sub(api, line2.R1, p1.Y)
return p, &line1, &line2
}
// doubleStep doubles a point in affine coordinates, and evaluates the line in Miller loop
// https://eprint.iacr.org/2022/1162 (Section 6.1)
func doubleStep(api frontend.API, p1 *g2AffP) (g2AffP, *lineEvaluation) {
var n, d, l, xr, yr fields_bls24315.E4
var p g2AffP
var line lineEvaluation
// lambda = 3*p1.x**2/2*p.y
n.Square(api, p1.X).MulByFp(api, n, 3)
d.MulByFp(api, p1.Y, 2)
l.DivUnchecked(api, n, d)
// xr = lambda**2-2*p1.x
xr.Square(api, l).
Sub(api, xr, p1.X).
Sub(api, xr, p1.X)
// yr = lambda*(p.x-xr)-p.y
yr.Sub(api, p1.X, xr).
Mul(api, l, yr).
Sub(api, yr, p1.Y)
p.X = xr
p.Y = yr
line.R0 = l
line.R1.Mul(api, l, p1.X).Sub(api, line.R1, p1.Y)
return p, &line
}
// addStep adds two points in affine coordinates, and evaluates the line in Miller loop
// https://eprint.iacr.org/2022/1162 (Section 6.1)
func addStep(api frontend.API, p1, p2 *g2AffP) (g2AffP, *lineEvaluation) {
var p2ypy, p2xpx, λ, λλ, pxrx, λpxrx, xr, yr fields_bls24315.E4
// compute λ = (y2-y1)/(x2-x1)
p2ypy.Sub(api, p2.Y, p1.Y)
p2xpx.Sub(api, p2.X, p1.X)
λ.DivUnchecked(api, p2ypy, p2xpx)
// xr = λ²-x1-x2
λλ.Square(api, λ)
p2xpx.Add(api, p1.X, p2.X)
xr.Sub(api, λλ, p2xpx)
// yr = λ(x1-xr) - y1
pxrx.Sub(api, p1.X, xr)
λpxrx.Mul(api, λ, pxrx)
yr.Sub(api, λpxrx, p1.Y)
var res g2AffP
res.X = xr
res.Y = yr
var line lineEvaluation
line.R0 = λ
line.R1.Mul(api, λ, p1.X)
line.R1.Sub(api, line.R1, p1.Y)
return res, &line
}
// linesCompute computes the lines that goes through p1 and p2, and (p1+p2) and p1 but does not compute 2p1+p2
func linesCompute(api frontend.API, p1, p2 *g2AffP) (*lineEvaluation, *lineEvaluation) {
var n, d, l1, l2, x3 fields_bls24315.E4
var line1, line2 lineEvaluation
// compute lambda1 = (y2-y1)/(x2-x1)
n.Sub(api, p1.Y, p2.Y)
d.Sub(api, p1.X, p2.X)
l1.DivUnchecked(api, n, d)
// x3 =lambda1**2-p1.x-p2.x
x3.Square(api, l1).
Sub(api, x3, p1.X).
Sub(api, x3, p2.X)
// omit y3 computation
// compute line1
line1.R0 = l1
line1.R1.Mul(api, l1, p1.X).Sub(api, line1.R1, p1.Y)
// compute lambda2 = -lambda1-2*y1/(x3-x1)
n.Double(api, p1.Y)
d.Sub(api, x3, p1.X)
l2.DivUnchecked(api, n, d)
l2.Add(api, l2, l1).Neg(api, l2)
// compute line2
line2.R0 = l2
line2.R1.Mul(api, l2, p1.X).Sub(api, line2.R1, p1.Y)
return &line1, &line2
}
// lineCompute computes the line that goes through p1 and p2 but does not compute p1+p2
func lineCompute(api frontend.API, p1, p2 *g2AffP) *lineEvaluation {
var qypy, qxpx, λ fields_bls24315.E4
// compute λ = (y2-y1)/(x2-x1)
qypy.Sub(api, p2.Y, p1.Y)
qxpx.Sub(api, p2.X, p1.X)
λ.DivUnchecked(api, qypy, qxpx)
var line lineEvaluation
line.R0 = λ
line.R1.Mul(api, λ, p1.X)
line.R1.Sub(api, line.R1, p1.Y)
return &line
}