1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 #include "arith_uint256.h"
9 #include "utilstrencodings.h"
10 #include "crypto/common.h"
15 template <unsigned int BITS
>
16 base_uint
<BITS
>::base_uint(const std::string
& str
)
18 static_assert(BITS
/32 > 0 && BITS
%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
23 template <unsigned int BITS
>
24 base_uint
<BITS
>& base_uint
<BITS
>::operator<<=(unsigned int shift
)
26 base_uint
<BITS
> a(*this);
27 for (int i
= 0; i
< WIDTH
; i
++)
31 for (int i
= 0; i
< WIDTH
; i
++) {
32 if (i
+ k
+ 1 < WIDTH
&& shift
!= 0)
33 pn
[i
+ k
+ 1] |= (a
.pn
[i
] >> (32 - shift
));
35 pn
[i
+ k
] |= (a
.pn
[i
] << shift
);
40 template <unsigned int BITS
>
41 base_uint
<BITS
>& base_uint
<BITS
>::operator>>=(unsigned int shift
)
43 base_uint
<BITS
> a(*this);
44 for (int i
= 0; i
< WIDTH
; i
++)
48 for (int i
= 0; i
< WIDTH
; i
++) {
49 if (i
- k
- 1 >= 0 && shift
!= 0)
50 pn
[i
- k
- 1] |= (a
.pn
[i
] << (32 - shift
));
52 pn
[i
- k
] |= (a
.pn
[i
] >> shift
);
57 template <unsigned int BITS
>
58 base_uint
<BITS
>& base_uint
<BITS
>::operator*=(uint32_t b32
)
61 for (int i
= 0; i
< WIDTH
; i
++) {
62 uint64_t n
= carry
+ (uint64_t)b32
* pn
[i
];
63 pn
[i
] = n
& 0xffffffff;
69 template <unsigned int BITS
>
70 base_uint
<BITS
>& base_uint
<BITS
>::operator*=(const base_uint
& b
)
72 base_uint
<BITS
> a
= *this;
74 for (int j
= 0; j
< WIDTH
; j
++) {
76 for (int i
= 0; i
+ j
< WIDTH
; i
++) {
77 uint64_t n
= carry
+ pn
[i
+ j
] + (uint64_t)a
.pn
[j
] * b
.pn
[i
];
78 pn
[i
+ j
] = n
& 0xffffffff;
85 template <unsigned int BITS
>
86 base_uint
<BITS
>& base_uint
<BITS
>::operator/=(const base_uint
& b
)
88 base_uint
<BITS
> div
= b
; // make a copy, so we can shift.
89 base_uint
<BITS
> num
= *this; // make a copy, so we can subtract.
90 *this = 0; // the quotient.
91 int num_bits
= num
.bits();
92 int div_bits
= div
.bits();
94 throw uint_error("Division by zero");
95 if (div_bits
> num_bits
) // the result is certainly 0.
97 int shift
= num_bits
- div_bits
;
98 div
<<= shift
; // shift so that div and num align.
102 pn
[shift
/ 32] |= (1 << (shift
& 31)); // set a bit of the result.
104 div
>>= 1; // shift back.
107 // num now contains the remainder of the division.
111 template <unsigned int BITS
>
112 int base_uint
<BITS
>::CompareTo(const base_uint
<BITS
>& b
) const
114 for (int i
= WIDTH
- 1; i
>= 0; i
--) {
123 template <unsigned int BITS
>
124 bool base_uint
<BITS
>::EqualTo(uint64_t b
) const
126 for (int i
= WIDTH
- 1; i
>= 2; i
--) {
130 if (pn
[1] != (b
>> 32))
132 if (pn
[0] != (b
& 0xfffffffful
))
137 template <unsigned int BITS
>
138 double base_uint
<BITS
>::getdouble() const
142 for (int i
= 0; i
< WIDTH
; i
++) {
144 fact
*= 4294967296.0;
149 template <unsigned int BITS
>
150 std::string base_uint
<BITS
>::GetHex() const
152 return ArithToUint256(*this).GetHex();
155 template <unsigned int BITS
>
156 void base_uint
<BITS
>::SetHex(const char* psz
)
158 *this = UintToArith256(uint256S(psz
));
161 template <unsigned int BITS
>
162 void base_uint
<BITS
>::SetHex(const std::string
& str
)
167 template <unsigned int BITS
>
168 std::string base_uint
<BITS
>::ToString() const
173 template <unsigned int BITS
>
174 unsigned int base_uint
<BITS
>::bits() const
176 for (int pos
= WIDTH
- 1; pos
>= 0; pos
--) {
178 for (int nbits
= 31; nbits
> 0; nbits
--) {
179 if (pn
[pos
] & 1 << nbits
)
180 return 32 * pos
+ nbits
+ 1;
188 // Explicit instantiations for base_uint<256>
189 template base_uint
<256>::base_uint(const std::string
&);
190 template base_uint
<256>& base_uint
<256>::operator<<=(unsigned int);
191 template base_uint
<256>& base_uint
<256>::operator>>=(unsigned int);
192 template base_uint
<256>& base_uint
<256>::operator*=(uint32_t b32
);
193 template base_uint
<256>& base_uint
<256>::operator*=(const base_uint
<256>& b
);
194 template base_uint
<256>& base_uint
<256>::operator/=(const base_uint
<256>& b
);
195 template int base_uint
<256>::CompareTo(const base_uint
<256>&) const;
196 template bool base_uint
<256>::EqualTo(uint64_t) const;
197 template double base_uint
<256>::getdouble() const;
198 template std::string base_uint
<256>::GetHex() const;
199 template std::string base_uint
<256>::ToString() const;
200 template void base_uint
<256>::SetHex(const char*);
201 template void base_uint
<256>::SetHex(const std::string
&);
202 template unsigned int base_uint
<256>::bits() const;
204 // This implementation directly uses shifts instead of going
205 // through an intermediate MPI representation.
206 arith_uint256
& arith_uint256::SetCompact(uint32_t nCompact
, bool* pfNegative
, bool* pfOverflow
)
208 int nSize
= nCompact
>> 24;
209 uint32_t nWord
= nCompact
& 0x007fffff;
211 nWord
>>= 8 * (3 - nSize
);
215 *this <<= 8 * (nSize
- 3);
218 *pfNegative
= nWord
!= 0 && (nCompact
& 0x00800000) != 0;
220 *pfOverflow
= nWord
!= 0 && ((nSize
> 34) ||
221 (nWord
> 0xff && nSize
> 33) ||
222 (nWord
> 0xffff && nSize
> 32));
226 uint32_t arith_uint256::GetCompact(bool fNegative
) const
228 int nSize
= (bits() + 7) / 8;
229 uint32_t nCompact
= 0;
231 nCompact
= GetLow64() << 8 * (3 - nSize
);
233 arith_uint256 bn
= *this >> 8 * (nSize
- 3);
234 nCompact
= bn
.GetLow64();
236 // The 0x00800000 bit denotes the sign.
237 // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
238 if (nCompact
& 0x00800000) {
242 assert((nCompact
& ~0x007fffff) == 0);
244 nCompact
|= nSize
<< 24;
245 nCompact
|= (fNegative
&& (nCompact
& 0x007fffff) ? 0x00800000 : 0);
249 uint256
ArithToUint256(const arith_uint256
&a
)
252 for(int x
=0; x
<a
.WIDTH
; ++x
)
253 WriteLE32(b
.begin() + x
*4, a
.pn
[x
]);
256 arith_uint256
UintToArith256(const uint256
&a
)
259 for(int x
=0; x
<b
.WIDTH
; ++x
)
260 b
.pn
[x
] = ReadLE32(a
.begin() + x
*4);