PIVX Core  5.6.99
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 developers
3 // Copyright (c) 2014-2015 The Dash developers
4 // Copyright (c) 2017-2021 The PIVX Core developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef PIVX_UINT256_H
9 #define PIVX_UINT256_H
10 
11 #include "crypto/common.h"
12 
13 #include <assert.h>
14 #include <cstring>
15 #include <stdexcept>
16 #include <stdint.h>
17 #include <string>
18 #include <vector>
19 
21 template<unsigned int BITS>
22 class base_blob
23 {
24 protected:
25  static constexpr int WIDTH = BITS / 8;
26  uint8_t m_data[WIDTH];
27 public:
28  /* construct 0 value by default */
29  constexpr base_blob() : m_data() {}
30 
31  /* constructor for constants between 1 and 255 */
32  constexpr explicit base_blob(uint8_t v) : m_data{v} {}
33 
34  explicit base_blob(const std::vector<unsigned char>& vch);
35 
36  bool IsNull() const
37  {
38  for (int i = 0; i < WIDTH; i++)
39  if (m_data[i] != 0)
40  return false;
41  return true;
42  }
43 
44  void SetNull()
45  {
46  memset(m_data, 0, sizeof(m_data));
47  }
48 
49  inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }
50 
51  friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
52  friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
53  friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
54 
55  std::string GetHex() const;
56  void SetHex(const char* psz);
57  void SetHex(const std::string& str);
58  std::string ToString() const;
59 
60  const unsigned char* data() const { return m_data; }
61  unsigned char* data() { return m_data; }
62 
63  unsigned char* begin()
64  {
65  return &m_data[0];
66  }
67 
68  unsigned char* end()
69  {
70  return &m_data[WIDTH];
71  }
72 
73  const unsigned char* begin() const
74  {
75  return &m_data[0];
76  }
77 
78  const unsigned char* end() const
79  {
80  return &m_data[WIDTH];
81  }
82 
83  unsigned int size() const
84  {
85  return sizeof(m_data);
86  }
87 
88  uint64_t GetUint64(int pos) const
89  {
90  const uint8_t* ptr = m_data + pos * 8;
91  return ((uint64_t)ptr[0]) | \
92  ((uint64_t)ptr[1]) << 8 | \
93  ((uint64_t)ptr[2]) << 16 | \
94  ((uint64_t)ptr[3]) << 24 | \
95  ((uint64_t)ptr[4]) << 32 | \
96  ((uint64_t)ptr[5]) << 40 | \
97  ((uint64_t)ptr[6]) << 48 | \
98  ((uint64_t)ptr[7]) << 56;
99  }
100 
101  template<typename Stream>
102  void Serialize(Stream& s) const
103  {
104  s.write((char*)m_data, sizeof(m_data));
105  }
106 
107  template<typename Stream>
108  void Unserialize(Stream& s)
109  {
110  s.read((char*)m_data, sizeof(m_data));
111  }
112 };
113 
116 class blob88 : public base_blob<88> {
117 public:
118  blob88() {}
119  explicit blob88(const base_blob<88>& b) : base_blob<88>(b) {}
120  explicit blob88(const std::vector<unsigned char>& vch) : base_blob<88>(vch) {}
121 };
122 
127 class uint160 : public base_blob<160> {
128 public:
129  uint160() {}
130  explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
131 };
132 
138 class uint256 : public base_blob<256> {
139 public:
140  uint256() {}
141  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
142 
149  uint64_t GetCheapHash() const { return ReadLE64(begin()); }
150 
151 };
152 
153 /* uint256 from const char *.
154  * This is a separate function because the constructor uint256(const char*) can result
155  * in dangerously catching UINT256_ZERO.
156  */
157 inline uint256 uint256S(const char* str)
158 {
159  uint256 rv;
160  rv.SetHex(str);
161  return rv;
162 }
163 /* uint256 from std::string.
164  * This is a separate function because the constructor uint256(const std::string &str) can result
165  * in dangerously catching UINT256_ZERO via std::string(const char*).
166  */
167 inline uint256 uint256S(const std::string& str)
168 {
169  uint256 rv;
170  rv.SetHex(str);
171  return rv;
172 }
173 
176 const uint256 UINT256_ONE = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
177 const uint256 UINT256_MAX = uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
178 
184 class uint512 : public base_blob<512> {
185 public:
186  uint512() {}
187  explicit uint512(const base_blob<512>& b) : base_blob<512>(b) {}
188  explicit uint512(const std::vector<unsigned char>& vch) : base_blob<512>(vch) {}
189 };
190 
191 /* uint512 from const char *.
192  * This is a separate function because the constructor uint512(const char*) can result
193  * in dangerously catching uint512(0).
194  */
195 inline uint512 uint512S(const char* str)
196 {
197  uint512 rv;
198  rv.SetHex(str);
199  return rv;
200 }
201 /* uint512 from std::string.
202  * This is a separate function because the constructor uint512(const std::string &str) can result
203  * in dangerously catching uint512(0) via std::string(const char*).
204  */
205 inline uint512 uint512S(const std::string& str)
206 {
207  uint512 rv;
208  rv.SetHex(str);
209  return rv;
210 }
211 
212 namespace std {
213  template <>
214  struct hash<uint256>
215  {
216  std::size_t operator()(const uint256& k) const
217  {
218  return (std::size_t)k.GetCheapHash();
219  }
220  };
221 }
222 
223 #endif // PIVX_UINT256_H
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:23
unsigned int size() const
Definition: uint256.h:83
constexpr base_blob(uint8_t v)
Definition: uint256.h:32
unsigned char * data()
Definition: uint256.h:61
static constexpr int WIDTH
Definition: uint256.h:25
void SetHex(const char *psz)
Definition: uint256.cpp:31
const unsigned char * data() const
Definition: uint256.h:60
void Unserialize(Stream &s)
Definition: uint256.h:108
int Compare(const base_blob &other) const
Definition: uint256.h:49
const unsigned char * begin() const
Definition: uint256.h:73
std::string ToString() const
Definition: uint256.cpp:65
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:52
void SetNull()
Definition: uint256.h:44
uint8_t m_data[WIDTH]
Definition: uint256.h:26
unsigned char * end()
Definition: uint256.h:68
bool IsNull() const
Definition: uint256.h:36
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:51
void Serialize(Stream &s) const
Definition: uint256.h:102
std::string GetHex() const
Definition: uint256.cpp:21
const unsigned char * end() const
Definition: uint256.h:78
unsigned char * begin()
Definition: uint256.h:63
uint64_t GetUint64(int pos) const
Definition: uint256.h:88
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:53
constexpr base_blob()
Definition: uint256.h:29
88-bit opaque blob.
Definition: uint256.h:116
blob88()
Definition: uint256.h:118
blob88(const std::vector< unsigned char > &vch)
Definition: uint256.h:120
blob88(const base_blob< 88 > &b)
Definition: uint256.h:119
160-bit opaque blob.
Definition: uint256.h:127
uint160()
Definition: uint256.h:129
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:130
256-bit opaque blob.
Definition: uint256.h:138
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:141
uint64_t GetCheapHash() const
A cheap hash function that just returns 64 bits from the result, it can be used when the contents are...
Definition: uint256.h:149
uint256()
Definition: uint256.h:140
512-bit opaque blob.
Definition: uint256.h:184
uint512(const std::vector< unsigned char > &vch)
Definition: uint256.h:188
uint512(const base_blob< 512 > &b)
Definition: uint256.h:187
uint512()
Definition: uint256.h:186
Definition: uint256.h:212
std::size_t operator()(const uint256 &k) const
Definition: uint256.h:216
const uint256 UINT256_MAX
Definition: uint256.h:177
const uint256 UINT256_ZERO
constant uint256 instances
Definition: uint256.h:175
uint256 uint256S(const char *str)
Definition: uint256.h:157
uint512 uint512S(const char *str)
Definition: uint256.h:195
const uint256 UINT256_ONE
Definition: uint256.h:176