6 #ifndef BITCOIN_UINT256_H 7 #define BITCOIN_UINT256_H 22 template<
unsigned int BITS>
26 static constexpr
int WIDTH = BITS / 8;
27 static_assert(BITS % 8 == 0,
"base_blob currently only supports whole bytes.");
28 std::array<uint8_t, WIDTH>
m_data;
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>
125 template <
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;
152 template <
class u
intN_t>
153 std::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);
168 static std::optional<uint160>
FromHex(std::string_view str) {
return detail::FromHex<uint160>(str); }
180 static std::optional<uint256>
FromHex(std::string_view str) {
return detail::FromHex<uint256>(str); }
199 #endif // BITCOIN_UINT256_H consteval uint256(std::string_view hex_str)
constexpr C * end() const noexcept
static constexpr unsigned int size()
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
bool IsHex(std::string_view str)
constexpr std::size_t size() const noexcept
void Serialize(Stream &s) const
constexpr uint256(uint8_t v)
static void from_hex(unsigned char *data, int len, const char *hex)
constexpr unsigned char * data()
friend constexpr bool operator<(const base_blob &a, const base_blob &b)
constexpr uint160(Span< const unsigned char > vch)
std::array< uint8_t, WIDTH > m_data
constexpr unsigned char * begin()
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! ...
void Unserialize(Stream &s)
friend constexpr bool operator!=(const base_blob &a, const base_blob &b)
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 ...
static std::optional< uint256 > FromHex(std::string_view str)
static const uint256 ZERO
constexpr uint160()=default
std::string ToString() const
constexpr bool IsNull() const
static uint64_t ReadLE64(const unsigned char *ptr)
constexpr int Compare(const base_blob &other) const
Lexicographic ordering.
constexpr C * begin() const noexcept
Template base class for fixed-sized opaque blobs.
constexpr const unsigned char * data() const
static std::optional< uint160 > FromHex(std::string_view str)
static constexpr int WIDTH
constexpr unsigned char * end()
constexpr uint64_t GetUint64(int pos) const
std::string GetHex() const
uint256 uint256S(std::string_view str)
constexpr const unsigned char * begin() const
constexpr const unsigned char * end() const
constexpr base_blob(Span< const unsigned char > vch)
Span(T *, EndOrSize) -> Span< T >
constexpr uint256(Span< const unsigned char > vch)
constexpr uint256()=default
constexpr base_blob(uint8_t v)