From 8f8c4c6c0cbcb70fa87c5c0d82eb8671a90d21b1 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Tue, 15 Mar 2016 14:16:05 -0600 Subject: [PATCH] Lazily load the proving key at time of first pour. --- src/init.cpp | 15 +++------------ src/zerocash/PourTransaction.cpp | 1 + src/zerocash/ZerocashParams.cpp | 11 +++++++++++ src/zerocash/ZerocashParams.h | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 43f99e813..ea58a1ed6 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -604,17 +604,8 @@ static void ZC_LoadParams() boost::filesystem::path pk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-proving.key"; boost::filesystem::path vk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-verification.key"; - LogPrintf("Loading proving key from %s\n", pk_path.string().c_str()); - gettimeofday(&tv_start, 0); libzerocash::ZerocashParams::zerocash_pp::init_public_params(); - auto pk_loaded = libzerocash::ZerocashParams::LoadProvingKeyFromFile( - pk_path.string(), - INCREMENTAL_MERKLE_TREE_DEPTH - ); - gettimeofday(&tv_end, 0); - elapsed = float(tv_end.tv_sec-tv_start.tv_sec) + (tv_end.tv_usec-tv_start.tv_usec)/float(1000000); - LogPrintf("Loaded proving key in %fs seconds.\n", elapsed); - + LogPrintf("Loading verification key from %s\n", vk_path.string().c_str()); gettimeofday(&tv_start, 0); @@ -628,8 +619,8 @@ static void ZC_LoadParams() pzerocashParams = new libzerocash::ZerocashParams( INCREMENTAL_MERKLE_TREE_DEPTH, - &pk_loaded, - &vk_loaded + &vk_loaded, + pk_path.string() ); } diff --git a/src/zerocash/PourTransaction.cpp b/src/zerocash/PourTransaction.cpp index 6a32ca141..c90c20ec6 100644 --- a/src/zerocash/PourTransaction.cpp +++ b/src/zerocash/PourTransaction.cpp @@ -132,6 +132,7 @@ void PourTransaction::init(uint16_t version_num, const Coin& c_1_new, const Coin& c_2_new) { + params.loadProvingKey(); this->version = version_num; convertIntToBytesVector(v_pub_old, this->publicOldValue); diff --git a/src/zerocash/ZerocashParams.cpp b/src/zerocash/ZerocashParams.cpp index aac89ef9b..db406e67f 100644 --- a/src/zerocash/ZerocashParams.cpp +++ b/src/zerocash/ZerocashParams.cpp @@ -127,6 +127,17 @@ ZerocashParams::ZerocashParams( params_vk_v1 = new zerocash_pour_verification_key(keypair->vk); } +ZerocashParams::ZerocashParams( + const unsigned int tree_depth, + zerocash_pour_verification_key* p_vk_1, + std::string proving_key_path +) : + treeDepth(tree_depth), provingKeyPath(proving_key_path) +{ + params_vk_v1 = new zerocash_pour_verification_key(*p_vk_1); + params_pk_v1 = NULL; +} + ZerocashParams::ZerocashParams( const unsigned int tree_depth, zerocash_pour_proving_key* p_pk_1, diff --git a/src/zerocash/ZerocashParams.h b/src/zerocash/ZerocashParams.h index 7b38226fa..4c2b3e7c5 100644 --- a/src/zerocash/ZerocashParams.h +++ b/src/zerocash/ZerocashParams.h @@ -35,6 +35,12 @@ public: zerocash_pour_verification_key* p_vk_1 ); + ZerocashParams( + const unsigned int tree_depth, + zerocash_pour_verification_key* p_vk_1, + std::string proving_key_path + ); + const zerocash_pour_proving_key& getProvingKey(); const zerocash_pour_verification_key& getVerificationKey(); int getTreeDepth(); @@ -49,10 +55,21 @@ public: static void SaveVerificationKeyToFile(const zerocash_pour_verification_key* p_vk_1, std::string path); static zerocash_pour_proving_key LoadProvingKeyFromFile(std::string path, const unsigned int tree_depth); static zerocash_pour_verification_key LoadVerificationKeyFromFile(std::string path, const unsigned int tree_depth); + + void loadProvingKey() + { + if (params_pk_v1 == NULL) { + std::cout << "loading proving key from path: " << provingKeyPath << std::endl; + params_pk_v1 = new zerocash_pour_proving_key( + LoadProvingKeyFromFile(provingKeyPath, treeDepth)); + std::cout << "done loading proving key!" << std::endl; + } + } private: int treeDepth; zerocash_pour_proving_key* params_pk_v1; zerocash_pour_verification_key* params_vk_v1; + std::string provingKeyPath; }; } /* namespace libzerocash */