-
Notifications
You must be signed in to change notification settings - Fork 11
/
AutoTest_Group.hpp
327 lines (264 loc) · 7.69 KB
/
AutoTest_Group.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
#ifndef _SNARKLIB_AUTOTEST_GROUP_HPP_
#define _SNARKLIB_AUTOTEST_GROUP_HPP_
#include <gmp.h>
#include <string>
#include /*libsnark*/ "algebra/fields/bigint.hpp"
#include "snarklib/AutoTest.hpp"
#include "snarklib/BigInt.hpp"
#include "snarklib/ForeignLib.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// zero remains zero
//
template <typename T>
class AutoTest_GroupZeroStaysZero : public AutoTest
{
public:
AutoTest_GroupZeroStaysZero()
: AutoTest()
{}
void runTest() {
const auto ZERO = T::zero();
const auto
a = ZERO + ZERO,
b = ZERO - ZERO,
c = BigInt<1>().randomize() * ZERO,
d = BigInt<2>().randomize() * ZERO,
e = BigInt<3>().randomize() * ZERO;
const auto f = fastAddSpecial(a, b);
checkPass(ZERO == a && a == ZERO && a.isZero());
checkPass(ZERO == b && b == ZERO && b.isZero());
checkPass(ZERO == c && c == ZERO && c.isZero());
checkPass(ZERO == d && d == ZERO && d.isZero());
checkPass(ZERO == e && e == ZERO && e.isZero());
checkPass(ZERO == f && f == ZERO && f.isZero());
}
};
////////////////////////////////////////////////////////////////////////////////
// additive identity
//
template <typename T>
class AutoTest_GroupZeroIdentity : public AutoTest
{
public:
AutoTest_GroupZeroIdentity(const T& value)
: AutoTest(value),
m_B(value)
{}
AutoTest_GroupZeroIdentity()
: AutoTest_GroupZeroIdentity{T::random()}
{}
void runTest() {
const auto
a = m_B + T::zero(),
b = T::zero() + m_B,
c = m_B - T::zero();
const auto
d = fastAddSpecial(a, T::zero()),
e = fastAddSpecial(T::zero(), b);
checkPass(a == b && a == m_B && b == m_B && c == m_B && m_B == c);
checkPass(d == e && d == a && e == b);
}
private:
const T m_B;
};
////////////////////////////////////////////////////////////////////////////////
// multiplicative identity
//
template <typename T>
class AutoTest_GroupOneIdentity : public AutoTest
{
typedef typename T::ScalarField Fr;
public:
AutoTest_GroupOneIdentity(const T& value)
: AutoTest(value),
m_B(value)
{}
AutoTest_GroupOneIdentity()
: AutoTest_GroupOneIdentity{T::random()}
{}
void runTest() {
const auto
a = Fr::random(),
b = Fr::random();
checkPass((a + b) * T::one() == a * T::one() + b * T::one());
const auto
c = a - Fr::one(),
d = a + Fr::one();
checkPass((c + Fr::one()) * T::one() == (d - Fr::one()) * T::one());
checkPass(c * T::one() == a * T::one() - T::one());
checkPass(d * T::one() == a * T::one() + T::one());
auto
specA = a * T::one(),
specB = b * T::one(),
specAB = (a + b) * T::one();
specA.toSpecial();
specB.toSpecial();
specAB.toSpecial();
checkPass(specAB == fastAddSpecial(specA, specB));
}
private:
const T m_B;
};
////////////////////////////////////////////////////////////////////////////////
// addition matches original
//
template <mp_size_t N, typename T, typename U>
class AutoTest_GroupAdd : public AutoTest
{
public:
AutoTest_GroupAdd(const std::string& lhs, const std::string& rhs)
: AutoTest(lhs, rhs),
m_lhsA(to_bigint<N>(lhs) * U::one()),
m_rhsA(to_bigint<N>(rhs) * U::one()),
m_lhsB(BigInt<N>(lhs) * T::one()),
m_rhsB(BigInt<N>(rhs) * T::one())
{}
void runTest() {
const auto a = m_lhsA + m_rhsA;
const auto b = m_lhsB + m_rhsB;
checkPass(equal_libsnark(a, b));
auto
a1 = m_lhsA,
a2 = m_rhsA;
a1.to_special();
a2.to_special();
auto
b1 = m_lhsB,
b2 = m_rhsB;
b1.toSpecial();
b2.toSpecial();
checkPass(
equal_libsnark(
#ifdef USE_OLD_LIBSNARK
a1.fast_add_special(a2),
#else
a1.mixed_add(a2),
#endif
fastAddSpecial(b1, b2)));
}
private:
const U m_lhsA, m_rhsA;
const T m_lhsB, m_rhsB;
};
////////////////////////////////////////////////////////////////////////////////
// subtraction matches original
//
template <mp_size_t N, typename T, typename U>
class AutoTest_GroupSub : public AutoTest
{
public:
AutoTest_GroupSub(const std::string& lhs, const std::string& rhs)
: AutoTest(lhs, rhs),
m_lhsA(to_bigint<N>(lhs) * U::one()),
m_rhsA(to_bigint<N>(rhs) * U::one()),
m_lhsB(BigInt<N>(lhs) * T::one()),
m_rhsB(BigInt<N>(rhs) * T::one())
{}
void runTest() {
const auto a = m_lhsA - m_rhsA;
const auto b = m_lhsB - m_rhsB;
checkPass(equal_libsnark(a, b));
auto
a1 = m_lhsA,
a2 = -m_rhsA;
a1.to_special();
a2.to_special();
auto
b1 = m_lhsB,
b2 = -m_rhsB;
b1.toSpecial();
b2.toSpecial();
checkPass(
equal_libsnark(
#ifdef USE_OLD_LIBSNARK
a1.fast_add_special(a2),
#else
a1.mixed_add(a2),
#endif
fastAddSpecial(b1, b2)));
}
private:
const U m_lhsA, m_rhsA;
const T m_lhsB, m_rhsB;
};
////////////////////////////////////////////////////////////////////////////////
// multiplication matches original
//
template <mp_size_t N, typename T, typename U>
class AutoTest_GroupMul : public AutoTest
{
public:
AutoTest_GroupMul(const std::string& pow, const std::string& base)
: AutoTest(pow, base),
m_powerA(pow.c_str()),
m_powerB(pow),
m_baseA(to_bigint<N>(base) * U::one()),
m_baseB(BigInt<N>(base) * T::one())
{}
void runTest() {
const auto a = m_powerA * m_baseA;
const auto b = m_powerB * m_baseB;
checkPass(equal_libsnark(a, b));
}
private:
const libsnark::bigint<N> m_powerA;
const BigInt<N> m_powerB;
const U m_baseA;
const T m_baseB;
};
////////////////////////////////////////////////////////////////////////////////
// doubling matches original
//
template <mp_size_t N, typename T, typename U>
class AutoTest_GroupDbl : public AutoTest
{
public:
AutoTest_GroupDbl(const std::string& value)
: AutoTest(value),
m_A(to_bigint<N>(value) * U::one()),
m_B(BigInt<N>(value) * T::one())
{}
void runTest() {
const auto a = m_A.dbl();
const auto b = m_B.dbl();
checkPass(equal_libsnark(a, b));
}
private:
const U m_A;
const T m_B;
};
////////////////////////////////////////////////////////////////////////////////
// special and well formed matches original
//
template <mp_size_t N, typename T, typename U>
class AutoTest_GroupSpecialWellFormed : public AutoTest
{
public:
AutoTest_GroupSpecialWellFormed(const std::string& value)
: AutoTest(value),
m_A(to_bigint<N>(value) * U::one()),
m_B(BigInt<N>(value) * T::one())
{}
void runTest() {
auto a = m_A;
auto b = m_B;
for (std::size_t i = 0; i < 1000; ++i) {
checkPass(a.is_well_formed() == b.wellFormed());
checkPass(a.is_special() == b.isSpecial());
auto c = a;
auto d = b;
c.to_special();
d.toSpecial();
checkPass(c.is_well_formed() == d.wellFormed());
checkPass(c.is_special() == d.isSpecial());
a = a + U::one();
b = b + T::one();
}
}
private:
const U m_A;
const T m_B;
};
} // namespace snarklib
#endif