Raven Core  3.0.0
P2P Digital Currency
arith_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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_ARITH_UINT256_H
8 #define RAVEN_ARITH_UINT256_H
9 
10 #include <assert.h>
11 #include <cstring>
12 #include <stdexcept>
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
17 class uint256;
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  enum { WIDTH=BITS/32 };
30  uint32_t pn[WIDTH];
31 public:
32 
34  {
35  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
36 
37  for (int i = 0; i < WIDTH; i++)
38  pn[i] = 0;
39  }
40 
41  base_uint(const base_uint& b)
42  {
43  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
44 
45  for (int i = 0; i < WIDTH; i++)
46  pn[i] = b.pn[i];
47  }
48 
50  {
51  for (int i = 0; i < WIDTH; i++)
52  pn[i] = b.pn[i];
53  return *this;
54  }
55 
56  base_uint(uint64_t b)
57  {
58  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
59 
60  pn[0] = (unsigned int)b;
61  pn[1] = (unsigned int)(b >> 32);
62  for (int i = 2; i < WIDTH; i++)
63  pn[i] = 0;
64  }
65 
66  explicit base_uint(const std::string& str);
67 
68  bool operator!() const
69  {
70  for (int i = 0; i < WIDTH; i++)
71  if (pn[i] != 0)
72  return false;
73  return true;
74  }
75 
76  const base_uint operator~() const
77  {
78  base_uint ret;
79  for (int i = 0; i < WIDTH; i++)
80  ret.pn[i] = ~pn[i];
81  return ret;
82  }
83 
84  const base_uint operator-() const
85  {
86  base_uint ret;
87  for (int i = 0; i < WIDTH; i++)
88  ret.pn[i] = ~pn[i];
89  ret++;
90  return ret;
91  }
92 
93  double getdouble() const;
94 
95  base_uint& operator=(uint64_t b)
96  {
97  pn[0] = (unsigned int)b;
98  pn[1] = (unsigned int)(b >> 32);
99  for (int i = 2; i < WIDTH; i++)
100  pn[i] = 0;
101  return *this;
102  }
103 
105  {
106  for (int i = 0; i < WIDTH; i++)
107  pn[i] ^= b.pn[i];
108  return *this;
109  }
110 
112  {
113  for (int i = 0; i < WIDTH; i++)
114  pn[i] &= b.pn[i];
115  return *this;
116  }
117 
119  {
120  for (int i = 0; i < WIDTH; i++)
121  pn[i] |= b.pn[i];
122  return *this;
123  }
124 
125  base_uint& operator^=(uint64_t b)
126  {
127  pn[0] ^= (unsigned int)b;
128  pn[1] ^= (unsigned int)(b >> 32);
129  return *this;
130  }
131 
132  base_uint& operator|=(uint64_t b)
133  {
134  pn[0] |= (unsigned int)b;
135  pn[1] |= (unsigned int)(b >> 32);
136  return *this;
137  }
138 
139  base_uint& operator<<=(unsigned int shift);
140  base_uint& operator>>=(unsigned int shift);
141 
143  {
144  uint64_t carry = 0;
145  for (int i = 0; i < WIDTH; i++)
146  {
147  uint64_t n = carry + pn[i] + b.pn[i];
148  pn[i] = n & 0xffffffff;
149  carry = n >> 32;
150  }
151  return *this;
152  }
153 
155  {
156  *this += -b;
157  return *this;
158  }
159 
160  base_uint& operator+=(uint64_t b64)
161  {
162  base_uint b;
163  b = b64;
164  *this += b;
165  return *this;
166  }
167 
168  base_uint& operator-=(uint64_t b64)
169  {
170  base_uint b;
171  b = b64;
172  *this += -b;
173  return *this;
174  }
175 
176  base_uint& operator*=(uint32_t b32);
177  base_uint& operator*=(const base_uint& b);
178  base_uint& operator/=(const base_uint& b);
179 
181  {
182  // prefix operator
183  int i = 0;
184  while (i < WIDTH && ++pn[i] == 0)
185  i++;
186  return *this;
187  }
188 
190  {
191  // postfix operator
192  const base_uint ret = *this;
193  ++(*this);
194  return ret;
195  }
196 
198  {
199  // prefix operator
200  int i = 0;
201  while (i < WIDTH && --pn[i] == (uint32_t)-1)
202  i++;
203  return *this;
204  }
205 
207  {
208  // postfix operator
209  const base_uint ret = *this;
210  --(*this);
211  return ret;
212  }
213 
214  int CompareTo(const base_uint& b) const;
215  bool EqualTo(uint64_t b) const;
216 
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, const base_uint& b) { return base_uint(a) *= b; }
220  friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
221  friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
222  friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
223  friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
224  friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
225  friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
226  friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
227  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
228  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
229  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
230  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
231  friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
232  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
233  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
234  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
235 
236  std::string GetHex() const;
237  void SetHex(const char* psz);
238  void SetHex(const std::string& str);
239  std::string ToString() const;
240 
241  unsigned int size() const
242  {
243  return sizeof(pn);
244  }
245 
250  unsigned int bits() const;
251 
252  uint64_t GetLow64() const
253  {
254  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
255  return pn[0] | (uint64_t)pn[1] << 32;
256  }
257 };
258 
260 class arith_uint256 : public base_uint<256> {
261 public:
263  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
264  arith_uint256(uint64_t b) : base_uint<256>(b) {}
265  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
266 
287  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
288  uint32_t GetCompact(bool fNegative = false) const;
289 
290  friend uint256 ArithToUint256(const arith_uint256 &);
291  friend arith_uint256 UintToArith256(const uint256 &);
292 };
293 
296 
297 #endif // RAVEN_ARITH_UINT256_H
bool EqualTo(uint64_t b) const
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:49
friend const base_uint operator^(const base_uint &a, const base_uint &b)
base_uint & operator &=(const base_uint &b)
arith_uint256(const std::string &str)
const base_uint operator~() const
Definition: arith_uint256.h:76
friend const base_uint operator*(const base_uint &a, uint32_t b)
base_uint & operator|=(uint64_t b)
friend const base_uint operator &(const base_uint &a, const base_uint &b)
friend bool operator!=(const base_uint &a, uint64_t b)
base_uint & operator+=(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-=(uint64_t b64)
friend bool operator>(const base_uint &a, const base_uint &b)
Template base class for unsigned big integers.
Definition: arith_uint256.h:26
friend const base_uint operator-(const base_uint &a, const base_uint &b)
base_uint & operator+=(uint64_t b64)
friend const base_uint operator/(const base_uint &a, const base_uint &b)
base_uint & operator--()
uint32_t pn[WIDTH]
Definition: arith_uint256.h:30
base_uint & operator-=(const base_uint &b)
friend bool operator==(const base_uint &a, const base_uint &b)
friend const base_uint operator+(const base_uint &a, const base_uint &b)
base_uint(uint64_t b)
Definition: arith_uint256.h:56
friend bool operator<(const base_uint &a, const base_uint &b)
uint256 ArithToUint256(const arith_uint256 &)
base_uint(const base_uint &b)
Definition: arith_uint256.h:41
friend const base_uint operator*(const base_uint &a, const base_uint &b)
int CompareTo(const base_uint &b) const
uint_error(const std::string &str)
Definition: arith_uint256.h:21
base_uint & operator^=(uint64_t b)
256-bit unsigned big integer.
bool operator!() const
Definition: arith_uint256.h:68
256-bit opaque blob.
Definition: uint256.h:123
base_uint & operator++()
friend bool operator!=(const base_uint &a, const base_uint &b)
uint64_t GetLow64() const
friend const base_uint operator|(const base_uint &a, const base_uint &b)
const base_uint operator-() const
Definition: arith_uint256.h:84
arith_uint256(uint64_t b)
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:95
const base_uint operator--(int)
const base_uint operator++(int)
friend const base_uint operator<<(const base_uint &a, int shift)
friend bool operator>=(const base_uint &a, const base_uint &b)
arith_uint256(const base_uint< 256 > &b)
arith_uint256 UintToArith256(const uint256 &)
unsigned int size() const
friend bool operator==(const base_uint &a, uint64_t b)
friend const base_uint operator>>(const base_uint &a, int shift)
base_uint & operator^=(const base_uint &b)