1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin 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
18 class uint_error : public std::runtime_error {
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>
28 enum { WIDTH=BITS/32 };
34 for (int i = 0; i < WIDTH; i++)
38 base_uint(const base_uint& b)
40 for (int i = 0; i < WIDTH; i++)
44 base_uint& operator=(const base_uint& b)
46 for (int i = 0; i < WIDTH; i++)
53 pn[0] = (unsigned int)b;
54 pn[1] = (unsigned int)(b >> 32);
55 for (int i = 2; i < WIDTH; i++)
59 explicit base_uint(const std::string& str);
60 explicit base_uint(const std::vector<unsigned char>& vch);
62 bool operator!() const
64 for (int i = 0; i < WIDTH; i++)
70 const base_uint operator~() const
73 for (int i = 0; i < WIDTH; i++)
78 const base_uint operator-() const
81 for (int i = 0; i < WIDTH; i++)
87 double getdouble() const;
89 base_uint& operator=(uint64_t b)
91 pn[0] = (unsigned int)b;
92 pn[1] = (unsigned int)(b >> 32);
93 for (int i = 2; i < WIDTH; i++)
98 base_uint& operator^=(const base_uint& b)
100 for (int i = 0; i < WIDTH; i++)
105 base_uint& operator&=(const base_uint& b)
107 for (int i = 0; i < WIDTH; i++)
112 base_uint& operator|=(const base_uint& b)
114 for (int i = 0; i < WIDTH; i++)
119 base_uint& operator^=(uint64_t b)
121 pn[0] ^= (unsigned int)b;
122 pn[1] ^= (unsigned int)(b >> 32);
126 base_uint& operator|=(uint64_t b)
128 pn[0] |= (unsigned int)b;
129 pn[1] |= (unsigned int)(b >> 32);
133 base_uint& operator<<=(unsigned int shift);
134 base_uint& operator>>=(unsigned int shift);
136 base_uint& operator+=(const base_uint& b)
139 for (int i = 0; i < WIDTH; i++)
141 uint64_t n = carry + pn[i] + b.pn[i];
142 pn[i] = n & 0xffffffff;
148 base_uint& operator-=(const base_uint& b)
154 base_uint& operator+=(uint64_t b64)
162 base_uint& operator-=(uint64_t b64)
170 base_uint& operator*=(uint32_t b32);
171 base_uint& operator*=(const base_uint& b);
172 base_uint& operator/=(const base_uint& b);
174 base_uint& operator++()
178 while (++pn[i] == 0 && i < WIDTH-1)
183 const base_uint operator++(int)
186 const base_uint ret = *this;
191 base_uint& operator--()
195 while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
200 const base_uint operator--(int)
203 const base_uint ret = *this;
208 int CompareTo(const base_uint& b) const;
209 bool EqualTo(uint64_t b) const;
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, 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, int shift) { return base_uint(a) >>= shift; }
219 friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
220 friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
221 friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
222 friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 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, const base_uint& b) { return a.CompareTo(b) >= 0; }
226 friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
227 friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
228 friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
230 std::string GetHex() const;
231 void SetHex(const char* psz);
232 void SetHex(const std::string& str);
233 std::string ToString() const;
235 unsigned char* begin()
237 return (unsigned char*)&pn[0];
242 return (unsigned char*)&pn[WIDTH];
245 const unsigned char* begin() const
247 return (unsigned char*)&pn[0];
250 const unsigned char* end() const
252 return (unsigned char*)&pn[WIDTH];
255 unsigned int size() const
261 * Returns the position of the highest bit set plus one, or zero if the
264 unsigned int bits() const;
266 uint64_t GetLow64() const
269 return pn[0] | (uint64_t)pn[1] << 32;
272 unsigned int GetSerializeSize(int nType, int nVersion) const
277 template<typename Stream>
278 void Serialize(Stream& s, int nType, int nVersion) const
280 s.write((char*)pn, sizeof(pn));
283 template<typename Stream>
284 void Unserialize(Stream& s, int nType, int nVersion)
286 s.read((char*)pn, sizeof(pn));
289 // Temporary for migration to blob160/256
290 uint64_t GetCheapHash() const
296 memset(pn, 0, sizeof(pn));
300 for (int i = 0; i < WIDTH; i++)
307 /** 160-bit unsigned big integer. */
308 class arith_uint160 : public base_uint<160> {
311 arith_uint160(const base_uint<160>& b) : base_uint<160>(b) {}
312 arith_uint160(uint64_t b) : base_uint<160>(b) {}
313 explicit arith_uint160(const std::string& str) : base_uint<160>(str) {}
314 explicit arith_uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
317 /** 256-bit unsigned big integer. */
318 class arith_uint256 : public base_uint<256> {
321 arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
322 arith_uint256(uint64_t b) : base_uint<256>(b) {}
323 explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
324 explicit arith_uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
327 * The "compact" format is a representation of a whole
328 * number N using an unsigned 32bit number similar to a
329 * floating point format.
330 * The most significant 8 bits are the unsigned exponent of base 256.
331 * This exponent can be thought of as "number of bytes of N".
332 * The lower 23 bits are the mantissa.
333 * Bit number 24 (0x800000) represents the sign of N.
334 * N = (-1^sign) * mantissa * 256^(exponent-3)
336 * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
337 * MPI uses the most significant bit of the first byte as sign.
338 * Thus 0x1234560000 is compact (0x05123456)
339 * and 0xc0de000000 is compact (0x0600c0de)
341 * Bitcoin only uses this "compact" format for encoding difficulty
342 * targets, which are unsigned 256bit quantities. Thus, all the
343 * complexities of the sign bit and using base 256 are probably an
344 * implementation accident.
346 arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
347 uint32_t GetCompact(bool fNegative = false) const;
349 uint64_t GetHash(const arith_uint256& salt) const;
351 friend uint256 ArithToUint256(const arith_uint256 &);
352 friend arith_uint256 UintToArith256(const uint256 &);
355 uint256 ArithToUint256(const arith_uint256 &);
356 arith_uint256 UintToArith256(const uint256 &);
358 #endif // BITCOIN_UINT256_H