BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
arith_uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 BITCOIN_ARITH_UINT256_H
7 #define BITCOIN_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  static constexpr int 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 
40  base_uint(const base_uint& b)
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  const base_uint operator~() const
68  {
69  base_uint ret;
70  for (int i = 0; i < WIDTH; i++)
71  ret.pn[i] = ~pn[i];
72  return ret;
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  ++ret;
81  return ret;
82  }
83 
84  double getdouble() const;
85 
86  base_uint& operator=(uint64_t b)
87  {
88  pn[0] = (unsigned int)b;
89  pn[1] = (unsigned int)(b >> 32);
90  for (int i = 2; i < WIDTH; i++)
91  pn[i] = 0;
92  return *this;
93  }
94 
96  {
97  for (int i = 0; i < WIDTH; i++)
98  pn[i] ^= b.pn[i];
99  return *this;
100  }
101 
103  {
104  for (int i = 0; i < WIDTH; i++)
105  pn[i] &= b.pn[i];
106  return *this;
107  }
108 
110  {
111  for (int i = 0; i < WIDTH; i++)
112  pn[i] |= b.pn[i];
113  return *this;
114  }
115 
116  base_uint& operator^=(uint64_t b)
117  {
118  pn[0] ^= (unsigned int)b;
119  pn[1] ^= (unsigned int)(b >> 32);
120  return *this;
121  }
122 
123  base_uint& operator|=(uint64_t b)
124  {
125  pn[0] |= (unsigned int)b;
126  pn[1] |= (unsigned int)(b >> 32);
127  return *this;
128  }
129 
130  base_uint& operator<<=(unsigned int shift);
131  base_uint& operator>>=(unsigned int shift);
132 
134  {
135  uint64_t carry = 0;
136  for (int i = 0; i < WIDTH; i++)
137  {
138  uint64_t n = carry + pn[i] + b.pn[i];
139  pn[i] = n & 0xffffffff;
140  carry = n >> 32;
141  }
142  return *this;
143  }
144 
146  {
147  *this += -b;
148  return *this;
149  }
150 
151  base_uint& operator+=(uint64_t b64)
152  {
153  base_uint b;
154  b = b64;
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*=(uint32_t b32);
168  base_uint& operator*=(const base_uint& b);
169  base_uint& operator/=(const base_uint& b);
170 
172  {
173  // prefix operator
174  int i = 0;
175  while (i < WIDTH && ++pn[i] == 0)
176  i++;
177  return *this;
178  }
179 
181  {
182  // postfix operator
183  const base_uint ret = *this;
184  ++(*this);
185  return ret;
186  }
187 
189  {
190  // prefix operator
191  int i = 0;
192  while (i < WIDTH && --pn[i] == (uint32_t)-1)
193  i++;
194  return *this;
195  }
196 
198  {
199  // postfix operator
200  const base_uint ret = *this;
201  --(*this);
202  return ret;
203  }
204 
205  int CompareTo(const base_uint& b) const;
206  bool EqualTo(uint64_t b) const;
207 
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, const base_uint& b) { return base_uint(a) /= b; }
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, int shift) { return base_uint(a) >>= shift; }
216  friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
217  friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
218  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
219  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
220  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
221  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
222  friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
223  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
224  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
225  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
226 
227  std::string GetHex() const;
228  void SetHex(const char* psz);
229  void SetHex(const std::string& str);
230  std::string ToString() const;
231 
232  unsigned int size() const
233  {
234  return sizeof(pn);
235  }
236 
241  unsigned int bits() const;
242 
243  uint64_t GetLow64() const
244  {
245  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
246  return pn[0] | (uint64_t)pn[1] << 32;
247  }
248 };
249 
251 class arith_uint256 : public base_uint<256> {
252 public:
254  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
255  arith_uint256(uint64_t b) : base_uint<256>(b) {}
256  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
257 
278  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
279  uint32_t GetCompact(bool fNegative = false) const;
280 
281  friend uint256 ArithToUint256(const arith_uint256 &);
282  friend arith_uint256 UintToArith256(const uint256 &);
283 };
284 
287 
288 #endif // BITCOIN_ARITH_UINT256_H
bool EqualTo(uint64_t b) const
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:48
void SetHex(const char *psz)
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)
std::string ToString() const
const base_uint operator~() const
Definition: arith_uint256.h:67
friend const base_uint operator*(const base_uint &a, uint32_t b)
base_uint & operator|=(uint64_t b)
base_uint & operator/=(const base_uint &b)
friend const base_uint operator &(const base_uint &a, const base_uint &b)
static constexpr int WIDTH
Definition: arith_uint256.h:28
UniValue ret(UniValue::VARR)
Definition: rpcwallet.cpp:1140
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:25
friend const base_uint operator-(const base_uint &a, const base_uint &b)
base_uint & operator<<=(unsigned int shift)
base_uint & operator+=(uint64_t b64)
friend const base_uint operator/(const base_uint &a, const base_uint &b)
uint32_t GetCompact(bool fNegative=false) const
base_uint & operator--()
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)
int CompareTo(const base_uint &b) const
uint_error(const std::string &str)
Definition: arith_uint256.h:20
base_uint & operator^=(uint64_t b)
friend uint256 ArithToUint256(const arith_uint256 &)
256-bit unsigned big integer.
256-bit opaque blob.
Definition: uint256.h:122
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:75
arith_uint256(uint64_t b)
base_uint & operator*=(uint32_t b32)
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:86
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...
const base_uint operator--(int)
std::string GetHex() const
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
double getdouble() const
friend bool operator==(const base_uint &a, uint64_t b)
friend const base_uint operator>>(const base_uint &a, int shift)
friend arith_uint256 UintToArith256(const uint256 &)
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:95
base_uint & operator>>=(unsigned int shift)
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.