Skip to content

Commit b52d547

Browse files
committed
Merge bitcoin#30377: refactor: Replace ParseHex with consteval ""_hex literals
8756ccd scripted-diff: Replace ParseHex[<std::byte>]("str") -> "str"_hex[_u8] (Hodlinator) 9cb6873 refactor: Prepare for ParseHex -> ""_hex scripted-diff (Hodlinator) 50bc017 refactor: Hand-replace some ParseHex -> ""_hex (Hodlinator) 5b74a84 util: Add consteval ""_hex[_v][_u8] literals (l0rinc) dc5f6f6 test refactor: util_tests - parse_hex clean up (Hodlinator) 2b5e6ef refactor: Make XOnlyPubKey tolerate constexpr std::arrays (Hodlinator) 403d86f refactor: vector -> span in CCrypter (Hodlinator) bd0830b refactor: de-Hungarianize CCrypter (Hodlinator) d99c816 refactor: Improve CCrypter related lines (Hodlinator) 7e1d9a8 refactor: Enforce lowercase hex digits for consteval uint256 (Hodlinator) Pull request description: Motivation: * Validates and converts the hex string into bytes at compile time instead of at runtime like `ParseHex()`. * Eliminates runtime dependencies: bitcoin#30377 (comment), bitcoin#30048 (comment) * Has stricter requirements than `ParseHex()` (disallows whitespace and uppercase hex digits) and replaces it in a bunch of places. * Makes it possible to derive other compile time constants. * Minor: should shave off a few runtime CPU cycles. `""_hex` produces `std::array<std::byte>` as the momentum in the codebase is to use `std::byte` over `uint8_t`. Also makes `uint256` hex string constructor disallow uppercase hex digits. Discussed: bitcoin#30560 (comment) Surprisingly does not change the size of the Guix **bitcoind** binary (on x86_64-linux-gnu) by 1 single byte. Spawned already merged PRs: bitcoin#30436, bitcoin#30482, bitcoin#30532, bitcoin#30560. ACKs for top commit: l0rinc: ACK 8756ccd stickies-v: Rebase re-ACK 8756ccd, no changes since a096215 ryanofsky: Code review ACK 8756ccd, just rebasing since last review and taking advantage of CScript constructors in bitcoin#29369, also tweaking a code comment Tree-SHA512: 9b2011b7c37e0ef004c669f8601270a214b388916316458370f5902c79c2856790b1b2c7c123efa65decad04886ab5eff95644301e0d84358bb265cf1f8ec195
2 parents e96f657 + 8756ccd commit b52d547

31 files changed

+482
-361
lines changed

src/bench/bech32.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
#include <vector>
1010

11+
using namespace util::hex_literals;
1112

1213
static void Bech32Encode(benchmark::Bench& bench)
1314
{
14-
std::vector<uint8_t> v = ParseHex("c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db");
15+
constexpr std::array<uint8_t, 32> v{"c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db"_hex_u8};
1516
std::vector<unsigned char> tmp = {0};
16-
tmp.reserve(1 + 32 * 8 / 5);
17+
tmp.reserve(1 + v.size() * 8 / 5);
1718
ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, v.begin(), v.end());
1819
bench.batch(v.size()).unit("byte").run([&] {
1920
bech32::Encode(bech32::Encoding::BECH32, "bc", tmp);

src/bench/index_blockfilter.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
#include <memory>
2626
#include <vector>
2727

28+
using namespace util::hex_literals;
29+
2830
// Very simple block filter index sync benchmark, only using coinbase outputs.
2931
static void BlockFilterIndexSync(benchmark::Bench& bench)
3032
{
3133
const auto test_setup = MakeNoLogFileContext<TestChain100Setup>();
3234

3335
// Create more blocks
3436
int CHAIN_SIZE = 600;
35-
CPubKey pubkey{ParseHex("02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9")};
37+
CPubKey pubkey{"02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9"_hex_u8};
3638
CScript script = GetScriptForDestination(WitnessV0KeyHash(pubkey));
3739
std::vector<CMutableTransaction> noTxns;
3840
for (int i = 0; i < CHAIN_SIZE - 100; i++) {

src/kernel/chainparams.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <cstring>
2727
#include <type_traits>
2828

29+
using namespace util::hex_literals;
30+
2931
// Workaround MSVC bug triggering C7595 when calling consteval constructors in
3032
// initializer lists.
3133
// A fix may be on the way:
@@ -71,7 +73,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
7173
static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
7274
{
7375
const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
74-
const CScript genesisOutputScript = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
76+
const CScript genesisOutputScript = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex_v_u8 << OP_CHECKSIG;
7577
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
7678
}
7779

@@ -351,7 +353,7 @@ class CTestNet4Params : public CChainParams {
351353
m_assumed_chain_state_size = 0;
352354

353355
const char* testnet4_genesis_msg = "03/May/2024 000000000000000000001ebd58c244970b3aa9d783bb001011fbe8ea8e98e00e";
354-
const CScript testnet4_genesis_script = CScript() << ParseHex("000000000000000000000000000000000000000000000000000000000000000000") << OP_CHECKSIG;
356+
const CScript testnet4_genesis_script = CScript() << "000000000000000000000000000000000000000000000000000000000000000000"_hex_v_u8 << OP_CHECKSIG;
355357
genesis = CreateGenesisBlock(testnet4_genesis_msg,
356358
testnet4_genesis_script,
357359
1714777860,
@@ -412,7 +414,7 @@ class SigNetParams : public CChainParams {
412414
vSeeds.clear();
413415

414416
if (!options.challenge) {
415-
bin = ParseHex("512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae");
417+
bin = "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae"_hex_v_u8;
416418
vSeeds.emplace_back("seed.signet.bitcoin.sprovoost.nl.");
417419
vSeeds.emplace_back("seed.signet.achownodes.xyz."); // Ava Chow, only supports x1, x5, x9, x49, x809, x849, xd, x400, x404, x408, x448, xc08, xc48, x40c
418420

src/net_processing.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#include <typeinfo>
5555
#include <utility>
5656

57+
using namespace util::hex_literals;
58+
5759
/** Headers download timeout.
5860
* Timeout = base + per_header * (expected number of headers) */
5961
static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE = 15min;
@@ -3931,8 +3933,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
39313933

39323934
// If the peer is old enough to have the old alert system, send it the final alert.
39333935
if (greatest_common_version <= 70012) {
3934-
const auto finalAlert{ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50")};
3935-
MakeAndPushMessage(pfrom, "alert", Span{finalAlert});
3936+
constexpr auto finalAlert{"60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"_hex};
3937+
MakeAndPushMessage(pfrom, "alert", finalAlert);
39363938
}
39373939

39383940
// Feeler connections exist only to verify if address is online.

src/pubkey.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <algorithm>
1919
#include <cassert>
2020

21+
using namespace util::hex_literals;
22+
2123
namespace {
2224

2325
struct Secp256k1SelfTester
@@ -190,14 +192,7 @@ int ecdsa_signature_parse_der_lax(secp256k1_ecdsa_signature* sig, const unsigned
190192
* For an example script for calculating H, refer to the unit tests in
191193
* ./test/functional/test_framework/crypto/secp256k1.py
192194
*/
193-
static const std::vector<unsigned char> NUMS_H_DATA{ParseHex("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")};
194-
const XOnlyPubKey XOnlyPubKey::NUMS_H{NUMS_H_DATA};
195-
196-
XOnlyPubKey::XOnlyPubKey(Span<const unsigned char> bytes)
197-
{
198-
assert(bytes.size() == 32);
199-
std::copy(bytes.begin(), bytes.end(), m_keydata.begin());
200-
}
195+
constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8};
201196

202197
std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
203198
{

src/pubkey.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class XOnlyPubKey
254254
bool IsNull() const { return m_keydata.IsNull(); }
255255

256256
/** Construct an x-only pubkey from exactly 32 bytes. */
257-
explicit XOnlyPubKey(Span<const unsigned char> bytes);
257+
constexpr explicit XOnlyPubKey(std::span<const unsigned char> bytes) : m_keydata{bytes} {}
258258

259259
/** Construct an x-only pubkey from a normal pubkey. */
260260
explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span{pubkey}.subspan(1, 32)) {}

src/test/base58_tests.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <string>
1818

1919
using namespace std::literals;
20+
using namespace util::hex_literals;
2021

2122
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
2223

@@ -72,7 +73,7 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
7273
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
7374
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
7475
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
75-
std::vector<unsigned char> expected = ParseHex("971a55");
76+
constexpr auto expected{"971a55"_hex_u8};
7677
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
7778

7879
BOOST_CHECK(DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));

0 commit comments

Comments
 (0)