Bitcoin Core  21.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 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
base_uint::operator-
const base_uint operator-() const
Definition: arith_uint256.h:74
base_uint::operator++
const base_uint operator++(int)
Definition: arith_uint256.h:179
base_uint::operator==
friend bool operator==(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:223
base_uint::operator++
base_uint & operator++()
Definition: arith_uint256.h:170
base_uint::operator|
const friend base_uint operator|(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:211
arith_uint256::UintToArith256
friend arith_uint256 UintToArith256(const uint256 &)
Definition: arith_uint256.cpp:253
base_uint::operator/
const friend base_uint operator/(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:210
ArithToUint256
uint256 ArithToUint256(const arith_uint256 &)
Definition: arith_uint256.cpp:246
base_uint::GetHex
std::string GetHex() const
Definition: arith_uint256.cpp:147
base_uint::operator>>
const friend base_uint operator>>(const base_uint &a, int shift)
Definition: arith_uint256.h:214
base_uint::operator<<=
base_uint & operator<<=(unsigned int shift)
Definition: arith_uint256.cpp:21
arith_uint256
256-bit unsigned big integer.
Definition: arith_uint256.h:250
base_uint::size
unsigned int size() const
Definition: arith_uint256.h:231
base_uint::operator-=
base_uint & operator-=(const base_uint &b)
Definition: arith_uint256.h:144
base_uint::bits
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
Definition: arith_uint256.cpp:171
arith_uint256::arith_uint256
arith_uint256(const std::string &str)
Definition: arith_uint256.h:255
base_uint::operator<=
friend bool operator<=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:222
base_uint::operator-
const friend base_uint operator-(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:208
base_uint::operator*
const friend base_uint operator*(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:209
base_uint::base_uint
base_uint(uint64_t b)
Definition: arith_uint256.h:54
base_uint::operator!=
friend bool operator!=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:218
base_uint::operator+=
base_uint & operator+=(const base_uint &b)
Definition: arith_uint256.h:132
base_uint::operator^
const friend base_uint operator^(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:213
base_uint::operator>=
friend bool operator>=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:221
base_uint::operator&=
base_uint & operator&=(const base_uint &b)
Definition: arith_uint256.h:101
base_uint::operator*=
base_uint & operator*=(uint32_t b32)
Definition: arith_uint256.cpp:55
base_uint::operator-=
base_uint & operator-=(uint64_t b64)
Definition: arith_uint256.h:158
base_uint::operator!=
friend bool operator!=(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:224
base_uint::EqualTo
bool EqualTo(uint64_t b) const
Definition: arith_uint256.cpp:121
base_uint::base_uint
base_uint(const base_uint &b)
Definition: arith_uint256.h:39
base_uint::GetLow64
uint64_t GetLow64() const
Definition: arith_uint256.h:242
arith_uint256::arith_uint256
arith_uint256()
Definition: arith_uint256.h:252
base_uint::operator<
friend bool operator<(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:220
arith_uint256::GetCompact
uint32_t GetCompact(bool fNegative=false) const
Definition: arith_uint256.cpp:223
base_uint::operator+
const friend base_uint operator+(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:207
base_uint::operator^=
base_uint & operator^=(uint64_t b)
Definition: arith_uint256.h:115
base_uint::operator|=
base_uint & operator|=(const base_uint &b)
Definition: arith_uint256.h:108
uint256
256-bit opaque blob.
Definition: uint256.h:124
base_uint::SetHex
void SetHex(const char *psz)
Definition: arith_uint256.cpp:153
base_uint::operator--
const base_uint operator--(int)
Definition: arith_uint256.h:196
base_uint::operator~
const base_uint operator~() const
Definition: arith_uint256.h:66
uint_error::uint_error
uint_error(const std::string &str)
Definition: arith_uint256.h:19
base_uint::operator&
const friend base_uint operator&(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:212
base_uint::operator=
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:47
base_uint
Template base class for unsigned big integers.
Definition: arith_uint256.h:24
base_uint::operator==
friend bool operator==(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:217
arith_uint256::arith_uint256
arith_uint256(const base_uint< 256 > &b)
Definition: arith_uint256.h:253
arith_uint256::ArithToUint256
friend uint256 ArithToUint256(const arith_uint256 &)
Definition: arith_uint256.cpp:246
base_uint::WIDTH
static constexpr int WIDTH
Definition: arith_uint256.h:27
base_uint::operator=
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:85
arith_uint256::SetCompact
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...
Definition: arith_uint256.cpp:203
base_uint::operator>
friend bool operator>(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:219
base_uint::operator<<
const friend base_uint operator<<(const base_uint &a, int shift)
Definition: arith_uint256.h:215
base_uint::operator>>=
base_uint & operator>>=(unsigned int shift)
Definition: arith_uint256.cpp:38
UintToArith256
arith_uint256 UintToArith256(const uint256 &)
Definition: arith_uint256.cpp:253
base_uint::operator*
const friend base_uint operator*(const base_uint &a, uint32_t b)
Definition: arith_uint256.h:216
arith_uint256::arith_uint256
arith_uint256(uint64_t b)
Definition: arith_uint256.h:254
base_uint::operator--
base_uint & operator--()
Definition: arith_uint256.h:187
base_uint::CompareTo
int CompareTo(const base_uint &b) const
Definition: arith_uint256.cpp:109
uint_error
Definition: arith_uint256.h:17
base_uint::base_uint
base_uint()
Definition: arith_uint256.h:31
base_uint::operator^=
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:94
base_uint::getdouble
double getdouble() const
Definition: arith_uint256.cpp:135
base_uint::operator+=
base_uint & operator+=(uint64_t b64)
Definition: arith_uint256.h:150
base_uint::operator/=
base_uint & operator/=(const base_uint &b)
Definition: arith_uint256.cpp:83
base_uint::operator|=
base_uint & operator|=(uint64_t b)
Definition: arith_uint256.h:122
base_uint::pn
uint32_t pn[WIDTH]
Definition: arith_uint256.h:28
base_uint::ToString
std::string ToString() const
Definition: arith_uint256.cpp:165