2 #include <boost/filesystem.hpp>
3 #include <boost/thread.hpp>
7 #include "primitives/transaction.h"
9 #include "crypto/equihash.h"
10 #include "chainparams.h"
11 #include "consensus/validation.h"
15 #include "script/sign.h"
18 #include "wallet/wallet.h"
20 #include "zcbenchmarks.h"
22 #include "zcash/Zcash.h"
23 #include "zcash/IncrementalMerkleTree.hpp"
25 using namespace libzcash;
27 struct timeval tv_start;
31 gettimeofday(&tv_start, 0);
37 struct timeval tv_end;
38 gettimeofday(&tv_end, 0);
39 elapsed = double(tv_end.tv_sec-tv_start.tv_sec) +
40 (tv_end.tv_usec-tv_start.tv_usec)/double(1000000);
44 double benchmark_sleep()
51 double benchmark_parameter_loading()
53 // FIXME: this is duplicated with the actual loading code
54 boost::filesystem::path pk_path = ZC_GetParamsDir() / "z7-proving.key";
55 boost::filesystem::path vk_path = ZC_GetParamsDir() / "z7-verifying.key";
59 auto newParams = ZCJoinSplit::Unopened();
61 newParams->loadVerifyingKey(vk_path.string());
62 newParams->setProvingKeyPath(pk_path.string());
63 newParams->loadProvingKey();
65 double ret = timer_stop();
72 double benchmark_create_joinsplit()
76 /* Get the anchor of an empty commitment tree. */
77 uint256 anchor = ZCIncrementalMerkleTree().root();
80 JSDescription jsdesc(*pzcashParams,
83 {JSInput(), JSInput()},
84 {JSOutput(), JSOutput()},
87 double ret = timer_stop();
89 assert(jsdesc.Verify(*pzcashParams, pubKeyHash));
93 double benchmark_verify_joinsplit(const JSDescription &joinsplit)
97 joinsplit.Verify(*pzcashParams, pubKeyHash);
101 double benchmark_solve_equihash(bool time)
104 CEquihashInput I{pblock};
105 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
108 unsigned int n = Params(CBaseChainParams::MAIN).EquihashN();
109 unsigned int k = Params(CBaseChainParams::MAIN).EquihashK();
110 crypto_generichash_blake2b_state eh_state;
111 EhInitialiseState(n, k, eh_state);
112 crypto_generichash_blake2b_update(&eh_state, (unsigned char*)&ss[0], ss.size());
115 randombytes_buf(nonce.begin(), 32);
116 crypto_generichash_blake2b_update(&eh_state,
122 std::set<std::vector<unsigned int>> solns;
123 EhOptimisedSolveUncancellable(n, k, eh_state,
124 [](std::vector<eh_index> soln) { return false; });
131 double benchmark_solve_equihash_threaded(int nThreads)
133 boost::thread_group solverThreads;
135 for (int i = 0; i < nThreads; i++)
136 solverThreads.create_thread(boost::bind(&benchmark_solve_equihash, false));
137 solverThreads.join_all();
141 double benchmark_verify_equihash()
143 CChainParams params = Params(CBaseChainParams::MAIN);
144 CBlock genesis = Params(CBaseChainParams::MAIN).GenesisBlock();
145 CBlockHeader genesis_header = genesis.GetBlockHeader();
147 CheckEquihashSolution(&genesis_header, params);
151 double benchmark_large_tx()
153 // Number of inputs in the spending transaction that we will simulate
154 const size_t NUM_INPUTS = 11100;
156 // Create priv/pub key
158 priv.MakeNewKey(false);
159 auto pub = priv.GetPubKey();
160 CBasicKeyStore tempKeystore;
161 tempKeystore.AddKey(priv);
163 // The "original" transaction that the spending transaction will spend
165 CMutableTransaction m_orig_tx;
166 m_orig_tx.vout.resize(1);
167 m_orig_tx.vout[0].nValue = 1000000;
168 CScript prevPubKey = GetScriptForDestination(pub.GetID());
169 m_orig_tx.vout[0].scriptPubKey = prevPubKey;
171 auto orig_tx = CTransaction(m_orig_tx);
173 CMutableTransaction spending_tx;
174 auto input_hash = orig_tx.GetTxid();
175 // Add NUM_INPUTS inputs
176 for (size_t i = 0; i < NUM_INPUTS; i++) {
177 spending_tx.vin.emplace_back(input_hash, 0);
180 // Sign for all the inputs
181 for (size_t i = 0; i < NUM_INPUTS; i++) {
182 SignSignature(tempKeystore, prevPubKey, spending_tx, i, SIGHASH_ALL);
187 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
189 //std::cout << "SIZE OF SPENDING TX: " << ss.size() << std::endl;
191 auto error = MAX_BLOCK_SIZE / 20; // 5% error
192 assert(ss.size() < MAX_BLOCK_SIZE + error);
193 assert(ss.size() > MAX_BLOCK_SIZE - error);
196 // Spending tx has all its inputs signed and does not need to be mutated anymore
197 CTransaction final_spending_tx(spending_tx);
199 // Benchmark signature verification costs:
201 for (size_t i = 0; i < NUM_INPUTS; i++) {
202 ScriptError serror = SCRIPT_ERR_OK;
203 assert(VerifyScript(final_spending_tx.vin[i].scriptSig,
205 STANDARD_SCRIPT_VERIFY_FLAGS,
206 TransactionSignatureChecker(&final_spending_tx, i),