Bitcoin Core  23.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-2021 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 <span.h>
10 
11 #include <assert.h>
12 #include <cstring>
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
18 template<unsigned int BITS>
19 class base_blob
20 {
21 protected:
22  static constexpr int WIDTH = BITS / 8;
23  uint8_t m_data[WIDTH];
24 public:
25  /* construct 0 value by default */
26  constexpr base_blob() : m_data() {}
27 
28  /* constructor for constants between 1 and 255 */
29  constexpr explicit base_blob(uint8_t v) : m_data{v} {}
30 
31  explicit base_blob(const std::vector<unsigned char>& vch);
32 
33  bool IsNull() const
34  {
35  for (int i = 0; i < WIDTH; i++)
36  if (m_data[i] != 0)
37  return false;
38  return true;
39  }
40 
41  void SetNull()
42  {
43  memset(m_data, 0, sizeof(m_data));
44  }
45 
46  inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }
47 
48  friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
49  friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
50  friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
51 
52  std::string GetHex() const;
53  void SetHex(const char* psz);
54  void SetHex(const std::string& str);
55  std::string ToString() const;
56 
57  const unsigned char* data() const { return m_data; }
58  unsigned char* data() { return m_data; }
59 
60  unsigned char* begin()
61  {
62  return &m_data[0];
63  }
64 
65  unsigned char* end()
66  {
67  return &m_data[WIDTH];
68  }
69 
70  const unsigned char* begin() const
71  {
72  return &m_data[0];
73  }
74 
75  const unsigned char* end() const
76  {
77  return &m_data[WIDTH];
78  }
79 
80  static constexpr unsigned int size()
81  {
82  return sizeof(m_data);
83  }
84 
85  uint64_t GetUint64(int pos) const
86  {
87  const uint8_t* ptr = m_data + pos * 8;
88  return ((uint64_t)ptr[0]) | \
89  ((uint64_t)ptr[1]) << 8 | \
90  ((uint64_t)ptr[2]) << 16 | \
91  ((uint64_t)ptr[3]) << 24 | \
92  ((uint64_t)ptr[4]) << 32 | \
93  ((uint64_t)ptr[5]) << 40 | \
94  ((uint64_t)ptr[6]) << 48 | \
95  ((uint64_t)ptr[7]) << 56;
96  }
97 
98  template<typename Stream>
99  void Serialize(Stream& s) const
100  {
101  s.write(MakeByteSpan(m_data));
102  }
103 
104  template<typename Stream>
105  void Unserialize(Stream& s)
106  {
107  s.read(MakeWritableByteSpan(m_data));
108  }
109 };
110 
115 class uint160 : public base_blob<160> {
116 public:
117  constexpr uint160() {}
118  explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
119 };
120 
126 class uint256 : public base_blob<256> {
127 public:
128  constexpr uint256() {}
129  constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
130  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
131  static const uint256 ZERO;
132  static const uint256 ONE;
133 };
134 
135 /* uint256 from const char *.
136  * This is a separate function because the constructor uint256(const char*) can result
137  * in dangerously catching uint256(0).
138  */
139 inline uint256 uint256S(const char *str)
140 {
141  uint256 rv;
142  rv.SetHex(str);
143  return rv;
144 }
145 /* uint256 from std::string.
146  * This is a separate function because the constructor uint256(const std::string &str) can result
147  * in dangerously catching uint256(0) via std::string(const char*).
148  */
149 inline uint256 uint256S(const std::string& str)
150 {
151  uint256 rv;
152  rv.SetHex(str);
153  return rv;
154 }
155 
156 #endif // BITCOIN_UINT256_H
uint256::uint256
constexpr uint256()
Definition: uint256.h:128
MakeByteSpan
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264
base_blob::Serialize
void Serialize(Stream &s) const
Definition: uint256.h:99
uint160::uint160
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:118
base_blob::WIDTH
static constexpr int WIDTH
Definition: uint256.h:22
base_blob::operator!=
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:49
base_blob::data
const unsigned char * data() const
Definition: uint256.h:57
base_blob::GetUint64
uint64_t GetUint64(int pos) const
Definition: uint256.h:85
base_blob::size
static constexpr unsigned int size()
Definition: uint256.h:80
base_blob::SetNull
void SetNull()
Definition: uint256.h:41
base_blob
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:19
base_blob::m_data
uint8_t m_data[WIDTH]
Definition: uint256.h:23
base_blob::base_blob
constexpr base_blob(uint8_t v)
Definition: uint256.h:29
base_blob::Compare
int Compare(const base_blob &other) const
Definition: uint256.h:46
base_blob::Unserialize
void Unserialize(Stream &s)
Definition: uint256.h:105
span.h
base_blob::GetHex
std::string GetHex() const
Definition: uint256.cpp:20
base_blob::operator<
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:50
base_blob::operator==
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:48
uint256::ZERO
static const uint256 ZERO
Definition: uint256.h:131
base_blob::ToString
std::string ToString() const
Definition: uint256.cpp:64
uint256
256-bit opaque blob.
Definition: uint256.h:126
uint256S
uint256 uint256S(const char *str)
Definition: uint256.h:139
uint256::uint256
constexpr uint256(uint8_t v)
Definition: uint256.h:129
base_blob::end
const unsigned char * end() const
Definition: uint256.h:75
uint160
160-bit opaque blob.
Definition: uint256.h:115
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:33
base_blob::data
unsigned char * data()
Definition: uint256.h:58
uint160::uint160
constexpr uint160()
Definition: uint256.h:117
base_blob::base_blob
constexpr base_blob()
Definition: uint256.h:26
uint256::uint256
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:130
base_blob::begin
const unsigned char * begin() const
Definition: uint256.h:70
MakeWritableByteSpan
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:269
base_blob::SetHex
void SetHex(const char *psz)
Definition: uint256.cpp:30
base_blob::begin
unsigned char * begin()
Definition: uint256.h:60
base_blob::end
unsigned char * end()
Definition: uint256.h:65
uint256::ONE
static const uint256 ONE
Definition: uint256.h:132