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");
46 consteval explicit base_blob(std::string_view hex_str);
50 return std::all_of(
m_data.begin(),
m_data.end(), [](uint8_t val) {
83 std::string
GetHex()
const;
101 constexpr const unsigned char*
data()
const {
return m_data.data(); }
107 constexpr const unsigned char*
begin()
const {
return m_data.data(); }
114 template<
typename Stream>
120 template<
typename Stream>
127template <
unsigned int BITS>
130 if (hex_str.length() != m_data.size() * 2)
throw "Hex string must fit exactly";
131 auto str_it = hex_str.rbegin();
132 for (
auto& elem : m_data) {
145template <
class u
intN_t>
146std::optional<uintN_t>
FromHex(std::string_view str)
148 if (uintN_t::size() * 2 != str.size() || !
IsHex(str))
return std::nullopt;
150 rv.SetHexDeprecated(str);
160template <
class u
intN_t>
164 constexpr auto expected_size{uintN_t::size() * 2};
165 if (input.size() < expected_size) {
166 auto padded = std::string(expected_size,
'0');
167 std::copy(input.begin(), input.end(), padded.begin() + expected_size - input.size());
168 return FromHex<uintN_t>(padded);
170 return FromHex<uintN_t>(input);
180 static std::optional<uint160>
FromHex(std::string_view str) {
return detail::FromHex<uint160>(str); }
192 static std::optional<uint256>
FromHex(std::string_view str) {
return detail::FromHex<uint256>(str); }
193 static std::optional<uint256>
FromUserHex(std::string_view str) {
return detail::FromUserHex<uint256>(str); }
constexpr std::size_t size() const noexcept
constexpr C * begin() const noexcept
constexpr C * end() const noexcept
Template base class for fixed-sized opaque blobs.
constexpr base_blob(uint8_t v)
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)
void SetHexDeprecated(std::string_view str)
Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
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
constexpr base_blob(Span< const unsigned char > vch)
std::array< uint8_t, WIDTH > m_data
constexpr uint160(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(Span< const unsigned char > vch)
constexpr uint256()=default
static const uint256 ZERO
static std::optional< uint256 > FromHex(std::string_view str)
constexpr uint256(uint8_t v)
consteval uint256(std::string_view hex_str)
static uint64_t ReadLE64(const unsigned char *ptr)
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)
Span(T *, EndOrSize) -> Span< T >
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
bool IsHex(std::string_view str)