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