2 #include <boost/filesystem.hpp>
6 #include "primitives/transaction.h"
8 #include "crypto/equihash.h"
9 #include "chainparams.h"
10 #include "consensus/validation.h"
14 #include "script/sign.h"
17 #include "wallet/wallet.h"
19 #include "zcbenchmarks.h"
21 #include "zcash/Zcash.h"
22 #include "zcash/IncrementalMerkleTree.hpp"
24 using namespace libzcash;
26 struct timeval tv_start;
30 gettimeofday(&tv_start, 0);
36 struct timeval tv_end;
37 gettimeofday(&tv_end, 0);
38 elapsed = double(tv_end.tv_sec-tv_start.tv_sec) +
39 (tv_end.tv_usec-tv_start.tv_usec)/double(1000000);
43 double benchmark_sleep()
50 double benchmark_parameter_loading()
52 // FIXME: this is duplicated with the actual loading code
53 boost::filesystem::path pk_path = ZC_GetParamsDir() / "z5-proving.key";
54 boost::filesystem::path vk_path = ZC_GetParamsDir() / "z5-verifying.key";
58 auto newParams = ZCJoinSplit::Unopened();
60 newParams->loadVerifyingKey(vk_path.string());
61 newParams->setProvingKeyPath(pk_path.string());
62 newParams->loadProvingKey();
64 double ret = timer_stop();
71 double benchmark_create_joinsplit()
75 /* Get the anchor of an empty commitment tree. */
76 uint256 anchor = ZCIncrementalMerkleTree().root();
79 CPourTx pourtx(*pzcashParams,
82 {JSInput(), JSInput()},
83 {JSOutput(), JSOutput()},
86 double ret = timer_stop();
88 assert(pourtx.Verify(*pzcashParams, pubKeyHash));
92 double benchmark_verify_joinsplit(const CPourTx &joinsplit)
96 joinsplit.Verify(*pzcashParams, pubKeyHash);
100 double benchmark_solve_equihash()
103 CEquihashInput I{pblock};
104 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
107 unsigned int n = Params(CBaseChainParams::MAIN).EquihashN();
108 unsigned int k = Params(CBaseChainParams::MAIN).EquihashK();
109 crypto_generichash_blake2b_state eh_state;
110 EhInitialiseState(n, k, eh_state);
111 crypto_generichash_blake2b_update(&eh_state, (unsigned char*)&ss[0], ss.size());
114 randombytes_buf(nonce.begin(), 32);
115 crypto_generichash_blake2b_update(&eh_state,
120 std::set<std::vector<unsigned int>> solns;
121 EhOptimisedSolve(n, k, eh_state, solns);
125 double benchmark_verify_equihash()
127 CChainParams params = Params(CBaseChainParams::MAIN);
128 CBlock genesis = Params(CBaseChainParams::MAIN).GenesisBlock();
129 CBlockHeader genesis_header = genesis.GetBlockHeader();
131 CheckEquihashSolution(&genesis_header, params);
135 double benchmark_large_tx()
137 // Number of inputs in the spending transaction that we will simulate
138 const size_t NUM_INPUTS = 5550;
140 // Create priv/pub key
142 priv.MakeNewKey(false);
143 auto pub = priv.GetPubKey();
144 CBasicKeyStore tempKeystore;
145 tempKeystore.AddKey(priv);
147 // The "original" transaction that the spending transaction will spend
149 CMutableTransaction m_orig_tx;
150 m_orig_tx.vout.resize(1);
151 m_orig_tx.vout[0].nValue = 1000000;
152 CScript prevPubKey = GetScriptForDestination(pub.GetID());
153 m_orig_tx.vout[0].scriptPubKey = prevPubKey;
155 auto orig_tx = CTransaction(m_orig_tx);
157 CMutableTransaction spending_tx;
158 auto input_hash = orig_tx.GetHash();
159 // Add NUM_INPUTS inputs
160 for (size_t i = 0; i < NUM_INPUTS; i++) {
161 spending_tx.vin.emplace_back(input_hash, 0);
164 // Sign for all the inputs
165 for (size_t i = 0; i < NUM_INPUTS; i++) {
166 SignSignature(tempKeystore, prevPubKey, spending_tx, i, SIGHASH_ALL);
171 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
173 //std::cout << "SIZE OF SPENDING TX: " << ss.size() << std::endl;
175 auto error = MAX_BLOCK_SIZE / 20; // 5% error
176 assert(ss.size() < MAX_BLOCK_SIZE + error);
177 assert(ss.size() > MAX_BLOCK_SIZE - error);
180 // Benchmark signature verification costs:
182 for (size_t i = 0; i < NUM_INPUTS; i++) {
183 ScriptError serror = SCRIPT_ERR_OK;
184 assert(VerifyScript(spending_tx.vin[i].scriptSig,
186 STANDARD_SCRIPT_VERIFY_FLAGS,
187 MutableTransactionSignatureChecker(&spending_tx, i),