Skip to content

Commit 2652506

Browse files
committed
Merge bitcoin/bitcoin#28455: refactor: share and use GenerateRandomKey helper
fa1d495 refactor: share and use `GenerateRandomKey` helper (Sebastian Falbesoner) Pull request description: Making the `GeneratingRandomKey` helper (recently introduced in PR #28433, commit b6934fd) available to other modules via key.{h.cpp} allows us to create random private keys directly at CKey instantiation, in contrast to the currently needed two-step process of creating an (invalid) CKey instance first and then having to call `MakeNewKey(...)`. This is mostly used in unit tests and a few instances in the wallet. ACKs for top commit: Sjors: re-ACK fa1d495 achow101: ACK fa1d495 sipa: utACK fa1d495 kristapsk: cr utACK fa1d495 stratospher: ACK fa1d495. Tree-SHA512: 6fec73f33efe5bd77ca7d3c2fc06725d96f789f229294c39377e682ff222cfc7990b77c92e0bfd4cb6cf891d007ab1f86d395907511f06e87044bae37652a2fd
2 parents d036a86 + fa1d495 commit 2652506

16 files changed

+63
-100
lines changed

src/bench/ellswift.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ static void EllSwiftCreate(benchmark::Bench& bench)
1111
{
1212
ECC_Start();
1313

14-
CKey key;
15-
key.MakeNewKey(true);
16-
14+
CKey key = GenerateRandomKey();
1715
uint256 entropy = GetRandHash();
1816

1917
bench.batch(1).unit("pubkey").run([&] {

src/key.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, c
369369
return output;
370370
}
371371

372+
CKey GenerateRandomKey(bool compressed) noexcept
373+
{
374+
CKey key;
375+
key.MakeNewKey(/*fCompressed=*/compressed);
376+
return key;
377+
}
378+
372379
bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
373380
if (nDepth == std::numeric_limits<unsigned char>::max()) return false;
374381
out.nDepth = nDepth + 1;
@@ -420,8 +427,7 @@ void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
420427
}
421428

422429
bool ECC_InitSanityCheck() {
423-
CKey key;
424-
key.MakeNewKey(true);
430+
CKey key = GenerateRandomKey();
425431
CPubKey pubkey = key.GetPubKey();
426432
return key.VerifyPubKey(pubkey);
427433
}

src/key.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class CKey
205205
bool initiating) const;
206206
};
207207

208+
CKey GenerateRandomKey(bool compressed = true) noexcept;
209+
208210
struct CExtKey {
209211
unsigned char nDepth;
210212
unsigned char vchFingerprint[4];

src/net.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <consensus/consensus.h>
1919
#include <crypto/sha256.h>
2020
#include <i2p.h>
21+
#include <key.h>
2122
#include <logging.h>
2223
#include <memusage.h>
2324
#include <net_permissions.h>
@@ -938,13 +939,6 @@ class V2MessageMap
938939

939940
const V2MessageMap V2_MESSAGE_MAP;
940941

941-
CKey GenerateRandomKey() noexcept
942-
{
943-
CKey key;
944-
key.MakeNewKey(/*fCompressed=*/true);
945-
return key;
946-
}
947-
948942
std::vector<uint8_t> GenerateRandomGarbage() noexcept
949943
{
950944
std::vector<uint8_t> ret;

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
8585
}
8686

8787
auto build_address = [&wallet]() {
88-
CKey key;
89-
key.MakeNewKey(true);
88+
CKey key = GenerateRandomKey();
9089
CTxDestination dest(GetDestinationForKey(
9190
key.GetPubKey(), wallet->m_default_address_type));
9291

src/test/blockfilter_index_tests.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
162162
LOCK(cs_main);
163163
tip = m_node.chainman->ActiveChain().Tip();
164164
}
165-
CKey coinbase_key_A, coinbase_key_B;
166-
coinbase_key_A.MakeNewKey(true);
167-
coinbase_key_B.MakeNewKey(true);
165+
CKey coinbase_key_A = GenerateRandomKey();
166+
CKey coinbase_key_B = GenerateRandomKey();
168167
CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
169168
CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
170169
std::vector<std::shared_ptr<CBlock>> chainA, chainB;

src/test/compress_tests.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ BOOST_AUTO_TEST_CASE(compress_amounts)
6565
BOOST_AUTO_TEST_CASE(compress_script_to_ckey_id)
6666
{
6767
// case CKeyID
68-
CKey key;
69-
key.MakeNewKey(true);
68+
CKey key = GenerateRandomKey();
7069
CPubKey pubkey = key.GetPubKey();
7170

7271
CScript script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
@@ -101,8 +100,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_cscript_id)
101100

102101
BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id)
103102
{
104-
CKey key;
105-
key.MakeNewKey(true); // case compressed PubKeyID
103+
CKey key = GenerateRandomKey(); // case compressed PubKeyID
106104

107105
CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // COMPRESSED_PUBLIC_KEY_SIZE (33)
108106
BOOST_CHECK_EQUAL(script.size(), 35U);
@@ -119,8 +117,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id)
119117

120118
BOOST_AUTO_TEST_CASE(compress_script_to_uncompressed_pubkey_id)
121119
{
122-
CKey key;
123-
key.MakeNewKey(false); // case uncompressed PubKeyID
120+
CKey key = GenerateRandomKey(/*compressed=*/false); // case uncompressed PubKeyID
124121
CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // PUBLIC_KEY_SIZE (65)
125122
BOOST_CHECK_EQUAL(script.size(), 67U); // 1 char code + 65 char pubkey + OP_CHECKSIG
126123

src/test/script_standard_tests.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,8 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_success)
136136

137137
BOOST_AUTO_TEST_CASE(script_standard_Solver_failure)
138138
{
139-
CKey key;
140-
CPubKey pubkey;
141-
key.MakeNewKey(true);
142-
pubkey = key.GetPubKey();
139+
CKey key = GenerateRandomKey();
140+
CPubKey pubkey = key.GetPubKey();
143141

144142
CScript s;
145143
std::vector<std::vector<unsigned char> > solutions;
@@ -192,10 +190,8 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_failure)
192190

193191
BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
194192
{
195-
CKey key;
196-
CPubKey pubkey;
197-
key.MakeNewKey(true);
198-
pubkey = key.GetPubKey();
193+
CKey key = GenerateRandomKey();
194+
CPubKey pubkey = key.GetPubKey();
199195

200196
CScript s;
201197
CTxDestination address;

src/test/script_tests.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,9 @@ sign_multisig(const CScript& scriptPubKey, const CKey& key, const CTransaction&
10511051
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
10521052
{
10531053
ScriptError err;
1054-
CKey key1, key2, key3;
1055-
key1.MakeNewKey(true);
1056-
key2.MakeNewKey(false);
1057-
key3.MakeNewKey(true);
1054+
CKey key1 = GenerateRandomKey();
1055+
CKey key2 = GenerateRandomKey(/*compressed=*/false);
1056+
CKey key3 = GenerateRandomKey();
10581057

10591058
CScript scriptPubKey12;
10601059
scriptPubKey12 << OP_1 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
@@ -1081,11 +1080,10 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
10811080
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
10821081
{
10831082
ScriptError err;
1084-
CKey key1, key2, key3, key4;
1085-
key1.MakeNewKey(true);
1086-
key2.MakeNewKey(false);
1087-
key3.MakeNewKey(true);
1088-
key4.MakeNewKey(false);
1083+
CKey key1 = GenerateRandomKey();
1084+
CKey key2 = GenerateRandomKey(/*compressed=*/false);
1085+
CKey key3 = GenerateRandomKey();
1086+
CKey key4 = GenerateRandomKey(/*compressed=*/false);
10891087

10901088
CScript scriptPubKey23;
10911089
scriptPubKey23 << OP_2 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << ToByteVector(key3.GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
@@ -1165,8 +1163,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs)
11651163
std::vector<CPubKey> pubkeys;
11661164
for (int i = 0; i < 3; i++)
11671165
{
1168-
CKey key;
1169-
key.MakeNewKey(i%2 == 1);
1166+
CKey key = GenerateRandomKey(/*compressed=*/i%2 == 1);
11701167
keys.push_back(key);
11711168
pubkeys.push_back(key.GetPubKey());
11721169
BOOST_CHECK(keystore.AddKey(key));

src/test/sigopcount_tests.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount)
5050
std::vector<CPubKey> keys;
5151
for (int i = 0; i < 3; i++)
5252
{
53-
CKey k;
54-
k.MakeNewKey(true);
53+
CKey k = GenerateRandomKey();
5554
keys.push_back(k.GetPubKey());
5655
}
5756
CScript s2 = GetScriptForMultisig(1, keys);
@@ -120,8 +119,7 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
120119
CCoinsView coinsDummy;
121120
CCoinsViewCache coins(&coinsDummy);
122121
// Create key
123-
CKey key;
124-
key.MakeNewKey(true);
122+
CKey key = GenerateRandomKey();
125123
CPubKey pubkey = key.GetPubKey();
126124
// Default flags
127125
const uint32_t flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};

0 commit comments

Comments
 (0)