Bitcoin
arith_uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <assert.h>
10 #include <cstring>
11 #include <limits>
12 #include <stdexcept>
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
17 class uint256;
18 
19 class uint_error : public std::runtime_error {
20 public:
21  explicit uint_error(const std::string& str) : std::runtime_error(str) {}
22 };
23 
25 template<unsigned int BITS>
26 class base_uint
27 {
28 protected:
29  static constexpr int WIDTH = BITS / 32;
31 public:
32 
34  {
35  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
36 
37  for (int i = 0; i < WIDTH; i++)
38  pn[i] = 0;
39  }
40 
41  base_uint(const base_uint& b)
42  {
43  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
44 
45  for (int i = 0; i < WIDTH; i++)
46  pn[i] = b.pn[i];
47  }
48 
50  {
51  for (int i = 0; i < WIDTH; i++)
52  pn[i] = b.pn[i];
53  return *this;
54  }
55 
57  {
58  static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
59 
60  pn[0] = (unsigned int)b;
61  pn[1] = (unsigned int)(b >> 32);
62  for (int i = 2; i < WIDTH; i++)
63  pn[i] = 0;
64  }
65 
66  explicit base_uint(const std::string& str);
67 
68  const base_uint operator~() const
69  {
70  base_uint ret;
71  for (int i = 0; i < WIDTH; i++)
72  ret.pn[i] = ~pn[i];
73  return ret;
74  }
75 
76  const base_uint operator-() const
77  {
78  base_uint ret;
79  for (int i = 0; i < WIDTH; i++)
80  ret.pn[i] = ~pn[i];
81  ++ret;
82  return ret;
83  }
84 
85  double getdouble() const;
86 
88  {
89  pn[0] = (unsigned int)b;
90  pn[1] = (unsigned int)(b >> 32);
91  for (int i = 2; i < WIDTH; i++)
92  pn[i] = 0;
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 
111  {
112  for (int i = 0; i < WIDTH; i++)
113  pn[i] |= b.pn[i];
114  return *this;
115  }
116 
118  {
119  pn[0] ^= (unsigned int)b;
120  pn[1] ^= (unsigned int)(b >> 32);
121  return *this;
122  }
123 
125  {
126  pn[0] |= (unsigned int)b;
127  pn[1] |= (unsigned int)(b >> 32);
128  return *this;
129  }
130 
131  base_uint& operator<<=(unsigned int shift);
132  base_uint& operator>>=(unsigned int shift);
133 
135  {
136  uint64_t carry = 0;
137  for (int i = 0; i < WIDTH; i++)
138  {
139  uint64_t n = carry + pn[i] + b.pn[i];
140  pn[i] = n & 0xffffffff;
141  carry = n >> 32;
142  }
143  return *this;
144  }
145 
147  {
148  *this += -b;
149  return *this;
150  }
151 
153  {
154  base_uint b;
155  b = b64;
156  *this += b;
157  return *this;
158  }
159 
161  {
162  base_uint b;
163  b = b64;
164  *this += -b;
165  return *this;
166  }
167 
169  base_uint& operator*=(const base_uint& b);
170  base_uint& operator/=(const base_uint& b);
171 
173  {
174  // prefix operator
175  int i = 0;
176  while (i < WIDTH && ++pn[i] == 0)
177  i++;
178  return *this;
179  }
180 
182  {
183  // postfix operator
184  const base_uint ret = *this;
185  ++(*this);
186  return ret;
187  }
188 
190  {
191  // prefix operator
192  int i = 0;
193  while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
194  i++;
195  return *this;
196  }
197 
199  {
200  // postfix operator
201  const base_uint ret = *this;
202  --(*this);
203  return ret;
204  }
205 
206  int CompareTo(const base_uint& b) const;
207  bool EqualTo(uint64_t b) const;
208 
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, const base_uint& b) { return base_uint(a) &= b; }
215  friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
216  friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
217  friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
218  friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
219  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
220  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 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, const base_uint& b) { return a.CompareTo(b) >= 0; }
224  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
225  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
226  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
227 
228  std::string GetHex() const;
229  void SetHex(const char* psz);
230  void SetHex(const std::string& str);
231  std::string ToString() const;
232 
233  unsigned int size() const
234  {
235  return sizeof(pn);
236  }
237 
242  unsigned int bits() const;
243 
245  {
246  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
247  return pn[0] | (uint64_t)pn[1] << 32;
248  }
249 };
250 
252 class arith_uint256 : public base_uint<256> {
253 public:
255  arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
257  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
258 
279  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
280  uint32_t GetCompact(bool fNegative = false) const;
281 
282  friend uint256 ArithToUint256(const arith_uint256 &);
283  friend arith_uint256 UintToArith256(const uint256 &);
284 };
285 
288 
289 #endif // BITCOIN_ARITH_UINT256_H
bool EqualTo(uint64_t b) const
Definition: arith_uint256.cpp:123
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:49
void SetHex(const char *psz)
Definition: arith_uint256.cpp:155
friend const base_uint operator^(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:215
arith_uint256(const std::string &str)
Definition: arith_uint256.h:257
std::string ToString() const
Definition: arith_uint256.cpp:167
const base_uint operator~() const
Definition: arith_uint256.h:68
base_uint & operator|=(uint64_t b)
Definition: arith_uint256.h:124
base_uint & operator/=(const base_uint &b)
Definition: arith_uint256.cpp:85
static constexpr int WIDTH
Definition: arith_uint256.h:29
friend bool operator!=(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:226
base_uint & operator+=(const base_uint &b)
Definition: arith_uint256.h:134
friend bool operator<=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:224
base_uint & operator|=(const base_uint &b)
Definition: arith_uint256.h:110
base_uint & operator-=(uint64_t b64)
Definition: arith_uint256.h:160
friend bool operator>(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:221
Definition: arith_uint256.h:26
base_uint & operator *=(uint32_t b32)
Definition: arith_uint256.cpp:57
friend const base_uint operator-(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:210
base_uint & operator<<=(unsigned int shift)
Definition: arith_uint256.cpp:23
arith_uint256()
Definition: arith_uint256.h:254
base_uint & operator+=(uint64_t b64)
Definition: arith_uint256.h:152
friend const base_uint operator/(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:212
uint32_t GetCompact(bool fNegative=false) const
Definition: arith_uint256.cpp:225
Definition: arith_uint256.h:19
base_uint & operator--()
Definition: arith_uint256.h:189
base_uint & operator&=(const base_uint &b)
Definition: arith_uint256.h:103
uint32_t pn[WIDTH]
Definition: arith_uint256.h:30
base_uint & operator-=(const base_uint &b)
Definition: arith_uint256.h:146
friend bool operator==(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:219
friend const base_uint operator+(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:209
base_uint(uint64_t b)
Definition: arith_uint256.h:56
friend bool operator<(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:222
unsigned int uint32_t
Definition: stdint.h:21
friend const base_uint operator &(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:214
uint256 ArithToUint256(const arith_uint256 &)
Definition: arith_uint256.cpp:248
base_uint(const base_uint &b)
Definition: arith_uint256.h:41
unsigned long long uint64_t
Definition: stdint.h:22
int CompareTo(const base_uint &b) const
Definition: arith_uint256.cpp:111
uint_error(const std::string &str)
Definition: arith_uint256.h:21
base_uint & operator^=(uint64_t b)
Definition: arith_uint256.h:117
friend uint256 ArithToUint256(const arith_uint256 &)
Definition: arith_uint256.cpp:248
Definition: arith_uint256.h:252
Definition: uint256.h:121
base_uint & operator++()
Definition: arith_uint256.h:172
friend bool operator!=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:220
uint64_t GetLow64() const
Definition: arith_uint256.h:244
friend const base_uint operator|(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:213
friend const base_uint operator *(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:211
const base_uint operator-() const
Definition: arith_uint256.h:76
arith_uint256(uint64_t b)
Definition: arith_uint256.h:256
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:87
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
Definition: arith_uint256.cpp:205
const base_uint operator--(int)
Definition: arith_uint256.h:198
std::string GetHex() const
Definition: arith_uint256.cpp:149
const base_uint operator++(int)
Definition: arith_uint256.h:181
friend const base_uint operator<<(const base_uint &a, int shift)
Definition: arith_uint256.h:217
friend bool operator>=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:223
arith_uint256(const base_uint< 256 > &b)
Definition: arith_uint256.h:255
arith_uint256 UintToArith256(const uint256 &)
Definition: arith_uint256.cpp:255
unsigned int size() const
Definition: arith_uint256.h:233
double getdouble() const
Definition: arith_uint256.cpp:137
friend bool operator==(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:225
friend const base_uint operator>>(const base_uint &a, int shift)
Definition: arith_uint256.h:216
friend arith_uint256 UintToArith256(const uint256 &)
Definition: arith_uint256.cpp:255
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:96
base_uint()
Definition: arith_uint256.h:33
base_uint & operator>>=(unsigned int shift)
Definition: arith_uint256.cpp:40
unsigned int bits() const
Definition: arith_uint256.cpp:173