forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoTest_WindowExp.hpp
More file actions
291 lines (234 loc) · 8.8 KB
/
Copy pathAutoTest_WindowExp.hpp
File metadata and controls
291 lines (234 loc) · 8.8 KB
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
#ifndef _SNARKLIB_AUTOTEST_WINDOW_EXP_HPP_
#define _SNARKLIB_AUTOTEST_WINDOW_EXP_HPP_
#include <cstdint>
#include <vector>
#include "AutoTest.hpp"
#include "AuxSTL.hpp"
#include "encoding/multiexp.hpp"
#include "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)
{
copyData(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, U::zero(), 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(sameData(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());
copyData(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, U::zero(), 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(sameData(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;
};
} // namespace snarklib
#endif