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
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 static constexpr int WIDTH
= BITS
/ 32;
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
++)
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
++)
48 base_uint
& operator=(const base_uint
& b
)
50 for (int i
= 0; i
< WIDTH
; i
++)
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
++)
65 explicit base_uint(const std::string
& str
);
67 bool operator!() const
69 for (int i
= 0; i
< WIDTH
; i
++)
75 const base_uint
operator~() const
78 for (int i
= 0; i
< WIDTH
; i
++)
83 const base_uint
operator-() const
86 for (int i
= 0; i
< WIDTH
; i
++)
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
++)
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|=(const base_uint
& b
)
119 for (int i
= 0; i
< WIDTH
; i
++)
124 base_uint
& operator^=(uint64_t b
)
126 pn
[0] ^= (unsigned int)b
;
127 pn
[1] ^= (unsigned int)(b
>> 32);
131 base_uint
& operator|=(uint64_t b
)
133 pn
[0] |= (unsigned int)b
;
134 pn
[1] |= (unsigned int)(b
>> 32);
138 base_uint
& operator<<=(unsigned int shift
);
139 base_uint
& operator>>=(unsigned int shift
);
141 base_uint
& operator+=(const base_uint
& b
)
144 for (int i
= 0; i
< WIDTH
; i
++)
146 uint64_t n
= carry
+ pn
[i
] + b
.pn
[i
];
147 pn
[i
] = n
& 0xffffffff;
153 base_uint
& operator-=(const base_uint
& b
)
159 base_uint
& operator+=(uint64_t b64
)
167 base_uint
& operator-=(uint64_t b64
)
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++()
183 while (i
< WIDTH
&& ++pn
[i
] == 0)
188 const base_uint
operator++(int)
191 const base_uint ret
= *this;
196 base_uint
& operator--()
200 while (i
< WIDTH
&& --pn
[i
] == (uint32_t)-1)
205 const base_uint
operator--(int)
208 const base_uint ret
= *this;
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
246 * Returns the position of the highest bit set plus one, or zero if the
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> {
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