Skip to content

Commit 2cedb42

Browse files
committed
Merge bitcoin#29252: kernel: Remove key module from kernel library
96378fe Refactor: Remove ECC_Start and ECC_Stop from key header (TheCharlatan) 41eba5b kernel: Remove key module from kernel library (TheCharlatan) a08d2b3 tools: Use ECC_Context helper in bitcoin-tx and bitcoin-wallet tools (Ryan Ofsky) 28905c1 test: Use ECC_Context helper in bench and fuzz tests (Ryan Ofsky) 538fedd common: Add ECC_Context RAII wrapper for ECC_Start/ECC_Stop (Ryan Ofsky) Pull request description: The key module's functionality is not used by the kernel library, but currently kernel users are still required to initialize the key module's `secp256k1_context_sign` global as part of the `kernel::Context` through `ECC_Start`. So move the `ECC_Start` call to the `NodeContext` ctor instead to completely remove the key module from the kernel library. The gui tests currently keep multiple `NodeContext` objects in memory, so call `ECC_Stop` manually to avoid triggering an assertion on `ECC_Start`. --- This PR is part of the [libbitcoinkernel project](bitcoin#27587). It removes a module from the kernel library. ACKs for top commit: achow101: ACK 96378fe ryanofsky: Code review ACK 96378fe. Just suggested comment changes since last review. theuni: utACK 96378fe Tree-SHA512: 40be427e8e2c920c0e3ce64a9bdd90551be27a89af11440bfb6ab0dd3a1d1ccb7cf1f82383cd782818cd1bb44d5ae5d2161cf4d78d3127ce4987342007090bab
2 parents 7066980 + 96378fe commit 2cedb42

33 files changed

+79
-71
lines changed

src/Makefile.am

-1
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,6 @@ libbitcoinkernel_la_SOURCES = \
945945
kernel/disconnected_transactions.cpp \
946946
kernel/mempool_persist.cpp \
947947
kernel/mempool_removal_reason.cpp \
948-
key.cpp \
949948
logging.cpp \
950949
node/blockstorage.cpp \
951950
node/chainstate.cpp \

src/bench/bip324_ecdh.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
static void BIP324_ECDH(benchmark::Bench& bench)
1616
{
17-
ECC_Start();
17+
ECC_Context ecc_context{};
1818
FastRandomContext rng;
1919

2020
std::array<std::byte, 32> key_data;
@@ -44,8 +44,6 @@ static void BIP324_ECDH(benchmark::Bench& bench)
4444
// - Copy 16 bytes from the resulting shared secret into the middle of their ellswift key.
4545
std::copy(ret.begin() + 16, ret.end(), their_ellswift_data.begin() + 24);
4646
});
47-
48-
ECC_Stop();
4947
}
5048

5149
BENCHMARK(BIP324_ECDH, benchmark::PriorityLevel::HIGH);

src/bench/ccoins_caching.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
1919
static void CCoinsCaching(benchmark::Bench& bench)
2020
{
21-
ECC_Start();
21+
ECC_Context ecc_context{};
2222

2323
FillableSigningProvider keystore;
2424
CCoinsView coinsDummy;
@@ -47,7 +47,6 @@ static void CCoinsCaching(benchmark::Bench& bench)
4747
bool success{AreInputsStandard(tx_1, coins)};
4848
assert(success);
4949
});
50-
ECC_Stop();
5150
}
5251

5352
BENCHMARK(CCoinsCaching, benchmark::PriorityLevel::HIGH);

src/bench/checkqueue.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
2525
// We shouldn't ever be running with the checkqueue on a single core machine.
2626
if (GetNumCores() <= 1) return;
2727

28-
ECC_Start();
28+
ECC_Context ecc_context{};
2929

3030
struct PrevectorJob {
3131
prevector<PREVECTOR_SIZE, uint8_t> p;
@@ -62,6 +62,5 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
6262
// it is done explicitly here for clarity
6363
control.Wait();
6464
});
65-
ECC_Stop();
6665
}
6766
BENCHMARK(CCheckQueueSpeedPrevectorJob, benchmark::PriorityLevel::HIGH);

src/bench/descriptors.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
static void ExpandDescriptor(benchmark::Bench& bench)
1414
{
15-
ECC_Start();
15+
ECC_Context ecc_context{};
1616

1717
const auto desc_str = "sh(wsh(multi(16,03669b8afcec803a0d323e9a17f3ea8e68e8abe5a278020a929adbec52421adbd0,0260b2003c386519fc9eadf2b5cf124dd8eea4c4e68d5e154050a9346ea98ce600,0362a74e399c39ed5593852a30147f2959b56bb827dfa3e60e464b02ccf87dc5e8,0261345b53de74a4d721ef877c255429961b7e43714171ac06168d7e08c542a8b8,02da72e8b46901a65d4374fe6315538d8f368557dda3a1dcf9ea903f3afe7314c8,0318c82dd0b53fd3a932d16e0ba9e278fcc937c582d5781be626ff16e201f72286,0297ccef1ef99f9d73dec9ad37476ddb232f1238aff877af19e72ba04493361009,02e502cfd5c3f972fe9a3e2a18827820638f96b6f347e54d63deb839011fd5765d,03e687710f0e3ebe81c1037074da939d409c0025f17eb86adb9427d28f0f7ae0e9,02c04d3a5274952acdbc76987f3184b346a483d43be40874624b29e3692c1df5af,02ed06e0f418b5b43a7ec01d1d7d27290fa15f75771cb69b642a51471c29c84acd,036d46073cbb9ffee90473f3da429abc8de7f8751199da44485682a989a4bebb24,02f5d1ff7c9029a80a4e36b9a5497027ef7f3e73384a4a94fbfe7c4e9164eec8bc,02e41deffd1b7cce11cde209a781adcffdabd1b91c0ba0375857a2bfd9302419f3,02d76625f7956a7fc505ab02556c23ee72d832f1bac391bcd2d3abce5710a13d06,0399eb0a5487515802dc14544cf10b3666623762fbed2ec38a3975716e2c29c232)))";
1818
const std::pair<int64_t, int64_t> range = {0, 1000};
@@ -27,8 +27,6 @@ static void ExpandDescriptor(benchmark::Bench& bench)
2727
assert(success);
2828
}
2929
});
30-
31-
ECC_Stop();
3230
}
3331

3432
BENCHMARK(ExpandDescriptor, benchmark::PriorityLevel::HIGH);

src/bench/ellswift.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
static void EllSwiftCreate(benchmark::Bench& bench)
1111
{
12-
ECC_Start();
12+
ECC_Context ecc_context{};
1313

1414
CKey key = GenerateRandomKey();
1515
uint256 entropy = GetRandHash();
@@ -22,8 +22,6 @@ static void EllSwiftCreate(benchmark::Bench& bench)
2222
/* Use the last 32 bytes of the ellswift encoded public key as next entropy. */
2323
std::copy(ret.begin() + 32, ret.begin() + 64, MakeWritableByteSpan(entropy).begin());
2424
});
25-
26-
ECC_Stop();
2725
}
2826

2927
BENCHMARK(EllSwiftCreate, benchmark::PriorityLevel::HIGH);

src/bench/verify_script.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// modified to measure performance of other types of scripts.
1616
static void VerifyScriptBench(benchmark::Bench& bench)
1717
{
18-
ECC_Start();
18+
ECC_Context ecc_context{};
1919

2020
const uint32_t flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
2121
const int witnessversion = 0;
@@ -57,7 +57,6 @@ static void VerifyScriptBench(benchmark::Bench& bench)
5757
assert(err == SCRIPT_ERR_OK);
5858
assert(success);
5959
});
60-
ECC_Stop();
6160
}
6261

6362
static void VerifyNestedIfScript(benchmark::Bench& bench)

src/bitcoin-chainstate.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <script/sigcache.h>
2727
#include <util/chaintype.h>
2828
#include <util/fs.h>
29+
#include <util/signalinterrupt.h>
2930
#include <util/task_runner.h>
3031
#include <validation.h>
3132
#include <validationinterface.h>

src/bitcoin-tx.cpp

+4-15
Original file line numberDiff line numberDiff line change
@@ -692,21 +692,10 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
692692
tx = mergedTx;
693693
}
694694

695-
class Secp256k1Init
696-
{
697-
public:
698-
Secp256k1Init() {
699-
ECC_Start();
700-
}
701-
~Secp256k1Init() {
702-
ECC_Stop();
703-
}
704-
};
705-
706695
static void MutateTx(CMutableTransaction& tx, const std::string& command,
707696
const std::string& commandVal)
708697
{
709-
std::unique_ptr<Secp256k1Init> ecc;
698+
std::unique_ptr<ECC_Context> ecc;
710699

711700
if (command == "nversion")
712701
MutateTxVersion(tx, commandVal);
@@ -726,18 +715,18 @@ static void MutateTx(CMutableTransaction& tx, const std::string& command,
726715
else if (command == "outaddr")
727716
MutateTxAddOutAddr(tx, commandVal);
728717
else if (command == "outpubkey") {
729-
ecc.reset(new Secp256k1Init());
718+
ecc.reset(new ECC_Context());
730719
MutateTxAddOutPubKey(tx, commandVal);
731720
} else if (command == "outmultisig") {
732-
ecc.reset(new Secp256k1Init());
721+
ecc.reset(new ECC_Context());
733722
MutateTxAddOutMultiSig(tx, commandVal);
734723
} else if (command == "outscript")
735724
MutateTxAddOutScript(tx, commandVal);
736725
else if (command == "outdata")
737726
MutateTxAddOutData(tx, commandVal);
738727

739728
else if (command == "sign") {
740-
ecc.reset(new Secp256k1Init());
729+
ecc.reset(new ECC_Context());
741730
MutateTxSign(tx, commandVal);
742731
}
743732

src/bitcoin-wallet.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,9 @@ MAIN_FUNCTION
128128
return EXIT_FAILURE;
129129
}
130130

131-
ECC_Start();
131+
ECC_Context ecc_context{};
132132
if (!wallet::WalletTool::ExecuteWalletToolFunc(args, command->command)) {
133133
return EXIT_FAILURE;
134134
}
135-
ECC_Stop();
136135
return EXIT_SUCCESS;
137136
}

src/bitcoind.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
#include <init.h>
1515
#include <interfaces/chain.h>
1616
#include <interfaces/init.h>
17+
#include <kernel/context.h>
1718
#include <node/context.h>
1819
#include <node/interface_ui.h>
1920
#include <noui.h>
2021
#include <util/check.h>
2122
#include <util/exception.h>
23+
#include <util/signalinterrupt.h>
2224
#include <util/strencodings.h>
2325
#include <util/syserror.h>
2426
#include <util/threadnames.h>
@@ -180,6 +182,7 @@ static bool AppInit(NodeContext& node)
180182
}
181183

182184
node.kernel = std::make_unique<kernel::Context>();
185+
node.ecc_context = std::make_unique<ECC_Context>();
183186
if (!AppInitSanityChecks(*node.kernel))
184187
{
185188
// InitError will have been called with detailed error, which ends up on console

src/init.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <interfaces/chain.h>
3333
#include <interfaces/init.h>
3434
#include <interfaces/node.h>
35+
#include <kernel/context.h>
36+
#include <key.h>
3537
#include <logging.h>
3638
#include <mapport.h>
3739
#include <net.h>
@@ -75,6 +77,7 @@
7577
#include <util/fs_helpers.h>
7678
#include <util/moneystr.h>
7779
#include <util/result.h>
80+
#include <util/signalinterrupt.h>
7881
#include <util/strencodings.h>
7982
#include <util/string.h>
8083
#include <util/syserror.h>
@@ -371,6 +374,7 @@ void Shutdown(NodeContext& node)
371374
node.chainman.reset();
372375
node.validation_signals.reset();
373376
node.scheduler.reset();
377+
node.ecc_context.reset();
374378
node.kernel.reset();
375379

376380
RemovePidFile(*node.args);
@@ -1084,6 +1088,10 @@ bool AppInitSanityChecks(const kernel::Context& kernel)
10841088
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), PACKAGE_NAME));
10851089
}
10861090

1091+
if (!ECC_InitSanityCheck()) {
1092+
return InitError(strprintf(_("Elliptic curve cryptography sanity check failure. %s is shutting down."), PACKAGE_NAME));
1093+
}
1094+
10871095
// Probe the data directory lock to give an early error message, if possible
10881096
// We cannot hold the data directory lock here, as the forking for daemon() hasn't yet happened,
10891097
// and a fork will cause weird behavior to it.

src/kernel/checks.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#include <kernel/checks.h>
66

7-
#include <key.h>
87
#include <random.h>
8+
#include <util/result.h>
99
#include <util/translation.h>
1010

1111
#include <memory>
@@ -14,10 +14,6 @@ namespace kernel {
1414

1515
util::Result<void> SanityChecks(const Context&)
1616
{
17-
if (!ECC_InitSanityCheck()) {
18-
return util::Error{Untranslated("Elliptic curve cryptography sanity check failure. Aborting.")};
19-
}
20-
2117
if (!Random_SanityCheck()) {
2218
return util::Error{Untranslated("OS cryptographic RNG sanity check failure. Aborting.")};
2319
}

src/kernel/context.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#include <kernel/context.h>
66

77
#include <crypto/sha256.h>
8-
#include <key.h>
98
#include <logging.h>
10-
#include <pubkey.h>
119
#include <random.h>
1210

1311
#include <string>
@@ -19,12 +17,7 @@ Context::Context()
1917
std::string sha256_algo = SHA256AutoDetect();
2018
LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
2119
RandomInit();
22-
ECC_Start();
2320
}
2421

25-
Context::~Context()
26-
{
27-
ECC_Stop();
28-
}
2922

3023
} // namespace kernel

src/kernel/context.h

-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#ifndef BITCOIN_KERNEL_CONTEXT_H
66
#define BITCOIN_KERNEL_CONTEXT_H
77

8-
#include <util/signalinterrupt.h>
9-
10-
#include <memory>
11-
128
namespace kernel {
139
//! Context struct holding the kernel library's logically global state, and
1410
//! passed to external libbitcoin_kernel functions which need access to this
@@ -19,7 +15,6 @@ namespace kernel {
1915
//! should be stored to std::unique_ptr members pointing to opaque types.
2016
struct Context {
2117
Context();
22-
~Context();
2318
};
2419
} // namespace kernel
2520

src/key.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ bool ECC_InitSanityCheck() {
432432
return key.VerifyPubKey(pubkey);
433433
}
434434

435-
void ECC_Start() {
435+
/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
436+
static void ECC_Start() {
436437
assert(secp256k1_context_sign == nullptr);
437438

438439
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
@@ -449,11 +450,22 @@ void ECC_Start() {
449450
secp256k1_context_sign = ctx;
450451
}
451452

452-
void ECC_Stop() {
453+
/** Deinitialize the elliptic curve support. No-op if ECC_Start wasn't called first. */
454+
static void ECC_Stop() {
453455
secp256k1_context *ctx = secp256k1_context_sign;
454456
secp256k1_context_sign = nullptr;
455457

456458
if (ctx) {
457459
secp256k1_context_destroy(ctx);
458460
}
459461
}
462+
463+
ECC_Context::ECC_Context()
464+
{
465+
ECC_Start();
466+
}
467+
468+
ECC_Context::~ECC_Context()
469+
{
470+
ECC_Stop();
471+
}

src/key.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,21 @@ struct CExtKey {
236236
void SetSeed(Span<const std::byte> seed);
237237
};
238238

239-
/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
240-
void ECC_Start();
241-
242-
/** Deinitialize the elliptic curve support. No-op if ECC_Start wasn't called first. */
243-
void ECC_Stop();
244-
245239
/** Check that required EC support is available at runtime. */
246240
bool ECC_InitSanityCheck();
247241

242+
/**
243+
* RAII class initializing and deinitializing global state for elliptic curve support.
244+
* Only one instance may be initialized at a time.
245+
*
246+
* In the future global ECC state could be removed, and this class could contain
247+
* state and be passed as an argument to ECC key functions.
248+
*/
249+
class ECC_Context
250+
{
251+
public:
252+
ECC_Context();
253+
~ECC_Context();
254+
};
255+
248256
#endif // BITCOIN_KEY_H

src/node/context.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <banman.h>
99
#include <interfaces/chain.h>
1010
#include <kernel/context.h>
11+
#include <key.h>
1112
#include <net.h>
1213
#include <net_processing.h>
1314
#include <netgroup.h>
@@ -16,6 +17,7 @@
1617
#include <scheduler.h>
1718
#include <txmempool.h>
1819
#include <validation.h>
20+
#include <validationinterface.h>
1921

2022
namespace node {
2123
NodeContext::NodeContext() = default;

0 commit comments

Comments
 (0)