Skip to content

Commit a388bea

Browse files
authored
Prover optimizations (part 2) (#35)
* Changes for mcl support * Extra info printing * Added server mode + misc optimizations * Changes for optimizations * Reserve the necessary number of sha256 hashers for deposits/withdrawals * Multi-threading fix for gcc * Fixed problem with swapping between different sized buffers * Fixed cli docs for pk_alt2mcl
1 parent c5474e7 commit a388bea

File tree

10 files changed

+419
-72
lines changed

10 files changed

+419
-72
lines changed

Circuits/DepositCircuit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ class DepositCircuit : public Circuit
183183
count.generate_r1cs_constraints(true);
184184

185185
// Deposits
186+
deposits.reserve(numDeposits);
187+
hashers.reserve(numDeposits);
186188
for (size_t j = 0; j < numDeposits; j++)
187189
{
188190
VariableT depositAccountsRoot = (j == 0) ? merkleRootBefore.packed : deposits.back().getNewAccountsRoot();

Circuits/InternalTransferCircuit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class InternalTransferCircuit : public Circuit
349349
publicKeyX_notZero.generate_r1cs_constraints();
350350

351351
// Internal transfers
352+
transfers.reserve(numTransfers);
352353
for (size_t j = 0; j < numTransfers; j++)
353354
{
354355
VariableT transAccountsRoot = (j == 0) ? merkleRootBefore.packed : transfers.back().getNewAccountsRoot();

Circuits/OffchainWithdrawalCircuit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class OffchainWithdrawalCircuit : public Circuit
328328
publicKeyX_notZero.generate_r1cs_constraints();
329329

330330
// Withdrawals
331+
withdrawals.reserve(numWithdrawals);
331332
for (size_t j = 0; j < numWithdrawals; j++)
332333
{
333334
VariableT withdrawalAccountsRoot = (j == 0) ? merkleRootBefore.packed : withdrawals.back().getNewAccountsRoot();

Circuits/OnchainWithdrawalCircuit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ class OnchainWithdrawalCircuit : public Circuit
242242
bShutdownMode.generate_r1cs_constraints();
243243

244244
// Withdrawals
245+
withdrawals.reserve(numWithdrawals);
246+
hashers.reserve(numWithdrawals);
245247
for (size_t j = 0; j < numWithdrawals; j++)
246248
{
247249
VariableT withdrawalAccountsRoot = (j == 0) ? merkleRootBefore.packed : withdrawals.back().getNewAccountsRoot();

Circuits/OrderCancellationCircuit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ class OrderCancellationCircuit : public Circuit
331331
publicKeyX_notZero.generate_r1cs_constraints();
332332

333333
// Cancels
334+
cancels.reserve(numCancels);
334335
for (size_t j = 0; j < numCancels; j++)
335336
{
336337
VariableT cancelAccountsRoot = (j == 0) ? merkleRootBefore.packed : cancels.back().getNewAccountsRoot();

Circuits/RingSettlementCircuit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ class RingSettlementCircuit : public Circuit
544544
nonce_after.generate_r1cs_constraints();
545545

546546
// Ring settlements
547+
ringSettlements.reserve(numRings);
547548
for (size_t j = 0; j < numRings; j++)
548549
{
549550
const VariableT ringAccountsRoot = (j == 0) ? merkleRootBefore.packed : ringSettlements.back().getNewAccountsRoot();

Gadgets/MathGadgets.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ class LabelHasher : public GadgetT
13811381
GadgetT(pb, prefix)
13821382
{
13831383
unsigned int numStages = (labels.size() + numInputs - 1) / numInputs;
1384+
stageHashers.reserve(numStages);
13841385
for (unsigned int i = 0; i < numStages; i++)
13851386
{
13861387
std::vector<VariableT> inputs;
@@ -1392,7 +1393,6 @@ class LabelHasher : public GadgetT
13921393
}
13931394
stageHashers.emplace_back(pb, var_array(inputs), FMT(this->annotation_prefix, ".stage1"));
13941395
}
1395-
13961396
hash.reset(new libsnark::dual_variable_gadget<FieldT>(
13971397
pb, stageHashers.back().result(), 256, ".labelHashToBits")
13981398
);
@@ -1405,7 +1405,7 @@ class LabelHasher : public GadgetT
14051405

14061406
void generate_r1cs_witness()
14071407
{
1408-
for (auto hasher : stageHashers)
1408+
for (auto& hasher : stageHashers)
14091409
{
14101410
hasher.generate_r1cs_witness();
14111411
}
@@ -1415,7 +1415,7 @@ class LabelHasher : public GadgetT
14151415

14161416
void generate_r1cs_constraints()
14171417
{
1418-
for (auto hasher : stageHashers)
1418+
for (auto& hasher : stageHashers)
14191419
{
14201420
hasher.generate_r1cs_constraints();
14211421
}

Gadgets/MerkleTree.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ template<typename HashT>
8383
class merkle_path_compute_4 : public GadgetT
8484
{
8585
public:
86-
const size_t m_depth;
87-
const VariableArrayT m_address_bits;
88-
const VariableT m_leaf;
89-
const VariableArrayT m_path;
90-
9186
std::vector<merkle_path_selector_4> m_selectors;
9287
std::vector<HashT> m_hashers;
9388

@@ -99,16 +94,14 @@ class merkle_path_compute_4 : public GadgetT
9994
const VariableArrayT& in_path,
10095
const std::string &in_annotation_prefix
10196
) :
102-
GadgetT(in_pb, in_annotation_prefix),
103-
m_depth(in_depth),
104-
m_address_bits(in_address_bits),
105-
m_leaf(in_leaf),
106-
m_path(in_path)
97+
GadgetT(in_pb, in_annotation_prefix)
10798
{
10899
assert( in_depth > 0 );
109100
assert( in_address_bits.size() == in_depth * 2 );
110101

111-
for( size_t i = 0; i < m_depth; i++ )
102+
m_selectors.reserve(in_depth);
103+
m_hashers.reserve(in_depth);
104+
for( size_t i = 0; i < in_depth; i++ )
112105
{
113106
m_selectors.push_back(
114107
merkle_path_selector_4(
@@ -117,11 +110,11 @@ class merkle_path_compute_4 : public GadgetT
117110
in_address_bits[i*2 + 0], in_address_bits[i*2 + 1],
118111
FMT(this->annotation_prefix, ".selector[%zu]", i)));
119112

120-
auto t = HashT(
121-
in_pb,
122-
var_array(m_selectors[i].getChildren()),
123-
FMT(this->annotation_prefix, ".hasher[%zu]", i));
124-
m_hashers.push_back(t);
113+
m_hashers.emplace_back(
114+
in_pb,
115+
var_array(m_selectors[i].getChildren()),
116+
FMT(this->annotation_prefix, ".hasher[%zu]", i)
117+
);
125118
}
126119
}
127120

Utils/Data.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "Constants.h"
55

6-
#include "../ThirdParty/json.hpp"
6+
//#include "../ThirdParty/json.hpp"
77
#include "ethsnarks.hpp"
88
#include "jubjub/point.hpp"
99
#include "jubjub/eddsa.hpp"

0 commit comments

Comments
 (0)