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