-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEC_MNT6_Pairing.hpp
363 lines (288 loc) · 10.8 KB
/
EC_MNT6_Pairing.hpp
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
#ifndef _SNARKLIB_EC_MNT6_PAIRING_HPP_
#define _SNARKLIB_EC_MNT6_PAIRING_HPP_
#include <vector>
#include <snarklib/EC.hpp>
#include <snarklib/EC_MNT6_GroupCurve.hpp>
#include <snarklib/EC_Pairing.hpp>
#include <snarklib/Group.hpp>
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// MNT6
// Paired groups
//
// fields R and Q have been initialized
template <mp_size_t N, const BigInt<N>& MODULUS_R, const BigInt<N>& MODULUS_Q>
class MNT6_Pairing : public ECInitGroups<N, MODULUS_R, MODULUS_Q>
{
typedef ECInitGroups<N, MODULUS_R, MODULUS_Q> BASE;
typedef MNT6_GroupCurve<N, MODULUS_R, MODULUS_Q> CURVE;
public:
typedef typename BASE::Fr Fr; // scalar field
typedef typename BASE::Fq Fq; // base field for G1
typedef typename BASE::Fq3 Fq3; // twist field for G2
typedef typename BASE::Fq32 Fq6; // pairing field
typedef Fq3 Fqe;
typedef Fq6 Fqk;
// paired groups
typedef Group<Fq, Fr, CURVE> G1;
typedef Group<Fq3, Fr, CURVE> G2;
typedef Fq6 GT;
static MNT6_Pairing& PAIRING() {
static MNT6_Pairing a;
return a;
}
//
// pairing parameters (MODULUS is Q)
//
static const BigInt<N>& ate_loop_count() {
static const BigInt<N> a("689871209842287392837045615510547309923794944");
return a;
}
static bool ate_is_loop_count_neg() {
return true;
}
static const BigInt<6 * N>& final_exponent() {
static const BigInt<6 * N> a("24416320138090509697890595414313438768353977489862543935904010715439066975957855922532159264213056712140358746422742237328406558352706591021642230618060502855451264045397444793186876199015256781648746888625527075466063075011307800862173764236311342105211681121426931616843635215852236649271569251468773714424208521977615548771268520882870120900360322044218806712027729351845307690474985502587527753847200130592058098363641559341826790559426614919168");
return a;
}
static const BigInt<N>& final_exponent_last_chunk_abs_of_w0() {
static const BigInt<N> a("689871209842287392837045615510547309923794944");
return a;
}
static bool final_exponent_last_chunk_is_w0_neg() {
return true;
}
static const BigInt<N>& final_exponent_last_chunk_w1() {
static const BigInt<N> a("1");
return a;
}
//
// pairing code
//
// group 1 precompute
struct G1_precomp {
Fq PX, PY;
Fq3 PX_twist, PY_twist;
G1_precomp(const G1& P) {
G1 Pcopy(P);
Pcopy.affineCoordinates();
PX = Pcopy.x();
PY = Pcopy.y();
PX_twist = Pcopy.x() * CURVE::twist();
PY_twist = Pcopy.y() * CURVE::twist();
}
};
struct G2_projective {
Fq3 X, Y, Z, T;
G2_projective(const Fq3& a, const Fq3& b, const Fq3& c, const Fq3& d)
: X(a), Y(b), Z(c), T(d)
{}
};
struct dbl_coeffs {
Fq3 c_H, c_4C, c_J, c_L;
dbl_coeffs(const Fq3& a, const Fq3& b, const Fq3& c, const Fq3& d)
: c_H(a), c_4C(b), c_J(c), c_L(d)
{}
};
struct add_coeffs {
Fq3 c_L1, c_RZ;
add_coeffs(const Fq3& a, const Fq3& b)
: c_L1(a), c_RZ(b)
{}
};
struct both_coeffs {
// if true, then union is dbl_coeffs
// if false, then union is add_coeffs
bool is_dbl_coeffs;
union {
dbl_coeffs as_dbl_coeffs;
add_coeffs as_add_coeffs;
};
both_coeffs(const dbl_coeffs& a)
: is_dbl_coeffs(true),
as_dbl_coeffs(a)
{}
both_coeffs(const add_coeffs& a)
: is_dbl_coeffs(false),
as_add_coeffs(a)
{}
};
// group 2 precompute
struct G2_precomp {
Fq3 QX, QY, QY2, QX_over_twist, QY_over_twist;
std::vector<both_coeffs> coeffs;
G2_precomp() = default;
G2_precomp(const G2& Q) {
G2 Qcopy(Q);
Qcopy.affineCoordinates();
QX = Qcopy.x();
QY = Qcopy.y();
QY2 = squared(Qcopy.y());
const auto inv_twist = inverse(CURVE::twist());
QX_over_twist = Qcopy.x() * inv_twist;
QY_over_twist = Qcopy.y() * inv_twist;
G2_projective R(Qcopy.x(), Qcopy.y(), Fq3::one(), Fq3::one());
precompLoop(coeffs, *this, R, PAIRING());
if (ate_is_loop_count_neg()) { // always true
const auto RZ_inv = inverse(R.Z);
const auto RZ2_inv = squared(RZ_inv);
const auto RZ3_inv = RZ2_inv * RZ_inv;
G2_precomp minus_R_affine;
minus_R_affine.QX = R.X * RZ2_inv;
minus_R_affine.QY = - R.Y * RZ3_inv;
minus_R_affine.QY2 = squared(minus_R_affine.QY);
coeffs.push_back(
mixed_addition_step_for_flipped_miller_loop(minus_R_affine, R));
}
}
};
// called by precompLoop()
static dbl_coeffs doubling_step_for_flipped_miller_loop(G2_projective& current)
{
const auto
X = current.X,
Y = current.Y,
Z = current.Z,
T = current.T;
const auto
A = squared(T),
B = squared(X),
C = squared(Y);
const auto D = squared(C);
const auto
E = squared(X + C) - B - D,
F = (B + B + B) + CURVE::twist_coeff_a() * A;
const auto G = squared(F);
current.X = - (E + E + E + E) + G;
current.Y = - CURVE::coeff_8() * D + F * (E + E - current.X);
current.Z = squared(Y + Z) - C - squared(Z);
current.T = squared(current.Z);
return dbl_coeffs(squared(current.Z + T) - current.T - A,
C + C + C + C,
squared(F + T) - G - A,
squared(F + X) - G - B);
}
// called by precompLoop()
static add_coeffs mixed_addition_step_for_flipped_miller_loop(const G2_precomp& base,
G2_projective& current)
{
const auto
X1 = current.X,
Y1 = current.Y,
Z1 = current.Z,
T1 = current.T,
&x2 = base.QX,
&y2 = base.QY,
&y2_squared = base.QY2;
const auto
B = x2 * T1,
D = (squared(y2 + Z1) - y2_squared - T1) * T1;
const auto H = B - X1;
const auto I = squared(H);
const auto E = I + I + I + I;
const auto
J = H * E,
V = X1 * E,
L1 = D - (Y1 + Y1);
current.X = squared(L1) - J - (V + V);
current.Y = L1 * (V - current.X) - (Y1 + Y1) * J;
current.Z = squared(Z1 + H) - T1 - I;
current.T = squared(current.Z);
return add_coeffs(L1,
current.Z);
}
static Fq6 ate_miller_loop(const G1_precomp& prec_P,
const G2_precomp& prec_Q)
{
return millerLoop(prec_P, prec_Q, PAIRING());
}
static Fq6 ate_double_miller_loop(const G1_precomp& prec_P1,
const G2_precomp& prec_Q1,
const G1_precomp& prec_P2,
const G2_precomp& prec_Q2)
{
return doubleMillerLoop(prec_P1, prec_Q1, prec_P2, prec_Q2, PAIRING());
}
// called by millerLoop() and doubleMillerLoop()
static Fq6 millerMul(const Fq6& f,
const G1_precomp& prec_P,
const G2_precomp& prec_Q,
const both_coeffs& c)
{
// expect dbl_coeffs
assert(c.is_dbl_coeffs);
const auto& dc = c.as_dbl_coeffs;
return f * Fq6(- dc.c_4C - dc.c_J * prec_P.PX_twist + dc.c_L,
dc.c_H * prec_P.PY_twist);
}
// called by millerLoop() and doubleMillerLoop()
static Fq6 millerMulBit(const Fq6& f,
const G1_precomp& prec_P,
const G2_precomp& prec_Q,
const both_coeffs& c)
{
// expect add_coeffs
assert(! c.is_dbl_coeffs);
const auto& ac = c.as_add_coeffs;
const Fq3 tmp(prec_P.PX[0], Fq::zero()[0], Fq::zero()[0]);
const auto L1_coeff = tmp - prec_Q.QX_over_twist;
return f * Fq6(ac.c_RZ * prec_P.PY_twist,
- (prec_Q.QY_over_twist * ac.c_RZ + L1_coeff * ac.c_L1));
}
// called by millerLoop()
static Fq6 millerFinish(const Fq6& f,
const G1_precomp& prec_P,
const G2_precomp& prec_Q,
const std::size_t idx)
{
if (ate_is_loop_count_neg()) { // always true
return inverse(
millerMulBit(f, prec_P, prec_Q, prec_Q.coeffs[idx]));
}
return f;
}
// called by doubleMillerLoop()
static Fq6 doubleMillerFinish(const Fq6& f,
const G1_precomp& prec_P1,
const G2_precomp& prec_Q1,
const G1_precomp& prec_P2,
const G2_precomp& prec_Q2,
const std::size_t idx)
{
if (ate_is_loop_count_neg()) { // always true
const auto f2 = millerMulBit(f, prec_P1, prec_Q1, prec_Q1.coeffs[idx]);
const auto f3 = millerMulBit(f2, prec_P2, prec_Q2, prec_Q2.coeffs[idx]);
return inverse(f3);
}
return f;
}
// called by final_exponentiation()
static Fq6 final_exponentiation_first_chunk(const Fq6& elt,
const Fq6& elt_inv)
{
const auto elt_q3 = Frobenius_map(elt, 3);
const auto elt_q3_over_elt = elt_q3 * elt_inv;
return Frobenius_map(elt_q3_over_elt, 1) * elt_q3_over_elt;
}
// called by final_exponentiation()
static Fq6 final_exponentiation_last_chunk(const Fq6& elt,
const Fq6& elt_inv)
{
const auto elt_q = Frobenius_map(elt, 1);
const auto
w1_part = cyclotomic_exp(elt_q, final_exponent_last_chunk_w1()),
w0_part = cyclotomic_exp( // ternary always true
final_exponent_last_chunk_is_w0_neg() ? elt_inv : elt,
final_exponent_last_chunk_abs_of_w0());
return w1_part * w0_part;
}
static Fq6 final_exponentiation(const Fq6& elt)
{
const auto elt_inv = inverse(elt);
return final_exponentiation_last_chunk(
final_exponentiation_first_chunk(elt, elt_inv),
final_exponentiation_first_chunk(elt_inv, elt));
}
};
} // namespace snarklib
#endif