-
Notifications
You must be signed in to change notification settings - Fork 12
/
CompileQAP.hpp
448 lines (373 loc) · 12.8 KB
/
CompileQAP.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#ifndef _SNARKFRONT_COMPILE_QAP_HPP_
#define _SNARKFRONT_COMPILE_QAP_HPP_
#include <array>
#include <cstdint>
#include <fstream>
#include <functional>
#include <string>
#include <snarklib/AuxSTL.hpp>
#include <snarklib/HugeSystem.hpp>
#include <snarklib/IndexSpace.hpp>
#include <snarklib/PPZK_randomness.hpp>
#include <snarklib/QAP_query.hpp>
#include <snarklib/QAP_system.hpp>
#include <snarklib/QAP_witness.hpp>
#include <snarklib/Rank1DSL.hpp>
#include <snarklib/Util.hpp>
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// query ABCH
//
template <typename PAIRING>
class QAP_query_ABCH
{
typedef typename PAIRING::Fr FR;
typedef typename snarklib::QAP_QueryABC<snarklib::HugeSystem, FR> Q_ABC;
typedef typename snarklib::QAP_QueryH<snarklib::HugeSystem, FR> Q_H;
typedef typename snarklib::QAP_SystemPoint<snarklib::HugeSystem, FR> SYSPT;
public:
QAP_query_ABCH(const std::size_t numBlocks,
const std::string& sysfile,
const std::string& randfile)
: m_numBlocks(numBlocks),
m_hugeSystem(sysfile)
{
std::ifstream ifs(randfile);
m_error = !ifs ||
!m_lagrangePoint.marshal_in(ifs) ||
!m_hugeSystem.loadIndex();
}
QAP_query_ABCH(const std::size_t numBlocks,
const std::string& sysfile,
const snarklib::PPZK_LagrangePoint<FR>& lagrangeRand)
: m_numBlocks(numBlocks),
m_hugeSystem(sysfile),
m_lagrangePoint(lagrangeRand)
{
m_error = !m_hugeSystem.loadIndex();
}
// for g1_exp_count() and g2_exp_count() only
QAP_query_ABCH(const std::string& sysfile)
: m_numBlocks(0),
m_hugeSystem(sysfile)
{
m_error = !m_hugeSystem.loadIndex();
}
bool operator! () const { return m_error; }
void A(const std::string& outfile) {
writeFilesABC(outfile, Q_ABC::VecSelect::A);
}
void B(const std::string& outfile) {
writeFilesABC(outfile, Q_ABC::VecSelect::B);
}
void C(const std::string& outfile) {
writeFilesABC(outfile, Q_ABC::VecSelect::C);
}
void H(const std::string& outfile) {
writeFilesH(outfile);
}
std::size_t g1_exp_count(const std::string& afile,
const std::string& bfile,
const std::string& cfile,
const std::string& hfile) {
return snarklib::g1_exp_count(
m_hugeSystem.maxIndex(), // same as QAP numVariables()
m_hugeSystem.numCircuitInputs(), // same as QAP numCircuitInputs()
nonzeroCount(afile),
nonzeroCount(bfile),
nonzeroCount(cfile),
nonzeroCount(hfile));
}
std::size_t g2_exp_count(const std::string& bfile) {
return snarklib::g2_exp_count(
nonzeroCount(bfile));
}
private:
template <typename QUERY>
void writeFiles(const std::string& outfile,
std::function<QUERY (const SYSPT&)> func)
{
const SYSPT qap(m_hugeSystem,
m_hugeSystem.numCircuitInputs(),
m_lagrangePoint.point());
if (qap.weakPoint()) {
// Lagrange evaluation point is root of unity
m_error = true;
return;
}
const auto Q = func(qap);
auto space = snarklib::BlockVector<FR>::space(Q.vec());
space.blockPartition(std::array<size_t, 1>{m_numBlocks});
space.param(Q.nonzeroCount());
std::ofstream ofs(outfile);
if (!ofs || !write_blockvector_raw(outfile, space, Q.vec()))
m_error = true;
else
space.marshal_out(ofs);
}
void writeFilesABC(const std::string& outfile,
const unsigned int mask) {
writeFiles<Q_ABC>(
outfile,
[&mask] (const SYSPT& qap) { return Q_ABC(qap, mask); });
}
void writeFilesH(const std::string& outfile) {
writeFiles<Q_H>(
outfile,
[] (const SYSPT& qap) { return Q_H(qap); });
}
std::size_t nonzeroCount(const std::string& abchfile) {
snarklib::IndexSpace<1> space;
std::ifstream ifs(abchfile);
return (!ifs || !space.marshal_in(ifs))
? m_error = false
: space.param()[0];
}
const std::size_t m_numBlocks;
snarklib::PPZK_LagrangePoint<FR> m_lagrangePoint;
snarklib::HugeSystem<FR> m_hugeSystem;
bool m_error;
};
////////////////////////////////////////////////////////////////////////////////
// query K
//
template <typename PAIRING>
class QAP_query_K
{
typedef typename PAIRING::Fr FR;
typedef typename snarklib::QAP_SystemPoint<snarklib::HugeSystem, FR> SYSPT;
typedef typename snarklib::QAP_QueryK<snarklib::HugeSystem, FR> QTYPE;
public:
QAP_query_K(const std::string& afile,
const std::string& bfile,
const std::string& cfile,
const std::string& sysfile,
const std::string& randfile)
: m_afile(afile),
m_bfile(bfile),
m_cfile(cfile),
m_hugeSystem(sysfile)
{
std::ifstream ifs(randfile);
m_error = !ifs ||
!m_lagrangePoint.marshal_in(ifs) ||
!m_clearGreeks.marshal_in(ifs) ||
!m_hugeSystem.loadIndex();
}
QAP_query_K(const std::string& afile,
const std::string& bfile,
const std::string& cfile,
const std::string& sysfile,
const snarklib::PPZK_LagrangePoint<FR>& lagrangeRand,
const snarklib::PPZK_BlindGreeks<FR, FR>& greeksRand)
: m_afile(afile),
m_bfile(bfile),
m_cfile(cfile),
m_hugeSystem(sysfile),
m_lagrangePoint(lagrangeRand),
m_clearGreeks(greeksRand)
{
m_error = !m_hugeSystem.loadIndex();
}
bool operator! () const { return m_error; }
void K(const std::string& outfile,
const std::size_t blocknum) {
writeFiles(outfile, blocknum);
}
private:
void writeFiles(const std::string& outfile,
const std::size_t blocknum)
{
const SYSPT qap(m_hugeSystem,
m_hugeSystem.numCircuitInputs(),
m_lagrangePoint.point());
if (qap.weakPoint()) {
// Lagrange evaluation point is root of unity
m_error = true;
return;
}
QTYPE Q(qap,
m_clearGreeks.beta_rA(),
m_clearGreeks.beta_rB(),
m_clearGreeks.beta_rC());
std::size_t block = (-1 == blocknum) ? 0 : blocknum;
bool b = true;
while (b) {
snarklib::BlockVector<FR> A, B, C;
if (!snarklib::read_blockvector_raw(m_afile, block, A) ||
!snarklib::read_blockvector_raw(m_bfile, block, B) ||
!snarklib::read_blockvector_raw(m_cfile, block, C)) {
m_error = true;
return;
}
const auto& space = A.space();
Q.accumVector(A, B, C);
if (!snarklib::write_blockvector_raw(outfile, block, space, Q.vec())) {
m_error = true;
return;
}
b = (-1 == blocknum)
? ++block < space.blockID()[0]
: false;
if (!b) {
std::ofstream ofs(outfile);
if (!ofs)
m_error = true;
else
space.marshal_out(ofs);
}
}
}
const std::string m_afile, m_bfile, m_cfile;
snarklib::PPZK_LagrangePoint<FR> m_lagrangePoint;
snarklib::PPZK_BlindGreeks<FR, FR> m_clearGreeks;
snarklib::HugeSystem<FR> m_hugeSystem;
bool m_error;
};
////////////////////////////////////////////////////////////////////////////////
// query IC
//
template <typename PAIRING>
class QAP_query_IC
{
typedef typename PAIRING::Fr FR;
typedef typename PAIRING::G1 G1;
typedef typename PAIRING::G2 G2;
typedef typename snarklib::QAP_SystemPoint<snarklib::HugeSystem, FR> SYSPT;
typedef typename snarklib::QAP_QueryIC<snarklib::HugeSystem, FR> QTYPE;
public:
QAP_query_IC(const std::string& afile, // side-effect: file is modified
const std::string& sysfile,
const std::string& randfile)
: m_afile(afile),
m_hugeSystem(sysfile)
{
std::ifstream ifs(randfile);
m_error = !ifs ||
!m_lagrangePoint.marshal_in(ifs) ||
!m_hugeSystem.loadIndex();
}
QAP_query_IC(const std::string& afile, // side-effect: file is modified
const std::string& sysfile,
const snarklib::PPZK_LagrangePoint<FR>& lagrangeRand)
: m_afile(afile),
m_hugeSystem(sysfile),
m_lagrangePoint(lagrangeRand)
{
m_error = !m_hugeSystem.loadIndex();
}
bool operator! () const { return m_error; }
void IC(const std::string& outfile) {
writeFiles(outfile);
}
private:
void writeFiles(const std::string& outfile)
{
const SYSPT qap(m_hugeSystem,
m_hugeSystem.numCircuitInputs(),
m_lagrangePoint.point());
if (qap.weakPoint()) {
// Lagrange evaluation point is root of unity
m_error = true;
return;
}
QTYPE Q(qap);
std::size_t block = 0;
bool b = true;
while (b) {
snarklib::BlockVector<FR> A;
if (!snarklib::read_blockvector_raw(m_afile, block, A)) {
m_error = true;
return;
}
if (!Q.accumVector(A)) break;
std::stringstream ss;
ss << m_afile << block;
std::ofstream ofs(ss.str());
if (!ofs)
m_error = true;
else
A.marshal_out( // side-effect: write back to file
ofs,
[] (std::ostream& o, const FR& a) {
a.marshal_out_raw(o);
});
b = ++block < A.space().blockID()[0];
}
std::ofstream ofs(outfile);
const auto space = snarklib::BlockVector<FR>::space(Q.vec());
if (!ofs || !snarklib::write_blockvector_raw(outfile, space, Q.vec()))
m_error = true;
else
space.marshal_out(ofs);
}
const std::string m_afile;
snarklib::PPZK_LagrangePoint<FR> m_lagrangePoint;
snarklib::HugeSystem<FR> m_hugeSystem;
bool m_error;
};
////////////////////////////////////////////////////////////////////////////////
// witness ABCH
//
template <typename PAIRING>
class QAP_witness_ABCH
{
typedef typename PAIRING::Fr FR;
typedef typename snarklib::QAP_SystemPoint<snarklib::HugeSystem, FR> SYSPT;
public:
QAP_witness_ABCH(const std::size_t numBlocks,
const std::string& sysfile,
const std::string& randfile,
const std::string& witfile)
: m_numBlocks(numBlocks),
m_hugeSystem(sysfile)
{
std::ifstream ifsR(randfile), ifsW(witfile);
m_error =
!ifsR || !m_randomness.marshal_in(ifsR) ||
!ifsW || !m_witness.marshal_in(ifsW) ||
!m_hugeSystem.loadIndex();
}
QAP_witness_ABCH(const std::size_t numBlocks,
const std::string& sysfile,
const snarklib::PPZK_ProofRandomness<FR>& proofRand,
const std::string& witfile)
: m_numBlocks(numBlocks),
m_hugeSystem(sysfile),
m_randomness(proofRand)
{
std::ifstream ifs(witfile);
m_error =
!ifs || !m_witness.marshal_in(ifs) ||
!m_hugeSystem.loadIndex();
}
bool operator! () const { return m_error; }
void writeFiles(const std::string& outfile)
{
const SYSPT qap(m_hugeSystem,
m_hugeSystem.numCircuitInputs());
if (qap.weakPoint()) {
// Lagrange evaluation point is root of unity
m_error = true;
return;
}
const snarklib::QAP_WitnessABCH<snarklib::HugeSystem, FR>
ABCH(qap,
m_witness,
m_randomness.d1(),
m_randomness.d2(),
m_randomness.d3());
auto space = snarklib::BlockVector<FR>::space(ABCH.vec());
space.blockPartition(std::array<size_t, 1>{m_numBlocks});
if (! write_blockvector_raw(outfile, space, ABCH.vec()))
m_error = true;
}
private:
const std::size_t m_numBlocks;
snarklib::R1Witness<FR> m_witness;
snarklib::PPZK_ProofRandomness<FR> m_randomness;
snarklib::HugeSystem<FR> m_hugeSystem;
bool m_error;
};
} // namespace snarkfront
#endif