Bitcoin Core  29.1.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-2022 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>
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;
30  uint32_t pn[WIDTH];
31 public:
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  for (int i = 0; i < WIDTH; i++)
42  pn[i] = b.pn[i];
43  }
44 
46  {
47  if (this != &b) {
48  for (int i = 0; i < WIDTH; i++)
49  pn[i] = b.pn[i];
50  }
51  return *this;
52  }
53 
54  base_uint(uint64_t b)
55  {
56  pn[0] = (unsigned int)b;
57  pn[1] = (unsigned int)(b >> 32);
58  for (int i = 2; i < WIDTH; i++)
59  pn[i] = 0;
60  }
61 
63  {
64  base_uint ret;
65  for (int i = 0; i < WIDTH; i++)
66  ret.pn[i] = ~pn[i];
67  return ret;
68  }
69 
71  {
72  base_uint ret;
73  for (int i = 0; i < WIDTH; i++)
74  ret.pn[i] = ~pn[i];
75  ++ret;
76  return ret;
77  }
78 
79  double getdouble() const;
80 
81  base_uint& operator=(uint64_t b)
82  {
83  pn[0] = (unsigned int)b;
84  pn[1] = (unsigned int)(b >> 32);
85  for (int i = 2; i < WIDTH; i++)
86  pn[i] = 0;
87  return *this;
88  }
89 
91  {
92  for (int i = 0; i < WIDTH; i++)
93  pn[i] ^= b.pn[i];
94  return *this;
95  }
96 
98  {
99  for (int i = 0; i < WIDTH; i++)
100  pn[i] &= b.pn[i];
101  return *this;
102  }
103 
105  {
106  for (int i = 0; i < WIDTH; i++)
107  pn[i] |= b.pn[i];
108  return *this;
109  }
110 
111  base_uint& operator^=(uint64_t b)
112  {
113  pn[0] ^= (unsigned int)b;
114  pn[1] ^= (unsigned int)(b >> 32);
115  return *this;
116  }
117 
118  base_uint& operator|=(uint64_t b)
119  {
120  pn[0] |= (unsigned int)b;
121  pn[1] |= (unsigned int)(b >> 32);
122  return *this;
123  }
124 
125  base_uint& operator<<=(unsigned int shift);
126  base_uint& operator>>=(unsigned int shift);
127 
129  {
130  uint64_t carry = 0;
131  for (int i = 0; i < WIDTH; i++)
132  {
133  uint64_t n = carry + pn[i] + b.pn[i];
134  pn[i] = n & 0xffffffff;
135  carry = n >> 32;
136  }
137  return *this;
138  }
139 
141  {
142  *this += -b;
143  return *this;
144  }
145 
146  base_uint& operator+=(uint64_t b64)
147  {
148  base_uint b;
149  b = b64;
150  *this += b;
151  return *this;
152  }
153 
154  base_uint& operator-=(uint64_t b64)
155  {
156  base_uint b;
157  b = b64;
158  *this += -b;
159  return *this;
160  }
161 
162  base_uint& operator*=(uint32_t b32);
163  base_uint& operator*=(const base_uint& b);
164  base_uint& operator/=(const base_uint& b);
165 
167  {
168  // prefix operator
169  int i = 0;
170  while (i < WIDTH && ++pn[i] == 0)
171  i++;
172  return *this;
173  }
174 
176  {
177  // postfix operator
178  const base_uint ret = *this;
179  ++(*this);
180  return ret;
181  }
182 
184  {
185  // prefix operator
186  int i = 0;
187  while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
188  i++;
189  return *this;
190  }
191 
193  {
194  // postfix operator
195  const base_uint ret = *this;
196  --(*this);
197  return ret;
198  }
199 
201  int CompareTo(const base_uint& b) const;
202  bool EqualTo(uint64_t b) const;
203 
204  friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
205  friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
206  friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
207  friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
208  friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
209  friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
210  friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
211  friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
212  friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
213  friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
214  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
215  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 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, const base_uint& b) { return a.CompareTo(b) >= 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, uint64_t b) { return a.EqualTo(b); }
221  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
222 
224  std::string GetHex() const;
225  std::string ToString() const;
226 
227  unsigned int size() const
228  {
229  return sizeof(pn);
230  }
231 
236  unsigned int bits() const;
237 
238  uint64_t GetLow64() const
239  {
240  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
241  return pn[0] | (uint64_t)pn[1] << 32;
242  }
243 };
244 
246 class arith_uint256 : public base_uint<256> {
247 public:
248  arith_uint256() = default;
249  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
250  arith_uint256(uint64_t b) : base_uint<256>(b) {}
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
bool EqualTo(uint64_t b) const
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:45
base_uint & operator &=(const base_uint &b)
Definition: arith_uint256.h:97
int ret
friend base_uint operator>>(const base_uint &a, int shift)
arith_uint256()=default
std::string ToString() const
base_uint operator--(int)
base_uint & operator|=(uint64_t b)
friend base_uint operator<<(const base_uint &a, int shift)
static constexpr int WIDTH
Definition: arith_uint256.h:28
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
base_uint & operator<<=(unsigned int shift)
base_uint & operator+=(uint64_t b64)
base_uint operator~() const
Definition: arith_uint256.h:62
friend base_uint operator &(const base_uint &a, const base_uint &b)
uint32_t GetCompact(bool fNegative=false) const
base_uint & operator--()
friend base_uint operator*(const base_uint &a, uint32_t b)
uint32_t pn[WIDTH]
Big integer represented with 32-bit digits, least-significant first.
Definition: arith_uint256.h:30
base_uint & operator-=(const base_uint &b)
friend bool operator==(const base_uint &a, const base_uint &b)
friend base_uint operator|(const base_uint &a, const base_uint &b)
base_uint(uint64_t b)
Definition: arith_uint256.h:54
friend base_uint operator/(const base_uint &a, const base_uint &b)
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 base_uint operator^(const base_uint &a, const base_uint &b)
friend base_uint operator+(const base_uint &a, const base_uint &b)
base_uint operator-() const
Definition: arith_uint256.h:70
int CompareTo(const base_uint &b) const
Numeric ordering (unlike base_blob::Compare)
uint_error(const std::string &str)
Definition: arith_uint256.h:19
base_uint & operator^=(uint64_t b)
friend uint256 ArithToUint256(const arith_uint256 &)
256-bit unsigned big integer.
base_uint & operator/=(const base_uint &b)
base_uint operator++(int)
256-bit opaque blob.
Definition: uint256.h:201
base_uint & operator++()
friend bool operator!=(const base_uint &a, const base_uint &b)
uint64_t GetLow64() const
arith_uint256(uint64_t b)
base_uint & operator*=(uint32_t b32)
friend base_uint operator*(const base_uint &a, const base_uint &b)
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:81
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...
friend base_uint operator-(const base_uint &a, const base_uint &b)
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
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 arith_uint256 UintToArith256(const uint256 &)
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:90
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.