Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / arith_uint256.h
blobdc2627592e64cd7a97f057eb7252bbbf3b8fa037
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.
6 #ifndef BITCOIN_ARITH_UINT256_H
7 #define BITCOIN_ARITH_UINT256_H
9 #include <assert.h>
10 #include <cstring>
11 #include <stdexcept>
12 #include <stdint.h>
13 #include <string>
14 #include <vector>
16 class uint256;
18 class uint_error : public std::runtime_error {
19 public:
20 explicit uint_error(const std::string& str) : std::runtime_error(str) {}
23 /** Template base class for unsigned big integers. */
24 template<unsigned int BITS>
25 class base_uint
27 protected:
28 static constexpr int WIDTH = BITS / 32;
29 uint32_t pn[WIDTH];
30 public:
32 base_uint()
34 static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
36 for (int i = 0; i < WIDTH; i++)
37 pn[i] = 0;
40 base_uint(const base_uint& b)
42 static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
44 for (int i = 0; i < WIDTH; i++)
45 pn[i] = b.pn[i];
48 base_uint& operator=(const base_uint& b)
50 for (int i = 0; i < WIDTH; i++)
51 pn[i] = b.pn[i];
52 return *this;
55 base_uint(uint64_t b)
57 static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
59 pn[0] = (unsigned int)b;
60 pn[1] = (unsigned int)(b >> 32);
61 for (int i = 2; i < WIDTH; i++)
62 pn[i] = 0;
65 explicit base_uint(const std::string& str);
67 bool operator!() const
69 for (int i = 0; i < WIDTH; i++)
70 if (pn[i] != 0)
71 return false;
72 return true;
75 const base_uint operator~() const
77 base_uint ret;
78 for (int i = 0; i < WIDTH; i++)
79 ret.pn[i] = ~pn[i];
80 return ret;
83 const base_uint operator-() const
85 base_uint ret;
86 for (int i = 0; i < WIDTH; i++)
87 ret.pn[i] = ~pn[i];
88 ret++;
89 return ret;
92 double getdouble() const;
94 base_uint& operator=(uint64_t b)
96 pn[0] = (unsigned int)b;
97 pn[1] = (unsigned int)(b >> 32);
98 for (int i = 2; i < WIDTH; i++)
99 pn[i] = 0;
100 return *this;
103 base_uint& operator^=(const base_uint& b)
105 for (int i = 0; i < WIDTH; i++)
106 pn[i] ^= b.pn[i];
107 return *this;
110 base_uint& operator&=(const base_uint& b)
112 for (int i = 0; i < WIDTH; i++)
113 pn[i] &= b.pn[i];
114 return *this;
117 base_uint& operator|=(const base_uint& b)
119 for (int i = 0; i < WIDTH; i++)
120 pn[i] |= b.pn[i];
121 return *this;
124 base_uint& operator^=(uint64_t b)
126 pn[0] ^= (unsigned int)b;
127 pn[1] ^= (unsigned int)(b >> 32);
128 return *this;
131 base_uint& operator|=(uint64_t b)
133 pn[0] |= (unsigned int)b;
134 pn[1] |= (unsigned int)(b >> 32);
135 return *this;
138 base_uint& operator<<=(unsigned int shift);
139 base_uint& operator>>=(unsigned int shift);
141 base_uint& operator+=(const base_uint& b)
143 uint64_t carry = 0;
144 for (int i = 0; i < WIDTH; i++)
146 uint64_t n = carry + pn[i] + b.pn[i];
147 pn[i] = n & 0xffffffff;
148 carry = n >> 32;
150 return *this;
153 base_uint& operator-=(const base_uint& b)
155 *this += -b;
156 return *this;
159 base_uint& operator+=(uint64_t b64)
161 base_uint b;
162 b = b64;
163 *this += b;
164 return *this;
167 base_uint& operator-=(uint64_t b64)
169 base_uint b;
170 b = b64;
171 *this += -b;
172 return *this;
175 base_uint& operator*=(uint32_t b32);
176 base_uint& operator*=(const base_uint& b);
177 base_uint& operator/=(const base_uint& b);
179 base_uint& operator++()
181 // prefix operator
182 int i = 0;
183 while (i < WIDTH && ++pn[i] == 0)
184 i++;
185 return *this;
188 const base_uint operator++(int)
190 // postfix operator
191 const base_uint ret = *this;
192 ++(*this);
193 return ret;
196 base_uint& operator--()
198 // prefix operator
199 int i = 0;
200 while (i < WIDTH && --pn[i] == (uint32_t)-1)
201 i++;
202 return *this;
205 const base_uint operator--(int)
207 // postfix operator
208 const base_uint ret = *this;
209 --(*this);
210 return ret;
213 int CompareTo(const base_uint& b) const;
214 bool EqualTo(uint64_t b) const;
216 friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
217 friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
218 friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
219 friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
220 friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
221 friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
222 friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
223 friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
224 friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
225 friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
226 friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
227 friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
228 friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
229 friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
230 friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
231 friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
232 friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
233 friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
235 std::string GetHex() const;
236 void SetHex(const char* psz);
237 void SetHex(const std::string& str);
238 std::string ToString() const;
240 unsigned int size() const
242 return sizeof(pn);
246 * Returns the position of the highest bit set plus one, or zero if the
247 * value is zero.
249 unsigned int bits() const;
251 uint64_t GetLow64() const
253 static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
254 return pn[0] | (uint64_t)pn[1] << 32;
258 /** 256-bit unsigned big integer. */
259 class arith_uint256 : public base_uint<256> {
260 public:
261 arith_uint256() {}
262 arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
263 arith_uint256(uint64_t b) : base_uint<256>(b) {}
264 explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
267 * The "compact" format is a representation of a whole
268 * number N using an unsigned 32bit number similar to a
269 * floating point format.
270 * The most significant 8 bits are the unsigned exponent of base 256.
271 * This exponent can be thought of as "number of bytes of N".
272 * The lower 23 bits are the mantissa.
273 * Bit number 24 (0x800000) represents the sign of N.
274 * N = (-1^sign) * mantissa * 256^(exponent-3)
276 * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
277 * MPI uses the most significant bit of the first byte as sign.
278 * Thus 0x1234560000 is compact (0x05123456)
279 * and 0xc0de000000 is compact (0x0600c0de)
281 * Bitcoin only uses this "compact" format for encoding difficulty
282 * targets, which are unsigned 256bit quantities. Thus, all the
283 * complexities of the sign bit and using base 256 are probably an
284 * implementation accident.
286 arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
287 uint32_t GetCompact(bool fNegative = false) const;
289 friend uint256 ArithToUint256(const arith_uint256 &);
290 friend arith_uint256 UintToArith256(const uint256 &);
293 uint256 ArithToUint256(const arith_uint256 &);
294 arith_uint256 UintToArith256(const uint256 &);
296 #endif // BITCOIN_ARITH_UINT256_H