1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 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
15 class uint_error : public std::runtime_error {
17 explicit uint_error(const std::string& str) : std::runtime_error(str) {}
20 /** Template base class for unsigned big integers. */
21 template<unsigned int BITS>
25 enum { WIDTH=BITS/32 };
31 for (int i = 0; i < WIDTH; i++)
35 base_uint(const base_uint& b)
37 for (int i = 0; i < WIDTH; i++)
41 base_uint& operator=(const base_uint& b)
43 for (int i = 0; i < WIDTH; i++)
50 pn[0] = (unsigned int)b;
51 pn[1] = (unsigned int)(b >> 32);
52 for (int i = 2; i < WIDTH; i++)
56 explicit base_uint(const std::string& str);
57 explicit base_uint(const std::vector<unsigned char>& vch);
59 bool operator!() const
61 for (int i = 0; i < WIDTH; i++)
67 const base_uint operator~() const
70 for (int i = 0; i < WIDTH; i++)
75 const base_uint operator-() const
78 for (int i = 0; i < WIDTH; i++)
84 double getdouble() const;
86 base_uint& operator=(uint64_t b)
88 pn[0] = (unsigned int)b;
89 pn[1] = (unsigned int)(b >> 32);
90 for (int i = 2; i < WIDTH; i++)
95 base_uint& operator^=(const base_uint& b)
97 for (int i = 0; i < WIDTH; i++)
102 base_uint& operator&=(const base_uint& b)
104 for (int i = 0; i < WIDTH; i++)
109 base_uint& operator|=(const base_uint& b)
111 for (int i = 0; i < WIDTH; i++)
116 base_uint& operator^=(uint64_t b)
118 pn[0] ^= (unsigned int)b;
119 pn[1] ^= (unsigned int)(b >> 32);
123 base_uint& operator|=(uint64_t b)
125 pn[0] |= (unsigned int)b;
126 pn[1] |= (unsigned int)(b >> 32);
130 base_uint& operator<<=(unsigned int shift);
131 base_uint& operator>>=(unsigned int shift);
133 base_uint& operator+=(const base_uint& b)
136 for (int i = 0; i < WIDTH; i++)
138 uint64_t n = carry + pn[i] + b.pn[i];
139 pn[i] = n & 0xffffffff;
145 base_uint& operator-=(const base_uint& b)
151 base_uint& operator+=(uint64_t b64)
159 base_uint& operator-=(uint64_t b64)
167 base_uint& operator*=(uint32_t b32);
168 base_uint& operator*=(const base_uint& b);
169 base_uint& operator/=(const base_uint& b);
171 base_uint& operator++()
175 while (++pn[i] == 0 && i < WIDTH-1)
180 const base_uint operator++(int)
183 const base_uint ret = *this;
188 base_uint& operator--()
192 while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
197 const base_uint operator--(int)
200 const base_uint ret = *this;
205 int CompareTo(const base_uint& b) const;
206 bool EqualTo(uint64_t b) const;
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, const base_uint& b) { return base_uint(a) ^= b; }
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, int shift) { return base_uint(a) <<= shift; }
217 friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
218 friend inline bool operator==(const base_uint& a, const base_uint& b) { return a.CompareTo(b) == 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, const base_uint& b) { return a.CompareTo(b) <= 0; }
224 friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
225 friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
227 std::string GetHex() const;
228 void SetHex(const char* psz);
229 void SetHex(const std::string& str);
230 std::string ToString() const;
232 unsigned char* begin()
234 return (unsigned char*)&pn[0];
239 return (unsigned char*)&pn[WIDTH];
242 const unsigned char* begin() const
244 return (unsigned char*)&pn[0];
247 const unsigned char* end() const
249 return (unsigned char*)&pn[WIDTH];
252 unsigned int size() const
257 // Returns the position of the highest bit set plus one, or zero if the
259 unsigned int bits() const;
261 uint64_t GetLow64() const
264 return pn[0] | (uint64_t)pn[1] << 32;
267 unsigned int GetSerializeSize(int nType, int nVersion) const
272 template<typename Stream>
273 void Serialize(Stream& s, int nType, int nVersion) const
275 s.write((char*)pn, sizeof(pn));
278 template<typename Stream>
279 void Unserialize(Stream& s, int nType, int nVersion)
281 s.read((char*)pn, sizeof(pn));
285 /** 160-bit unsigned big integer. */
286 class uint160 : public base_uint<160> {
289 uint160(const base_uint<160>& b) : base_uint<160>(b) {}
290 uint160(uint64_t b) : base_uint<160>(b) {}
291 explicit uint160(const std::string& str) : base_uint<160>(str) {}
292 explicit uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
295 /** 256-bit unsigned big integer. */
296 class uint256 : public base_uint<256> {
299 uint256(const base_uint<256>& b) : base_uint<256>(b) {}
300 uint256(uint64_t b) : base_uint<256>(b) {}
301 explicit uint256(const std::string& str) : base_uint<256>(str) {}
302 explicit uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
304 // The "compact" format is a representation of a whole
305 // number N using an unsigned 32bit number similar to a
306 // floating point format.
307 // The most significant 8 bits are the unsigned exponent of base 256.
308 // This exponent can be thought of as "number of bytes of N".
309 // The lower 23 bits are the mantissa.
310 // Bit number 24 (0x800000) represents the sign of N.
311 // N = (-1^sign) * mantissa * 256^(exponent-3)
313 // Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
314 // MPI uses the most significant bit of the first byte as sign.
315 // Thus 0x1234560000 is compact (0x05123456)
316 // and 0xc0de000000 is compact (0x0600c0de)
317 // (0x05c0de00) would be -0x40de000000
319 // Bitcoin only uses this "compact" format for encoding difficulty
320 // targets, which are unsigned 256bit quantities. Thus, all the
321 // complexities of the sign bit and using base 256 are probably an
322 // implementation accident.
323 uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
324 uint32_t GetCompact(bool fNegative = false) const;
326 uint64_t GetHash(const uint256& salt) const;