6#ifndef BITCOIN_UINT256_H
7#define BITCOIN_UINT256_H
24template<
unsigned int BITS>
28 static constexpr int WIDTH = BITS / 8;
29 static_assert(BITS % 8 == 0,
"base_blob currently only supports whole bytes.");
31 static_assert(
WIDTH ==
sizeof(
m_data),
"Sanity check");
40 constexpr explicit base_blob(std::span<const unsigned char> vch)
43 std::copy(vch.begin(), vch.end(),
m_data.begin());
46 consteval explicit base_blob(std::string_view hex_str);
50 return std::all_of(
m_data.begin(),
m_data.end(), [](uint8_t val) {
94 std::string
GetHex()
const;
98 constexpr const unsigned char*
data()
const {
return m_data.data(); }
99 constexpr unsigned char*
data() {
return m_data.data(); }
104 constexpr const unsigned char*
begin()
const {
return m_data.data(); }
111 template<
typename Stream>
117 template<
typename Stream>
124template <
unsigned int BITS>
127 if (hex_str.length() != m_data.size() * 2)
throw "Hex string must fit exactly";
128 auto str_it = hex_str.rbegin();
129 for (
auto& elem : m_data) {
142template <
class u
intN_t>
143std::optional<uintN_t>
FromHex(std::string_view str)
145 if (uintN_t::size() * 2 != str.size() || !
IsHex(str))
return std::nullopt;
147 unsigned char* p1 = rv.begin();
148 unsigned char* pend = rv.end();
149 size_t digits = str.size();
150 while (digits > 0 && p1 < pend) {
153 *p1 |= ((
unsigned char)
::HexDigit(str[--digits]) << 4);
166template <
class u
intN_t>
170 constexpr auto expected_size{uintN_t::size() * 2};
171 if (input.size() < expected_size) {
172 auto padded = std::string(expected_size,
'0');
173 std::copy(input.begin(), input.end(), padded.begin() + expected_size - input.size());
174 return FromHex<uintN_t>(padded);
176 return FromHex<uintN_t>(input);
186 static std::optional<uint160>
FromHex(std::string_view str) {
return detail::FromHex<uint160>(str); }
198 static std::optional<uint256>
FromHex(std::string_view str) {
return detail::FromHex<uint256>(str); }
199 static std::optional<uint256>
FromUserHex(std::string_view str) {
return detail::FromUserHex<uint256>(str); }
Template base class for fixed-sized opaque blobs.
constexpr base_blob(uint8_t v)
constexpr base_blob(std::span< const unsigned char > vch)
constexpr bool IsNull() const
constexpr unsigned char * data()
consteval base_blob(std::string_view hex_str)
constexpr const unsigned char * begin() const
friend constexpr bool operator!=(const base_blob &a, const base_blob &b)
constexpr unsigned char * end()
static constexpr int WIDTH
static constexpr unsigned int size()
friend constexpr bool operator<(const base_blob &a, const base_blob &b)
constexpr uint64_t GetUint64(int pos) const
void Unserialize(Stream &s)
std::string ToString() const
friend constexpr bool operator==(const base_blob &a, const base_blob &b)
constexpr const unsigned char * end() const
constexpr int Compare(const base_blob &other) const
Lexicographic ordering.
constexpr unsigned char * begin()
void Serialize(Stream &s) const
std::string GetHex() const
constexpr const unsigned char * data() const
std::array< uint8_t, WIDTH > m_data
constexpr uint160(std::span< const unsigned char > vch)
static std::optional< uint160 > FromHex(std::string_view str)
constexpr uint160()=default
static std::optional< uint256 > FromUserHex(std::string_view str)
constexpr uint256()=default
static const uint256 ZERO
constexpr uint256(std::span< const unsigned char > vch)
static std::optional< uint256 > FromHex(std::string_view str)
constexpr uint256(uint8_t v)
consteval uint256(std::string_view hex_str)
uint64_t ReadLE64(const B *ptr)
signed char HexDigit(char c)
std::optional< uintN_t > FromHex(std::string_view str)
Writes the hex string (in reverse byte order) into a new uintN_t object and only returns a value iff ...
std::optional< uintN_t > FromUserHex(std::string_view input)
Like FromHex(std::string_view str), but allows an "0x" prefix and pads the input with leading zeroes ...
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
auto MakeWritableByteSpan(V &&v) noexcept
bool IsHex(std::string_view str)