hdac SDK
SDK for hdac blockchain development
uint256.h
1 #ifndef UINT256_H
2 #define UINT256_H
3 
4 #include <stdint.h>
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8 #include <cassert>
9 #include <cstring>
10 
11 class uint_error : public std::runtime_error {
12 public:
13  explicit uint_error(const std::string& str) : std::runtime_error(str) {}
14 };
15 
17 template<unsigned int BITS>
18 class base_uint
19 {
20 protected:
21  enum { WIDTH=BITS/32 };
22  uint32_t pn[WIDTH];
23 public:
24 
25  base_uint()
26  {
27  for (int i = 0; i < WIDTH; i++)
28  pn[i] = 0;
29  }
30 
31  base_uint(const base_uint& b)
32  {
33  for (int i = 0; i < WIDTH; i++)
34  pn[i] = b.pn[i];
35  }
36 
37  base_uint& operator=(const base_uint& b)
38  {
39  for (int i = 0; i < WIDTH; i++)
40  pn[i] = b.pn[i];
41  return *this;
42  }
43 
44  base_uint(uint64_t b)
45  {
46  pn[0] = (unsigned int)b;
47  pn[1] = (unsigned int)(b >> 32);
48  for (int i = 2; i < WIDTH; i++)
49  pn[i] = 0;
50  }
51 
52  explicit base_uint(const std::string& str);
53  explicit base_uint(const std::vector<unsigned char>& vch);
54 
55  bool operator!() const
56  {
57  for (int i = 0; i < WIDTH; i++)
58  if (pn[i] != 0)
59  return false;
60  return true;
61  }
62 
63  const base_uint operator~() const
64  {
65  base_uint ret;
66  for (int i = 0; i < WIDTH; i++)
67  ret.pn[i] = ~pn[i];
68  return ret;
69  }
70 
71  const base_uint operator-() const
72  {
73  base_uint ret;
74  for (int i = 0; i < WIDTH; i++)
75  ret.pn[i] = ~pn[i];
76  ret++;
77  return ret;
78  }
79 
80  double getdouble() const;
81 
82  base_uint& operator=(uint64_t b)
83  {
84  pn[0] = (unsigned int)b;
85  pn[1] = (unsigned int)(b >> 32);
86  for (int i = 2; i < WIDTH; i++)
87  pn[i] = 0;
88  return *this;
89  }
90 
91  base_uint& operator^=(const base_uint& b)
92  {
93  for (int i = 0; i < WIDTH; i++)
94  pn[i] ^= b.pn[i];
95  return *this;
96  }
97 
98  base_uint& operator&=(const base_uint& b)
99  {
100  for (int i = 0; i < WIDTH; i++)
101  pn[i] &= b.pn[i];
102  return *this;
103  }
104 
105  base_uint& operator|=(const base_uint& b)
106  {
107  for (int i = 0; i < WIDTH; i++)
108  pn[i] |= b.pn[i];
109  return *this;
110  }
111 
112  base_uint& operator^=(uint64_t b)
113  {
114  pn[0] ^= (unsigned int)b;
115  pn[1] ^= (unsigned int)(b >> 32);
116  return *this;
117  }
118 
119  base_uint& operator|=(uint64_t b)
120  {
121  pn[0] |= (unsigned int)b;
122  pn[1] |= (unsigned int)(b >> 32);
123  return *this;
124  }
125 
126  base_uint& operator<<=(unsigned int shift);
127  base_uint& operator>>=(unsigned int shift);
128 
129  base_uint& operator+=(const base_uint& b)
130  {
131  uint64_t carry = 0;
132  for (int i = 0; i < WIDTH; i++)
133  {
134  uint64_t n = carry + pn[i] + b.pn[i];
135  pn[i] = n & 0xffffffff;
136  carry = n >> 32;
137  }
138  return *this;
139  }
140 
141  base_uint& operator-=(const base_uint& b)
142  {
143  *this += -b;
144  return *this;
145  }
146 
147  base_uint& operator+=(uint64_t b64)
148  {
149  base_uint b;
150  b = b64;
151  *this += b;
152  return *this;
153  }
154 
155  base_uint& operator-=(uint64_t b64)
156  {
157  base_uint b;
158  b = b64;
159  *this += -b;
160  return *this;
161  }
162 
163  base_uint& operator*=(uint32_t b32);
164  base_uint& operator*=(const base_uint& b);
165  base_uint& operator/=(const base_uint& b);
166 
167  base_uint& operator++()
168  {
169  // prefix operator
170  int i = 0;
171  while (++pn[i] == 0 && i < WIDTH-1)
172  i++;
173  return *this;
174  }
175 
176  const base_uint operator++(int)
177  {
178  // postfix operator
179  const base_uint ret = *this;
180  ++(*this);
181  return ret;
182  }
183 
184  base_uint& operator--()
185  {
186  // prefix operator
187  int i = 0;
188  while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
189  i++;
190  return *this;
191  }
192 
193  const base_uint operator--(int)
194  {
195  // postfix operator
196  const base_uint ret = *this;
197  --(*this);
198  return ret;
199  }
200 
201  int CompareTo(const base_uint& b) const;
202  bool EqualTo(uint64_t b) const;
203 
204  friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
205  friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
206  friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
207  friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
208  friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
209  friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
210  friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
211  friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
212  friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
213  friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
214  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
215  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
216  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
217  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
218  friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
219  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
220  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
221  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
222 
223  std::string GetHex() const;
224  void SetHex(const char* psz);
225  void SetHex(const std::string& str);
226  std::string ToString() const;
227 
228  unsigned char* begin()
229  {
230  return (unsigned char*)&pn[0];
231  }
232 
233  unsigned char* end()
234  {
235  return (unsigned char*)&pn[WIDTH];
236  }
237 
238  const unsigned char* begin() const
239  {
240  return (unsigned char*)&pn[0];
241  }
242 
243  const unsigned char* end() const
244  {
245  return (unsigned char*)&pn[WIDTH];
246  }
247 
248  unsigned int size() const
249  {
250  return sizeof(pn);
251  }
252 
257  unsigned int bits() const;
258 
259  uint64_t GetLow64() const
260  {
261  assert(WIDTH >= 2);
262  return pn[0] | (uint64_t)pn[1] << 32;
263  }
264 
265  unsigned int GetSerializeSize(int nType, int nVersion) const
266  {
267  return sizeof(pn);
268  }
269 
270  template<typename Stream>
271  void Serialize(Stream& s, int nType, int nVersion) const
272  {
273  s.write((char*)pn, sizeof(pn));
274  }
275 
276  template<typename Stream>
277  void Unserialize(Stream& s, int nType, int nVersion)
278  {
279  s.read((char*)pn, sizeof(pn));
280  }
281 };
282 
284 class uint160 : public base_uint<160> {
285 public:
286  uint160() {}
287  uint160(const base_uint<160>& b) : base_uint<160>(b) {}
288  uint160(uint64_t b) : base_uint<160>(b) {}
289  explicit uint160(const std::string& str) : base_uint<160>(str) {}
290  explicit uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
291 };
292 
294 class uint256 : public base_uint<256> {
295 public:
296  uint256() {}
297  uint256(const base_uint<256>& b) : base_uint<256>(b) {}
298  uint256(uint64_t b) : base_uint<256>(b) {}
299  explicit uint256(const std::string& str) : base_uint<256>(str) {}
300  explicit uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
301 
322  uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
323  uint32_t GetCompact(bool fNegative = false) const;
324 
325  uint64_t GetHash(const uint256& salt) const;
326 };
327 
328 #endif // UINT256_H
Definition: uint256.h:18
Definition: uint256.h:11
Definition: uint256.h:294
Definition: uint256.h:284