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) {
94 std::string
GetHex()
const;
112 constexpr const unsigned char*
data()
const {
return m_data.data(); }
118 constexpr const unsigned char*
begin()
const {
return m_data.data(); }
125 template<
typename Stream>
131 template<
typename Stream>
138template <
unsigned int BITS>
141 if (hex_str.length() != m_data.size() * 2)
throw "Hex string must fit exactly";
142 auto str_it = hex_str.rbegin();
143 for (
auto& elem : m_data) {
156template <
class u
intN_t>
157std::optional<uintN_t>
FromHex(std::string_view str)
159 if (uintN_t::size() * 2 != str.size() || !
IsHex(str))
return std::nullopt;
161 rv.SetHexDeprecated(str);
171template <
class u
intN_t>
175 constexpr auto expected_size{uintN_t::size() * 2};
176 if (input.size() < expected_size) {
177 auto padded = std::string(expected_size,
'0');
178 std::copy(input.begin(), input.end(), padded.begin() + expected_size - input.size());
179 return FromHex<uintN_t>(padded);
181 return FromHex<uintN_t>(input);
191 static std::optional<uint160>
FromHex(std::string_view str) {
return detail::FromHex<uint160>(str); }
203 static std::optional<uint256>
FromHex(std::string_view str) {
return detail::FromHex<uint256>(str); }
204 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)
uint64_t ReadLE64(const B *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)