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