Bitcoin ABC  0.24.7
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
uint256::uint256
constexpr uint256()
Definition: uint256.h:129
base_blob::Serialize
void Serialize(Stream &s) const
Definition: uint256.h:101
base_blob::WIDTH
static constexpr int WIDTH
Definition: uint256.h:18
base_blob::operator!=
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:59
base_blob::GetUint64
uint64_t GetUint64(int pos) const
Definition: uint256.h:93
base_blob::SetNull
void SetNull()
Definition: uint256.h:39
base_blob
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:16
base_blob::end
uint8_t * end()
Definition: uint256.h:85
base_blob::m_data
uint8_t m_data[WIDTH]
Definition: uint256.h:19
base_blob::operator>
friend bool operator>(const base_blob &a, const base_blob &b)
Definition: uint256.h:68
base_blob::data
uint8_t * data()
Definition: uint256.h:81
base_blob::base_blob
constexpr base_blob(uint8_t v)
Definition: uint256.h:26
base_blob::Compare
int Compare(const base_blob &other) const
Definition: uint256.h:41
base_blob::Unserialize
void Unserialize(Stream &s)
Definition: uint256.h:105
base_blob::GetHex
std::string GetHex() const
Definition: uint256.cpp:16
base_blob::operator<
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:62
base_blob::operator==
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:56
base_blob::operator>=
friend bool operator>=(const base_blob &a, const base_blob &b)
Definition: uint256.h:71
uint256::ZERO
static const uint256 ZERO
Definition: uint256.h:132
base_blob::begin
const uint8_t * begin() const
Definition: uint256.h:87
base_blob::ToString
std::string ToString() const
Definition: uint256.h:78
uint256
256-bit opaque blob.
Definition: uint256.h:127
uint256S
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:141
uint256::uint256
constexpr uint256(uint8_t v)
Definition: uint256.h:130
uint256::uint256
uint256(const std::vector< uint8_t > &vch)
Definition: uint256.h:131
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
uint160S
uint160 uint160S(const char *str)
Definition: uint256.h:159
base_blob::end
const uint8_t * end() const
Definition: uint256.h:89
uint160
160-bit opaque blob.
Definition: uint256.h:115
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:30
uint160::uint160
uint160(const std::vector< uint8_t > &vch)
Definition: uint256.h:118
uint160::uint160
constexpr uint160()
Definition: uint256.h:117
base_blob::base_blob
constexpr base_blob()
Definition: uint256.h:23
base_blob::size
unsigned int size() const
Definition: uint256.h:91
base_blob::data
const uint8_t * data() const
Definition: uint256.h:80
base_blob::SetHex
void SetHex(const char *psz)
Definition: uint256.cpp:24
base_blob::operator<=
friend bool operator<=(const base_blob &a, const base_blob &b)
Definition: uint256.h:65
uint256::ONE
static const uint256 ONE
Definition: uint256.h:133