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 54e0213
Show file tree
Hide file tree
Showing 3 changed files with 27 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()
13 changes: 13 additions & 0 deletions src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class base_blob
std::copy(vch.begin(), vch.end(), m_data.begin());
}

template <size_t Size>
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) {
Expand Down Expand Up @@ -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<const unsigned char> vch) : base_blob<256>(vch) {}
static const uint256 ZERO;
static const uint256 ONE;
Expand Down

0 comments on commit 54e0213

Please sign in to comment.