forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTest.hpp
607 lines (511 loc) · 17.6 KB
/
AutoTest.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#ifndef _SNARKLIB_AUTO_TEST_HPP_
#define _SNARKLIB_AUTO_TEST_HPP_
#include <cassert>
#include <cstdint>
#include <gmp.h>
#include <iostream>
#include <memory>
#include <ostream>
#include <random>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>
#include "algebra/curves/alt_bn128/alt_bn128_g1.hpp"
#include "algebra/curves/alt_bn128/alt_bn128_g2.hpp"
#include "algebra/curves/edwards/edwards_g1.hpp"
#include "algebra/curves/edwards/edwards_g2.hpp"
#include "algebra/fields/bigint.hpp"
#include "algebra/fields/fp.hpp"
#include "algebra/fields/fp2.hpp"
#include "algebra/fields/fp3.hpp"
#include "algebra/fields/fp6_2over3.hpp"
#include "algebra/fields/fp6_3over2.hpp"
#include "algebra/fields/fp12_2over3over2.hpp"
#include "AuxSTL.hpp"
#include "BigInt.hpp"
#include "common/types.hpp"
#include "EC.hpp"
#include "encoding/knowledge_commitment.hpp"
#include "Field.hpp"
#include "FpModel.hpp"
#include "Group.hpp"
#include "Pairing.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// autotest base class
//
class AutoTest
{
public:
~AutoTest() = default;
std::string testName() const {
std::stringstream ss;
ss << typeid(*this).name() << m_testNameSuffix;
return ss.str();
}
std::size_t testNumber() const {
return m_testCounter;
}
virtual void runTest() = 0;
bool testPass() const {
return m_testPass;
}
void testLog(std::ostream& out) const {
out << testNumber() << "\t"
<< (testPass() ? "PASS" : "FAIL") << "\t"
<< testName() << std::endl;
}
protected:
AutoTest()
: m_testPass(true)
{
static std::size_t testCounter = 0;
m_testCounter = testCounter++;
}
template <typename A>
AutoTest(const A& a)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a;
m_testNameSuffix = ss.str();
}
template <typename A, typename B>
AutoTest(const A& a, const B& b)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a << " " << b;
m_testNameSuffix = ss.str();
}
template <typename A, typename B, typename C>
AutoTest(const A& a, const B& b, const C& c)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a << " " << b << " " << c;
m_testNameSuffix = ss.str();
}
template <typename A, typename B, typename C, typename D>
AutoTest(const A& a, const B& b, const C& c, const D& d)
: AutoTest{}
{
std::stringstream ss;
ss << " " << a << " " << b << " " << c << " " << d;
m_testNameSuffix = ss.str();
}
bool checkPass(const bool v) {
m_testPass &= v;
return v;
}
// bigint == BigInt
template <mp_size_t N>
bool sameData(const libsnark::bigint<N>& a,
const BigInt<N>& b) const
{
for (std::size_t i = 0; i < N; ++i) {
if (a.data[i] != b.data()[i])
return false;
}
return true;
}
// Fp_model == FpModel
template <mp_size_t N, const libsnark::bigint<N>& MODULUS_A, const BigInt<N>& MODULUS_B>
bool sameData(const libsnark::Fp_model<N, MODULUS_A>& a,
const FpModel<N, MODULUS_B>& b) const
{
return sameData(a.as_bigint(), b.asBigInt());
}
// Fp_model == Field<FpModel>
template <mp_size_t N, const libsnark::bigint<N>& MODULUS_A, const BigInt<N>& MODULUS_B>
bool sameData(const libsnark::Fp_model<N, MODULUS_A>& a,
const Field<FpModel<N, MODULUS_B>>& b) const
{
return sameData(a, b[0]);
}
// Fp2_model == Field<FpModel, 2>
template <mp_size_t N, const libsnark::bigint<N>& MODULUS_A, const BigInt<N>& MODULUS_B>
bool sameData(const libsnark::Fp2_model<N, MODULUS_A>& a,
const Field<FpModel<N, MODULUS_B>, 2>& b) const
{
return
sameData(a.c0, b[0]) &&
sameData(a.c1, b[1]);
}
// Fp3_model == Field<FpModel, 3>
template <mp_size_t N, const libsnark::bigint<N>& MODULUS_A, const BigInt<N>& MODULUS_B>
bool sameData(const libsnark::Fp3_model<N, MODULUS_A>& a,
const Field<FpModel<N, MODULUS_B>, 3>& b) const
{
return
sameData(a.c0, b[0]) &&
sameData(a.c1, b[1]) &&
sameData(a.c2, b[2]);
}
// Fp6_2over3_model == Field<Field<FpModel, 3>, 2>
template <mp_size_t N, const libsnark::bigint<N>& MODULUS_A, const BigInt<N>& MODULUS_B>
bool sameData(const libsnark::Fp6_2over3_model<N, MODULUS_A>& a,
const Field<Field<FpModel<N, MODULUS_B>, 3>, 2>& b) const
{
return
sameData(a.c0, b[0]) &&
sameData(a.c1, b[1]);
}
// Fp6_3over2_model == Field<Field<FpModel, 2>, 3>
template <mp_size_t N, const libsnark::bigint<N>& MODULUS_A, const BigInt<N>& MODULUS_B>
bool sameData(const libsnark::Fp6_3over2_model<N, MODULUS_A>& a,
const Field<Field<FpModel<N, MODULUS_B>, 2>, 3>& b) const
{
return
sameData(a.c0, b[0]) &&
sameData(a.c1, b[1]) &&
sameData(a.c2, b[2]);
}
// Fp12_2over3over2_model == Field<Field<Field<FpModel, 2>, 3>, 2>
template <mp_size_t N, const libsnark::bigint<N>& MODULUS_A, const BigInt<N>& MODULUS_B>
bool sameData(const libsnark::Fp12_2over3over2_model<N, MODULUS_A>& a,
const Field<Field<Field<FpModel<N, MODULUS_B>, 2>, 3>, 2>& b) const
{
return
sameData(a.c0, b[0]) &&
sameData(a.c1, b[1]);
}
// alt_bn128_G1 == BN128::Groups<>::G1
template <typename GROUP>
bool sameData(const libsnark::alt_bn128_G1& a, const GROUP& b) const {
return
sameData(a.X, b.x()) &&
sameData(a.Y, b.y()) &&
sameData(a.Z, b.z());
}
// alt_bn128_G2 == BN128::Groups<>::G2
template <typename GROUP>
bool sameData(const libsnark::alt_bn128_G2& a, const GROUP& b) const {
return
sameData(a.X, b.x()) &&
sameData(a.Y, b.y()) &&
sameData(a.Z, b.z());
}
// edwards_G1 == Edwards::Groups<>::G1
template <typename GROUP>
bool sameData(const libsnark::edwards_G1& a, const GROUP& b) const {
return
sameData(a.X, b.x()) &&
sameData(a.Y, b.y()) &&
sameData(a.Z, b.z());
}
// edwards_G2 == Edwards::Groups<>::G2
template <typename GROUP>
bool sameData(const libsnark::edwards_G2& a, const GROUP& b) const {
return
sameData(a.X, b.x()) &&
sameData(a.Y, b.y()) &&
sameData(a.Z, b.z());
}
// knowledge_commitment<> == Pairing<>
template <typename UG, typename UH, typename TG, typename TH>
bool sameData(const libsnark::knowledge_commitment<UG, UH>& a,
const Pairing<TG, TH>& b) const {
return
sameData(a.g, b.G()) &&
sameData(a.h, b.H());
}
// vector<> == vector<>
template <typename T, typename U>
bool sameData(const std::vector<T>& a,
const std::vector<U>& b) const {
if (a.size() != b.size()) {
return false;
}
for (std::size_t i = 0; i < a.size(); ++i) {
if (! sameData(a[i], b[i]))
return false;
}
return true;
}
// BigInt -> bigint
template <mp_size_t N>
void copyData(const BigInt<N>& a, libsnark::bigint<N>& b) {
std::stringstream ssA;
ssA << a;
std::stringstream ssB(ssA.str());
ssB >> b;
}
// bigint -> BigInt
template <mp_size_t N>
void copyData(const libsnark::bigint<N>& a, BigInt<N>& b) {
std::stringstream ssA;
ssA << a;
std::stringstream ssB(ssA.str());
ssB >> b;
}
#define DEFN_COPY_DATA_F2M(A, B) \
template <mp_size_t N, \
const BigInt<N>& MA, \
const libsnark::bigint<N>& MB> \
void copyData(const A & a, libsnark:: B & b) { \
std::stringstream ssA; \
ssA << a; \
std::stringstream ssB(ssA.str()); \
ssB >> b; \
}
#define DEFN_COPY_DATA_M2F(A, B) \
template <mp_size_t N, \
const libsnark::bigint<N>& MA, \
const BigInt<N>& MB> \
void copyData(const libsnark:: A & a, B & b) { \
std::stringstream ssA; \
ssA << a; \
std::stringstream ssB(ssA.str()); \
ssB >> b; \
}
#define DEFN_COPY_DATA_G2M(B) \
template <typename GROUP> \
void copyData(const GROUP& a, libsnark:: B & b) { \
copyData(a.x(), b.X); \
copyData(a.y(), b.Y); \
copyData(a.z(), b.Z); \
}
#define DEFN_COPY_DATA_M2G(A) \
template <typename GROUP> \
void copyData(const libsnark:: A & a, GROUP& b) { \
typename GROUP::BaseField tmp; \
copyData(a.X, tmp); \
b.x(tmp); \
copyData(a.Y, tmp); \
b.y(tmp); \
copyData(a.Z, tmp); \
b.z(tmp); \
}
#define COMMA ,
// from snarklib Field<> to libsnark FpX_model
DEFN_COPY_DATA_F2M(Field<FpModel<N COMMA MA>>, Fp_model<N COMMA MB>)
DEFN_COPY_DATA_F2M(Field<FpModel<N COMMA MA> COMMA 2>, Fp2_model<N COMMA MB>)
DEFN_COPY_DATA_F2M(Field<FpModel<N COMMA MA> COMMA 3>, Fp3_model<N COMMA MB>)
//DEFN_COPY_DATA_F2M(Field<Field<FpModel<N COMMA MA> COMMA 3> COMMA 2>, Fp6_2over3_model<N COMMA MB>)
DEFN_COPY_DATA_F2M(Field<Field<FpModel<N COMMA MA> COMMA 2> COMMA 3>, Fp6_3over2_model<N COMMA MB>)
DEFN_COPY_DATA_F2M(Field<Field<Field<FpModel<N COMMA MA> COMMA 2> COMMA 3> COMMA 2>, Fp12_2over3over2_model<N COMMA MB>)
// from libsnark FpX_model to snarklib Field<>
DEFN_COPY_DATA_M2F(Fp_model<N COMMA MA>, Field<FpModel<N COMMA MB>>)
DEFN_COPY_DATA_M2F(Fp2_model<N COMMA MA>, Field<FpModel<N COMMA MB> COMMA 2>)
DEFN_COPY_DATA_M2F(Fp3_model<N COMMA MA>, Field<FpModel<N COMMA MB> COMMA 3>)
//DEFN_COPY_DATA_M2F(Fp6_2over3_model<N COMMA MA>, Field<Field<FpModel<N COMMA MB> COMMA 3> COMMA 2>)
DEFN_COPY_DATA_M2F(Fp6_3over2_model<N COMMA MA>, Field<Field<FpModel<N COMMA MB> COMMA 2> COMMA 3>)
DEFN_COPY_DATA_M2F(Fp12_2over3over2_model<N COMMA MA>, Field<Field<Field<FpModel<N COMMA MB> COMMA 2> COMMA 3> COMMA 2>)
// special case for snarklib Field<> to libsnark Fp32 model
template <mp_size_t N, const BigInt<N>& MA, const libsnark::bigint<N>& MB>
void copyData(const Field<Field<FpModel<N, MA>, 3>, 2>& a,
libsnark::Fp6_2over3_model<N, MB>& b)
{
std::stringstream ssA;
ssA << a;
std::stringstream ssB(ssA.str());
#ifdef CURVE_EDWARDS
// Fp6_2over3_model has no overloaded operators for stream
// insertion and extraction.
ssB >> b.c0 >> b.c1;
#else
ssB >> b;
#endif
}
// special case for libsnark Fp32 model to snarklib Field<>
template <mp_size_t N, const libsnark::bigint<N>& MA, const BigInt<N>& MB>
void copyData(const libsnark::Fp6_2over3_model<N, MA>& a,
Field<Field<FpModel<N, MB>, 3>, 2>& b)
{
std::stringstream ssA;
#ifdef CURVE_EDWARDS
// Fp6_2over3_model has no overloaded operators for stream
// insertion and extraction.
ssA << a.c0 << " " << a.c1;
#else
ssA << a;
#endif
std::stringstream ssB(ssA.str());
ssB >> b;
}
// from snarklib Group<> to libsnark elliptic curve group
DEFN_COPY_DATA_G2M(alt_bn128_G1)
DEFN_COPY_DATA_G2M(alt_bn128_G2)
DEFN_COPY_DATA_G2M(edwards_G1)
DEFN_COPY_DATA_G2M(edwards_G2)
// from libsnark elliptic curve group to snarklib Group<>
DEFN_COPY_DATA_M2G(alt_bn128_G1)
DEFN_COPY_DATA_M2G(alt_bn128_G2)
DEFN_COPY_DATA_M2G(edwards_G1)
DEFN_COPY_DATA_M2G(edwards_G2)
#undef DEFN_COPY_DATA_F2M
#undef DEFN_COPY_DATA_M2F
#undef DEFN_COPY_DATA_G2M
#undef DEFN_COPY_DATA_M2G
#undef COMMA
// from libsnark group vector to snarklib vector
template <typename G1>
void copyData(
const libsnark::G1_vector<libsnark::default_pp>& a,
std::vector<G1>& b)
{
const std::size_t vecSize = a.size();
b.clear();
b.reserve(vecSize);
G1 tmp;
for (std::size_t i = 0; i < vecSize; ++i) {
copyData(a[i], tmp);
b.emplace_back(tmp);
}
}
// from libsnark paired groups vector to snarklib sparse vector
template <typename G1>
void copyData(
const libsnark::G1G1_knowledge_commitment_vector<libsnark::default_pp>& a,
SparseVector<Pairing<G1, G1>>& b)
{
const std::size_t vecSize = a.values.size();
b.clear();
b.reserve(vecSize);
G1 tmpG, tmpH;
for (std::size_t i = 0; i < vecSize; ++i) {
copyData(a.values[i].g, tmpG);
copyData(a.values[i].h, tmpH);
b.pushBack(
a.indices[i],
Pairing<G1, G1>(tmpG, tmpH));
}
}
// from libsnark paired groups vector to snarklib sparse vector
template <typename G2, typename G1>
void copyData(
const libsnark::G2G1_knowledge_commitment_vector<libsnark::default_pp>& a,
SparseVector<Pairing<G2, G1>>& b)
{
const std::size_t vecSize = a.values.size();
b.clear();
b.reserve(vecSize);
G2 tmpG;
G1 tmpH;
for (std::size_t i = 0; i < vecSize; ++i) {
copyData(a.values[i].g, tmpG);
copyData(a.values[i].h, tmpH);
b.pushBack(
a.indices[i],
Pairing<G2, G1>(tmpG, tmpH));
}
}
// vector<>
template <typename T>
void printData(std::ostream& out,
const std::string& prefix,
const std::vector<T>& v) const
{
for (std::size_t i = 0; i < v.size(); ++i) {
out << prefix << "[" << i << "] = " << v[i] << std::endl;
}
}
private:
std::string m_testNameSuffix;
std::size_t m_testCounter;
bool m_testPass;
};
////////////////////////////////////////////////////////////////////////////////
// battery of autotests
//
class AutoTestBattery
{
public:
AutoTestBattery() = default;
std::size_t testCount() const {
return m_testVector.size();
}
void addTest(AutoTest* ptr) {
m_testVector.push_back(std::unique_ptr<AutoTest>(ptr));
}
bool runTest() {
bool allPass = true;
for (const auto& a : m_testVector) {
a->runTest();
allPass &= a->testPass();
}
return allPass;
}
bool runTest(const std::size_t indexNumber) {
m_testVector[indexNumber]->runTest();
return m_testVector[indexNumber]->testPass();
}
void testLog(std::ostream& out) const {
for (const auto& a : m_testVector) {
a->testLog(out);
}
}
void testLog(std::ostream& out, const std::size_t indexNumber) const {
m_testVector[indexNumber]->testLog(out);
}
private:
std::vector<std::unique_ptr<AutoTest>> m_testVector;
};
////////////////////////////////////////////////////////////////////////////////
// convenient functions
//
std::string randomBase10(std::random_device& rd, const mp_size_t N) {
std::stringstream ss;
for (size_t i = 0; i < N; ++i) {
ss << rd();
}
return ss.str();
}
std::string uniformBase10(const unsigned long low, const unsigned long high) {
assert(low <= high);
std::default_random_engine generator;
std::uniform_int_distribution<unsigned long> distribution(low, high);
std::stringstream ss;
ss << distribution(generator);
return ss.str();
}
std::string sparseUniformBase10(const unsigned long low, const unsigned long high) {
assert(low <= high);
std::default_random_engine generator;
std::uniform_int_distribution<unsigned long>
selector(0, 2),
distribution(low, high);
std::stringstream ss;
switch (selector(generator)) {
case (0) :
ss << 0;
break;
case (1) :
ss << 1;
break;
default:
ss << distribution(generator);
break;
}
return ss.str();
}
template <mp_size_t N>
libsnark::bigint<N> to_bigint(const std::string& base10) {
return libsnark::bigint<N>(base10.c_str());
}
template <typename GA, typename GB>
void randomSparseVector(SparseVector<Pairing<GA, GB>>& a,
const std::size_t numberElems,
const std::size_t startIndex)
{
a.reserve(numberElems);
for (std::size_t i = 0; i < numberElems; ++i) {
a.pushBack(
startIndex + i,
Pairing<GA, GB>(GA::random(), GB::random()));
}
}
template <typename T>
void randomVector(std::vector<T>& a,
const std::size_t numberElems)
{
a.reserve(numberElems);
for (std::size_t i = 0; i < numberElems; ++i) {
a.emplace_back(
T::random());
}
}
} // namespace snarklib
#endif