From 6e51b3bddf782f53527cf968445b298ebdec9bbc Mon Sep 17 00:00:00 2001 From: patrick s Date: Wed, 28 Aug 2013 23:53:26 -0700 Subject: [PATCH 1/3] improve wallet load time by removing duplicated calls to EC_KEY_check_key and adding a hash for vchPubKey/vchPrivKey entries in wallet.dat backwards compatible with previous wallet.dat format --- src/key.cpp | 17 ++++++++++++++++- src/key.h | 3 +++ src/walletdb.cpp | 34 ++++++++++++++++++++++++++++------ src/walletdb.h | 10 ++++++++-- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/key.cpp b/src/key.cpp index 85dc9cda2..0377140f7 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -166,9 +166,12 @@ public: assert(nSize == nSize2); } - bool SetPrivKey(const CPrivKey &privkey) { + bool SetPrivKey(const CPrivKey &privkey, bool fSkipCheck=false) { const unsigned char* pbegin = &privkey[0]; if (d2i_ECPrivateKey(&pkey, &pbegin, privkey.size())) { + if(fSkipCheck) + return true; + // d2i_ECPrivateKey returns true if parsing succeeds. // This doesn't necessarily mean the key is valid. if (EC_KEY_check_key(pkey)) @@ -409,6 +412,18 @@ bool CKey::SignCompact(const uint256 &hash, std::vector& vchSig) return true; } +bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) { + CECKey key; + if (!key.SetPrivKey(privkey, fSkipCheck)) + return false; + + key.GetSecretBytes(vch); + fCompressed = vchPubKey.IsCompressed(); + fValid = true; + + return true; +} + bool CPubKey::Verify(const uint256 &hash, const std::vector& vchSig) const { if (!IsValid()) return false; diff --git a/src/key.h b/src/key.h index 75431e944..ac050356f 100644 --- a/src/key.h +++ b/src/key.h @@ -261,6 +261,9 @@ public: // Derive BIP32 child key. bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const; + + // Load private key and check that public key matches. + bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck); }; struct CExtPubKey { diff --git a/src/walletdb.cpp b/src/walletdb.cpp index 554d14002..f7edd0845 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -306,6 +306,8 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, } CKey key; CPrivKey pkey; + uint256 hash = 0; + if (strType == "key") { wss.nKeys++; @@ -315,16 +317,36 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> wkey; pkey = wkey.vchPrivKey; } - if (!key.SetPrivKey(pkey, vchPubKey.IsCompressed())) + try + { + ssValue >> hash; + } + catch(...){} + + bool fSkipCheck = false; + + if (hash != 0) + { + // hash pubkey/privkey to accelerate wallet load + std::vector vchKey; + vchKey.reserve(vchPubKey.size() + pkey.size()); + vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); + vchKey.insert(vchKey.end(), pkey.begin(), pkey.end()); + + if (Hash(vchKey.begin(), vchKey.end()) != hash) + { + strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt"; + return false; + } + + fSkipCheck = true; + } + + if (!key.Load(pkey, vchPubKey, fSkipCheck)) { strErr = "Error reading wallet database: CPrivKey corrupt"; return false; } - if (key.GetPubKey() != vchPubKey) - { - strErr = "Error reading wallet database: CPrivKey pubkey inconsistency"; - return false; - } if (!pwallet->LoadKey(key, vchPubKey)) { strErr = "Error reading wallet database: LoadKey failed"; diff --git a/src/walletdb.h b/src/walletdb.h index 09ebebe5a..2d01a5cf7 100644 --- a/src/walletdb.h +++ b/src/walletdb.h @@ -93,8 +93,14 @@ public: if (!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta)) return false; - - return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false); + + // hash pubkey/privkey to accelerate wallet load + std::vector vchKey; + vchKey.reserve(vchPubKey.size() + vchPrivKey.size()); + vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); + vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end()); + + return Write(std::make_pair(std::string("key"), vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false); } bool WriteCryptedKey(const CPubKey& vchPubKey, From a42eef6f10e1da1c76e1c9ba49a2ff2459c62fea Mon Sep 17 00:00:00 2001 From: patrick s Date: Thu, 29 Aug 2013 01:11:47 -0700 Subject: [PATCH 2/3] verify vchPubKey matches calculated public key unless fSkipCheck is set --- src/key.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/key.cpp b/src/key.cpp index 0377140f7..fe5222378 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -421,6 +421,12 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) { fCompressed = vchPubKey.IsCompressed(); fValid = true; + if (fSkipCheck) + return true; + + if (GetPubKey() != vchPubKey) + return false; + return true; } From bc68788317a4ece16f0cfb0cb7eb1e0e220cbc6f Mon Sep 17 00:00:00 2001 From: phantomcircuit Date: Sat, 12 Oct 2013 23:44:28 -0700 Subject: [PATCH 3/3] comment explaining new wallet format for key/wkey entries --- src/walletdb.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/walletdb.cpp b/src/walletdb.cpp index f7edd0845..d31db5e42 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -317,6 +317,12 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> wkey; pkey = wkey.vchPrivKey; } + + // Old wallets store keys as "key" [pubkey] => [privkey] + // ... which was slow for wallets with lots of keys, because the public key is re-derived from the private key + // using EC operations as a checksum. + // Newer wallets store keys as "key"[pubkey] => [privkey][hash(pubkey,privkey)], which is much faster while + // remaining backwards-compatible. try { ssValue >> hash;