Bitcoin ABC  0.29.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-2016 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 <cstdint>
10 #include <cstring>
11 #include <limits>
12 #include <stdexcept>
13 #include <string>
14 
15 class uint256;
16 
17 class uint_error : public std::runtime_error {
18 public:
19  explicit uint_error(const std::string &str) : std::runtime_error(str) {}
20 };
21 
23 template <unsigned int BITS> class base_uint {
24 protected:
25  static constexpr int WIDTH = BITS / 32;
26  uint32_t pn[WIDTH];
27 
28 public:
30  static_assert(
31  BITS / 32 > 0 && BITS % 32 == 0,
32  "Template parameter BITS must be a positive multiple of 32.");
33 
34  for (int i = 0; i < WIDTH; i++) {
35  pn[i] = 0;
36  }
37  }
38 
39  base_uint(const base_uint &b) {
40  static_assert(
41  BITS / 32 > 0 && BITS % 32 == 0,
42  "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  }
48 
50  for (int i = 0; i < WIDTH; i++) {
51  pn[i] = b.pn[i];
52  }
53  return *this;
54  }
55 
56  base_uint(uint64_t b) {
57  static_assert(
58  BITS / 32 > 0 && BITS % 32 == 0,
59  "Template parameter BITS must be a positive multiple of 32.");
60 
61  pn[0] = (unsigned int)b;
62  pn[1] = (unsigned int)(b >> 32);
63  for (int i = 2; i < WIDTH; i++) {
64  pn[i] = 0;
65  }
66  }
67 
68  explicit base_uint(const std::string &str);
69 
70  const base_uint operator~() const {
71  base_uint ret;
72  for (int i = 0; i < WIDTH; i++) {
73  ret.pn[i] = ~pn[i];
74  }
75  return ret;
76  }
77 
78  const base_uint operator-() const {
79  base_uint ret;
80  for (int i = 0; i < WIDTH; i++) {
81  ret.pn[i] = ~pn[i];
82  }
83  ++ret;
84  return ret;
85  }
86 
87  double getdouble() const;
88 
89  base_uint &operator=(uint64_t b) {
90  pn[0] = (unsigned int)b;
91  pn[1] = (unsigned int)(b >> 32);
92  for (int i = 2; i < WIDTH; i++) {
93  pn[i] = 0;
94  }
95  return *this;
96  }
97 
99  for (int i = 0; i < WIDTH; i++) {
100  pn[i] ^= b.pn[i];
101  }
102  return *this;
103  }
104 
106  for (int i = 0; i < WIDTH; i++) {
107  pn[i] &= b.pn[i];
108  }
109  return *this;
110  }
111 
113  for (int i = 0; i < WIDTH; i++) {
114  pn[i] |= b.pn[i];
115  }
116  return *this;
117  }
118 
119  base_uint &operator^=(uint64_t b) {
120  pn[0] ^= (unsigned int)b;
121  pn[1] ^= (unsigned int)(b >> 32);
122  return *this;
123  }
124 
125  base_uint &operator|=(uint64_t b) {
126  pn[0] |= (unsigned int)b;
127  pn[1] |= (unsigned int)(b >> 32);
128  return *this;
129  }
130 
131  base_uint &operator<<=(unsigned int shift);
132  base_uint &operator>>=(unsigned int shift);
133 
135  uint64_t carry = 0;
136  for (int i = 0; i < WIDTH; i++) {
137  uint64_t n = carry + pn[i] + b.pn[i];
138  pn[i] = n & 0xffffffff;
139  carry = n >> 32;
140  }
141  return *this;
142  }
143 
145  *this += -b;
146  return *this;
147  }
148 
149  base_uint &operator+=(uint64_t b64) {
150  base_uint b;
151  b = b64;
152  *this += b;
153  return *this;
154  }
155 
156  base_uint &operator-=(uint64_t b64) {
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 
168  // prefix operator
169  int i = 0;
170  while (i < WIDTH && ++pn[i] == 0) {
171  i++;
172  }
173  return *this;
174  }
175 
176  const base_uint operator++(int) {
177  // postfix operator
178  const base_uint ret = *this;
179  ++(*this);
180  return ret;
181  }
182 
184  // prefix operator
185  int i = 0;
186  while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max()) {
187  i++;
188  }
189  return *this;
190  }
191 
192  const base_uint operator--(int) {
193  // postfix operator
194  const base_uint ret = *this;
195  --(*this);
196  return ret;
197  }
198 
199  int CompareTo(const base_uint &b) const;
200  bool EqualTo(uint64_t b) const;
201 
202  friend inline const base_uint operator+(const base_uint &a,
203  const base_uint &b) {
204  return base_uint(a) += b;
205  }
206  friend inline const base_uint operator-(const base_uint &a,
207  const base_uint &b) {
208  return base_uint(a) -= b;
209  }
210  friend inline const base_uint operator*(const base_uint &a,
211  const base_uint &b) {
212  return base_uint(a) *= b;
213  }
214  friend inline const base_uint operator/(const base_uint &a,
215  const base_uint &b) {
216  return base_uint(a) /= b;
217  }
218  friend inline const base_uint operator|(const base_uint &a,
219  const base_uint &b) {
220  return base_uint(a) |= b;
221  }
222  friend inline const base_uint operator&(const base_uint &a,
223  const base_uint &b) {
224  return base_uint(a) &= b;
225  }
226  friend inline const base_uint operator^(const base_uint &a,
227  const base_uint &b) {
228  return base_uint(a) ^= b;
229  }
230  friend inline const base_uint operator>>(const base_uint &a, int shift) {
231  return base_uint(a) >>= shift;
232  }
233  friend inline const base_uint operator<<(const base_uint &a, int shift) {
234  return base_uint(a) <<= shift;
235  }
236  friend inline const base_uint operator*(const base_uint &a, uint32_t b) {
237  return base_uint(a) *= b;
238  }
239  friend inline bool operator==(const base_uint &a, const base_uint &b) {
240  return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0;
241  }
242  friend inline bool operator!=(const base_uint &a, const base_uint &b) {
243  return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0;
244  }
245  friend inline bool operator>(const base_uint &a, const base_uint &b) {
246  return a.CompareTo(b) > 0;
247  }
248  friend inline bool operator<(const base_uint &a, const base_uint &b) {
249  return a.CompareTo(b) < 0;
250  }
251  friend inline bool operator>=(const base_uint &a, const base_uint &b) {
252  return a.CompareTo(b) >= 0;
253  }
254  friend inline bool operator<=(const base_uint &a, const base_uint &b) {
255  return a.CompareTo(b) <= 0;
256  }
257  friend inline bool operator==(const base_uint &a, uint64_t b) {
258  return a.EqualTo(b);
259  }
260  friend inline bool operator!=(const base_uint &a, uint64_t b) {
261  return !a.EqualTo(b);
262  }
263 
264  std::string GetHex() const;
265  void SetHex(const char *psz);
266  void SetHex(const std::string &str);
267  std::string ToString() const;
268 
269  unsigned int size() const { return sizeof(pn); }
270 
275  unsigned int bits() const;
276 
277  uint64_t GetLow64() const {
278  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / "
279  "32). BITS is a template parameter.");
280  return pn[0] | uint64_t(pn[1]) << 32;
281  }
282 };
283 
285 class arith_uint256 : public base_uint<256> {
286 public:
288  arith_uint256(const base_uint<256> &b) : base_uint<256>(b) {}
289  arith_uint256(uint64_t b) : base_uint<256>(b) {}
290  explicit arith_uint256(const std::string &str) : base_uint<256>(str) {}
291 
310  arith_uint256 &SetCompact(uint32_t nCompact, bool *pfNegative = nullptr,
311  bool *pfOverflow = nullptr);
312  uint32_t GetCompact(bool fNegative = false) const;
313 
314  friend uint256 ArithToUint256(const arith_uint256 &);
315  friend arith_uint256 UintToArith256(const uint256 &);
316 };
317 
320 
321 #endif // BITCOIN_ARITH_UINT256_H
arith_uint256 UintToArith256(const uint256 &)
uint256 ArithToUint256(const arith_uint256 &)
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
arith_uint256(uint64_t b)
arith_uint256(const base_uint< 256 > &b)
friend arith_uint256 UintToArith256(const uint256 &)
friend uint256 ArithToUint256(const arith_uint256 &)
Template base class for unsigned big integers.
Definition: arith_uint256.h:23
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:49
uint32_t pn[WIDTH]
Definition: arith_uint256.h:26
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:89
int CompareTo(const base_uint &b) const
base_uint & operator+=(uint64_t b64)
unsigned int size() const
base_uint(uint64_t b)
Definition: arith_uint256.h:56
const base_uint operator~() const
Definition: arith_uint256.h:70
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:78
friend bool operator!=(const base_uint &a, const base_uint &b)
base_uint & operator^=(uint64_t b)
friend const base_uint operator-(const base_uint &a, const base_uint &b)
base_uint & operator>>=(unsigned int shift)
base_uint & operator++()
base_uint(const base_uint &b)
Definition: arith_uint256.h:39
static constexpr int WIDTH
Definition: arith_uint256.h:25
base_uint & operator&=(const base_uint &b)
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)
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)
bool EqualTo(uint64_t b) 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)
friend bool operator>(const base_uint &a, const base_uint &b)
friend bool operator<=(const base_uint &a, const base_uint &b)
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:98
base_uint & operator/=(const base_uint &b)
uint64_t GetLow64() const
void SetHex(const char *psz)
std::string GetHex() const
friend const base_uint operator|(const base_uint &a, const base_uint &b)
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
256-bit opaque blob.
Definition: uint256.h:129
uint_error(const std::string &str)
Definition: arith_uint256.h:19
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257