Bitcoin Core  27.99.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 
13 #include <algorithm>
14 #include <array>
15 #include <cassert>
16 #include <cstdint>
17 #include <cstring>
18 #include <optional>
19 #include <string>
20 
22 template<unsigned int BITS>
23 class base_blob
24 {
25 protected:
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");
30 
31 public:
32  /* construct 0 value by default */
33  constexpr base_blob() : m_data() {}
34 
35  /* constructor for constants between 1 and 255 */
36  constexpr explicit base_blob(uint8_t v) : m_data{v} {}
37 
38  constexpr explicit base_blob(Span<const unsigned char> vch)
39  {
40  assert(vch.size() == WIDTH);
41  std::copy(vch.begin(), vch.end(), m_data.begin());
42  }
43 
44  constexpr bool IsNull() const
45  {
46  return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
47  return val == 0;
48  });
49  }
50 
51  constexpr void SetNull()
52  {
53  std::fill(m_data.begin(), m_data.end(), 0);
54  }
55 
56  constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
57 
58  friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
59  friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
60  friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
61 
62  // Hex string representations are little-endian.
63  std::string GetHex() const;
65  void SetHexDeprecated(std::string_view str);
66  std::string ToString() const;
67 
68  constexpr const unsigned char* data() const { return m_data.data(); }
69  constexpr unsigned char* data() { return m_data.data(); }
70 
71  constexpr unsigned char* begin() { return m_data.data(); }
72  constexpr unsigned char* end() { return m_data.data() + WIDTH; }
73 
74  constexpr const unsigned char* begin() const { return m_data.data(); }
75  constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
76 
77  static constexpr unsigned int size() { return WIDTH; }
78 
79  constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
80 
81  template<typename Stream>
82  void Serialize(Stream& s) const
83  {
84  s << Span(m_data);
85  }
86 
87  template<typename Stream>
88  void Unserialize(Stream& s)
89  {
91  }
92 };
93 
94 namespace detail {
101 template <class uintN_t>
102 std::optional<uintN_t> FromHex(std::string_view str)
103 {
104  if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
105  uintN_t rv;
106  rv.SetHexDeprecated(str);
107  return rv;
108 }
109 } // namespace detail
110 
115 class uint160 : public base_blob<160> {
116 public:
117  static std::optional<uint160> FromHex(std::string_view str) { return detail::FromHex<uint160>(str); }
118  constexpr uint160() = default;
119  constexpr explicit uint160(Span<const unsigned char> vch) : base_blob<160>(vch) {}
120 };
121 
127 class uint256 : public base_blob<256> {
128 public:
129  static std::optional<uint256> FromHex(std::string_view str) { return detail::FromHex<uint256>(str); }
130  constexpr uint256() = default;
131  constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
132  constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
133  static const uint256 ZERO;
134  static const uint256 ONE;
135 };
136 
137 /* uint256 from std::string_view, treated as little-endian.
138  * DEPRECATED. Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
139  */
140 inline uint256 uint256S(std::string_view str)
141 {
142  uint256 rv;
143  rv.SetHexDeprecated(str);
144  return rv;
145 }
146 
147 #endif // BITCOIN_UINT256_H
constexpr std::size_t size() const noexcept
Definition: span.h:187
constexpr C * end() const noexcept
Definition: span.h:176
constexpr C * begin() const noexcept
Definition: span.h:175
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:24
constexpr base_blob(uint8_t v)
Definition: uint256.h:36
constexpr bool IsNull() const
Definition: uint256.h:44
constexpr unsigned char * end()
Definition: uint256.h:72
static constexpr int WIDTH
Definition: uint256.h:26
static constexpr unsigned int size()
Definition: uint256.h:77
constexpr uint64_t GetUint64(int pos) const
Definition: uint256.h:79
void Unserialize(Stream &s)
Definition: uint256.h:88
std::string ToString() const
Definition: uint256.cpp:47
void SetHexDeprecated(std::string_view str)
Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated.
Definition: uint256.cpp:21
constexpr const unsigned char * data() const
Definition: uint256.h:68
constexpr const unsigned char * begin() const
Definition: uint256.h:74
constexpr friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:59
constexpr friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:60
constexpr int Compare(const base_blob &other) const
Definition: uint256.h:56
constexpr unsigned char * begin()
Definition: uint256.h:71
constexpr friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:58
constexpr unsigned char * data()
Definition: uint256.h:69
void Serialize(Stream &s) const
Definition: uint256.h:82
constexpr const unsigned char * end() const
Definition: uint256.h:75
constexpr void SetNull()
Definition: uint256.h:51
std::string GetHex() const
Definition: uint256.cpp:11
constexpr base_blob(Span< const unsigned char > vch)
Definition: uint256.h:38
std::array< uint8_t, WIDTH > m_data
Definition: uint256.h:27
constexpr base_blob()
Definition: uint256.h:33
160-bit opaque blob.
Definition: uint256.h:115
constexpr uint160(Span< const unsigned char > vch)
Definition: uint256.h:119
static std::optional< uint160 > FromHex(std::string_view str)
Definition: uint256.h:117
constexpr uint160()=default
256-bit opaque blob.
Definition: uint256.h:127
constexpr uint256(Span< const unsigned char > vch)
Definition: uint256.h:132
constexpr uint256()=default
static const uint256 ONE
Definition: uint256.h:134
static const uint256 ZERO
Definition: uint256.h:133
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:129
constexpr uint256(uint8_t v)
Definition: uint256.h:131
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:27
Definition: uint256.h:94
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...
Definition: uint256.h:102
Span(T *, EndOrSize) -> Span< T >
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:282
uint256 uint256S(std::string_view str)
Definition: uint256.h:140
bool IsHex(std::string_view str)
assert(!tx.IsCoinBase())