PIVX Core  5.6.99
P2P Digital Currency
arith_uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin 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 PIVX_ARITH_UINT256_H
7 #define PIVX_ARITH_UINT256_H
8 
9 #include <assert.h>
10 #include <cstring>
11 #include <stdexcept>
12 #include <stdint.h>
13 #include <string>
14 #include <vector>
15 
16 class uint256;
17 class uint512;
18 
19 class uint_error : public std::runtime_error {
20 public:
21  explicit uint_error(const std::string& str) : std::runtime_error(str) {}
22 };
23 
25 template<unsigned int BITS>
26 class base_uint
27 {
28 protected:
29  static constexpr int WIDTH = BITS / 32;
30  uint32_t pn[WIDTH];
31 
32 public:
34  {
35  for (int i = 0; i < WIDTH; i++)
36  pn[i] = 0;
37  }
38 
39  base_uint(const base_uint& b)
40  {
41  for (int i = 0; i < WIDTH; i++)
42  pn[i] = b.pn[i];
43  }
44 
46  {
47  for (int i = 0; i < WIDTH; i++)
48  pn[i] = b.pn[i];
49  return *this;
50  }
51 
52  base_uint(uint64_t b)
53  {
54  pn[0] = (unsigned int)b;
55  pn[1] = (unsigned int)(b >> 32);
56  for (int i = 2; i < WIDTH; i++)
57  pn[i] = 0;
58  }
59 
60  explicit base_uint(const std::string& str);
61  explicit base_uint(const std::vector<unsigned char>& vch);
62 
63  bool operator!() const
64  {
65  for (int i = 0; i < WIDTH; i++)
66  if (pn[i] != 0)
67  return false;
68  return true;
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  return ret;
77  }
78 
79  const base_uint operator-() const
80  {
81  base_uint ret;
82  for (int i = 0; i < WIDTH; i++)
83  ret.pn[i] = ~pn[i];
84  ret++;
85  return ret;
86  }
87 
88  double getdouble() const;
89 
90  base_uint& operator=(uint64_t b)
91  {
92  pn[0] = (unsigned int)b;
93  pn[1] = (unsigned int)(b >> 32);
94  for (int i = 2; i < WIDTH; i++)
95  pn[i] = 0;
96  return *this;
97  }
98 
100  {
101  for (int i = 0; i < WIDTH; i++)
102  pn[i] ^= b.pn[i];
103  return *this;
104  }
105 
107  {
108  for (int i = 0; i < WIDTH; i++)
109  pn[i] &= b.pn[i];
110  return *this;
111  }
112 
114  {
115  for (int i = 0; i < WIDTH; i++)
116  pn[i] |= b.pn[i];
117  return *this;
118  }
119 
120  base_uint& operator^=(uint64_t b)
121  {
122  pn[0] ^= (unsigned int)b;
123  pn[1] ^= (unsigned int)(b >> 32);
124  return *this;
125  }
126 
127  base_uint& operator|=(uint64_t b)
128  {
129  pn[0] |= (unsigned int)b;
130  pn[1] |= (unsigned int)(b >> 32);
131  return *this;
132  }
133 
134  base_uint& operator<<=(unsigned int shift);
135  base_uint& operator>>=(unsigned int shift);
136 
138  {
139  uint64_t carry = 0;
140  for (int i = 0; i < WIDTH; i++)
141  {
142  uint64_t n = carry + pn[i] + b.pn[i];
143  pn[i] = n & 0xffffffff;
144  carry = n >> 32;
145  }
146  return *this;
147  }
148 
150  {
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-=(uint64_t b64)
164  {
165  base_uint b;
166  b = b64;
167  *this += -b;
168  return *this;
169  }
170 
171  base_uint& operator*=(uint32_t b32);
172  base_uint& operator*=(const base_uint& b);
173  base_uint& operator/=(const base_uint& b);
174 
176  {
177  // prefix operator
178  int i = 0;
179  while (++pn[i] == 0 && i < WIDTH-1)
180  i++;
181  return *this;
182  }
183 
185  {
186  // postfix operator
187  const base_uint ret = *this;
188  ++(*this);
189  return ret;
190  }
191 
193  {
194  // prefix operator
195  int i = 0;
196  while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
197  i++;
198  return *this;
199  }
200 
202  {
203  // postfix operator
204  const base_uint ret = *this;
205  --(*this);
206  return ret;
207  }
208 
209  int CompareTo(const base_uint& b) const;
210  bool EqualTo(uint64_t b) const;
211 
212  friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
213  friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
214  friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
215  friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
216  friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
217  friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
218  friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
219  friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
220  friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
221  friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
222  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
223  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
224  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
225  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
226  friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
227  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
228  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
229  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
230 
231  std::string GetHex() const;
232  void SetHex(const char* psz);
233  void SetHex(const std::string& str);
234  std::string ToString() const;
235  std::string ToStringReverseEndian() const;
236 
237  unsigned char* begin()
238  {
239  return (unsigned char*)&pn[0];
240  }
241 
242  unsigned char* end()
243  {
244  return (unsigned char*)&pn[WIDTH];
245  }
246 
247  const unsigned char* begin() const
248  {
249  return (unsigned char*)&pn[0];
250  }
251 
252  const unsigned char* end() const
253  {
254  return (unsigned char*)&pn[WIDTH];
255  }
256 
257  unsigned int size() const
258  {
259  return sizeof(pn);
260  }
261 
262  uint64_t Get64(int n = 0) const
263  {
264  return pn[2 * n] | (uint64_t)pn[2 * n + 1] << 32;
265  }
266 
267  uint32_t Get32(int n = 0) const
268  {
269  return pn[2 * n];
270  }
275  unsigned int bits() const;
276 
277  uint64_t GetLow64() const
278  {
279  assert(WIDTH >= 2);
280  return pn[0] | (uint64_t)pn[1] << 32;
281  }
282 
283  template<typename Stream>
284  void Serialize(Stream& s) const
285  {
286  s.write((char*)pn, sizeof(pn));
287  }
288 
289  template<typename Stream>
290  void Unserialize(Stream& s)
291  {
292  s.read((char*)pn, sizeof(pn));
293  }
294 
295  void SetNull()
296  {
297  memset(pn, 0, sizeof(pn));
298  }
299 
300  bool IsNull() const
301  {
302  for (int i = 0; i < WIDTH; i++)
303  if (pn[i] != 0)
304  return false;
305  return true;
306  }
307 
308  friend class uint160;
309  friend class uint256;
310  friend class uint512;
311 
312  friend class arith_uint160;
313  friend class arith_uint256;
314  friend class arith_uint512;
315 };
316 
318 class arith_uint160 : public base_uint<160> {
319 public:
321  arith_uint160(const base_uint<160>& b) : base_uint<160>(b) {}
322  arith_uint160(uint64_t b) : base_uint<160>(b) {}
323  explicit arith_uint160(const std::string& str) : base_uint<160>(str) {}
324  explicit arith_uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
325 };
326 
328 class arith_uint256 : public base_uint<256> {
329 public:
331  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
332  arith_uint256(uint64_t b) : base_uint<256>(b) {}
333  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
334  explicit arith_uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
335 
356  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
357  uint32_t GetCompact(bool fNegative = false) const;
358  uint32_t Get32(int n = 0) const { return pn[2 * n]; }
359 
360  friend arith_uint256 UintToArith256(const uint256 &a);
361  friend uint256 ArithToUint256(const arith_uint256 &a);
362 };
363 
365 class arith_uint512 : public base_uint<512> {
366 public:
368  arith_uint512(const base_uint<512>& b) : base_uint<512>(b) {}
369  arith_uint512(uint64_t b) : base_uint<512>(b) {}
370  explicit arith_uint512(const std::string& str) : base_uint<512>(str) {}
371  explicit arith_uint512(const std::vector<unsigned char>& vch) : base_uint<512>(vch) {}
372 
373  uint256 trim256() const;
374 
375  friend arith_uint512 UintToArith512(const uint512 &a);
376  friend uint512 ArithToUint512(const arith_uint512 &a);
377 
378 };
379 
384 
387 
388 #endif // PIVX_ARITH_UINT256_H
arith_uint512 UintToArith512(const uint512 &)
uint512 ArithToUint512(const arith_uint512 &)
const arith_uint256 ARITH_UINT256_ONE
arith_uint256 UintToArith256(const uint256 &)
uint256 ArithToUint256(const arith_uint256 &)
const arith_uint256 ARITH_UINT256_ZERO
160-bit unsigned big integer.
arith_uint160(const base_uint< 160 > &b)
arith_uint160(const std::vector< unsigned char > &vch)
arith_uint160(uint64_t b)
arith_uint160(const std::string &str)
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
arith_uint256(const std::string &str)
uint32_t GetCompact(bool fNegative=false) const
friend arith_uint256 UintToArith256(const uint256 &a)
arith_uint256(uint64_t b)
arith_uint256(const base_uint< 256 > &b)
uint32_t Get32(int n=0) const
arith_uint256(const std::vector< unsigned char > &vch)
friend uint256 ArithToUint256(const arith_uint256 &a)
512-bit unsigned big integer.
arith_uint512(const std::vector< unsigned char > &vch)
friend arith_uint512 UintToArith512(const uint512 &a)
uint256 trim256() const
friend uint512 ArithToUint512(const arith_uint512 &a)
arith_uint512(uint64_t b)
arith_uint512(const std::string &str)
arith_uint512(const base_uint< 512 > &b)
Template base class for unsigned big integers.
Definition: arith_uint256.h:27
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:45
uint32_t pn[WIDTH]
Definition: arith_uint256.h:30
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:90
int CompareTo(const base_uint &b) const
base_uint & operator+=(uint64_t b64)
unsigned int size() const
const unsigned char * end() const
base_uint(uint64_t b)
Definition: arith_uint256.h:52
const base_uint operator~() const
Definition: arith_uint256.h:71
base_uint & operator--()
const base_uint operator++(int)
friend const base_uint operator/(const base_uint &a, const base_uint &b)
friend const base_uint operator*(const base_uint &a, uint32_t b)
const base_uint operator-() const
Definition: arith_uint256.h:79
friend bool operator!=(const base_uint &a, const base_uint &b)
base_uint & operator^=(uint64_t b)
const unsigned char * begin() const
friend const base_uint operator-(const base_uint &a, const base_uint &b)
base_uint & operator>>=(unsigned int shift)
base_uint & operator++()
bool IsNull() const
base_uint(const base_uint &b)
Definition: arith_uint256.h:39
static constexpr int WIDTH
Definition: arith_uint256.h:29
base_uint & operator&=(const base_uint &b)
unsigned char * end()
const base_uint operator--(int)
friend const base_uint operator*(const base_uint &a, const base_uint &b)
friend const base_uint operator&(const base_uint &a, const base_uint &b)
friend bool operator<(const base_uint &a, const base_uint &b)
base_uint & operator-=(const base_uint &b)
base_uint & operator+=(const base_uint &b)
std::string ToStringReverseEndian() const
friend bool operator==(const base_uint &a, uint64_t b)
friend const base_uint operator>>(const base_uint &a, int shift)
friend bool operator>=(const base_uint &a, const base_uint &b)
base_uint & operator*=(uint32_t b32)
friend const base_uint operator^(const base_uint &a, const base_uint &b)
unsigned char * begin()
bool EqualTo(uint64_t b) const
uint64_t Get64(int n=0) const
friend bool operator==(const base_uint &a, const base_uint &b)
base_uint & operator|=(const base_uint &b)
friend const base_uint operator+(const base_uint &a, const base_uint &b)
base_uint & operator-=(uint64_t b64)
friend bool operator!=(const base_uint &a, uint64_t b)
void SetNull()
uint32_t Get32(int n=0) const
friend bool operator>(const base_uint &a, const base_uint &b)
friend bool operator<=(const base_uint &a, const base_uint &b)
void Serialize(Stream &s) const
base_uint & operator|=(uint64_t b)
double getdouble() const
base_uint & operator<<=(unsigned int shift)
std::string ToString() const
friend const base_uint operator<<(const base_uint &a, int shift)
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:99
base_uint & operator/=(const base_uint &b)
uint64_t GetLow64() const
void SetHex(const char *psz)
std::string GetHex() const
void Unserialize(Stream &s)
friend const base_uint operator|(const base_uint &a, const base_uint &b)
bool operator!() const
Definition: arith_uint256.h:63
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
160-bit opaque blob.
Definition: uint256.h:127
256-bit opaque blob.
Definition: uint256.h:138
512-bit opaque blob.
Definition: uint256.h:184
uint_error(const std::string &str)
Definition: arith_uint256.h:21
Definition: uint256.h:212