-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAutoTest_WindowExp.hpp
352 lines (282 loc) · 10.6 KB
/
AutoTest_WindowExp.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
#ifndef _SNARKLIB_AUTOTEST_WINDOW_EXP_HPP_
#define _SNARKLIB_AUTOTEST_WINDOW_EXP_HPP_
#include <cstdint>
#include <vector>
#ifdef USE_OLD_LIBSNARK
#include /*libsnark*/ "encoding/multiexp.hpp"
#else
#include /*libsnark*/ "algebra/scalar_multiplication/multiexp.hpp"
#endif
#include "snarklib/AutoTest.hpp"
#include "snarklib/AuxSTL.hpp"
#include "snarklib/ForeignLib.hpp"
#include "snarklib/WindowExp.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// window table size matches original
//
template <typename T, typename U>
class AutoTest_WindowExpSize : public AutoTest
{
public:
AutoTest_WindowExpSize(const std::size_t exp_count)
: AutoTest(exp_count),
m_exp_count(exp_count)
{}
void runTest() {
const auto a = libsnark::get_exp_window_size<U>(m_exp_count);
const auto b = WindowExp<T>::windowBits(m_exp_count);
checkPass(a == b);
}
private:
const std::size_t m_exp_count;
};
////////////////////////////////////////////////////////////////////////////////
// window table exponentiation matches original
//
template <typename T, typename F, typename U, typename G>
class AutoTest_WindowExp_exp : public AutoTest
{
public:
AutoTest_WindowExp_exp(const std::size_t exp_count,
const F& value)
: AutoTest(exp_count, value),
m_exp_count(exp_count),
m_B(value)
{
copy_libsnark(m_B, m_A);
}
AutoTest_WindowExp_exp(const std::size_t exp_count)
: AutoTest_WindowExp_exp{exp_count, F::random()}
{}
void runTest() {
const auto a = libsnark::get_exp_window_size<U>(m_exp_count);
const auto b = WindowExp<T>::windowBits(m_exp_count);
if (! checkPass(a == b)) return;
const auto A = libsnark::get_window_table(G::num_bits,
#ifdef USE_OLD_LIBSNARK
U::zero(),
#endif
a,
U::one());
const WindowExp<T> B(m_exp_count);
const auto valueA = libsnark::windowed_exp(G::num_bits, a, A, m_A);
const auto valueB = B.exp(m_B);
checkPass(equal_libsnark(valueA, valueB));
}
private:
const std::size_t m_exp_count;
G m_A;
const F m_B;
};
////////////////////////////////////////////////////////////////////////////////
// window table batch exponentiation matches original
//
template <typename T, typename F, typename U, typename G>
class AutoTest_WindowExp_batchExp : public AutoTest
{
public:
AutoTest_WindowExp_batchExp(const std::size_t exp_count,
const std::size_t vecSize)
: AutoTest(exp_count, vecSize),
m_exp_count(exp_count),
m_vecSize(vecSize),
m_A(vecSize, G::zero())
{
m_B.reserve(vecSize);
for (std::size_t i = 0; i < vecSize; ++i) {
m_B.emplace_back(F::random());
copy_libsnark(m_B[i], m_A[i]);
}
}
void runTest() {
const auto a = libsnark::get_exp_window_size<U>(m_exp_count);
const auto b = WindowExp<T>::windowBits(m_exp_count);
if (! checkPass(a == b)) return;
const auto A = libsnark::get_window_table(G::num_bits,
#ifdef USE_OLD_LIBSNARK
U::zero(),
#endif
a,
U::one());
const WindowExp<T> B(m_exp_count);
const auto valueA = libsnark::batch_exp(G::num_bits, a, A, m_A);
const auto valueB = B.batchExp(m_B);
if (checkPass(valueA.size() == valueB.size()) &&
checkPass(valueA.size() == m_vecSize))
{
for (std::size_t i = 0; i < m_vecSize; ++i){
checkPass(equal_libsnark(m_A[i], m_B[i]));
}
}
}
private:
const std::size_t m_exp_count, m_vecSize;
std::vector<G> m_A;
std::vector<F> m_B;
};
////////////////////////////////////////////////////////////////////////////////
// compare map-reduce with monolithic window table exponentiation
//
template <typename T, typename F>
class AutoTest_WindowExp_expMapReduce : public AutoTest
{
public:
AutoTest_WindowExp_expMapReduce(const std::size_t exp_count,
const F& value)
: AutoTest(exp_count, value),
m_exp_count(exp_count),
m_value(value)
{}
AutoTest_WindowExp_expMapReduce(const std::size_t exp_count)
: AutoTest_WindowExp_expMapReduce{exp_count, F::random()}
{}
void runTest() {
const WindowExp<T> A(m_exp_count);
const auto result_A = A.exp(m_value);
const auto space = WindowExp<T>::space(m_exp_count);
// try all possible block partitionings
for (std::size_t numBlocks = 1; numBlocks <= space.globalID()[0]; ++numBlocks) {
auto idx = space;
idx.blockPartition(std::array<std::size_t, 1>{ numBlocks });
auto result_B = T::zero();
// mapping
for (std::size_t block = 0; block < numBlocks; ++block) {
const WindowExp<T> B(idx, block);
// reducing
result_B = result_B + B.exp(m_value);
}
checkPass(result_A == result_B);
}
}
private:
const std::size_t m_exp_count;
const F m_value;
};
////////////////////////////////////////////////////////////////////////////////
// compare block partitioned with standard vector batch exponentiation
//
template <typename T, typename F>
class AutoTest_WindowExp_batchExpMapReduce1 : public AutoTest
{
public:
AutoTest_WindowExp_batchExpMapReduce1(const std::size_t exp_count,
const std::size_t vecSize)
: AutoTest(exp_count, vecSize),
m_exp_count(exp_count),
m_vecSize(vecSize)
{
m_vec.reserve(vecSize);
for (std::size_t i = 0; i < vecSize; ++i)
m_vec.emplace_back(F::random());
}
void runTest() {
const WindowExp<T> A(m_exp_count);
const auto result_A = A.batchExp(m_vec);
const auto space = BlockVector<F>::space(m_vec);
// try all possible block partitionings
for (std::size_t numBlocks = 1; numBlocks <= space.globalID()[0]; ++numBlocks) {
auto idx = space;
idx.blockPartition(std::array<std::size_t, 1>{ numBlocks });
std::vector<T> result_B(m_vecSize);
for (std::size_t block = 0; block < numBlocks; ++block) {
BlockVector<F> partvec(idx, block, m_vec);
A.batchExp(partvec).emplace(result_B);
}
checkPass(result_A == result_B);
}
}
private:
const std::size_t m_exp_count, m_vecSize;
std::vector<F> m_vec;
};
////////////////////////////////////////////////////////////////////////////////
// map-reduce window tables and block partitioned vector batch exponentiation
//
template <typename T, typename F>
class AutoTest_WindowExp_batchExpMapReduce2 : public AutoTest
{
public:
AutoTest_WindowExp_batchExpMapReduce2(const std::size_t exp_count,
const std::size_t vecSize)
: AutoTest(exp_count, vecSize),
m_exp_count(exp_count),
m_vecSize(vecSize)
{
m_vec.reserve(vecSize);
for (std::size_t i = 0; i < vecSize; ++i)
m_vec.emplace_back(F::random());
}
void runTest() {
const WindowExp<T> A(m_exp_count);
const auto result_A = A.batchExp(m_vec);
const auto winSpace = WindowExp<T>::space(m_exp_count);
const auto vecSpace = BlockVector<F>::space(m_vec);
// just try three partitionings of window table
for (const auto numWinBlks : std::array<std::size_t, 3>{ 1, 2, winSpace.globalID()[0] }) {
auto winIdx = winSpace;
winIdx.blockPartition(std::array<std::size_t, 1>{ numWinBlks });
// try all possible block partitionings of vector
for (std::size_t numVecBlks = 1; numVecBlks <= vecSpace.globalID()[0]; ++numVecBlks) {
auto vecIdx = vecSpace;
vecIdx.blockPartition(std::array<std::size_t, 1>{ numVecBlks });
std::vector<T> result_B(m_vecSize);
// (outer loop) iterate over windows
for (std::size_t winblock = 0; winblock < numWinBlks; ++winblock) {
// partial window table is expensive
// ***must be in outer loop***
const WindowExp<T> partwin(winIdx, winblock);
// (inner loop) iterate over vector blocks
for (std::size_t vecblock = 0; vecblock < numVecBlks; ++vecblock) {
// read in
BlockVector<T> result(vecIdx, vecblock, result_B);
// accumulate from partial window table
const BlockVector<F> partvec(vecIdx, vecblock, m_vec);
result += partwin.batchExp(partvec);
// write back
result.emplace(result_B);
}
}
checkPass(result_A == result_B);
}
}
}
private:
const std::size_t m_exp_count, m_vecSize;
std::vector<F> m_vec;
};
////////////////////////////////////////////////////////////////////////////////
// window exponentiation with index space block partition
//
template <typename G, typename F>
class AutoTest_WindowExp_expPartition : public AutoTest
{
public:
AutoTest_WindowExp_expPartition(const std::size_t exp_count,
const std::size_t numWindowBlocks)
: AutoTest(exp_count, numWindowBlocks),
m_exp_count(exp_count),
m_numWindowBlocks(numWindowBlocks)
{}
void runTest() {
const auto space = WindowExp<G>::space(m_exp_count);
const WindowExp<G> gTable(space, 0);
auto spaceBlk = space;
spaceBlk.blockPartition(std::array<std::size_t, 1>{ m_numWindowBlocks });
auto x = F::zero();
for (std::size_t i = 0; i < 100; ++i) {
const auto a = gTable.exp(x);
G b = G::zero();
for (std::size_t j = 0; j < spaceBlk.blockID()[0]; ++j) {
const WindowExp<G> gTableBlk(spaceBlk, j);
b = b + gTableBlk.exp(x);
}
checkPass(a == b);
x = x + F::one();
}
}
private:
const std::size_t m_exp_count, m_numWindowBlocks;
};
} // namespace snarklib
#endif