Bitcoin Core  24.99.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-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_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");
28  static constexpr int WIDTH = BITS / 32;
29  uint32_t pn[WIDTH];
30 public:
31 
33  {
34  for (int i = 0; i < WIDTH; i++)
35  pn[i] = 0;
36  }
37 
38  base_uint(const base_uint& b)
39  {
40  for (int i = 0; i < WIDTH; i++)
41  pn[i] = b.pn[i];
42  }
43 
45  {
46  for (int i = 0; i < WIDTH; i++)
47  pn[i] = b.pn[i];
48  return *this;
49  }
50 
51  base_uint(uint64_t b)
52  {
53  pn[0] = (unsigned int)b;
54  pn[1] = (unsigned int)(b >> 32);
55  for (int i = 2; i < WIDTH; i++)
56  pn[i] = 0;
57  }
58 
59  explicit base_uint(const std::string& str);
60 
61  const base_uint operator~() const
62  {
63  base_uint ret;
64  for (int i = 0; i < WIDTH; i++)
65  ret.pn[i] = ~pn[i];
66  return ret;
67  }
68 
69  const base_uint operator-() const
70  {
71  base_uint ret;
72  for (int i = 0; i < WIDTH; i++)
73  ret.pn[i] = ~pn[i];
74  ++ret;
75  return ret;
76  }
77 
78  double getdouble() const;
79 
80  base_uint& operator=(uint64_t b)
81  {
82  pn[0] = (unsigned int)b;
83  pn[1] = (unsigned int)(b >> 32);
84  for (int i = 2; i < WIDTH; i++)
85  pn[i] = 0;
86  return *this;
87  }
88 
90  {
91  for (int i = 0; i < WIDTH; i++)
92  pn[i] ^= b.pn[i];
93  return *this;
94  }
95 
97  {
98  for (int i = 0; i < WIDTH; i++)
99  pn[i] &= b.pn[i];
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 
110  base_uint& operator^=(uint64_t b)
111  {
112  pn[0] ^= (unsigned int)b;
113  pn[1] ^= (unsigned int)(b >> 32);
114  return *this;
115  }
116 
117  base_uint& operator|=(uint64_t b)
118  {
119  pn[0] |= (unsigned int)b;
120  pn[1] |= (unsigned int)(b >> 32);
121  return *this;
122  }
123 
124  base_uint& operator<<=(unsigned int shift);
125  base_uint& operator>>=(unsigned int shift);
126 
128  {
129  uint64_t carry = 0;
130  for (int i = 0; i < WIDTH; i++)
131  {
132  uint64_t n = carry + pn[i] + b.pn[i];
133  pn[i] = n & 0xffffffff;
134  carry = n >> 32;
135  }
136  return *this;
137  }
138 
140  {
141  *this += -b;
142  return *this;
143  }
144 
145  base_uint& operator+=(uint64_t b64)
146  {
147  base_uint b;
148  b = b64;
149  *this += b;
150  return *this;
151  }
152 
153  base_uint& operator-=(uint64_t b64)
154  {
155  base_uint b;
156  b = b64;
157  *this += -b;
158  return *this;
159  }
160 
161  base_uint& operator*=(uint32_t b32);
162  base_uint& operator*=(const base_uint& b);
163  base_uint& operator/=(const base_uint& b);
164 
166  {
167  // prefix operator
168  int i = 0;
169  while (i < WIDTH && ++pn[i] == 0)
170  i++;
171  return *this;
172  }
173 
175  {
176  // postfix operator
177  const base_uint ret = *this;
178  ++(*this);
179  return ret;
180  }
181 
183  {
184  // prefix operator
185  int i = 0;
186  while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
187  i++;
188  return *this;
189  }
190 
192  {
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, const base_uint& b) { return base_uint(a) += b; }
203  friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
204  friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
205  friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
206  friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
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, int shift) { return base_uint(a) >>= shift; }
210  friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
211  friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
212  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
213  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
214  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
215  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
216  friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
217  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
218  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
219  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
220 
221  std::string GetHex() const;
222  void SetHex(const char* psz);
223  void SetHex(const std::string& str);
224  std::string ToString() const;
225 
226  unsigned int size() const
227  {
228  return sizeof(pn);
229  }
230 
235  unsigned int bits() const;
236 
237  uint64_t GetLow64() const
238  {
239  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
240  return pn[0] | (uint64_t)pn[1] << 32;
241  }
242 };
243 
245 class arith_uint256 : public base_uint<256> {
246 public:
248  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
249  arith_uint256(uint64_t b) : base_uint<256>(b) {}
250  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
251 
272  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
273  uint32_t GetCompact(bool fNegative = false) const;
274 
275  friend uint256 ArithToUint256(const arith_uint256 &);
276  friend arith_uint256 UintToArith256(const uint256 &);
277 };
278 
281 
282 extern template class base_uint<256>;
283 
284 #endif // BITCOIN_ARITH_UINT256_H
arith_uint256 UintToArith256(const uint256 &)
uint256 ArithToUint256(const arith_uint256 &)
int ret
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:25
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:44
uint32_t pn[WIDTH]
Definition: arith_uint256.h:29
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:80
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:51
const base_uint operator~() const
Definition: arith_uint256.h:61
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:69
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:38
static constexpr int WIDTH
Definition: arith_uint256.h:28
base_uint & operator&=(const base_uint &b)
Definition: arith_uint256.h:96
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:89
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:119
uint_error(const std::string &str)
Definition: arith_uint256.h:19