forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pairing.hpp
380 lines (308 loc) · 10.6 KB
/
Pairing.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#ifndef _SNARKLIB_PAIRING_HPP_
#define _SNARKLIB_PAIRING_HPP_
#include <cstdint>
#include <gmp.h>
#include <istream>
#include <ostream>
#include <vector>
#include "AuxSTL.hpp"
#include "BigInt.hpp"
#include "Group.hpp"
#include "ProgressCallback.hpp"
#include "WindowExp.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// Paired group knowledge commitment
//
template <typename GA, typename GB>
class Pairing
{
public:
Pairing() = default;
Pairing(const GA& a, const GB& b)
: m_G(a), m_H(b)
{}
// breaks encapsulation
const GA& G() const { return m_G; }
const GB& H() const { return m_H; }
bool operator== (const Pairing& other) const {
return m_G == other.m_G && m_H == other.m_H;
}
bool operator!= (const Pairing& other) const {
return ! (*this == other);
}
bool isZero() const {
return *this == zero();
}
static Pairing zero() {
return Pairing<GA, GB>(GA::zero(), GB::zero());
}
void marshal_out(std::ostream& os) const {
G().marshal_out(os);
H().marshal_out(os);
}
bool marshal_in(std::istream& is) {
return
m_G.marshal_in(is) &&
m_H.marshal_in(is);
}
private:
GA m_G;
GB m_H;
};
////////////////////////////////////////////////////////////////////////////////
// Operator functions
//
template <typename GA, typename GB>
Pairing<GA, GB> operator+ (const Pairing<GA, GB>& a, const Pairing<GA, GB>& b) {
return Pairing<GA, GB>(a.G() + b.G(),
a.H() + b.H());
}
template <typename T, typename GA, typename GB>
Pairing<GA, GB> operator* (const T& a, const Pairing<GA, GB>& b) {
return Pairing<GA, GB>(a * b.G(),
a * b.H());
}
template <typename GA, typename GB>
Pairing<GA, GB> fastAddSpecial(const Pairing<GA, GB>& a,
const Pairing<GA, GB>& b) {
return Pairing<GA, GB>(fastAddSpecial(a.G(), b.G()),
fastAddSpecial(a.H(), b.H()));
}
template <typename GA, typename GB>
SparseVector<Pairing<GA, GB>>& batchSpecial(SparseVector<Pairing<GA, GB>>& vec)
{
std::vector<GA> G_vec(vec.size(), GA::zero());
std::vector<GB> H_vec(vec.size(), GB::zero());
for (std::size_t i = 0; i < vec.size(); ++i) {
G_vec[i] = vec.getElement(i).G();
H_vec[i] = vec.getElement(i).H();
}
batchSpecial(G_vec);
batchSpecial(H_vec);
for (std::size_t i = 0; i < vec.size(); ++i) {
vec.setElement(i,
Pairing<GA, GB>(G_vec[i], H_vec[i]));
}
return vec;
}
template <mp_size_t N, typename GA, typename GB>
Pairing<GA, GB> wnafExp(const BigInt<N>& scalar,
const Pairing<GA, GB>& base)
{
return Pairing<GA, GB>(wnafExp(scalar, base.G()),
wnafExp(scalar, base.H()));
}
// standard vector, works with map-reduce or monolithic window tables
template <typename GA, typename GB, typename FR>
SparseVector<Pairing<GA, GB>> batchExp(const WindowExp<GA>& tableA,
const WindowExp<GB>& tableB,
const FR& coeffA,
const FR& coeffB,
const std::vector<FR>& vec,
ProgressCallback* callback = nullptr)
{
const std::size_t M = callback ? callback->minorSteps() : 0;
const std::size_t N = vec.size();
SparseVector<Pairing<GA, GB>> res(N, Pairing<GA, GB>::zero());
std::size_t i = 0, index = 0;
// full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
if (! vec[i].isZero()) {
res.setIndexElement(
index++,
i,
Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
++i;
}
callback->minor();
}
// remaining steps smaller than one block
while (i < N) {
if (! vec[i].isZero()) {
res.setIndexElement(
index++,
i,
Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
++i;
}
res.resize(index);
#ifdef USE_ADD_SPECIAL
batchSpecial(res);
#endif
return res;
}
// block partitioned vector, works with map-reduce or monolithic window table
template <typename GA, typename GB, typename FR>
SparseVector<Pairing<GA, GB>> batchExp(const WindowExp<GA>& tableA,
const WindowExp<GB>& tableB,
const FR& coeffA,
const FR& coeffB,
const BlockVector<FR>& vec,
ProgressCallback* callback = nullptr)
{
const std::size_t M = callback ? callback->minorSteps() : 0;
const std::size_t N = vec.size();
SparseVector<Pairing<GA, GB>> res(vec.size(), Pairing<GA, GB>::zero());
std::size_t i = vec.startIndex(), index = 0;
// full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
if (! vec[i].isZero()) {
res.setIndexElement(
index++,
i,
Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
++i;
}
callback->minor();
}
// remaining steps smaller than one block
while (i < vec.stopIndex()) {
if (! vec[i].isZero()) {
res.setIndexElement(
index++,
i,
Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
++i;
}
res.resize(index);
#ifdef USE_ADD_SPECIAL
batchSpecial(res);
#endif
return res;
}
// used with map-reduce
template <typename GA, typename GB, typename FR>
void batchExp(SparseVector<Pairing<GA, GB>>& res, // returned from batchExp()
const WindowExp<GA>& tableA,
const WindowExp<GB>& tableB,
const FR& coeffA,
const FR& coeffB,
const std::vector<FR>& vec,
ProgressCallback* callback = nullptr)
{
const std::size_t M = callback ? callback->minorSteps() : 0;
const std::size_t N = res.size(); // iterate over sparse vector directly
std::size_t index = 0;
// full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
const auto i = res.getIndex(index);
const auto elem = res.getElement(index);
res.setIndexElement(
index++,
i,
elem + Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
callback->minor();
}
// remaining steps smaller than one block
while (index < N) {
const auto i = res.getIndex(index);
const auto elem = res.getElement(index);
res.setIndexElement(
index++,
i,
elem + Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
}
// block partitioned vector, used with map-reduce
template <typename GA, typename GB, typename FR>
void batchExp(SparseVector<Pairing<GA, GB>>& res, // returned from batchExp()
const WindowExp<GA>& tableA,
const WindowExp<GB>& tableB,
const FR& coeffA,
const FR& coeffB,
const BlockVector<FR>& vec,
ProgressCallback* callback = nullptr)
{
const std::size_t M = callback ? callback->minorSteps() : 0;
const std::size_t N = res.size(); // iterate over sparse vector directly
std::size_t index = 0;
// full blocks
for (std::size_t j = 0; j < M; ++j) {
for (std::size_t k = 0; k < N / M; ++k) {
const auto i = res.getIndex(index);
const auto elem = res.getElement(index);
res.setIndexElement(
index++,
i,
elem + Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
callback->minor();
}
// remaining steps smaller than one block
while (index < N) {
const auto i = res.getIndex(index);
const auto elem = res.getElement(index);
res.setIndexElement(
index++,
i,
elem + Pairing<GA, GB>(tableA.exp(coeffA * vec[i]),
tableB.exp(coeffB * vec[i])));
}
}
template <typename GA, typename GB, typename FR>
Pairing<GA, GB> multiExp01(const SparseVector<Pairing<GA, GB>>& base,
const std::vector<FR>& scalar,
const std::size_t minIndex,
const std::size_t maxIndex,
const std::size_t reserveCount, // for performance tuning
ProgressCallback* callback)
{
const auto
ZERO = FR::zero(),
ONE = FR::one();
std::vector<Pairing<GA, GB>> base2;
std::vector<FR> scalar2;
if (reserveCount) {
base2.reserve(reserveCount);
scalar2.reserve(reserveCount);
}
auto accum = Pairing<GA, GB>::zero();
for (std::size_t i = 0; i < base.size(); ++i) {
const auto idx = base.getIndex(i);
if (idx >= maxIndex) {
break;
} else if (idx >= minIndex) {
const auto a = scalar[idx - minIndex];
if (ZERO == a) {
continue;
} else if (ONE == a) {
#ifdef USE_ADD_SPECIAL
accum = fastAddSpecial(accum, base.getElement(i));
#else
accum = accum + base.getElement(i);
#endif
} else {
base2.emplace_back(base.getElement(i));
scalar2.emplace_back(a);
}
}
}
return accum + multiExp(base2, scalar2, callback);
}
template <typename GA, typename GB, typename FR>
Pairing<GA, GB> multiExp01(const SparseVector<Pairing<GA, GB>>& base,
const std::vector<FR>& scalar,
const std::size_t minIndex,
const std::size_t maxIndex,
ProgressCallback* callback = nullptr)
{
return multiExp01(base, scalar, minIndex, maxIndex, 0, callback);
}
} // namespace snarklib
#endif