Bitcoin Core  29.1.0
P2P Digital Currency
uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_UINT256_H
7 #define BITCOIN_UINT256_H
8 
9 #include <crypto/common.h>
10 #include <span.h>
11 #include <util/strencodings.h>
12 #include <util/string.h>
13 
14 #include <algorithm>
15 #include <array>
16 #include <cassert>
17 #include <cstdint>
18 #include <cstring>
19 #include <optional>
20 #include <string>
21 #include <string_view>
22 
24 template<unsigned int BITS>
25 class base_blob
26 {
27 protected:
28  static constexpr int WIDTH = BITS / 8;
29  static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
30  std::array<uint8_t, WIDTH> m_data;
31  static_assert(WIDTH == sizeof(m_data), "Sanity check");
32 
33 public:
34  /* construct 0 value by default */
35  constexpr base_blob() : m_data() {}
36 
37  /* constructor for constants between 1 and 255 */
38  constexpr explicit base_blob(uint8_t v) : m_data{v} {}
39 
40  constexpr explicit base_blob(Span<const unsigned char> vch)
41  {
42  assert(vch.size() == WIDTH);
43  std::copy(vch.begin(), vch.end(), m_data.begin());
44  }
45 
46  consteval explicit base_blob(std::string_view hex_str);
47 
48  constexpr bool IsNull() const
49  {
50  return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
51  return val == 0;
52  });
53  }
54 
55  constexpr void SetNull()
56  {
57  std::fill(m_data.begin(), m_data.end(), 0);
58  }
59 
64  constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
65 
66  friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
67  friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
68  friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
69 
94  std::string GetHex() const;
108  void SetHexDeprecated(std::string_view str);
109  std::string ToString() const;
112  constexpr const unsigned char* data() const { return m_data.data(); }
113  constexpr unsigned char* data() { return m_data.data(); }
114 
115  constexpr unsigned char* begin() { return m_data.data(); }
116  constexpr unsigned char* end() { return m_data.data() + WIDTH; }
117 
118  constexpr const unsigned char* begin() const { return m_data.data(); }
119  constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
120 
121  static constexpr unsigned int size() { return WIDTH; }
122 
123  constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
124 
125  template<typename Stream>
126  void Serialize(Stream& s) const
127  {
128  s << Span(m_data);
129  }
130 
131  template<typename Stream>
132  void Unserialize(Stream& s)
133  {
135  }
136 };
137 
138 template <unsigned int BITS>
139 consteval base_blob<BITS>::base_blob(std::string_view hex_str)
140 {
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) {
144  auto lo = util::ConstevalHexDigit(*(str_it++));
145  elem = (util::ConstevalHexDigit(*(str_it++)) << 4) | lo;
146  }
147 }
148 
149 namespace detail {
156 template <class uintN_t>
157 std::optional<uintN_t> FromHex(std::string_view str)
158 {
159  if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
160  uintN_t rv;
161  rv.SetHexDeprecated(str);
162  return rv;
163 }
171 template <class uintN_t>
172 std::optional<uintN_t> FromUserHex(std::string_view input)
173 {
174  input = util::RemovePrefixView(input, "0x");
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);
180  }
181  return FromHex<uintN_t>(input);
182 }
183 } // namespace detail
184 
189 class uint160 : public base_blob<160> {
190 public:
191  static std::optional<uint160> FromHex(std::string_view str) { return detail::FromHex<uint160>(str); }
192  constexpr uint160() = default;
193  constexpr explicit uint160(Span<const unsigned char> vch) : base_blob<160>(vch) {}
194 };
195 
201 class uint256 : public base_blob<256> {
202 public:
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); }
205  constexpr uint256() = default;
206  consteval explicit uint256(std::string_view hex_str) : base_blob<256>(hex_str) {}
207  constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
208  constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
209  static const uint256 ZERO;
210  static const uint256 ONE;
211 };
212 
213 #endif // BITCOIN_UINT256_H
consteval uint256(std::string_view hex_str)
Definition: uint256.h:206
static const uint256 ONE
Definition: uint256.h:210
assert(!tx.IsCoinBase())
constexpr C * end() const noexcept
Definition: span.h:176
static constexpr unsigned int size()
Definition: uint256.h:121
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:274
bool IsHex(std::string_view str)
constexpr std::size_t size() const noexcept
Definition: span.h:187
void Serialize(Stream &s) const
Definition: uint256.h:126
constexpr uint256(uint8_t v)
Definition: uint256.h:207
constexpr unsigned char * data()
Definition: uint256.h:113
friend constexpr bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:68
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 ...
Definition: uint256.h:172
constexpr uint160(Span< const unsigned char > vch)
Definition: uint256.h:193
static std::optional< uint256 > FromUserHex(std::string_view str)
Definition: uint256.h:204
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:169
std::array< uint8_t, WIDTH > m_data
Definition: uint256.h:29
constexpr unsigned char * begin()
Definition: uint256.h:115
friend constexpr bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:66
void SetHexDeprecated(std::string_view str)
Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated! ...
Definition: uint256.cpp:21
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
Definition: strencodings.h:372
void Unserialize(Stream &s)
Definition: uint256.h:132
friend constexpr bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:67
constexpr base_blob()
Definition: uint256.h:35
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 ...
Definition: uint256.h:157
uint64_t ReadLE64(const B *ptr)
Definition: common.h:35
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:203
static const uint256 ZERO
Definition: uint256.h:209
constexpr uint160()=default
std::string ToString() const
Definition: uint256.cpp:47
constexpr bool IsNull() const
Definition: uint256.h:48
constexpr int Compare(const base_blob &other) const
Lexicographic ordering.
Definition: uint256.h:64
constexpr C * begin() const noexcept
Definition: span.h:175
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:25
256-bit opaque blob.
Definition: uint256.h:201
constexpr const unsigned char * data() const
Definition: uint256.h:112
constexpr void SetNull()
Definition: uint256.h:55
static std::optional< uint160 > FromHex(std::string_view str)
Definition: uint256.h:191
static constexpr int WIDTH
Definition: uint256.h:28
constexpr unsigned char * end()
Definition: uint256.h:116
constexpr uint64_t GetUint64(int pos) const
Definition: uint256.h:123
std::string GetHex() const
Definition: uint256.cpp:11
160-bit opaque blob.
Definition: uint256.h:189
constexpr const unsigned char * begin() const
Definition: uint256.h:118
constexpr const unsigned char * end() const
Definition: uint256.h:119
constexpr base_blob(Span< const unsigned char > vch)
Definition: uint256.h:40
Span(T *, EndOrSize) -> Span< T >
constexpr uint256(Span< const unsigned char > vch)
Definition: uint256.h:208
constexpr uint256()=default
constexpr base_blob(uint8_t v)
Definition: uint256.h:38