6#ifndef BITCOIN_UINT256_H
7#define BITCOIN_UINT256_H
26 static constexpr int WIDTH = BITS / 8;
27 static_assert(BITS % 8 == 0,
28 "base_blob currently only supports whole bytes.");
30 static_assert(
WIDTH ==
sizeof(
m_data),
"Sanity check");
41 std::copy(vch.
begin(), vch.
end(), m_data.begin());
45 return std::all_of(m_data.begin(), m_data.end(),
46 [](uint8_t val) { return val == 0; });
49 constexpr void SetNull() { std::fill(m_data.begin(), m_data.end(), 0); }
52 for (
size_t i = 0; i < WIDTH; i++) {
53 uint8_t a = m_data[WIDTH - 1 - i];
54 uint8_t b = other.
m_data[WIDTH - 1 - i];
99 std::string GetHex()
const;
116 void SetHexDeprecated(std::string_view str);
119 constexpr const uint8_t *
data()
const {
return m_data.data(); }
120 constexpr uint8_t *
data() {
return m_data.data(); }
122 constexpr uint8_t *
begin() {
return m_data.data(); }
123 constexpr uint8_t *
end() {
return m_data.data() + WIDTH; }
125 constexpr const uint8_t *
begin()
const {
return m_data.data(); }
126 constexpr const uint8_t *
end()
const {
return m_data.data() + WIDTH; }
128 static constexpr unsigned int size() {
return WIDTH; }
131 return ReadLE64(m_data.data() + pos * 8);
134 template <
typename Stream>
void Serialize(Stream &s)
const {
150template <
class u
intN_t> std::optional<uintN_t>
FromHex(std::string_view str) {
151 if (uintN_t::size() * 2 != str.size() || !
IsHex(str)) {
155 rv.SetHexDeprecated(str);
165template <
class u
intN_t>
168 constexpr auto expected_size{uintN_t::size() * 2};
169 if (input.size() < expected_size) {
170 auto padded = std::string(expected_size,
'0');
171 std::copy(input.begin(), input.end(),
172 padded.begin() + expected_size - input.size());
173 return FromHex<uintN_t>(padded);
175 return FromHex<uintN_t>(input);
186 static std::optional<uint160>
FromHex(std::string_view str) {
187 return detail::FromHex<uint160>(str);
201 static std::optional<uint256>
FromHex(std::string_view str) {
202 return detail::FromHex<uint256>(str);
205 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 const uint8_t * data() const
friend constexpr bool operator!=(const base_blob &a, const base_blob &b)
static constexpr int WIDTH
constexpr base_blob(Span< const uint8_t > vch)
static constexpr unsigned int size()
friend constexpr bool operator<(const base_blob &a, const base_blob &b)
friend constexpr bool operator>(const base_blob &a, const base_blob &b)
constexpr uint64_t GetUint64(int pos) const
void Unserialize(Stream &s)
constexpr uint8_t * begin()
constexpr uint8_t * data()
friend constexpr bool operator<=(const base_blob &a, const base_blob &b)
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 uint8_t * begin() const
constexpr uint8_t * end()
friend constexpr bool operator>=(const base_blob &a, const base_blob &b)
constexpr int Compare(const base_blob &other) const
constexpr const uint8_t * end() const
void Serialize(Stream &s) const
std::array< uint8_t, WIDTH > m_data
constexpr uint160(Span< const uint8_t > 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
constexpr uint256(Span< const uint8_t > vch)
static const uint256 ZERO
static std::optional< uint256 > FromHex(std::string_view str)
constexpr uint256(uint8_t v)
static uint64_t ReadLE64(const uint8_t *ptr)
std::optional< uintN_t > FromHex(std::string_view str)
Writes the hex string (treated as little-endian) into a new uintN_t object and only returns a value i...
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 ...
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Span(T *, EndOrSize) -> Span< T >
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
uint256 uint256S(std::string_view str)
DEPRECATED.
bool IsHex(std::string_view str)
Returns true if each character in str is a hex character, and has an even number of hex digits.