From 54e0213c9c7a7a942dd320264c1f9224c494b777 Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:48:04 +0200 Subject: [PATCH] uint256: Add constexpr ctor taking 4 uint64_t --- src/kernel/chainparams.cpp | 4 ++-- src/test/uint256_tests.cpp | 12 ++++++++++++ src/uint256.h | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 2b729e3b7a9b43..e795f06bbcaf38 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -76,9 +76,9 @@ class CMainParams : public CChainParams { consensus.signet_challenge.clear(); consensus.nSubsidyHalvingInterval = 210000; consensus.script_flag_exceptions.emplace( // BIP16 exception - uint256S("0x00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22"), SCRIPT_VERIFY_NONE); + uint256{0x00000000000002dc, 0x756eebf4f49723ed, 0x8d30cc28a5f108eb, 0x94b1ba88ac4f9c22}, SCRIPT_VERIFY_NONE); consensus.script_flag_exceptions.emplace( // Taproot exception - uint256S("0x0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad"), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS); + uint256{0x0000000000000000, 0x000f14c35b2d841e, 0x986ab5441de8c585, 0xd5ffe55ea1e395ad}, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS); consensus.BIP34Height = 227931; consensus.BIP34Hash = uint256S("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8"); consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0 diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index 2053a42d241f5f..b2866ed4a9adb9 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -329,4 +329,16 @@ BOOST_AUTO_TEST_CASE( check_ONE ) BOOST_CHECK_EQUAL(one, uint256::ONE); } +BOOST_AUTO_TEST_CASE( check_uint256_uint64_t ) +{ + // Forces compile time evaluation + auto compile_time = [] (auto input) consteval { + return input; + }; + + const uint256 runtime_string = uint256S("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + constexpr uint256 uint64_ctor{compile_time(uint256{0x4a5e1e4baab89f3a, 0x32518a88c31bc87f, 0x618f76673e2cc77a, 0xb2127b7afdeda33b})}; + BOOST_CHECK_EQUAL(runtime_string, uint64_ctor); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/uint256.h b/src/uint256.h index d11a51b0bbc446..d4fce655b132fe 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -39,6 +39,18 @@ class base_blob std::copy(vch.begin(), vch.end(), m_data.begin()); } + template + constexpr explicit base_blob(const uint64_t (&a)[Size]) + requires(BITS % 64 == 0 && sizeof(a) == WIDTH) + { + uint8_t* writeIt = m_data.begin(); + for (signed partIt = Size - 1; partIt >= 0; --partIt) { + for (unsigned bitOffset = 0; bitOffset < 64; bitOffset += 8) { + *(writeIt++) = a[partIt] >> bitOffset; + } + } + } + constexpr bool IsNull() const { return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) { @@ -106,6 +118,7 @@ class uint256 : public base_blob<256> { public: constexpr uint256() = default; constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {} + constexpr explicit uint256(uint64_t a, uint64_t b, uint64_t c, uint64_t d) : base_blob<256>{{a, b, c, d}} {} constexpr explicit uint256(Span vch) : base_blob<256>(vch) {} static const uint256 ZERO; static const uint256 ONE;