6#ifndef BITCOIN_UINT256_H
7#define BITCOIN_UINT256_H
22template<
unsigned int BITS>
26 static constexpr int WIDTH = BITS / 8;
27 static_assert(BITS % 8 == 0,
"base_blob currently only supports whole bytes.");
29 static_assert(
WIDTH ==
sizeof(
m_data),
"Sanity check");
44 consteval explicit base_blob(std::string_view hex_str);
48 return std::all_of(
m_data.begin(),
m_data.end(), [](uint8_t val) {
81 std::string
GetHex()
const;
99 constexpr const unsigned char*
data()
const {
return m_data.data(); }
105 constexpr const unsigned char*
begin()
const {
return m_data.data(); }
112 template<
typename Stream>
118 template<
typename Stream>
125template <
unsigned int BITS>
129 auto from_hex = [](
const char c) -> int8_t {
130 if (c >=
'0' && c <=
'9')
return c -
'0';
131 if (c >=
'a' && c <=
'f')
return c -
'a' + 0xA;
132 if (c >=
'A' && c <=
'F')
return c -
'A' + 0xA;
137 assert(hex_str.length() == m_data.size() * 2);
138 auto str_it = hex_str.rbegin();
139 for (
auto& elem : m_data) {
141 elem = (
from_hex(*(str_it++)) << 4) | lo;
152template <
class u
intN_t>
153std::optional<uintN_t>
FromHex(std::string_view str)
155 if (uintN_t::size() * 2 != str.size() || !
IsHex(str))
return std::nullopt;
157 rv.SetHexDeprecated(str);
A Span is an object that can refer to a contiguous sequence of objects.
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
std::string GetHex() 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)
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()
std::string ToString() const
void Serialize(Stream &s) const
void SetHexDeprecated(std::string_view str)
Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
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
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 ...
Span(T *, EndOrSize) -> Span< T >
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
static void from_hex(unsigned char *data, int len, const char *hex)
uint256 uint256S(std::string_view str)
bool IsHex(std::string_view str)