Skip to content

Commit

Permalink
uint256: Add constexpr ctor taking 4 uint64_t
Browse files Browse the repository at this point in the history
  • Loading branch information
hodlinator committed Jul 17, 2024
1 parent c3a9c71 commit f957901
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/kernel/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/test/uint256_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 16 additions & 0 deletions src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class base_blob
protected:
static constexpr int WIDTH = BITS / 8;
static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
static_assert(WIDTH > 0, "base_blob currently only supports non-zero width.");
std::array<uint8_t, WIDTH> m_data;
static_assert(WIDTH == sizeof(m_data), "Sanity check");

Expand All @@ -39,6 +40,19 @@ class base_blob
std::copy(vch.begin(), vch.end(), m_data.begin());
}

// Uses std::span instead of Span to get rbegin() & rend().
constexpr explicit base_blob(std::span<const uint64_t> parts)
requires(BITS % 64 == 0)
{
assert(parts.size() * sizeof(parts[0]) == WIDTH);
auto writeIt = m_data.begin();
for (auto partIt = parts.rbegin(); partIt != parts.rend(); ++partIt) {
for (unsigned bitOffset = 0; bitOffset < 64; bitOffset += 8) {
*(writeIt++) = (*partIt >> bitOffset) & 0xFF;
}
}
}

constexpr bool IsNull() const
{
return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
Expand Down Expand Up @@ -106,6 +120,8 @@ class uint256 : public base_blob<256> {
public:
constexpr uint256() = default;
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
// Currently implicitly constructs an intermediate std::vector using extra curly-braces until, see C++ P2447R6.
constexpr explicit uint256(uint64_t a, uint64_t b, uint64_t c, uint64_t d) : base_blob<256>{std::span<const uint64_t>{{a, b, c, d}}} {}
constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
static const uint256 ZERO;
static const uint256 ONE;
Expand Down

0 comments on commit f957901

Please sign in to comment.