-
Notifications
You must be signed in to change notification settings - Fork 11
/
WindowExp.hpp
354 lines (277 loc) · 10.4 KB
/
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
353
354
#ifndef _SNARKLIB_WINDOW_EXP_HPP_
#define _SNARKLIB_WINDOW_EXP_HPP_
#include <cassert>
#include <cstdint>
#include <gmp.h>
#include <vector>
#include <snarklib/AuxSTL.hpp>
#include <snarklib/BigInt.hpp>
#include <snarklib/Group.hpp>
#include <snarklib/IndexSpace.hpp>
#include <snarklib/ProgressCallback.hpp>
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// Window table made from powers of group generator
//
template <typename GROUP>
class WindowExp
{
typedef typename GROUP::ScalarField Fr;
public:
// public for direct testing with libsnark::get_exp_window_size()
static std::size_t windowBits(const std::size_t expCount) {
const auto& WT = GROUP::params.fixed_base_exp_window_table();
for (long i = WT.size() - 1; i >= 0; --i) {
if (WT[i] != 0 && expCount >= WT[i])
return i + 1;
}
return 1;
}
// one-dimensional index space over windows (rows)
static IndexSpace<1> space(const std::size_t expCount) {
const auto wb = windowBits(expCount);
IndexSpace<1> a(numWindows(wb));
a.param(wb);
return a;
}
const IndexSpace<1>& space() const { return m_space; }
const std::array<std::size_t, 1>& block() const { return m_block; }
// null window
WindowExp()
: m_space(),
m_windowBits(0),
m_block{0},
m_powers_of_g()
{}
// map-reduce version
WindowExp(const IndexSpace<1>& space,
const std::array<std::size_t, 1>& block,
const GROUP generator = GROUP::one())
: m_space(space),
m_windowBits(space.param()[0]),
m_block(block),
m_powers_of_g(space.indexSize(m_block)[0],
std::vector<GROUP>(windowSize(), GROUP::zero()))
{
GROUP outerG = generator;
const std::size_t startLen = startRow() * m_windowBits;
for (std::size_t i = 0; i < startLen; ++i)
outerG = outerG + outerG;
const std::size_t N = m_powers_of_g.size();
const bool lastBlock = block[0] == space.blockID()[0] - 1;
// iterate over window rows
for (std::size_t outer = 0; outer < N; ++outer) {
GROUP innerG = GROUP::zero();
const bool lastRow = lastBlock && outer == N - 1;
const std::size_t cur_in_window = lastRow
? lastInWindow()
: m_powers_of_g[outer].size();
// iterate inside window
for (std::size_t inner = 0; inner < cur_in_window; ++inner) {
m_powers_of_g[outer][inner] = innerG;
innerG = innerG + outerG;
}
if (! lastRow) {
for (std::size_t i = 0; i < m_windowBits; ++i)
outerG = outerG + outerG;
}
}
}
WindowExp(const IndexSpace<1>& space,
const std::size_t block,
const GROUP generator = GROUP::one())
: WindowExp{space, std::array<std::size_t, 1>{block}, generator}
{}
// monolithic version with progress bar
WindowExp(const std::size_t expCount,
ProgressCallback* callback = nullptr,
const GROUP generator = GROUP::one())
: m_space(space(expCount)),
m_windowBits(m_space.param()[0]),
m_block{0},
m_powers_of_g(m_space.indexSize(m_block)[0],
std::vector<GROUP>(windowSize(), GROUP::zero()))
{
const std::size_t N = m_powers_of_g.size();
const std::size_t M = callback ? callback->minorSteps() : 0;
GROUP outerG = generator;
std::size_t outer = 0;
// full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
GROUP innerG = GROUP::zero();
const bool lastRow = (outer == N - 1);
const std::size_t cur_in_window = lastRow
? lastInWindow()
: m_powers_of_g[outer].size();
for (std::size_t inner = 0; inner < cur_in_window; ++inner) {
m_powers_of_g[outer][inner] = innerG;
innerG = innerG + outerG;
}
if (! lastRow) {
for (std::size_t i = 0; i < m_windowBits; ++i)
outerG = outerG + outerG;
}
++outer;
}
callback->minor();
}
// remaining steps smaller than one block
while (outer < N) {
GROUP innerG = GROUP::zero();
const bool lastRow = (outer == N - 1);
const std::size_t cur_in_window = lastRow
? lastInWindow()
: m_powers_of_g[outer].size();
for (std::size_t inner = 0; inner < cur_in_window; ++inner) {
m_powers_of_g[outer][inner] = innerG;
innerG = innerG + outerG;
}
if (! lastRow) {
for (std::size_t i = 0; i < m_windowBits; ++i)
outerG = outerG + outerG;
}
++outer;
}
}
// works for both map-reduce and monolithic versions
GROUP exp(const Fr& exponent) const {
const auto pow_val = exponent[0].asBigInt();
GROUP res = GROUP::zero();
const std::size_t offset = startRow();
for (std::size_t j = 0; j < m_powers_of_g.size(); ++j) {
const std::size_t outer = offset + j;
std::size_t inner = 0;
for (std::size_t i = 0; i < m_windowBits; ++i) {
if (pow_val.testBit(outer * m_windowBits + i))
inner |= 1u << i;
}
res = res + m_powers_of_g[j][inner];
}
return res;
}
// works for both map-reduce and monolithic versions
std::vector<GROUP> batchExp(const std::vector<Fr>& exponentVec,
ProgressCallback* callback = nullptr) const
{
const std::size_t N = exponentVec.size();
const std::size_t M = callback ? callback->minorSteps() : 0;
std::vector<GROUP> res(N, GROUP::zero());
std::size_t i = 0;
// for full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
res[i] = exp(exponentVec[i]);
++i;
}
callback->minor();
}
// remaining steps smaller than one block
while (i < N) {
res[i] = exp(exponentVec[i]);
++i;
}
return res;
}
// works for both map-reduce and monolithic versions
void batchExp(std::vector<GROUP>& res,
const std::vector<Fr>& exponentVec,
ProgressCallback* callback = nullptr) const
{
#ifdef USE_ASSERT
assert(res.size() == exponentVec.size());
#endif
const std::size_t N = exponentVec.size();
const std::size_t M = callback ? callback->minorSteps() : 0;
std::size_t i = 0;
// for full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
res[i] = res[i] + exp(exponentVec[i]);
++i;
}
callback->minor();
}
// remaining steps smaller than one block
while (i < N) {
res[i] = res[i] + exp(exponentVec[i]);
++i;
}
}
// works for both map-reduce and monolithic versions
// additional map-reduce dimension from block partitioning of vector
BlockVector<GROUP> batchExp(const BlockVector<Fr>& exponentVec,
ProgressCallback* callback = nullptr) const
{
const std::size_t N = exponentVec.size();
const std::size_t M = callback ? callback->minorSteps() : 0;
BlockVector<GROUP> res(exponentVec.space(), exponentVec.block());
std::size_t i = exponentVec.startIndex();
// for full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
res[i] = exp(exponentVec[i]);
++i;
}
callback->minor();
}
// remaining steps smaller than one block
while (i < exponentVec.stopIndex()) {
res[i] = exp(exponentVec[i]);
++i;
}
return res;
}
// works for both map-reduce and monolithic versions
// additional map-reduce dimension from block partitioning of vector
void batchExp(BlockVector<GROUP>& res,
const BlockVector<Fr>& exponentVec,
ProgressCallback* callback = nullptr) const
{
#ifdef USE_ASSERT
assert(res.space() == exponentVec.space() &&
res.block() == exponentVec.block());
#endif
const std::size_t N = exponentVec.size();
const std::size_t M = callback ? callback->minorSteps() : 0;
std::size_t i = exponentVec.startIndex();
// for full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
res[i] = res[i] + exp(exponentVec[i]);
++i;
}
callback->minor();
}
// remaining steps smaller than one block
while (i < exponentVec.stopIndex()) {
res[i] = res[i] + exp(exponentVec[i]);
++i;
}
}
private:
static std::size_t numBits() {
return GROUP::ScalarField::BaseType::sizeInBits();
}
static std::size_t numWindows(const std::size_t windowbits) {
return (numBits() + windowbits - 1) / windowbits;
}
static std::size_t windowSize(const std::size_t windowbits) {
return 1u << windowbits;
}
static std::size_t lastInWindow(const std::size_t windowbits) {
return 1u << (numBits() - (numWindows(windowbits) - 1) * windowbits);
}
std::size_t numWindows() const { return numWindows(m_windowBits); }
std::size_t windowSize() const { return windowSize(m_windowBits); }
std::size_t lastInWindow() const { return lastInWindow(m_windowBits); }
std::size_t startRow() const {
return m_space.indexOffset(m_block)[0];
}
const IndexSpace<1> m_space;
const std::size_t m_windowBits;
const std::array<std::size_t, 1> m_block;
std::vector<std::vector<GROUP>> m_powers_of_g;
};
} // namespace snarklib
#endif