Bitcoin Core  27.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-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;
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 
60  {
61  base_uint ret;
62  for (int i = 0; i < WIDTH; i++)
63  ret.pn[i] = ~pn[i];
64  return ret;
65  }
66 
68  {
69  base_uint ret;
70  for (int i = 0; i < WIDTH; i++)
71  ret.pn[i] = ~pn[i];
72  ++ret;
73  return ret;
74  }
75 
76  double getdouble() const;
77 
78  base_uint& operator=(uint64_t b)
79  {
80  pn[0] = (unsigned int)b;
81  pn[1] = (unsigned int)(b >> 32);
82  for (int i = 2; i < WIDTH; i++)
83  pn[i] = 0;
84  return *this;
85  }
86 
88  {
89  for (int i = 0; i < WIDTH; i++)
90  pn[i] ^= b.pn[i];
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 
108  base_uint& operator^=(uint64_t b)
109  {
110  pn[0] ^= (unsigned int)b;
111  pn[1] ^= (unsigned int)(b >> 32);
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<<=(unsigned int shift);
123  base_uint& operator>>=(unsigned int shift);
124 
126  {
127  uint64_t carry = 0;
128  for (int i = 0; i < WIDTH; i++)
129  {
130  uint64_t n = carry + pn[i] + b.pn[i];
131  pn[i] = n & 0xffffffff;
132  carry = n >> 32;
133  }
134  return *this;
135  }
136 
138  {
139  *this += -b;
140  return *this;
141  }
142 
143  base_uint& operator+=(uint64_t b64)
144  {
145  base_uint b;
146  b = b64;
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*=(uint32_t b32);
160  base_uint& operator*=(const base_uint& b);
161  base_uint& operator/=(const base_uint& b);
162 
164  {
165  // prefix operator
166  int i = 0;
167  while (i < WIDTH && ++pn[i] == 0)
168  i++;
169  return *this;
170  }
171 
173  {
174  // postfix operator
175  const base_uint ret = *this;
176  ++(*this);
177  return ret;
178  }
179 
181  {
182  // prefix operator
183  int i = 0;
184  while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
185  i++;
186  return *this;
187  }
188 
190  {
191  // postfix operator
192  const base_uint ret = *this;
193  --(*this);
194  return ret;
195  }
196 
197  int CompareTo(const base_uint& b) const;
198  bool EqualTo(uint64_t b) const;
199 
200  friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
201  friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
202  friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
203  friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
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, int shift) { return base_uint(a) >>= shift; }
208  friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
209  friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
210  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
211  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
212  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
213  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 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, uint64_t b) { return a.EqualTo(b); }
217  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
218 
219  std::string GetHex() const;
220  std::string ToString() const;
221 
222  unsigned int size() const
223  {
224  return sizeof(pn);
225  }
226 
231  unsigned int bits() const;
232 
233  uint64_t GetLow64() const
234  {
235  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
236  return pn[0] | (uint64_t)pn[1] << 32;
237  }
238 };
239 
241 class arith_uint256 : public base_uint<256> {
242 public:
244  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
245  arith_uint256(uint64_t b) : base_uint<256>(b) {}
246 
267  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
268  uint32_t GetCompact(bool fNegative = false) const;
269 
270  friend uint256 ArithToUint256(const arith_uint256 &);
271  friend arith_uint256 UintToArith256(const uint256 &);
272 };
273 
276 
277 extern template class base_uint<256>;
278 
279 #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...
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
base_uint operator~() const
Definition: arith_uint256.h:59
uint32_t pn[WIDTH]
Definition: arith_uint256.h:29
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:78
int CompareTo(const base_uint &b) const
base_uint & operator+=(uint64_t b64)
base_uint operator--(int)
unsigned int size() const
base_uint(uint64_t b)
Definition: arith_uint256.h:51
base_uint & operator--()
friend bool operator!=(const base_uint &a, const base_uint &b)
base_uint & operator^=(uint64_t b)
base_uint & operator>>=(unsigned int shift)
base_uint & operator++()
friend base_uint operator-(const base_uint &a, const base_uint &b)
base_uint(const base_uint &b)
Definition: arith_uint256.h:38
friend base_uint operator*(const base_uint &a, const base_uint &b)
static constexpr int WIDTH
Definition: arith_uint256.h:28
base_uint & operator&=(const base_uint &b)
Definition: arith_uint256.h:94
friend base_uint operator<<(const base_uint &a, int shift)
friend 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)
base_uint operator-() const
Definition: arith_uint256.h:67
friend 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 base_uint operator|(const base_uint &a, const base_uint &b)
bool EqualTo(uint64_t b) const
friend base_uint operator*(const base_uint &a, uint32_t 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, uint64_t b)
friend base_uint operator/(const base_uint &a, const base_uint &b)
friend base_uint operator^(const base_uint &a, const base_uint &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
base_uint operator++(int)
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:87
base_uint & operator/=(const base_uint &b)
uint64_t GetLow64() const
std::string GetHex() const
friend 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:106
uint_error(const std::string &str)
Definition: arith_uint256.h:19