Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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.
5 : :
6 : : #ifndef BITCOIN_ARITH_UINT256_H
7 : : #define BITCOIN_ARITH_UINT256_H
8 : :
9 : : #include <cstdint>
10 : : #include <cstring>
11 : : #include <limits>
12 : : #include <stdexcept>
13 : : #include <string>
14 : :
15 : : class uint256;
16 : :
17 : : class uint_error : public std::runtime_error {
18 : : public:
19 : 110964 : explicit uint_error(const std::string& str) : std::runtime_error(str) {}
20 : : };
21 : :
22 : : /** Template base class for unsigned big integers. */
23 : : template<unsigned int BITS>
24 : : class base_uint
25 : : {
26 : : protected:
27 : : static_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");
28 : : static constexpr int WIDTH = BITS / 32;
29 : : uint32_t pn[WIDTH];
30 : : public:
31 : :
32 : 25726114 : base_uint()
33 : : {
34 [ + + ]: 231535026 : for (int i = 0; i < WIDTH; i++)
35 : 205808912 : pn[i] = 0;
36 : 25726114 : }
37 : :
38 : 65238978 : base_uint(const base_uint& b)
39 : : {
40 [ + + ]: 587150802 : for (int i = 0; i < WIDTH; i++)
41 : 521911824 : pn[i] = b.pn[i];
42 : 65238978 : }
43 : :
44 : 3132092 : base_uint& operator=(const base_uint& b)
45 : : {
46 [ + - ]: 3132092 : if (this != &b) {
47 [ + + ]: 28188828 : for (int i = 0; i < WIDTH; i++)
48 : 25056736 : pn[i] = b.pn[i];
49 : 3132092 : }
50 : 3132092 : return *this;
51 : : }
52 : :
53 : 4451284 : base_uint(uint64_t b)
54 : : {
55 : 4451284 : pn[0] = (unsigned int)b;
56 : 4451284 : pn[1] = (unsigned int)(b >> 32);
57 [ + + ]: 31158988 : for (int i = 2; i < WIDTH; i++)
58 : 26707704 : pn[i] = 0;
59 : 4451284 : }
60 : :
61 : 763240 : base_uint operator~() const
62 : : {
63 : 763240 : base_uint ret;
64 [ + + ]: 6869160 : for (int i = 0; i < WIDTH; i++)
65 : 6105920 : ret.pn[i] = ~pn[i];
66 : 763240 : return ret;
67 : : }
68 : :
69 : 16379772 : base_uint operator-() const
70 : : {
71 : 16379772 : base_uint ret;
72 [ + + ]: 147417948 : for (int i = 0; i < WIDTH; i++)
73 : 131038176 : ret.pn[i] = ~pn[i];
74 : 16379772 : ++ret;
75 : 16379772 : return ret;
76 : : }
77 : :
78 : : double getdouble() const;
79 : :
80 : 1152308 : base_uint& operator=(uint64_t b)
81 : : {
82 : 1152308 : pn[0] = (unsigned int)b;
83 : 1152308 : pn[1] = (unsigned int)(b >> 32);
84 [ + + ]: 8066156 : for (int i = 2; i < WIDTH; i++)
85 : 6913848 : pn[i] = 0;
86 : 1152308 : return *this;
87 : : }
88 : :
89 : 0 : base_uint& operator^=(const base_uint& b)
90 : : {
91 [ # # ]: 0 : for (int i = 0; i < WIDTH; i++)
92 : 0 : pn[i] ^= b.pn[i];
93 : 0 : return *this;
94 : : }
95 : :
96 : 0 : base_uint& operator&=(const base_uint& b)
97 : : {
98 [ # # ]: 0 : for (int i = 0; i < WIDTH; i++)
99 : 0 : pn[i] &= b.pn[i];
100 : 0 : return *this;
101 : : }
102 : :
103 : 0 : base_uint& operator|=(const base_uint& b)
104 : : {
105 [ # # ]: 0 : for (int i = 0; i < WIDTH; i++)
106 : 0 : pn[i] |= b.pn[i];
107 : 0 : return *this;
108 : : }
109 : :
110 : 0 : base_uint& operator^=(uint64_t b)
111 : : {
112 : 0 : pn[0] ^= (unsigned int)b;
113 : 0 : pn[1] ^= (unsigned int)(b >> 32);
114 : 0 : return *this;
115 : : }
116 : :
117 : 0 : base_uint& operator|=(uint64_t b)
118 : : {
119 : 0 : pn[0] |= (unsigned int)b;
120 : 0 : pn[1] |= (unsigned int)(b >> 32);
121 : 0 : return *this;
122 : : }
123 : :
124 : : base_uint& operator<<=(unsigned int shift);
125 : : base_uint& operator>>=(unsigned int shift);
126 : :
127 : 18578173 : base_uint& operator+=(const base_uint& b)
128 : : {
129 : 18578173 : uint64_t carry = 0;
130 [ + + ]: 167203557 : for (int i = 0; i < WIDTH; i++)
131 : : {
132 : 148625384 : uint64_t n = carry + pn[i] + b.pn[i];
133 : 148625384 : pn[i] = n & 0xffffffff;
134 : 148625384 : carry = n >> 32;
135 : 148625384 : }
136 : 18578173 : return *this;
137 : 18578173 : }
138 : :
139 : 16379772 : base_uint& operator-=(const base_uint& b)
140 : : {
141 : 16379772 : *this += -b;
142 : 16379772 : return *this;
143 : : }
144 : :
145 : 0 : base_uint& operator+=(uint64_t b64)
146 : : {
147 : 0 : base_uint b;
148 : 0 : b = b64;
149 : 0 : *this += b;
150 : 0 : return *this;
151 : 0 : }
152 : :
153 : 0 : base_uint& operator-=(uint64_t b64)
154 : : {
155 : 0 : base_uint b;
156 : 0 : b = b64;
157 : 0 : *this += -b;
158 : 0 : return *this;
159 : 0 : }
160 : :
161 : : base_uint& operator*=(uint32_t b32);
162 : : base_uint& operator*=(const base_uint& b);
163 : : base_uint& operator/=(const base_uint& b);
164 : :
165 : 16379772 : base_uint& operator++()
166 : : {
167 : : // prefix operator
168 : 16379772 : int i = 0;
169 [ + + + + ]: 49867936 : while (i < WIDTH && ++pn[i] == 0)
170 : 33488164 : i++;
171 : 16379772 : return *this;
172 : 16379772 : }
173 : :
174 : 0 : base_uint operator++(int)
175 : : {
176 : : // postfix operator
177 : 0 : const base_uint ret = *this;
178 : 0 : ++(*this);
179 : 0 : return ret;
180 : : }
181 : :
182 : 0 : base_uint& operator--()
183 : : {
184 : : // prefix operator
185 : 0 : int i = 0;
186 [ # # # # ]: 0 : while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
187 : 0 : i++;
188 : 0 : return *this;
189 : 0 : }
190 : :
191 : 0 : base_uint operator--(int)
192 : : {
193 : : // postfix operator
194 : 0 : const base_uint ret = *this;
195 : 0 : --(*this);
196 : 0 : return ret;
197 : : }
198 : :
199 : : /** Numeric ordering (unlike \ref base_blob::Compare) */
200 : : int CompareTo(const base_uint& b) const;
201 : : bool EqualTo(uint64_t b) const;
202 : :
203 : 1863846 : friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
204 : 199553 : friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
205 : 199553 : friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
206 : 957214 : friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
207 : : friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
208 : : friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
209 : : friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
210 : 874129 : friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
211 : : friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
212 : 0 : friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
213 : : friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
214 : : friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
215 : 221800502 : friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
216 : 127259879 : friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
217 : 142302317 : friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
218 : 718 : friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
219 : 1731354 : friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
220 : : friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
221 : :
222 : : /** Hex encoding of the number (with the most significant digits first). */
223 : : std::string GetHex() const;
224 : : std::string ToString() const;
225 : :
226 : 265 : unsigned int size() const
227 : : {
228 : 265 : return sizeof(pn);
229 : : }
230 : :
231 : : /**
232 : : * Returns the position of the highest bit set plus one, or zero if the
233 : : * value is zero.
234 : : */
235 : : unsigned int bits() const;
236 : :
237 : 1047889 : uint64_t GetLow64() const
238 : : {
239 : : static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
240 : 1047889 : return pn[0] | (uint64_t)pn[1] << 32;
241 : : }
242 : : };
243 : :
244 : : /** 256-bit unsigned big integer. */
245 : : class arith_uint256 : public base_uint<256> {
246 : : public:
247 : 8383549 : arith_uint256() = default;
248 : 2260975 : arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
249 : 2720222 : arith_uint256(uint64_t b) : base_uint<256>(b) {}
250 : :
251 : : /**
252 : : * The "compact" format is a representation of a whole
253 : : * number N using an unsigned 32bit number similar to a
254 : : * floating point format.
255 : : * The most significant 8 bits are the unsigned exponent of base 256.
256 : : * This exponent can be thought of as "number of bytes of N".
257 : : * The lower 23 bits are the mantissa.
258 : : * Bit number 24 (0x800000) represents the sign of N.
259 : : * N = (-1^sign) * mantissa * 256^(exponent-3)
260 : : *
261 : : * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
262 : : * MPI uses the most significant bit of the first byte as sign.
263 : : * Thus 0x1234560000 is compact (0x05123456)
264 : : * and 0xc0de000000 is compact (0x0600c0de)
265 : : *
266 : : * Bitcoin only uses this "compact" format for encoding difficulty
267 : : * targets, which are unsigned 256bit quantities. Thus, all the
268 : : * complexities of the sign bit and using base 256 are probably an
269 : : * implementation accident.
270 : : */
271 : : arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
272 : : uint32_t GetCompact(bool fNegative = false) const;
273 : :
274 : : friend uint256 ArithToUint256(const arith_uint256 &);
275 : : friend arith_uint256 UintToArith256(const uint256 &);
276 : : };
277 : :
278 : : uint256 ArithToUint256(const arith_uint256 &);
279 : : arith_uint256 UintToArith256(const uint256 &);
280 : :
281 : : extern template class base_uint<256>;
282 : :
283 : : #endif // BITCOIN_ARITH_UINT256_H
|