4 #include <boost/filesystem.hpp>
9 #include "primitives/transaction.h"
11 #include "crypto/equihash.h"
12 #include "chainparams.h"
13 #include "consensus/validation.h"
17 #include "script/sign.h"
20 #include "wallet/wallet.h"
22 #include "zcbenchmarks.h"
24 #include "zcash/Zcash.h"
25 #include "zcash/IncrementalMerkleTree.hpp"
27 using namespace libzcash;
29 void timer_start(timeval &tv_start)
31 gettimeofday(&tv_start, 0);
34 double timer_stop(timeval &tv_start)
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()
46 struct timeval tv_start;
47 timer_start(tv_start);
49 return timer_stop(tv_start);
52 double benchmark_parameter_loading()
54 // FIXME: this is duplicated with the actual loading code
55 boost::filesystem::path pk_path = ZC_GetParamsDir() / "sprout-proving.key";
56 boost::filesystem::path vk_path = ZC_GetParamsDir() / "sprout-verifying.key";
58 struct timeval tv_start;
59 timer_start(tv_start);
61 auto newParams = ZCJoinSplit::Unopened();
63 newParams->loadVerifyingKey(vk_path.string());
64 newParams->setProvingKeyPath(pk_path.string());
65 newParams->loadProvingKey();
67 double ret = timer_stop(tv_start);
74 double benchmark_create_joinsplit()
78 /* Get the anchor of an empty commitment tree. */
79 uint256 anchor = ZCIncrementalMerkleTree().root();
81 struct timeval tv_start;
82 timer_start(tv_start);
83 JSDescription jsdesc(*pzcashParams,
86 {JSInput(), JSInput()},
87 {JSOutput(), JSOutput()},
90 double ret = timer_stop(tv_start);
92 assert(jsdesc.Verify(*pzcashParams, pubKeyHash));
96 double benchmark_verify_joinsplit(const JSDescription &joinsplit)
98 struct timeval tv_start;
99 timer_start(tv_start);
101 joinsplit.Verify(*pzcashParams, pubKeyHash);
102 return timer_stop(tv_start);
105 double benchmark_solve_equihash()
108 CEquihashInput I{pblock};
109 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
112 unsigned int n = Params(CBaseChainParams::MAIN).EquihashN();
113 unsigned int k = Params(CBaseChainParams::MAIN).EquihashK();
114 crypto_generichash_blake2b_state eh_state;
115 EhInitialiseState(n, k, eh_state);
116 crypto_generichash_blake2b_update(&eh_state, (unsigned char*)&ss[0], ss.size());
119 randombytes_buf(nonce.begin(), 32);
120 crypto_generichash_blake2b_update(&eh_state,
124 struct timeval tv_start;
125 timer_start(tv_start);
126 std::set<std::vector<unsigned int>> solns;
127 EhOptimisedSolveUncancellable(n, k, eh_state,
128 [](std::vector<unsigned char> soln) { return false; });
129 return timer_stop(tv_start);
132 std::vector<double> benchmark_solve_equihash_threaded(int nThreads)
134 std::vector<double> ret;
135 std::vector<std::future<double>> tasks;
136 std::vector<std::thread> threads;
137 for (int i = 0; i < nThreads; i++) {
138 std::packaged_task<double(void)> task(&benchmark_solve_equihash);
139 tasks.emplace_back(task.get_future());
140 threads.emplace_back(std::move(task));
142 std::future_status status;
143 for (auto it = tasks.begin(); it != tasks.end(); it++) {
145 ret.push_back(it->get());
147 for (auto it = threads.begin(); it != threads.end(); it++) {
153 double benchmark_verify_equihash()
155 CChainParams params = Params(CBaseChainParams::MAIN);
156 CBlock genesis = Params(CBaseChainParams::MAIN).GenesisBlock();
157 CBlockHeader genesis_header = genesis.GetBlockHeader();
158 struct timeval tv_start;
159 timer_start(tv_start);
160 CheckEquihashSolution(&genesis_header, params);
161 return timer_stop(tv_start);
164 double benchmark_large_tx()
166 // Number of inputs in the spending transaction that we will simulate
167 const size_t NUM_INPUTS = 555;
169 // Create priv/pub key
171 priv.MakeNewKey(false);
172 auto pub = priv.GetPubKey();
173 CBasicKeyStore tempKeystore;
174 tempKeystore.AddKey(priv);
176 // The "original" transaction that the spending transaction will spend
178 CMutableTransaction m_orig_tx;
179 m_orig_tx.vout.resize(1);
180 m_orig_tx.vout[0].nValue = 1000000;
181 CScript prevPubKey = GetScriptForDestination(pub.GetID());
182 m_orig_tx.vout[0].scriptPubKey = prevPubKey;
184 auto orig_tx = CTransaction(m_orig_tx);
186 CMutableTransaction spending_tx;
187 auto input_hash = orig_tx.GetHash();
188 // Add NUM_INPUTS inputs
189 for (size_t i = 0; i < NUM_INPUTS; i++) {
190 spending_tx.vin.emplace_back(input_hash, 0);
193 // Sign for all the inputs
194 for (size_t i = 0; i < NUM_INPUTS; i++) {
195 SignSignature(tempKeystore, prevPubKey, spending_tx, i, SIGHASH_ALL);
200 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
202 //std::cout << "SIZE OF SPENDING TX: " << ss.size() << std::endl;
204 auto error = MAX_TX_SIZE / 20; // 5% error
205 assert(ss.size() < MAX_TX_SIZE + error);
206 assert(ss.size() > MAX_TX_SIZE - error);
209 // Spending tx has all its inputs signed and does not need to be mutated anymore
210 CTransaction final_spending_tx(spending_tx);
212 // Benchmark signature verification costs:
213 struct timeval tv_start;
214 timer_start(tv_start);
215 for (size_t i = 0; i < NUM_INPUTS; i++) {
216 ScriptError serror = SCRIPT_ERR_OK;
217 assert(VerifyScript(final_spending_tx.vin[i].scriptSig,
219 STANDARD_SCRIPT_VERIFY_FLAGS,
220 TransactionSignatureChecker(&final_spending_tx, i),
223 return timer_stop(tv_start);