1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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
);
61 bool operator!() const
63 for (int i
= 0; i
< WIDTH
; i
++)
69 const base_uint
operator~() const
72 for (int i
= 0; i
< WIDTH
; i
++)
77 const base_uint
operator-() const
80 for (int i
= 0; i
< WIDTH
; i
++)
86 double getdouble() const;
88 base_uint
& operator=(uint64_t b
)
90 pn
[0] = (unsigned int)b
;
91 pn
[1] = (unsigned int)(b
>> 32);
92 for (int i
= 2; i
< WIDTH
; i
++)
97 base_uint
& operator^=(const base_uint
& b
)
99 for (int i
= 0; i
< WIDTH
; i
++)
104 base_uint
& operator&=(const base_uint
& b
)
106 for (int i
= 0; i
< WIDTH
; i
++)
111 base_uint
& operator|=(const base_uint
& b
)
113 for (int i
= 0; i
< WIDTH
; i
++)
118 base_uint
& operator^=(uint64_t b
)
120 pn
[0] ^= (unsigned int)b
;
121 pn
[1] ^= (unsigned int)(b
>> 32);
125 base_uint
& operator|=(uint64_t b
)
127 pn
[0] |= (unsigned int)b
;
128 pn
[1] |= (unsigned int)(b
>> 32);
132 base_uint
& operator<<=(unsigned int shift
);
133 base_uint
& operator>>=(unsigned int shift
);
135 base_uint
& operator+=(const base_uint
& b
)
138 for (int i
= 0; i
< WIDTH
; i
++)
140 uint64_t n
= carry
+ pn
[i
] + b
.pn
[i
];
141 pn
[i
] = n
& 0xffffffff;
147 base_uint
& operator-=(const base_uint
& b
)
153 base_uint
& operator+=(uint64_t b64
)
161 base_uint
& operator-=(uint64_t b64
)
169 base_uint
& operator*=(uint32_t b32
);
170 base_uint
& operator*=(const base_uint
& b
);
171 base_uint
& operator/=(const base_uint
& b
);
173 base_uint
& operator++()
177 while (++pn
[i
] == 0 && i
< WIDTH
-1)
182 const base_uint
operator++(int)
185 const base_uint ret
= *this;
190 base_uint
& operator--()
194 while (--pn
[i
] == (uint32_t)-1 && i
< WIDTH
-1)
199 const base_uint
operator--(int)
202 const base_uint ret
= *this;
207 int CompareTo(const base_uint
& b
) const;
208 bool EqualTo(uint64_t b
) const;
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
, const base_uint
& b
) { return base_uint(a
) ^= b
; }
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
, int shift
) { return base_uint(a
) <<= shift
; }
219 friend inline const base_uint
operator*(const base_uint
& a
, uint32_t b
) { return base_uint(a
) *= b
; }
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 memcmp(a
.pn
, b
.pn
, sizeof(a
.pn
)) != 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
, const base_uint
& b
) { return a
.CompareTo(b
) <= 0; }
226 friend inline bool operator==(const base_uint
& a
, uint64_t b
) { return a
.EqualTo(b
); }
227 friend inline bool operator!=(const base_uint
& a
, uint64_t b
) { return !a
.EqualTo(b
); }
229 std::string
GetHex() const;
230 void SetHex(const char* psz
);
231 void SetHex(const std::string
& str
);
232 std::string
ToString() const;
234 unsigned int size() const
240 * Returns the position of the highest bit set plus one, or zero if the
243 unsigned int bits() const;
245 uint64_t GetLow64() const
248 return pn
[0] | (uint64_t)pn
[1] << 32;
252 /** 256-bit unsigned big integer. */
253 class arith_uint256
: public base_uint
<256> {
256 arith_uint256(const base_uint
<256>& b
) : base_uint
<256>(b
) {}
257 arith_uint256(uint64_t b
) : base_uint
<256>(b
) {}
258 explicit arith_uint256(const std::string
& str
) : base_uint
<256>(str
) {}
261 * The "compact" format is a representation of a whole
262 * number N using an unsigned 32bit number similar to a
263 * floating point format.
264 * The most significant 8 bits are the unsigned exponent of base 256.
265 * This exponent can be thought of as "number of bytes of N".
266 * The lower 23 bits are the mantissa.
267 * Bit number 24 (0x800000) represents the sign of N.
268 * N = (-1^sign) * mantissa * 256^(exponent-3)
270 * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
271 * MPI uses the most significant bit of the first byte as sign.
272 * Thus 0x1234560000 is compact (0x05123456)
273 * and 0xc0de000000 is compact (0x0600c0de)
275 * Bitcoin only uses this "compact" format for encoding difficulty
276 * targets, which are unsigned 256bit quantities. Thus, all the
277 * complexities of the sign bit and using base 256 are probably an
278 * implementation accident.
280 arith_uint256
& SetCompact(uint32_t nCompact
, bool *pfNegative
= NULL
, bool *pfOverflow
= NULL
);
281 uint32_t GetCompact(bool fNegative
= false) const;
283 friend uint256
ArithToUint256(const arith_uint256
&);
284 friend arith_uint256
UintToArith256(const uint256
&);
287 uint256
ArithToUint256(const arith_uint256
&);
288 arith_uint256
UintToArith256(const uint256
&);
290 #endif // BITCOIN_ARITH_UINT256_H