Bitcoin Core  24.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-2022 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 
12 #include <algorithm>
13 #include <array>
14 #include <cassert>
15 #include <cstring>
16 #include <stdint.h>
17 #include <string>
18 
20 template<unsigned int BITS>
21 class base_blob
22 {
23 protected:
24  static constexpr int WIDTH = BITS / 8;
25  std::array<uint8_t, WIDTH> m_data;
26  static_assert(WIDTH == sizeof(m_data), "Sanity check");
27 
28 public:
29  /* construct 0 value by default */
30  constexpr base_blob() : m_data() {}
31 
32  /* constructor for constants between 1 and 255 */
33  constexpr explicit base_blob(uint8_t v) : m_data{v} {}
34 
35  constexpr explicit base_blob(Span<const unsigned char> vch)
36  {
37  assert(vch.size() == WIDTH);
38  std::copy(vch.begin(), vch.end(), m_data.begin());
39  }
40 
41  constexpr bool IsNull() const
42  {
43  return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
44  return val == 0;
45  });
46  }
47 
48  constexpr void SetNull()
49  {
50  std::fill(m_data.begin(), m_data.end(), 0);
51  }
52 
53  constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
54 
55  friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
56  friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
57  friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
58 
59  std::string GetHex() const;
60  void SetHex(const char* psz);
61  void SetHex(const std::string& str);
62  std::string ToString() const;
63 
64  constexpr const unsigned char* data() const { return m_data.data(); }
65  constexpr unsigned char* data() { return m_data.data(); }
66 
67  constexpr unsigned char* begin() { return m_data.data(); }
68  constexpr unsigned char* end() { return m_data.data() + WIDTH; }
69 
70  constexpr const unsigned char* begin() const { return m_data.data(); }
71  constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
72 
73  static constexpr unsigned int size() { return WIDTH; }
74 
75  constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
76 
77  template<typename Stream>
78  void Serialize(Stream& s) const
79  {
80  s.write(MakeByteSpan(m_data));
81  }
82 
83  template<typename Stream>
84  void Unserialize(Stream& s)
85  {
87  }
88 };
89 
94 class uint160 : public base_blob<160> {
95 public:
96  constexpr uint160() = default;
97  constexpr explicit uint160(Span<const unsigned char> vch) : base_blob<160>(vch) {}
98 };
99 
105 class uint256 : public base_blob<256> {
106 public:
107  constexpr uint256() = default;
108  constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
109  constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
110  static const uint256 ZERO;
111  static const uint256 ONE;
112 };
113 
114 /* uint256 from const char *.
115  * This is a separate function because the constructor uint256(const char*) can result
116  * in dangerously catching uint256(0).
117  */
118 inline uint256 uint256S(const char *str)
119 {
120  uint256 rv;
121  rv.SetHex(str);
122  return rv;
123 }
124 /* uint256 from std::string.
125  * This is a separate function because the constructor uint256(const std::string &str) can result
126  * in dangerously catching uint256(0) via std::string(const char*).
127  */
128 inline uint256 uint256S(const std::string& str)
129 {
130  uint256 rv;
131  rv.SetHex(str);
132  return rv;
133 }
134 
135 #endif // BITCOIN_UINT256_H
constexpr std::size_t size() const noexcept
Definition: span.h:186
constexpr C * end() const noexcept
Definition: span.h:175
constexpr C * begin() const noexcept
Definition: span.h:174
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:22
constexpr base_blob(uint8_t v)
Definition: uint256.h:33
constexpr bool IsNull() const
Definition: uint256.h:41
constexpr unsigned char * end()
Definition: uint256.h:68
static constexpr int WIDTH
Definition: uint256.h:24
static constexpr unsigned int size()
Definition: uint256.h:73
void SetHex(const char *psz)
Definition: uint256.cpp:21
constexpr uint64_t GetUint64(int pos) const
Definition: uint256.h:75
void Unserialize(Stream &s)
Definition: uint256.h:84
std::string ToString() const
Definition: uint256.cpp:55
constexpr const unsigned char * data() const
Definition: uint256.h:64
constexpr const unsigned char * begin() const
Definition: uint256.h:70
constexpr friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:56
constexpr friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:57
constexpr int Compare(const base_blob &other) const
Definition: uint256.h:53
constexpr unsigned char * begin()
Definition: uint256.h:67
constexpr friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:55
constexpr unsigned char * data()
Definition: uint256.h:65
void Serialize(Stream &s) const
Definition: uint256.h:78
constexpr const unsigned char * end() const
Definition: uint256.h:71
constexpr void SetNull()
Definition: uint256.h:48
std::string GetHex() const
Definition: uint256.cpp:11
constexpr base_blob(Span< const unsigned char > vch)
Definition: uint256.h:35
std::array< uint8_t, WIDTH > m_data
Definition: uint256.h:25
constexpr base_blob()
Definition: uint256.h:30
160-bit opaque blob.
Definition: uint256.h:94
constexpr uint160(Span< const unsigned char > vch)
Definition: uint256.h:97
constexpr uint160()=default
256-bit opaque blob.
Definition: uint256.h:105
constexpr uint256(Span< const unsigned char > vch)
Definition: uint256.h:109
constexpr uint256()=default
static const uint256 ONE
Definition: uint256.h:111
static const uint256 ZERO
Definition: uint256.h:110
constexpr uint256(uint8_t v)
Definition: uint256.h:108
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:269
uint256 uint256S(const char *str)
Definition: uint256.h:118
assert(!tx.IsCoinBase())