1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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_UINT256_H
7 #define BITCOIN_UINT256_H
16 class uint_error : public std::runtime_error {
18 explicit uint_error(const std::string& str) : std::runtime_error(str) {}
21 /** Template base class for unsigned big integers. */
22 template<unsigned int BITS>
26 enum { WIDTH=BITS/32 };
32 for (int i = 0; i < WIDTH; i++)
36 base_uint(const base_uint& b)
38 for (int i = 0; i < WIDTH; i++)
42 base_uint& operator=(const base_uint& b)
44 for (int i = 0; i < WIDTH; i++)
51 pn[0] = (unsigned int)b;
52 pn[1] = (unsigned int)(b >> 32);
53 for (int i = 2; i < WIDTH; i++)
57 explicit base_uint(const std::string& str);
58 explicit base_uint(const std::vector<unsigned char>& vch);
60 bool operator!() const
62 for (int i = 0; i < WIDTH; i++)
68 const base_uint operator~() const
71 for (int i = 0; i < WIDTH; i++)
76 const base_uint operator-() const
79 for (int i = 0; i < WIDTH; i++)
85 double getdouble() const;
87 base_uint& operator=(uint64_t b)
89 pn[0] = (unsigned int)b;
90 pn[1] = (unsigned int)(b >> 32);
91 for (int i = 2; i < WIDTH; i++)
96 base_uint& operator^=(const base_uint& b)
98 for (int i = 0; i < WIDTH; i++)
103 base_uint& operator&=(const base_uint& b)
105 for (int i = 0; i < WIDTH; i++)
110 base_uint& operator|=(const base_uint& b)
112 for (int i = 0; i < WIDTH; i++)
117 base_uint& operator^=(uint64_t b)
119 pn[0] ^= (unsigned int)b;
120 pn[1] ^= (unsigned int)(b >> 32);
124 base_uint& operator|=(uint64_t b)
126 pn[0] |= (unsigned int)b;
127 pn[1] |= (unsigned int)(b >> 32);
131 base_uint& operator<<=(unsigned int shift);
132 base_uint& operator>>=(unsigned int shift);
134 base_uint& operator+=(const base_uint& b)
137 for (int i = 0; i < WIDTH; i++)
139 uint64_t n = carry + pn[i] + b.pn[i];
140 pn[i] = n & 0xffffffff;
146 base_uint& operator-=(const base_uint& b)
152 base_uint& operator+=(uint64_t b64)
160 base_uint& operator-=(uint64_t b64)
168 base_uint& operator*=(uint32_t b32);
169 base_uint& operator*=(const base_uint& b);
170 base_uint& operator/=(const base_uint& b);
172 base_uint& operator++()
176 while (++pn[i] == 0 && i < WIDTH-1)
181 const base_uint operator++(int)
184 const base_uint ret = *this;
189 base_uint& operator--()
193 while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
198 const base_uint operator--(int)
201 const base_uint ret = *this;
206 int CompareTo(const base_uint& b) const;
207 bool EqualTo(uint64_t b) const;
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); }
228 std::string GetHex() const;
229 void SetHex(const char* psz);
230 void SetHex(const std::string& str);
231 std::string ToString() const;
233 unsigned char* begin()
235 return (unsigned char*)&pn[0];
240 return (unsigned char*)&pn[WIDTH];
243 const unsigned char* begin() const
245 return (unsigned char*)&pn[0];
248 const unsigned char* end() const
250 return (unsigned char*)&pn[WIDTH];
253 unsigned int size() const
259 * Returns the position of the highest bit set plus one, or zero if the
262 unsigned int bits() const;
264 uint64_t GetLow64() const
267 return pn[0] | (uint64_t)pn[1] << 32;
270 unsigned int GetSerializeSize(int nType, int nVersion) const
275 template<typename Stream>
276 void Serialize(Stream& s, int nType, int nVersion) const
278 s.write((char*)pn, sizeof(pn));
281 template<typename Stream>
282 void Unserialize(Stream& s, int nType, int nVersion)
284 s.read((char*)pn, sizeof(pn));
288 /** 160-bit unsigned big integer. */
289 class uint160 : public base_uint<160> {
292 uint160(const base_uint<160>& b) : base_uint<160>(b) {}
293 uint160(uint64_t b) : base_uint<160>(b) {}
294 explicit uint160(const std::string& str) : base_uint<160>(str) {}
295 explicit uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
298 /** 256-bit unsigned big integer. */
299 class uint256 : public base_uint<256> {
302 uint256(const base_uint<256>& b) : base_uint<256>(b) {}
303 uint256(uint64_t b) : base_uint<256>(b) {}
304 explicit uint256(const std::string& str) : base_uint<256>(str) {}
305 explicit uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
308 * The "compact" format is a representation of a whole
309 * number N using an unsigned 32bit number similar to a
310 * floating point format.
311 * The most significant 8 bits are the unsigned exponent of base 256.
312 * This exponent can be thought of as "number of bytes of N".
313 * The lower 23 bits are the mantissa.
314 * Bit number 24 (0x800000) represents the sign of N.
315 * N = (-1^sign) * mantissa * 256^(exponent-3)
317 * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
318 * MPI uses the most significant bit of the first byte as sign.
319 * Thus 0x1234560000 is compact (0x05123456)
320 * and 0xc0de000000 is compact (0x0600c0de)
322 * Bitcoin only uses this "compact" format for encoding difficulty
323 * targets, which are unsigned 256bit quantities. Thus, all the
324 * complexities of the sign bit and using base 256 are probably an
325 * implementation accident.
327 uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
328 uint32_t GetCompact(bool fNegative = false) const;
330 uint64_t GetHash(const uint256& salt) const;
333 #endif // BITCOIN_UINT256_H