Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel: Remove key module from kernel library #29252

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,6 @@ libbitcoinkernel_la_SOURCES = \
kernel/disconnected_transactions.cpp \
kernel/mempool_persist.cpp \
kernel/mempool_removal_reason.cpp \
key.cpp \
logging.cpp \
node/blockstorage.cpp \
node/chainstate.cpp \
Expand Down
4 changes: 1 addition & 3 deletions src/bench/bip324_ecdh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

static void BIP324_ECDH(benchmark::Bench& bench)
{
ECC_Start();
ECC_Context ecc_context{};
FastRandomContext rng;

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

ECC_Stop();
}

BENCHMARK(BIP324_ECDH, benchmark::PriorityLevel::HIGH);
3 changes: 1 addition & 2 deletions src/bench/ccoins_caching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
static void CCoinsCaching(benchmark::Bench& bench)
{
ECC_Start();
ECC_Context ecc_context{};

FillableSigningProvider keystore;
CCoinsView coinsDummy;
Expand Down Expand Up @@ -47,7 +47,6 @@ static void CCoinsCaching(benchmark::Bench& bench)
bool success{AreInputsStandard(tx_1, coins)};
assert(success);
});
ECC_Stop();
}

BENCHMARK(CCoinsCaching, benchmark::PriorityLevel::HIGH);
3 changes: 1 addition & 2 deletions src/bench/checkqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
// We shouldn't ever be running with the checkqueue on a single core machine.
if (GetNumCores() <= 1) return;

ECC_Start();
ECC_Context ecc_context{};

struct PrevectorJob {
prevector<PREVECTOR_SIZE, uint8_t> p;
Expand Down Expand Up @@ -62,6 +62,5 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
// it is done explicitly here for clarity
control.Wait();
});
ECC_Stop();
}
BENCHMARK(CCheckQueueSpeedPrevectorJob, benchmark::PriorityLevel::HIGH);
4 changes: 1 addition & 3 deletions src/bench/descriptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

static void ExpandDescriptor(benchmark::Bench& bench)
{
ECC_Start();
ECC_Context ecc_context{};

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

ECC_Stop();
}

BENCHMARK(ExpandDescriptor, benchmark::PriorityLevel::HIGH);
4 changes: 1 addition & 3 deletions src/bench/ellswift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

static void EllSwiftCreate(benchmark::Bench& bench)
{
ECC_Start();
ECC_Context ecc_context{};

CKey key = GenerateRandomKey();
uint256 entropy = GetRandHash();
Expand All @@ -22,8 +22,6 @@ static void EllSwiftCreate(benchmark::Bench& bench)
/* Use the last 32 bytes of the ellswift encoded public key as next entropy. */
std::copy(ret.begin() + 32, ret.begin() + 64, MakeWritableByteSpan(entropy).begin());
});

ECC_Stop();
}

BENCHMARK(EllSwiftCreate, benchmark::PriorityLevel::HIGH);
3 changes: 1 addition & 2 deletions src/bench/verify_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// modified to measure performance of other types of scripts.
static void VerifyScriptBench(benchmark::Bench& bench)
{
ECC_Start();
ECC_Context ecc_context{};

const uint32_t flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
const int witnessversion = 0;
Expand Down Expand Up @@ -57,7 +57,6 @@ static void VerifyScriptBench(benchmark::Bench& bench)
assert(err == SCRIPT_ERR_OK);
assert(success);
});
ECC_Stop();
}

static void VerifyNestedIfScript(benchmark::Bench& bench)
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <script/sigcache.h>
#include <util/chaintype.h>
#include <util/fs.h>
#include <util/signalinterrupt.h>
#include <util/task_runner.h>
#include <validation.h>
#include <validationinterface.h>
Expand Down
19 changes: 4 additions & 15 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,21 +692,10 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
tx = mergedTx;
}

class Secp256k1Init
{
public:
Secp256k1Init() {
ECC_Start();
}
~Secp256k1Init() {
ECC_Stop();
}
};

static void MutateTx(CMutableTransaction& tx, const std::string& command,
const std::string& commandVal)
{
std::unique_ptr<Secp256k1Init> ecc;
std::unique_ptr<ECC_Context> ecc;

if (command == "nversion")
MutateTxVersion(tx, commandVal);
Expand All @@ -726,18 +715,18 @@ static void MutateTx(CMutableTransaction& tx, const std::string& command,
else if (command == "outaddr")
MutateTxAddOutAddr(tx, commandVal);
else if (command == "outpubkey") {
ecc.reset(new Secp256k1Init());
ecc.reset(new ECC_Context());
MutateTxAddOutPubKey(tx, commandVal);
} else if (command == "outmultisig") {
ecc.reset(new Secp256k1Init());
ecc.reset(new ECC_Context());
MutateTxAddOutMultiSig(tx, commandVal);
} else if (command == "outscript")
MutateTxAddOutScript(tx, commandVal);
else if (command == "outdata")
MutateTxAddOutData(tx, commandVal);

else if (command == "sign") {
ecc.reset(new Secp256k1Init());
ecc.reset(new ECC_Context());
MutateTxSign(tx, commandVal);
}

Expand Down
3 changes: 1 addition & 2 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ MAIN_FUNCTION
return EXIT_FAILURE;
}

ECC_Start();
ECC_Context ecc_context{};
if (!wallet::WalletTool::ExecuteWalletToolFunc(args, command->command)) {
return EXIT_FAILURE;
}
ECC_Stop();
return EXIT_SUCCESS;
}
3 changes: 3 additions & 0 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
#include <init.h>
#include <interfaces/chain.h>
#include <interfaces/init.h>
#include <kernel/context.h>
#include <node/context.h>
#include <node/interface_ui.h>
#include <noui.h>
#include <util/check.h>
#include <util/exception.h>
#include <util/signalinterrupt.h>
#include <util/strencodings.h>
#include <util/syserror.h>
#include <util/threadnames.h>
Expand Down Expand Up @@ -180,6 +182,7 @@ static bool AppInit(NodeContext& node)
}

node.kernel = std::make_unique<kernel::Context>();
node.ecc_context = std::make_unique<ECC_Context>();
if (!AppInitSanityChecks(*node.kernel))
{
// InitError will have been called with detailed error, which ends up on console
Expand Down
8 changes: 8 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <interfaces/chain.h>
#include <interfaces/init.h>
#include <interfaces/node.h>
#include <kernel/context.h>
#include <key.h>
#include <logging.h>
#include <mapport.h>
#include <net.h>
Expand Down Expand Up @@ -75,6 +77,7 @@
#include <util/fs_helpers.h>
#include <util/moneystr.h>
#include <util/result.h>
#include <util/signalinterrupt.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/syserror.h>
Expand Down Expand Up @@ -371,6 +374,7 @@ void Shutdown(NodeContext& node)
node.chainman.reset();
node.validation_signals.reset();
node.scheduler.reset();
node.ecc_context.reset();
node.kernel.reset();

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

if (!ECC_InitSanityCheck()) {
return InitError(strprintf(_("Elliptic curve cryptography sanity check failure. %s is shutting down."), PACKAGE_NAME));
}

// Probe the data directory lock to give an early error message, if possible
// We cannot hold the data directory lock here, as the forking for daemon() hasn't yet happened,
// and a fork will cause weird behavior to it.
Expand Down
6 changes: 1 addition & 5 deletions src/kernel/checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include <kernel/checks.h>

#include <key.h>
#include <random.h>
#include <util/result.h>
#include <util/translation.h>

#include <memory>
Expand All @@ -14,10 +14,6 @@ namespace kernel {

util::Result<void> SanityChecks(const Context&)
{
if (!ECC_InitSanityCheck()) {
return util::Error{Untranslated("Elliptic curve cryptography sanity check failure. Aborting.")};
}

if (!Random_SanityCheck()) {
return util::Error{Untranslated("OS cryptographic RNG sanity check failure. Aborting.")};
}
Expand Down
7 changes: 0 additions & 7 deletions src/kernel/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include <kernel/context.h>

#include <crypto/sha256.h>
#include <key.h>
#include <logging.h>
#include <pubkey.h>
#include <random.h>

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

Context::~Context()
{
ECC_Stop();
}

} // namespace kernel
5 changes: 0 additions & 5 deletions src/kernel/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#ifndef BITCOIN_KERNEL_CONTEXT_H
#define BITCOIN_KERNEL_CONTEXT_H

#include <util/signalinterrupt.h>

#include <memory>

namespace kernel {
//! Context struct holding the kernel library's logically global state, and
//! passed to external libbitcoin_kernel functions which need access to this
Expand All @@ -19,7 +15,6 @@ namespace kernel {
//! should be stored to std::unique_ptr members pointing to opaque types.
struct Context {
TheCharlatan marked this conversation as resolved.
Show resolved Hide resolved
Context();
~Context();
TheCharlatan marked this conversation as resolved.
Show resolved Hide resolved
};
} // namespace kernel

Expand Down
16 changes: 14 additions & 2 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ bool ECC_InitSanityCheck() {
return key.VerifyPubKey(pubkey);
}

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

secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
Expand All @@ -449,11 +450,22 @@ void ECC_Start() {
secp256k1_context_sign = ctx;
}

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

if (ctx) {
secp256k1_context_destroy(ctx);
}
}

ECC_Context::ECC_Context()
{
ECC_Start();
}

ECC_Context::~ECC_Context()
{
ECC_Stop();
}
20 changes: 14 additions & 6 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,21 @@ struct CExtKey {
void SetSeed(Span<const std::byte> seed);
};

/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
void ECC_Start();
TheCharlatan marked this conversation as resolved.
Show resolved Hide resolved

/** Deinitialize the elliptic curve support. No-op if ECC_Start wasn't called first. */
void ECC_Stop();

/** Check that required EC support is available at runtime. */
bool ECC_InitSanityCheck();

/**
* RAII class initializing and deinitializing global state for elliptic curve support.
* Only one instance may be initialized at a time.
*
* In the future global ECC state could be removed, and this class could contain
* state and be passed as an argument to ECC key functions.
*/
class ECC_Context
{
public:
ECC_Context();
~ECC_Context();
};

#endif // BITCOIN_KEY_H
2 changes: 2 additions & 0 deletions src/node/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <banman.h>
#include <interfaces/chain.h>
#include <kernel/context.h>
#include <key.h>
#include <net.h>
#include <net_processing.h>
#include <netgroup.h>
Expand All @@ -16,6 +17,7 @@
#include <scheduler.h>
#include <txmempool.h>
#include <validation.h>
#include <validationinterface.h>

namespace node {
NodeContext::NodeContext() = default;
Expand Down
Loading