Skip to content

Commit c5474e7

Browse files
authored
Prover optimizations (#34)
* Changes for mcl support * Extra info printing * Added server mode + misc optimizations
1 parent 1fa8423 commit c5474e7

File tree

11 files changed

+5211
-300
lines changed

11 files changed

+5211
-300
lines changed

Circuits/Circuit.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef _CIRCUIT_H_
2+
#define _CIRCUIT_H_
3+
4+
#include "ethsnarks.hpp"
5+
#include "../Utils/Data.h"
6+
7+
using namespace ethsnarks;
8+
9+
namespace Loopring
10+
{
11+
12+
class Circuit : public GadgetT
13+
{
14+
public:
15+
Circuit(libsnark::protoboard<FieldT> &pb, const std::string &annotation_prefix) : GadgetT(pb, annotation_prefix) {};
16+
virtual ~Circuit() {};
17+
virtual void generateConstraints(bool onchainDataAvailability, unsigned int blockSize) = 0;
18+
virtual bool generateWitness(const json& input) = 0;
19+
virtual BlockType getBlockType() = 0;
20+
virtual unsigned int getBlockSize() = 0;
21+
virtual void printInfo() = 0;
22+
23+
libsnark::protoboard<FieldT>& getPb()
24+
{
25+
return pb;
26+
}
27+
};
28+
29+
}
30+
31+
#endif

Circuits/DepositCircuit.h

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _DEPOSITCIRCUIT_H_
22
#define _DEPOSITCIRCUIT_H_
33

4+
#include "Circuit.h"
45
#include "../Utils/Constants.h"
56
#include "../Utils/Data.h"
67

@@ -130,7 +131,7 @@ class DepositGadget : public GadgetT
130131
}
131132
};
132133

133-
class DepositCircuit : public GadgetT
134+
class DepositCircuit : public Circuit
134135
{
135136
public:
136137

@@ -151,7 +152,7 @@ class DepositCircuit : public GadgetT
151152
std::vector<sha256_many> hashers;
152153

153154
DepositCircuit(ProtoboardT& pb, const std::string& prefix) :
154-
GadgetT(pb, prefix),
155+
Circuit(pb, prefix),
155156

156157
publicData(pb, FMT(prefix, ".publicData")),
157158
constants(pb, FMT(prefix, ".constants")),
@@ -167,9 +168,9 @@ class DepositCircuit : public GadgetT
167168

168169
}
169170

170-
void generate_r1cs_constraints(int numDeposits)
171+
void generateConstraints(bool onchainDataAvailability, unsigned int blockSize) override
171172
{
172-
this->numDeposits = numDeposits;
173+
this->numDeposits = blockSize;
173174

174175
constants.generate_r1cs_constraints();
175176

@@ -231,9 +232,16 @@ class DepositCircuit : public GadgetT
231232

232233
// Deposits
233234
assert(deposits.size() == hashers.size());
235+
#ifdef MULTICORE
236+
#pragma omp parallel for
237+
#endif
234238
for(unsigned int i = 0; i < block.deposits.size(); i++)
235239
{
236240
deposits[i].generate_r1cs_witness(block.deposits[i]);
241+
}
242+
// Cannot be done in parallel
243+
for(unsigned int i = 0; i < block.deposits.size(); i++)
244+
{
237245
hashers[i].generate_r1cs_witness();
238246
}
239247
// printBits("DepositBlockHash: 0x", hashers.back().result().bits.get_bits(pb));
@@ -244,7 +252,22 @@ class DepositCircuit : public GadgetT
244252
return true;
245253
}
246254

247-
void printInfo()
255+
bool generateWitness(const json& input) override
256+
{
257+
return generateWitness(input.get<Loopring::DepositBlock>());
258+
}
259+
260+
BlockType getBlockType() override
261+
{
262+
return BlockType::Deposit;
263+
}
264+
265+
unsigned int getBlockSize() override
266+
{
267+
return numDeposits;
268+
}
269+
270+
void printInfo() override
248271
{
249272
std::cout << pb.num_constraints() << " constraints (" << (pb.num_constraints() / numDeposits) << "/deposit)" << std::endl;
250273
}

Circuits/InternalTransferCircuit.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _INTERNAL_TRANSFER_CIRCUIT_H_
22
#define _INTERNAL_TRANSFER_CIRCUIT_H_
33

4+
#include "Circuit.h"
45
#include "../Utils/Constants.h"
56
#include "../Utils/Data.h"
67
#include "../Utils/Utils.h"
@@ -280,7 +281,7 @@ class InternalTransferGadget : public GadgetT
280281
}
281282
};
282283

283-
class InternalTransferCircuit : public GadgetT
284+
class InternalTransferCircuit : public Circuit
284285
{
285286
public:
286287
PublicDataGadget publicData;
@@ -312,7 +313,7 @@ class InternalTransferCircuit : public GadgetT
312313
std::unique_ptr<LabelHasher> labelHasher;
313314

314315
InternalTransferCircuit(ProtoboardT &pb, const std::string &prefix)
315-
: GadgetT(pb, prefix),
316+
: Circuit(pb, prefix),
316317

317318
publicData(pb, FMT(prefix, ".publicData")),
318319
constants(pb, FMT(prefix, ".constants")),
@@ -331,10 +332,10 @@ class InternalTransferCircuit : public GadgetT
331332
{
332333
}
333334

334-
void generate_r1cs_constraints(bool onchainDataAvailability, int numTransfers)
335+
void generateConstraints(bool onchainDataAvailability, unsigned int blockSize) override
335336
{
336337
this->onchainDataAvailability = onchainDataAvailability;
337-
this->numTransfers = numTransfers;
338+
this->numTransfers = blockSize;
338339

339340
constants.generate_r1cs_constraints();
340341

@@ -413,6 +414,9 @@ class InternalTransferCircuit : public GadgetT
413414
publicKeyX_notZero.generate_r1cs_witness();
414415

415416
// Internal transfers
417+
#ifdef MULTICORE
418+
#pragma omp parallel for
419+
#endif
416420
for (unsigned int i = 0; i < block.transfers.size(); i++)
417421
{
418422
transfers[i].generate_r1cs_witness(block.transfers[i]);
@@ -430,7 +434,22 @@ class InternalTransferCircuit : public GadgetT
430434
return true;
431435
}
432436

433-
void printInfo()
437+
bool generateWitness(const json& input) override
438+
{
439+
return generateWitness(input.get<Loopring::InternalTransferBlock>());
440+
}
441+
442+
BlockType getBlockType() override
443+
{
444+
return BlockType::InternalTransfer;
445+
}
446+
447+
unsigned int getBlockSize() override
448+
{
449+
return numTransfers;
450+
}
451+
452+
void printInfo() override
434453
{
435454
std::cout << pb.num_constraints() << " constraints (" << (pb.num_constraints() / numTransfers) << "/transfer)" << std::endl;
436455
}

Circuits/OffchainWithdrawalCircuit.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _OFFCHAINWITHDRAWALCIRCUIT_H_
22
#define _OFFCHAINWITHDRAWALCIRCUIT_H_
33

4+
#include "Circuit.h"
45
#include "../Utils/Constants.h"
56
#include "../Utils/Data.h"
67
#include "../Utils/Utils.h"
@@ -257,7 +258,7 @@ class OffchainWithdrawalGadget : public GadgetT
257258
}
258259
};
259260

260-
class OffchainWithdrawalCircuit : public GadgetT
261+
class OffchainWithdrawalCircuit : public Circuit
261262
{
262263
public:
263264

@@ -290,7 +291,7 @@ class OffchainWithdrawalCircuit : public GadgetT
290291
std::unique_ptr<LabelHasher> labelHasher;
291292

292293
OffchainWithdrawalCircuit(ProtoboardT& pb, const std::string& prefix) :
293-
GadgetT(pb, prefix),
294+
Circuit(pb, prefix),
294295

295296
publicData(pb, FMT(prefix, ".publicData")),
296297
constants(pb, FMT(prefix, ".constants")),
@@ -310,10 +311,10 @@ class OffchainWithdrawalCircuit : public GadgetT
310311

311312
}
312313

313-
void generate_r1cs_constraints(bool onchainDataAvailability, int numWithdrawals)
314+
void generateConstraints(bool onchainDataAvailability, unsigned int blockSize) override
314315
{
315316
this->onchainDataAvailability = onchainDataAvailability;
316-
this->numWithdrawals = numWithdrawals;
317+
this->numWithdrawals = blockSize;
317318

318319
constants.generate_r1cs_constraints();
319320

@@ -398,6 +399,9 @@ class OffchainWithdrawalCircuit : public GadgetT
398399
publicKeyX_notZero.generate_r1cs_witness();
399400

400401
// Withdrawals
402+
#ifdef MULTICORE
403+
#pragma omp parallel for
404+
#endif
401405
for(unsigned int i = 0; i < block.withdrawals.size(); i++)
402406
{
403407
withdrawals[i].generate_r1cs_witness(block.withdrawals[i]);
@@ -415,7 +419,22 @@ class OffchainWithdrawalCircuit : public GadgetT
415419
return true;
416420
}
417421

418-
void printInfo()
422+
bool generateWitness(const json& input) override
423+
{
424+
return generateWitness(input.get<Loopring::OffchainWithdrawalBlock>());
425+
}
426+
427+
BlockType getBlockType() override
428+
{
429+
return BlockType::OffchainWithdrawal;
430+
}
431+
432+
unsigned int getBlockSize() override
433+
{
434+
return numWithdrawals;
435+
}
436+
437+
void printInfo() override
419438
{
420439
std::cout << pb.num_constraints() << " constraints (" << (pb.num_constraints() / numWithdrawals) << "/offchain withdrawal)" << std::endl;
421440
}

Circuits/OnchainWithdrawalCircuit.h

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _ONCHAINWITHDRAWALCIRCUIT_H_
22
#define _ONCHAINWITHDRAWALCIRCUIT_H_
33

4+
#include "Circuit.h"
45
#include "../Utils/Constants.h"
56
#include "../Utils/Data.h"
67
#include "../Utils/Utils.h"
@@ -180,7 +181,7 @@ class OnchainWithdrawalGadget : public GadgetT
180181
}
181182
};
182183

183-
class OnchainWithdrawalCircuit : public GadgetT
184+
class OnchainWithdrawalCircuit : public Circuit
184185
{
185186
public:
186187

@@ -204,7 +205,7 @@ class OnchainWithdrawalCircuit : public GadgetT
204205
std::vector<sha256_many> hashers;
205206

206207
OnchainWithdrawalCircuit(ProtoboardT& pb, const std::string& prefix) :
207-
GadgetT(pb, prefix),
208+
Circuit(pb, prefix),
208209

209210
publicData(pb, FMT(prefix, ".publicData")),
210211
constants(pb, FMT(prefix, ".constants")),
@@ -223,9 +224,9 @@ class OnchainWithdrawalCircuit : public GadgetT
223224

224225
}
225226

226-
void generate_r1cs_constraints(int numWithdrawals)
227+
void generateConstraints(bool onchainDataAvailability, unsigned int blockSize) override
227228
{
228-
this->numWithdrawals = numWithdrawals;
229+
this->numWithdrawals = blockSize;
229230

230231
constants.generate_r1cs_constraints();
231232

@@ -299,9 +300,16 @@ class OnchainWithdrawalCircuit : public GadgetT
299300

300301
// Withdrawals
301302
assert(withdrawals.size() == hashers.size());
303+
#ifdef MULTICORE
304+
#pragma omp parallel for
305+
#endif
302306
for(unsigned int i = 0; i < block.withdrawals.size(); i++)
303307
{
304308
withdrawals[i].generate_r1cs_witness(block.withdrawals[i]);
309+
}
310+
// Cannot be done in parallel
311+
for(unsigned int i = 0; i < block.withdrawals.size(); i++)
312+
{
305313
hashers[i].generate_r1cs_witness();
306314
}
307315
// printBits("WithdrawBlockHash: 0x", hashers.back().result().bits.get_bits(pb));
@@ -312,7 +320,22 @@ class OnchainWithdrawalCircuit : public GadgetT
312320
return true;
313321
}
314322

315-
void printInfo()
323+
bool generateWitness(const json& input) override
324+
{
325+
return generateWitness(input.get<Loopring::OnchainWithdrawalBlock>());
326+
}
327+
328+
BlockType getBlockType() override
329+
{
330+
return BlockType::OnchainWithdrawal;
331+
}
332+
333+
unsigned int getBlockSize() override
334+
{
335+
return numWithdrawals;
336+
}
337+
338+
void printInfo() override
316339
{
317340
std::cout << pb.num_constraints() << " constraints (" << (pb.num_constraints() / numWithdrawals) << "/onchain withdrawal)" << std::endl;
318341
}

0 commit comments

Comments
 (0)