Bitcoin Core  0.20.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-2019 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 <cstring>
10 #include <limits>
11 #include <stdexcept>
12 #include <stdint.h>
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>
24 class base_uint
25 {
26 protected:
27  static constexpr int WIDTH = BITS / 32;
28  uint32_t pn[WIDTH];
29 public:
30 
32  {
33  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
34 
35  for (int i = 0; i < WIDTH; i++)
36  pn[i] = 0;
37  }
38 
39  base_uint(const base_uint& b)
40  {
41  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
42 
43  for (int i = 0; i < WIDTH; i++)
44  pn[i] = b.pn[i];
45  }
46 
48  {
49  for (int i = 0; i < WIDTH; i++)
50  pn[i] = b.pn[i];
51  return *this;
52  }
53 
54  base_uint(uint64_t b)
55  {
56  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
57 
58  pn[0] = (unsigned int)b;
59  pn[1] = (unsigned int)(b >> 32);
60  for (int i = 2; i < WIDTH; i++)
61  pn[i] = 0;
62  }
63 
64  explicit base_uint(const std::string& str);
65 
66  const base_uint operator~() const
67  {
68  base_uint ret;
69  for (int i = 0; i < WIDTH; i++)
70  ret.pn[i] = ~pn[i];
71  return ret;
72  }
73 
74  const base_uint operator-() const
75  {
76  base_uint ret;
77  for (int i = 0; i < WIDTH; i++)
78  ret.pn[i] = ~pn[i];
79  ++ret;
80  return ret;
81  }
82 
83  double getdouble() const;
84 
85  base_uint& operator=(uint64_t b)
86  {
87  pn[0] = (unsigned int)b;
88  pn[1] = (unsigned int)(b >> 32);
89  for (int i = 2; i < WIDTH; i++)
90  pn[i] = 0;
91  return *this;
92  }
93 
95  {
96  for (int i = 0; i < WIDTH; i++)
97  pn[i] ^= b.pn[i];
98  return *this;
99  }
100 
102  {
103  for (int i = 0; i < WIDTH; i++)
104  pn[i] &= b.pn[i];
105  return *this;
106  }
107 
109  {
110  for (int i = 0; i < WIDTH; i++)
111  pn[i] |= b.pn[i];
112  return *this;
113  }
114 
115  base_uint& operator^=(uint64_t b)
116  {
117  pn[0] ^= (unsigned int)b;
118  pn[1] ^= (unsigned int)(b >> 32);
119  return *this;
120  }
121 
122  base_uint& operator|=(uint64_t b)
123  {
124  pn[0] |= (unsigned int)b;
125  pn[1] |= (unsigned int)(b >> 32);
126  return *this;
127  }
128 
129  base_uint& operator<<=(unsigned int shift);
130  base_uint& operator>>=(unsigned int shift);
131 
133  {
134  uint64_t carry = 0;
135  for (int i = 0; i < WIDTH; i++)
136  {
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  {
146  *this += -b;
147  return *this;
148  }
149 
150  base_uint& operator+=(uint64_t b64)
151  {
152  base_uint b;
153  b = b64;
154  *this += b;
155  return *this;
156  }
157 
158  base_uint& operator-=(uint64_t b64)
159  {
160  base_uint b;
161  b = b64;
162  *this += -b;
163  return *this;
164  }
165 
166  base_uint& operator*=(uint32_t b32);
167  base_uint& operator*=(const base_uint& b);
168  base_uint& operator/=(const base_uint& b);
169 
171  {
172  // prefix operator
173  int i = 0;
174  while (i < WIDTH && ++pn[i] == 0)
175  i++;
176  return *this;
177  }
178 
180  {
181  // postfix operator
182  const base_uint ret = *this;
183  ++(*this);
184  return ret;
185  }
186 
188  {
189  // prefix operator
190  int i = 0;
191  while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
192  i++;
193  return *this;
194  }
195 
197  {
198  // postfix operator
199  const base_uint ret = *this;
200  --(*this);
201  return ret;
202  }
203 
204  int CompareTo(const base_uint& b) const;
205  bool EqualTo(uint64_t b) const;
206 
207  friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
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, int shift) { return base_uint(a) >>= shift; }
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, uint32_t b) { return base_uint(a) *= b; }
217  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
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 a.CompareTo(b) > 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, uint64_t b) { return a.EqualTo(b); }
224  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
225 
226  std::string GetHex() const;
227  void SetHex(const char* psz);
228  void SetHex(const std::string& str);
229  std::string ToString() const;
230 
231  unsigned int size() const
232  {
233  return sizeof(pn);
234  }
235 
240  unsigned int bits() const;
241 
242  uint64_t GetLow64() const
243  {
244  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
245  return pn[0] | (uint64_t)pn[1] << 32;
246  }
247 };
248 
250 class arith_uint256 : public base_uint<256> {
251 public:
253  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
254  arith_uint256(uint64_t b) : base_uint<256>(b) {}
255  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
256 
277  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
278  uint32_t GetCompact(bool fNegative = false) const;
279 
280  friend uint256 ArithToUint256(const arith_uint256 &);
281  friend arith_uint256 UintToArith256(const uint256 &);
282 };
283 
286 
287 #endif // BITCOIN_ARITH_UINT256_H
bool EqualTo(uint64_t b) const
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:47
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:66
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:24
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:28
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:69
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:54
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:39
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:19
base_uint & operator^=(uint64_t b)
256-bit unsigned big integer.
256-bit opaque blob.
Definition: uint256.h:120
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:74
arith_uint256(uint64_t b)
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:85
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)
Definition: arith_uint256.h:94