Fabcoin Core  0.16.2
P2P Digital Currency
arith_uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_ARITH_UINT256_H
7 #define FABCOIN_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 
18 class uint_error : public std::runtime_error {
19 public:
20  explicit uint_error(const std::string& str) : std::runtime_error(str) {}
21 };
22 
24 template<unsigned int BITS>
25 class base_uint
26 {
27 protected:
28  enum { WIDTH=BITS/32 };
29  uint32_t pn[WIDTH];
30 public:
31 
33  {
34  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
35 
36  for (int i = 0; i < WIDTH; i++)
37  pn[i] = 0;
38  }
39 
41  {
42  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
43 
44  for (int i = 0; i < WIDTH; i++)
45  pn[i] = b.pn[i];
46  }
47 
49  {
50  for (int i = 0; i < WIDTH; i++)
51  pn[i] = b.pn[i];
52  return *this;
53  }
54 
55  base_uint(uint64_t b)
56  {
57  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
58 
59  pn[0] = (unsigned int)b;
60  pn[1] = (unsigned int)(b >> 32);
61  for (int i = 2; i < WIDTH; i++)
62  pn[i] = 0;
63  }
64 
65  explicit base_uint(const std::string& str);
66 
67  bool operator!() const
68  {
69  for (int i = 0; i < WIDTH; i++)
70  if (pn[i] != 0)
71  return false;
72  return true;
73  }
74 
75  const base_uint operator~() const
76  {
77  base_uint ret;
78  for (int i = 0; i < WIDTH; i++)
79  ret.pn[i] = ~pn[i];
80  return ret;
81  }
82 
83  const base_uint operator-() const
84  {
85  base_uint ret;
86  for (int i = 0; i < WIDTH; i++)
87  ret.pn[i] = ~pn[i];
88  ret++;
89  return ret;
90  }
91 
92  double getdouble() const;
93 
94  base_uint& operator=(uint64_t b)
95  {
96  pn[0] = (unsigned int)b;
97  pn[1] = (unsigned int)(b >> 32);
98  for (int i = 2; i < WIDTH; i++)
99  pn[i] = 0;
100  return *this;
101  }
102 
104  {
105  for (int i = 0; i < WIDTH; i++)
106  pn[i] ^= b.pn[i];
107  return *this;
108  }
109 
111  {
112  for (int i = 0; i < WIDTH; i++)
113  pn[i] &= b.pn[i];
114  return *this;
115  }
116 
118  {
119  for (int i = 0; i < WIDTH; i++)
120  pn[i] |= b.pn[i];
121  return *this;
122  }
123 
124  base_uint& operator^=(uint64_t b)
125  {
126  pn[0] ^= (unsigned int)b;
127  pn[1] ^= (unsigned int)(b >> 32);
128  return *this;
129  }
130 
131  base_uint& operator|=(uint64_t b)
132  {
133  pn[0] |= (unsigned int)b;
134  pn[1] |= (unsigned int)(b >> 32);
135  return *this;
136  }
137 
138  base_uint& operator<<=(unsigned int shift);
139  base_uint& operator>>=(unsigned int shift);
140 
142  {
143  uint64_t carry = 0;
144  for (int i = 0; i < WIDTH; i++)
145  {
146  uint64_t n = carry + pn[i] + b.pn[i];
147  pn[i] = n & 0xffffffff;
148  carry = n >> 32;
149  }
150  return *this;
151  }
152 
154  {
155  *this += -b;
156  return *this;
157  }
158 
159  base_uint& operator+=(uint64_t b64)
160  {
161  base_uint b;
162  b = b64;
163  *this += b;
164  return *this;
165  }
166 
167  base_uint& operator-=(uint64_t b64)
168  {
169  base_uint b;
170  b = b64;
171  *this += -b;
172  return *this;
173  }
174 
175  base_uint& operator*=(uint32_t b32);
176  base_uint& operator*=(const base_uint& b);
177  base_uint& operator/=(const base_uint& b);
178 
180  {
181  // prefix operator
182  int i = 0;
183  while (i < WIDTH && ++pn[i] == 0)
184  i++;
185  return *this;
186  }
187 
189  {
190  // postfix operator
191  const base_uint ret = *this;
192  ++(*this);
193  return ret;
194  }
195 
197  {
198  // prefix operator
199  int i = 0;
200  while (i < WIDTH && --pn[i] == (uint32_t)-1)
201  i++;
202  return *this;
203  }
204 
206  {
207  // postfix operator
208  const base_uint ret = *this;
209  --(*this);
210  return ret;
211  }
212 
213  int CompareTo(const base_uint& b) const;
214  bool EqualTo(uint64_t b) const;
215 
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, 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, int shift) { return base_uint(a) >>= shift; }
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, uint32_t b) { return base_uint(a) *= b; }
226  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
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 a.CompareTo(b) > 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, uint64_t b) { return a.EqualTo(b); }
233  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
234 
235  std::string GetHex() const;
236  void SetHex(const char* psz);
237  void SetHex(const std::string& str);
238  std::string GetReverseHex() const;
239  void SetReverseHex(const char* psz);
240  void SetReverseHex(const std::string& str);
241  std::string ToString() const;
242 
243  unsigned int size() const
244  {
245  return sizeof(pn);
246  }
247 
252  unsigned int bits() const;
253 
254  uint64_t GetLow64() const
255  {
256  assert(WIDTH >= 2);
257  return pn[0] | (uint64_t)pn[1] << 32;
258  }
259 };
260 
262 class arith_uint256 : public base_uint<256> {
263 public:
266  arith_uint256(uint64_t b) : base_uint<256>(b) {}
267  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
268 
289  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
290  uint32_t GetCompact(bool fNegative = false) const;
291 
292  friend uint256 ArithToUint256(const arith_uint256 &);
293  friend arith_uint256 UintToArith256(const uint256 &);
294 };
295 
298 
299 #endif // FABCOIN_ARITH_UINT256_H
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:48
friend const base_uint operator^(const base_uint &a, const base_uint &b)
arith_uint256(const std::string &str)
friend const base_uint operator*(const base_uint &a, uint32_t b)
base_uint & operator|=(uint64_t b)
const base_uint operator-() const
Definition: arith_uint256.h:83
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)
bool operator!() const
Definition: arith_uint256.h:67
friend bool operator>(const base_uint &a, const base_uint &b)
Template base class for unsigned big integers.
Definition: arith_uint256.h:25
std::hash for asio::adress
Definition: Common.h:323
friend const base_uint operator-(const base_uint &a, const base_uint &b)
assert(len-trim+(2 *lenIndices)<=WIDTH)
base_uint & operator+=(uint64_t b64)
friend const base_uint operator/(const base_uint &a, const base_uint &b)
base_uint & operator--()
base_uint & operator&=(const base_uint &b)
#define a(i)
uint32_t pn[WIDTH]
Definition: arith_uint256.h:29
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:55
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:40
friend const base_uint operator*(const base_uint &a, const base_uint &b)
uint_error(const std::string &str)
Definition: arith_uint256.h:20
bool EqualTo(uint64_t b) const
base_uint & operator^=(uint64_t b)
friend const base_uint operator&(const base_uint &a, const base_uint &b)
256-bit unsigned big integer.
#define b(i, j)
int CompareTo(const base_uint &b) const
256-bit opaque blob.
Definition: uint256.h:132
base_uint & operator++()
friend bool operator!=(const base_uint &a, const base_uint &b)
friend const base_uint operator|(const base_uint &a, const base_uint &b)
arith_uint256(uint64_t b)
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:94
const base_uint operator--(int)
const base_uint operator~() const
Definition: arith_uint256.h:75
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
uint64_t GetLow64() 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)