Skip to content

Commit 7e1d9a8

Browse files
committed
refactor: Enforce lowercase hex digits for consteval uint256
Also changes compile-time asserts with comments into throws.
1 parent 4ee1940 commit 7e1d9a8

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

src/test/pow_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void sanity_check_chainparams(const ArgsManager& args, ChainType chain_type)
177177

178178
// check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
179179
if (!consensus.fPowNoRetargeting) {
180-
arith_uint256 targ_max{UintToArith256(uint256{"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"})};
180+
arith_uint256 targ_max{UintToArith256(uint256{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"})};
181181
targ_max /= consensus.nPowTargetTimespan*4;
182182
BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
183183
}

src/test/uint256_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ BOOST_AUTO_TEST_CASE( check_ONE )
431431
BOOST_AUTO_TEST_CASE(FromHex_vs_uint256)
432432
{
433433
auto runtime_uint{uint256::FromHex("4A5E1E4BAAB89F3A32518A88C31BC87F618f76673e2cc77ab2127b7afdeda33b").value()};
434-
constexpr uint256 consteval_uint{ "4a5e1e4baab89f3a32518a88c31bc87f618F76673E2CC77AB2127B7AFDEDA33B"};
434+
constexpr uint256 consteval_uint{ "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"};
435435
BOOST_CHECK_EQUAL(consteval_uint, runtime_uint);
436436
}
437437

src/uint256.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,12 @@ consteval base_blob<BITS>::base_blob(std::string_view hex_str)
130130
// Non-lookup table version of HexDigit().
131131
auto from_hex = [](const char c) -> int8_t {
132132
if (c >= '0' && c <= '9') return c - '0';
133-
if (c >= 'a' && c <= 'f') return c - 'a' + 0xA;
134-
if (c >= 'A' && c <= 'F') return c - 'A' + 0xA;
133+
if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
135134

136-
assert(false); // Reached if ctor is called with an invalid hex digit.
135+
throw "Only lowercase hex digits are allowed, for consistency";
137136
};
138137

139-
assert(hex_str.length() == m_data.size() * 2); // 2 hex digits per byte.
138+
if (hex_str.length() != m_data.size() * 2) throw "Hex string must fit exactly";
140139
auto str_it = hex_str.rbegin();
141140
for (auto& elem : m_data) {
142141
auto lo = from_hex(*(str_it++));

0 commit comments

Comments
 (0)