-
Notifications
You must be signed in to change notification settings - Fork 11
/
AutoTest_BigInt.hpp
252 lines (208 loc) · 5.92 KB
/
AutoTest_BigInt.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
#ifndef _SNARKLIB_AUTOTEST_BIGINT_HPP_
#define _SNARKLIB_AUTOTEST_BIGINT_HPP_
#include <cstdint>
#include <gmp.h>
#include <sstream>
#include <string>
#ifdef USE_OLD_LIBSNARK
#include /*libsnark*/ "algebra/fields/bigint.hpp"
#include /*libsnark*/ "common/wnaf.hpp"
#else
#include /*libsnark*/ "algebra/fields/bigint.hpp"
#include /*libsnark*/ "algebra/scalar_multiplication/wnaf.hpp"
#endif
#include "snarklib/AutoTest.hpp"
#include "snarklib/BigInt.hpp"
#include "snarklib/ForeignLib.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// default constructor creates zero number
//
template <mp_size_t N>
class AutoTest_BigIntDefaultConstructorZero : public AutoTest
{
public:
AutoTest_BigIntDefaultConstructorZero()
: AutoTest(N)
{}
void runTest() {
checkPass(m_A.is_zero() && 0 == m_A.as_ulong());
checkPass(m_B.isZero() && 0 == m_B.asUnsignedLong());
checkPass(equal_libsnark(m_A, m_B));
}
private:
const libsnark::bigint<N> m_A;
const BigInt<N> m_B;
};
////////////////////////////////////////////////////////////////////////////////
// unsigned long constructor matches original
//
template <mp_size_t N>
class AutoTest_BigIntUnsignedLongConstructor : public AutoTest
{
public:
AutoTest_BigIntUnsignedLongConstructor(const unsigned long start,
const unsigned long step,
const std::size_t count)
: AutoTest(start, step, count),
m_start(start),
m_step(step),
m_count(count)
{}
void runTest() {
unsigned long v = m_start;
for (std::size_t i = 0; i < m_count; ++i) {
const libsnark::bigint<N> A(v);
const BigInt<N> B(v);
checkPass(equal_libsnark(A, B));
checkPass(v == A.as_ulong());
checkPass(v == B.asUnsignedLong());
v += m_step;
}
}
private:
const unsigned long m_start, m_step;
const std::size_t m_count;
};
////////////////////////////////////////////////////////////////////////////////
// string constructor matches original
//
template <mp_size_t N>
class AutoTest_BigIntStringConstructor : public AutoTest
{
public:
AutoTest_BigIntStringConstructor(const std::string& base10)
: AutoTest(base10),
m_A(base10.c_str()),
m_B(base10)
{}
void runTest() {
checkPass(equal_libsnark(m_A, m_B));
}
private:
const libsnark::bigint<N> m_A;
const BigInt<N> m_B;
};
////////////////////////////////////////////////////////////////////////////////
// equality matches original
//
template <mp_size_t N>
class AutoTest_BigIntEquality : public AutoTest
{
public:
AutoTest_BigIntEquality(const std::string& base10_LHS,
const std::string& base10_RHS)
: AutoTest(base10_LHS, base10_RHS),
m_lhsA(base10_LHS.c_str()),
m_rhsA(base10_RHS.c_str()),
m_lhsB(base10_LHS),
m_rhsB(base10_RHS)
{}
void runTest() {
checkPass((m_lhsA == m_rhsA) == (m_lhsB == m_rhsB));
checkPass((m_lhsA != m_rhsA) == (m_lhsB != m_rhsB));
}
private:
const libsnark::bigint<N> m_lhsA, m_rhsA;
const BigInt<N> m_lhsB, m_rhsB;
};
////////////////////////////////////////////////////////////////////////////////
// stream output matches original
//
template <mp_size_t N>
class AutoTest_BigIntStreamOutput : public AutoTest
{
public:
AutoTest_BigIntStreamOutput(const std::string& base10)
: AutoTest(base10),
m_A(base10.c_str()),
m_B(base10)
{}
void runTest() {
std::stringstream ssA, ssB;
ssA << m_A;
ssB << m_B;
checkPass(ssA.str() == ssB.str());
}
private:
const libsnark::bigint<N> m_A;
const BigInt<N> m_B;
};
////////////////////////////////////////////////////////////////////////////////
// number bits matches original
//
template <mp_size_t N>
class AutoTest_BigIntNumBits : public AutoTest
{
public:
AutoTest_BigIntNumBits(const std::string& base10)
: AutoTest(base10),
m_A(base10.c_str()),
m_B(base10)
{}
void runTest() {
checkPass(m_A.num_bits() == m_B.numBits());
}
private:
const libsnark::bigint<N> m_A;
const BigInt<N> m_B;
};
////////////////////////////////////////////////////////////////////////////////
// test bits match original
//
template <mp_size_t N>
class AutoTest_BigIntTestBits : public AutoTest
{
public:
AutoTest_BigIntTestBits(const std::string& base10)
: AutoTest(base10),
m_A(base10.c_str()),
m_B(base10)
{}
void runTest() {
const std::size_t
A_bits = m_A.max_bits(),
B_bits = m_B.maxBits();
if (checkPass(A_bits == B_bits)) {
for (std::size_t i = 0; i < A_bits; ++i)
checkPass(m_A.test_bit(i) == m_B.testBit(i));
}
}
private:
const libsnark::bigint<N> m_A;
const BigInt<N> m_B;
};
////////////////////////////////////////////////////////////////////////////////
// weighted non-adjacent form matches original
//
template <mp_size_t N>
class AutoTest_BigIntFindwNAF : public AutoTest
{
public:
AutoTest_BigIntFindwNAF(const std::size_t w,
const std::string& base10)
: AutoTest(w, base10),
m_w(w),
m_A(base10.c_str()),
m_B(base10)
{}
void runTest() {
#ifdef USE_OLD_LIBSNARK
const auto a = libsnark::find_wNAF(m_w, m_A);
#else
const auto a = libsnark::find_wnaf(m_w, m_A);
#endif
const auto b = find_wNAF(m_w, m_B);
if (checkPass(a.size() == b.size())) {
for (std::size_t i = 0; i < a.size(); ++i) {
checkPass(a[i] == b[i]);
}
}
}
private:
const std::size_t m_w;
const libsnark::bigint<N> m_A;
const BigInt<N> m_B;
};
} // namespace snarklib
#endif