forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTest_Marshalling.hpp
379 lines (296 loc) · 9.27 KB
/
AutoTest_Marshalling.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
#ifndef _SNARKLIB_AUTOTEST_MARSHALLING_HPP_
#define _SNARKLIB_AUTOTEST_MARSHALLING_HPP_
#include <cstdint>
#include <gmp.h>
#include <sstream>
#include "AutoTest.hpp"
#include "AuxSTL.hpp"
#include "BigInt.hpp"
#include "Field.hpp"
#include "Group.hpp"
#include "Pairing.hpp"
#include "PPZK_keypair.hpp"
#include "PPZK_keystruct.hpp"
#include "PPZK_proof.hpp"
#include "PPZK_query.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// roundtrip for SparseVector<Pairing<GA, GB>>
//
template <typename GA, typename GB>
class AutoTest_Marshal_SparseVectorPairing : public AutoTest
{
public:
AutoTest_Marshal_SparseVectorPairing(const std::size_t numberElems,
const std::size_t startIndex)
: AutoTest(numberElems, startIndex)
{
randomSparseVector(m_A, numberElems, startIndex);
}
void runTest() {
std::stringstream oss;
m_A.marshal_out(oss);
SparseVector<Pairing<GA, GB>> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(m_A == B);
}
private:
SparseVector<Pairing<GA, GB>> m_A;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for BigInt<>, Field<>, Group<>
//
template <typename T>
class AutoTest_Marshal_BFG : public AutoTest
{
public:
AutoTest_Marshal_BFG()
: AutoTest(),
m_A(T::random())
{}
void runTest() {
std::stringstream oss;
m_A.marshal_out(oss);
T B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(m_A == B);
}
private:
const T m_A;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for Pairing<GA, GB>
//
template <typename GA, typename GB>
class AutoTest_Marshal_Pairing : public AutoTest
{
public:
AutoTest_Marshal_Pairing()
: AutoTest(),
m_A(GA::random(), GB::random())
{}
void runTest() {
std::stringstream oss;
m_A.marshal_out(oss);
Pairing<GA, GB> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(m_A == B);
}
private:
const Pairing<GA, GB> m_A;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for PPZK_ProvingKey<PAIRING>
//
template <typename PAIRING>
class AutoTest_Marshal_ProvingKey : public AutoTest
{
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
public:
AutoTest_Marshal_ProvingKey(const std::size_t numberElems,
const std::size_t startIndex)
: AutoTest(numberElems, startIndex),
m_numberElems(numberElems),
m_startIndex(startIndex)
{}
void runTest() {
SparseVector<Pairing<G1, G1>> A_query;
SparseVector<Pairing<G2, G1>> B_query;
SparseVector<Pairing<G1, G1>> C_query;
std::vector<G1> H_query;
std::vector<G1> K_query;
randomSparseVector(A_query, m_numberElems, m_startIndex);
randomSparseVector(B_query, m_numberElems, m_startIndex);
randomSparseVector(C_query, m_numberElems, m_startIndex);
randomVector(H_query, m_numberElems);
randomVector(K_query, m_numberElems);
const PPZK_ProvingKey<PAIRING> A(A_query,
B_query,
C_query,
H_query,
K_query);
std::stringstream oss;
A.marshal_out(oss);
PPZK_ProvingKey<PAIRING> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(A == B);
}
private:
const std::size_t m_numberElems, m_startIndex;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for PPZK_QueryIC<PAIRING>
//
template <typename PAIRING>
class AutoTest_Marshal_QueryIC : public AutoTest
{
typedef typename PAIRING::G1 G1;
public:
AutoTest_Marshal_QueryIC(const std::size_t numberElems)
: AutoTest(numberElems),
m_numberElems(numberElems)
{}
void runTest() {
std::vector<G1> encoded_terms;
randomVector(encoded_terms, m_numberElems);
const PPZK_QueryIC<PAIRING> A(G1::random(),
encoded_terms);
std::stringstream oss;
A.marshal_out(oss);
PPZK_QueryIC<PAIRING> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(A == B);
}
private:
const std::size_t m_numberElems;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for PPZK_VerificationKey<PAIRING>
//
template <typename PAIRING>
class AutoTest_Marshal_VerificationKey : public AutoTest
{
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
public:
AutoTest_Marshal_VerificationKey(const std::size_t numberElems)
: AutoTest(numberElems),
m_numberElems(numberElems)
{}
void runTest() {
std::vector<G1> encoded_terms;
randomVector(encoded_terms, m_numberElems);
const PPZK_VerificationKey<PAIRING>
A(G2::random(),
G1::random(),
G2::random(),
G2::random(),
G1::random(),
G2::random(),
G2::random(),
PPZK_QueryIC<PAIRING>(G1::random(), encoded_terms));
std::stringstream oss;
A.marshal_out(oss);
PPZK_VerificationKey<PAIRING> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(A == B);
}
private:
const std::size_t m_numberElems;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for PPZK_Keypair<PAIRING>
//
template <typename PAIRING>
class AutoTest_Marshal_Keypair : public AutoTest
{
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
public:
AutoTest_Marshal_Keypair(const std::size_t numberElems,
const std::size_t startIndex)
: AutoTest(numberElems, startIndex),
m_numberElems(numberElems),
m_startIndex(startIndex)
{}
void runTest() {
SparseVector<Pairing<G1, G1>> A_query;
SparseVector<Pairing<G2, G1>> B_query;
SparseVector<Pairing<G1, G1>> C_query;
std::vector<G1> H_query;
std::vector<G1> K_query;
randomSparseVector(A_query, m_numberElems, m_startIndex);
randomSparseVector(B_query, m_numberElems, m_startIndex);
randomSparseVector(C_query, m_numberElems, m_startIndex);
randomVector(H_query, m_numberElems);
randomVector(K_query, m_numberElems);
std::vector<G1> encoded_terms;
randomVector(encoded_terms, m_numberElems);
const PPZK_Keypair<PAIRING>
A(PPZK_ProvingKey<PAIRING>(
A_query,
B_query,
C_query,
H_query,
K_query),
PPZK_VerificationKey<PAIRING>(
G2::random(),
G1::random(),
G2::random(),
G2::random(),
G1::random(),
G2::random(),
G2::random(),
PPZK_QueryIC<PAIRING>(G1::random(), encoded_terms)));
std::stringstream oss;
A.marshal_out(oss);
PPZK_Keypair<PAIRING> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(A == B);
}
private:
const std::size_t m_numberElems, m_startIndex;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for PPZK_Proof<PAIRING>
//
template <typename PAIRING>
class AutoTest_Marshal_Proof : public AutoTest
{
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
public:
AutoTest_Marshal_Proof()
: AutoTest(),
m_A(Pairing<G1, G1>(G1::random(), G1::random()),
Pairing<G2, G1>(G2::random(), G1::random()),
Pairing<G1, G1>(G1::random(), G1::random()),
G1::random(),
G1::random())
{}
void runTest() {
std::stringstream oss;
m_A.marshal_out(oss);
PPZK_Proof<PAIRING> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(m_A == B);
}
private:
const PPZK_Proof<PAIRING> m_A;
};
////////////////////////////////////////////////////////////////////////////////
// roundtrip for R1Witness<FIELD>
//
template <typename FIELD>
class AutoTest_Marshal_R1Witness : public AutoTest
{
public:
AutoTest_Marshal_R1Witness(const std::size_t numberElems)
: AutoTest(numberElems)
{
for (std::size_t i = 0; i < numberElems; ++i) {
m_A.assignVar(R1Variable<FIELD>(i + 1), FIELD::random());
}
}
void runTest() {
std::stringstream oss;
m_A.marshal_out(oss);
R1Witness<FIELD> B;
std::stringstream iss(oss.str());
checkPass(B.marshal_in(iss));
checkPass(m_A == B);
}
private:
R1Witness<FIELD> m_A;
};
} // namespace snarklib
#endif