forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTest_EC_Pairing.hpp
542 lines (431 loc) · 16.7 KB
/
AutoTest_EC_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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#ifndef _SNARKLIB_AUTOTEST_EC_PAIRING_HPP_
#define _SNARKLIB_AUTOTEST_EC_PAIRING_HPP_
#include <string>
#ifdef CURVE_ALT_BN128
#include /*libsnark*/ "algebra/curves/alt_bn128/alt_bn128_pairing.hpp"
#endif
#ifdef CURVE_EDWARDS
#include /*libsnark*/ "algebra/curves/edwards/edwards_pairing.hpp"
#endif
#ifdef CURVE_MNT4
#include /*libsnark*/ "algebra/curves/mnt/mnt4/mnt4_pairing.hpp"
#endif
#ifdef CURVE_MNT6
#include /*libsnark*/ "algebra/curves/mnt/mnt6/mnt6_pairing.hpp"
#endif
#include "snarklib/AutoTest.hpp"
#include "snarklib/BigInt.hpp"
#include "snarklib/ForeignLib.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// precomputed G1 matches original
//
template <mp_size_t N, typename PAIRING, typename U>
class AutoTest_EC_PairingPrecompG1 : public AutoTest
{
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G1_precomp G1_precomp;
public:
AutoTest_EC_PairingPrecompG1(const std::string& value)
: AutoTest(value),
m_A(to_bigint<N>(value) * U::one()),
m_B(BigInt<N>(value) * G1::one())
{}
void runTest() {
const G1_precomp b(m_B);
#ifdef CURVE_MNT6
const auto a = mnt6_ate_precompute_G1(m_A);
checkPass(equal_libsnark(a.PX, b.PX));
checkPass(equal_libsnark(a.PY, b.PY));
checkPass(equal_libsnark(a.PX_twist, b.PX_twist));
checkPass(equal_libsnark(a.PY_twist, b.PY_twist));
#endif
#ifdef CURVE_MNT4
const auto a = mnt4_ate_precompute_G1(m_A);
checkPass(equal_libsnark(a.PX, b.PX));
checkPass(equal_libsnark(a.PY, b.PY));
checkPass(equal_libsnark(a.PX_twist, b.PX_twist));
checkPass(equal_libsnark(a.PY_twist, b.PY_twist));
#endif
#ifdef CURVE_EDWARDS
const auto a = edwards_ate_precompute_G1(m_A);
checkPass(equal_libsnark(a.P_XY, b.P_XY));
checkPass(equal_libsnark(a.P_XZ, b.P_XZ));
checkPass(equal_libsnark(a.P_ZZplusYZ, b.P_ZZplusYZ));
#endif
#ifdef CURVE_ALT_BN128
const auto a = alt_bn128_ate_precompute_G1(m_A);
checkPass(equal_libsnark(a.PX, b.PX));
checkPass(equal_libsnark(a.PY, b.PY));
#endif
}
private:
const U m_A;
const G1 m_B;
};
} // namespace snarklib
////////////////////////////////////////////////////////////////////////////////
// precomputed G2 doubling step for flipped Miller loop matches original
//
namespace libsnark {
#ifdef CURVE_MNT6
struct extended_mnt6_G2_projective { mnt6_Fq3 X; mnt6_Fq3 Y; mnt6_Fq3 Z; mnt6_Fq3 T; };
void doubling_step_for_flipped_miller_loop(extended_mnt6_G2_projective ¤t, mnt6_ate_dbl_coeffs &dc);
#endif
#ifdef CURVE_MNT4
struct extended_mnt4_G2_projective { mnt4_Fq2 X; mnt4_Fq2 Y; mnt4_Fq2 Z; mnt4_Fq2 T; };
void doubling_step_for_flipped_miller_loop(extended_mnt4_G2_projective ¤t, mnt4_ate_dbl_coeffs &dc);
#endif
#ifdef CURVE_EDWARDS
struct extended_edwards_G2_projective { edwards_Fq3 X; edwards_Fq3 Y; edwards_Fq3 Z; edwards_Fq3 T; };
void doubling_step_for_flipped_miller_loop(extended_edwards_G2_projective ¤t, edwards_Fq3_conic_coefficients &cc);
#endif
#ifdef CURVE_ALT_BN128
void doubling_step_for_flipped_miller_loop(const alt_bn128_Fq two_inv, alt_bn128_G2 ¤t, alt_bn128_ate_ell_coeffs &c);
#endif
} // namespace libsnark
namespace snarklib {
template <mp_size_t N, typename PAIRING, typename U>
class AutoTest_EC_PairingDoublingStepForFlippedMillerLoop : public AutoTest
{
typedef typename PAIRING::G2 G2;
public:
AutoTest_EC_PairingDoublingStepForFlippedMillerLoop(const std::string& value)
: AutoTest(value),
m_A(to_bigint<N>(value) * U::one()),
m_B(BigInt<N>(value) * G2::one())
{}
void runTest() {
auto a = m_A;
a.to_affine_coordinates();
auto b = m_B;
b.affineCoordinates();
#ifdef CURVE_MNT6
libsnark::mnt6_ate_dbl_coeffs aC;
libsnark::extended_mnt6_G2_projective aR;
aR.X = a.X();
aR.Y = a.Y();
aR.Z = libsnark::mnt6_Fq3::one();
aR.T = libsnark::mnt6_Fq3::one();
libsnark::doubling_step_for_flipped_miller_loop(aR, aC);
typename PAIRING::G2_projective bR(b.x(), b.y(), PAIRING::Fq3::one(), PAIRING::Fq3::one());
const auto bC = PAIRING::doubling_step_for_flipped_miller_loop(bR);
checkPass(equal_libsnark(aC.c_H, bC.c_H));
checkPass(equal_libsnark(aC.c_4C, bC.c_4C));
checkPass(equal_libsnark(aC.c_J, bC.c_J));
checkPass(equal_libsnark(aC.c_L, bC.c_L));
#endif
#ifdef CURVE_MNT4
libsnark::mnt4_ate_dbl_coeffs aC;
libsnark::extended_mnt4_G2_projective aR;
aR.X = a.X();
aR.Y = a.Y();
aR.Z = libsnark::mnt4_Fq2::one();
aR.T = libsnark::mnt4_Fq2::one();
libsnark::doubling_step_for_flipped_miller_loop(aR, aC);
typename PAIRING::G2_projective bR(b.x(), b.y(), PAIRING::Fq2::one(), PAIRING::Fq2::one());
const auto bC = PAIRING::doubling_step_for_flipped_miller_loop(bR);
checkPass(equal_libsnark(aC.c_H, bC.c_H));
checkPass(equal_libsnark(aC.c_4C, bC.c_4C));
checkPass(equal_libsnark(aC.c_J, bC.c_J));
checkPass(equal_libsnark(aC.c_L, bC.c_L));
#endif
#ifdef CURVE_EDWARDS
libsnark::edwards_Fq3_conic_coefficients aC;
libsnark::extended_edwards_G2_projective aR;
aR.X = a.X;
aR.Y = a.Y;
aR.Z = a.Z;
aR.T = a.X * a.Y;
libsnark::doubling_step_for_flipped_miller_loop(aR, aC);
typename PAIRING::G2_projective bR(b.x(), b.y(), b.z(), b.x() * b.y());
const auto bC = PAIRING::doubling_step_for_flipped_miller_loop(bR);
checkPass(equal_libsnark(aC.c_ZZ, bC.c_ZZ));
checkPass(equal_libsnark(aC.c_XY, bC.c_XY));
checkPass(equal_libsnark(aC.c_XZ, bC.c_XZ));
#endif
#ifdef CURVE_ALT_BN128
libsnark::alt_bn128_ate_ell_coeffs aC;
libsnark::alt_bn128_G2 aR;
aR.X = a.X;
aR.Y = a.Y;
aR.Z = libsnark::alt_bn128_Fq2::one();
const libsnark::alt_bn128_Fq two_inv = libsnark::alt_bn128_Fq("2").inverse();
libsnark::doubling_step_for_flipped_miller_loop(two_inv, aR, aC);
typename PAIRING::G2 bR(b.x(), b.y(), PAIRING::Fq2::one());
const auto bC = PAIRING::doubling_step_for_flipped_miller_loop(bR);
checkPass(equal_libsnark(aC.ell_0, bC.ell_0));
checkPass(equal_libsnark(aC.ell_VW, bC.ell_VW));
checkPass(equal_libsnark(aC.ell_VV, bC.ell_VV));
#endif
}
private:
const U m_A;
const G2 m_B;
};
////////////////////////////////////////////////////////////////////////////////
// precomputed G2 matches original
//
template <mp_size_t N, typename PAIRING, typename U>
class AutoTest_EC_PairingPrecompG2 : public AutoTest
{
typedef typename PAIRING::G2 G2;
typedef typename PAIRING::G2_precomp G2_precomp;
public:
AutoTest_EC_PairingPrecompG2(const std::string& value)
: AutoTest(value),
m_A(to_bigint<N>(value) * U::one()),
m_B(BigInt<N>(value) * G2::one())
{}
void runTest() {
const G2_precomp b(m_B);
#ifdef CURVE_MNT6
// FIXME - AutoTest_EC_PairingPrecompG2 fails for MNT6 elliptic curve
const auto a = mnt6_ate_precompute_G2(m_A);
// The libsnark structs dbl_coeffs and add_coeffs are combined
// as a union inside the snarklib struct both_coeffs.
const auto
dblSize = a.dbl_coeffs.size(),
addSize = a.add_coeffs.size(),
totalSize = b.coeffs.size();
if (checkPass(dblSize + addSize == totalSize)) {
std::size_t
dblIdx = 0,
addIdx = 0;
for (std::size_t i = 0; i < totalSize; ++i) {
if (b.coeffs[i].is_dbl_coeffs) {
const auto& a_coeff = a.dbl_coeffs[dblIdx++];
const auto& b_coeff = b.coeffs[i].as_dbl_coeffs;
checkPass(equal_libsnark(a_coeff.c_H, b_coeff.c_H));
checkPass(equal_libsnark(a_coeff.c_4C, b_coeff.c_4C));
checkPass(equal_libsnark(a_coeff.c_J, b_coeff.c_J));
checkPass(equal_libsnark(a_coeff.c_L, b_coeff.c_L));
} else {
const auto& a_coeff = a.add_coeffs[dblIdx++];
const auto& b_coeff = b.coeffs[i].as_add_coeffs;
checkPass(equal_libsnark(a_coeff.c_L1, b_coeff.c_L1));
checkPass(equal_libsnark(a_coeff.c_RZ, b_coeff.c_RZ));
}
}
}
#endif
#ifdef CURVE_MNT4
// FIXME - AutoTest_EC_PairingPrecompG2 fails for MNT4 elliptic curve
const auto a = mnt4_ate_precompute_G2(m_A);
// The libsnark structs dbl_coeffs and add_coeffs are combined
// as a union inside the snarklib struct both_coeffs.
const auto
dblSize = a.dbl_coeffs.size(),
addSize = a.add_coeffs.size(),
totalSize = b.coeffs.size();
if (checkPass(dblSize + addSize == totalSize)) {
std::size_t
dblIdx = 0,
addIdx = 0;
for (std::size_t i = 0; i < totalSize; ++i) {
if (b.coeffs[i].is_dbl_coeffs) {
const auto& a_coeff = a.dbl_coeffs[dblIdx++];
const auto& b_coeff = b.coeffs[i].as_dbl_coeffs;
checkPass(equal_libsnark(a_coeff.c_H, b_coeff.c_H));
checkPass(equal_libsnark(a_coeff.c_4C, b_coeff.c_4C));
checkPass(equal_libsnark(a_coeff.c_J, b_coeff.c_J));
checkPass(equal_libsnark(a_coeff.c_L, b_coeff.c_L));
} else {
const auto& a_coeff = a.add_coeffs[dblIdx++];
const auto& b_coeff = b.coeffs[i].as_add_coeffs;
checkPass(equal_libsnark(a_coeff.c_L1, b_coeff.c_L1));
checkPass(equal_libsnark(a_coeff.c_RZ, b_coeff.c_RZ));
}
}
}
#endif
#ifdef CURVE_EDWARDS
const auto a = edwards_ate_precompute_G2(m_A);
if (checkPass(a.size() == b.coeffs.size())) {
for (std::size_t i = 0; i < a.size(); ++i) {
checkPass(equal_libsnark(a[i].c_ZZ, b.coeffs[i].c_ZZ));
checkPass(equal_libsnark(a[i].c_XY, b.coeffs[i].c_XY));
checkPass(equal_libsnark(a[i].c_XZ, b.coeffs[i].c_XZ));
}
}
#endif
#ifdef CURVE_ALT_BN128
const auto a = alt_bn128_ate_precompute_G2(m_A);
checkPass(equal_libsnark(a.QX, b.QX));
checkPass(equal_libsnark(a.QY, b.QY));
if (checkPass(a.coeffs.size() == b.coeffs.size())) {
for (std::size_t i = 0; i < a.coeffs.size(); ++i) {
checkPass(equal_libsnark(a.coeffs[i].ell_0, b.coeffs[i].ell_0));
checkPass(equal_libsnark(a.coeffs[i].ell_VW, b.coeffs[i].ell_VW));
checkPass(equal_libsnark(a.coeffs[i].ell_VV, b.coeffs[i].ell_VV));
}
}
#endif
}
private:
const U m_A;
const G2 m_B;
};
////////////////////////////////////////////////////////////////////////////////
// Ate Miller loop matches original
//
template <mp_size_t N, typename PAIRING, typename UG1, typename UG2>
class AutoTest_EC_PairingAteMillerLoop : public AutoTest
{
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
typedef typename PAIRING::G1_precomp G1_precomp;
typedef typename PAIRING::G2_precomp G2_precomp;
public:
AutoTest_EC_PairingAteMillerLoop(const std::string& g1,
const std::string& g2)
: AutoTest(g1, g2),
m_g1A(to_bigint<N>(g1) * UG1::one()),
m_g2A(to_bigint<N>(g2) * UG2::one()),
m_g1B(BigInt<N>(g1) * G1::one()),
m_g2B(BigInt<N>(g2) * G2::one())
{}
void runTest() {
#ifdef CURVE_MNT6
const auto a1 = mnt6_ate_precompute_G1(m_g1A);
const auto a2 = mnt6_ate_precompute_G2(m_g2A);
const auto a = mnt6_ate_miller_loop(a1, a2);
#endif
#ifdef CURVE_MNT4
const auto a1 = mnt4_ate_precompute_G1(m_g1A);
const auto a2 = mnt4_ate_precompute_G2(m_g2A);
const auto a = mnt4_ate_miller_loop(a1, a2);
#endif
#ifdef CURVE_EDWARDS
const auto a1 = edwards_ate_precompute_G1(m_g1A);
const auto a2 = edwards_ate_precompute_G2(m_g2A);
const auto a = edwards_ate_miller_loop(a1, a2);
#endif
#ifdef CURVE_ALT_BN128
const auto a1 = alt_bn128_ate_precompute_G1(m_g1A);
const auto a2 = alt_bn128_ate_precompute_G2(m_g2A);
const auto a = alt_bn128_ate_miller_loop(a1, a2);
#endif
const G1_precomp b1(m_g1B);
const G2_precomp b2(m_g2B);
const auto b = PAIRING::ate_miller_loop(b1, b2);
checkPass(equal_libsnark(a, b));
}
private:
const UG1 m_g1A;
const UG2 m_g2A;
const G1 m_g1B;
const G2 m_g2B;
};
////////////////////////////////////////////////////////////////////////////////
// Ate double Miller loop matches original
//
template <mp_size_t N, typename PAIRING, typename UG1, typename UG2>
class AutoTest_EC_PairingAteDoubleMillerLoop : public AutoTest
{
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
typedef typename PAIRING::G1_precomp G1_precomp;
typedef typename PAIRING::G2_precomp G2_precomp;
public:
AutoTest_EC_PairingAteDoubleMillerLoop(const std::string& g1_0,
const std::string& g2_1,
const std::string& g1_2,
const std::string& g2_3)
: AutoTest(g1_0, g2_1, g1_2, g2_3),
m_g1_0A(to_bigint<N>(g1_0) * UG1::one()),
m_g2_1A(to_bigint<N>(g2_1) * UG2::one()),
m_g1_2A(to_bigint<N>(g1_2) * UG1::one()),
m_g2_3A(to_bigint<N>(g2_3) * UG2::one()),
m_g1_0B(BigInt<N>(g1_0) * G1::one()),
m_g2_1B(BigInt<N>(g2_1) * G2::one()),
m_g1_2B(BigInt<N>(g1_2) * G1::one()),
m_g2_3B(BigInt<N>(g2_3) * G2::one())
{}
void runTest() {
#ifdef CURVE_MNT6
const auto
a0 = mnt6_ate_precompute_G1(m_g1_0A),
a2 = mnt6_ate_precompute_G1(m_g1_2A);
const auto
a1 = mnt6_ate_precompute_G2(m_g2_1A),
a3 = mnt6_ate_precompute_G2(m_g2_3A);
const auto a = mnt6_ate_double_miller_loop(a0, a1, a2, a3);
#endif
#ifdef CURVE_MNT4
const auto
a0 = mnt4_ate_precompute_G1(m_g1_0A),
a2 = mnt4_ate_precompute_G1(m_g1_2A);
const auto
a1 = mnt4_ate_precompute_G2(m_g2_1A),
a3 = mnt4_ate_precompute_G2(m_g2_3A);
const auto a = mnt4_ate_double_miller_loop(a0, a1, a2, a3);
#endif
#ifdef CURVE_EDWARDS
const auto
a0 = edwards_ate_precompute_G1(m_g1_0A),
a2 = edwards_ate_precompute_G1(m_g1_2A);
const auto
a1 = edwards_ate_precompute_G2(m_g2_1A),
a3 = edwards_ate_precompute_G2(m_g2_3A);
const auto a = edwards_ate_double_miller_loop(a0, a1, a2, a3);
#endif
#ifdef CURVE_ALT_BN128
const auto
a0 = alt_bn128_ate_precompute_G1(m_g1_0A),
a2 = alt_bn128_ate_precompute_G1(m_g1_2A);
const auto
a1 = alt_bn128_ate_precompute_G2(m_g2_1A),
a3 = alt_bn128_ate_precompute_G2(m_g2_3A);
const auto a = alt_bn128_ate_double_miller_loop(a0, a1, a2, a3);
#endif
const G1_precomp b0(m_g1_0B), b2(m_g1_2B);
const G2_precomp b1(m_g2_1B), b3(m_g2_3B);
const auto b = PAIRING::ate_double_miller_loop(b0, b1, b2, b3);
checkPass(equal_libsnark(a, b));
}
private:
const UG1 m_g1_0A, m_g1_2A;
const UG2 m_g2_1A, m_g2_3A;
const G1 m_g1_0B, m_g1_2B;
const G2 m_g2_1B, m_g2_3B;
};
////////////////////////////////////////////////////////////////////////////////
// final exponentiation matches original
//
template <mp_size_t N, typename PAIRING, typename U>
class AutoTest_EC_PairingFinalExponentiation : public AutoTest
{
typedef typename PAIRING::GT GT;
public:
AutoTest_EC_PairingFinalExponentiation(const GT& value)
: AutoTest(value),
m_B(value)
{
copy_libsnark(m_B, m_A);
}
AutoTest_EC_PairingFinalExponentiation()
: AutoTest_EC_PairingFinalExponentiation{GT::random()}
{}
void runTest() {
#ifdef CURVE_MNT6
const auto a = mnt6_final_exponentiation(m_A);
#endif
#ifdef CURVE_MNT4
const auto a = mnt4_final_exponentiation(m_A);
#endif
#ifdef CURVE_EDWARDS
const auto a = edwards_final_exponentiation(m_A);
#endif
#ifdef CURVE_ALT_BN128
const auto a = alt_bn128_final_exponentiation(m_A);
#endif
const auto b = PAIRING::final_exponentiation(m_B);
checkPass(equal_libsnark(a, b));
}
private:
U m_A;
const GT m_B;
};
} // namespace snarklib
#endif