/root/bitcoin/src/arith_uint256.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present 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 | | #include <arith_uint256.h> |
7 | | |
8 | | #include <crypto/common.h> |
9 | | #include <uint256.h> |
10 | | #include <util/overflow.h> |
11 | | |
12 | | #include <cassert> |
13 | | |
14 | | template <unsigned int BITS> |
15 | | base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift) |
16 | 81.1M | { |
17 | 81.1M | base_uint<BITS> a(*this); |
18 | 730M | for (int i = 0; i < WIDTH; i++649M ) |
19 | 649M | pn[i] = 0; |
20 | 81.1M | int k = shift / 32; |
21 | 81.1M | shift = shift % 32; |
22 | 730M | for (int i = 0; i < WIDTH; i++649M ) { |
23 | 649M | if (i + k + 1 < WIDTH && shift != 0283M ) |
24 | 283M | pn[i + k + 1] |= (a.pn[i] >> (32 - shift)); |
25 | 649M | if (i + k < WIDTH) |
26 | 365M | pn[i + k] |= (a.pn[i] << shift); |
27 | 649M | } |
28 | 81.1M | return *this; |
29 | 81.1M | } base_uint<256u>::operator<<=(unsigned int) Line | Count | Source | 16 | 81.1M | { | 17 | 81.1M | base_uint<BITS> a(*this); | 18 | 730M | for (int i = 0; i < WIDTH; i++649M ) | 19 | 649M | pn[i] = 0; | 20 | 81.1M | int k = shift / 32; | 21 | 81.1M | shift = shift % 32; | 22 | 730M | for (int i = 0; i < WIDTH; i++649M ) { | 23 | 649M | if (i + k + 1 < WIDTH && shift != 0283M ) | 24 | 283M | pn[i + k + 1] |= (a.pn[i] >> (32 - shift)); | 25 | 649M | if (i + k < WIDTH) | 26 | 365M | pn[i + k] |= (a.pn[i] << shift); | 27 | 649M | } | 28 | 81.1M | return *this; | 29 | 81.1M | } |
Unexecuted instantiation: base_uint<6144u>::operator<<=(unsigned int) |
30 | | |
31 | | template <unsigned int BITS> |
32 | | base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift) |
33 | 205M | { |
34 | 205M | base_uint<BITS> a(*this); |
35 | 1.84G | for (int i = 0; i < WIDTH; i++1.64G ) |
36 | 1.64G | pn[i] = 0; |
37 | 205M | int k = shift / 32; |
38 | 205M | shift = shift % 32; |
39 | 1.84G | for (int i = 0; i < WIDTH; i++1.64G ) { |
40 | 1.64G | if (i - k - 1 >= 0 && shift != 0567M ) |
41 | 567M | pn[i - k - 1] |= (a.pn[i] << (32 - shift)); |
42 | 1.64G | if (i - k >= 0) |
43 | 772M | pn[i - k] |= (a.pn[i] >> shift); |
44 | 1.64G | } |
45 | 205M | return *this; |
46 | 205M | } base_uint<256u>::operator>>=(unsigned int) Line | Count | Source | 33 | 205M | { | 34 | 205M | base_uint<BITS> a(*this); | 35 | 1.84G | for (int i = 0; i < WIDTH; i++1.64G ) | 36 | 1.64G | pn[i] = 0; | 37 | 205M | int k = shift / 32; | 38 | 205M | shift = shift % 32; | 39 | 1.84G | for (int i = 0; i < WIDTH; i++1.64G ) { | 40 | 1.64G | if (i - k - 1 >= 0 && shift != 0567M ) | 41 | 567M | pn[i - k - 1] |= (a.pn[i] << (32 - shift)); | 42 | 1.64G | if (i - k >= 0) | 43 | 772M | pn[i - k] |= (a.pn[i] >> shift); | 44 | 1.64G | } | 45 | 205M | return *this; | 46 | 205M | } |
Unexecuted instantiation: base_uint<6144u>::operator>>=(unsigned int) |
47 | | |
48 | | template <unsigned int BITS> |
49 | | base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32) |
50 | 99.8k | { |
51 | 99.8k | uint64_t carry = 0; |
52 | 898k | for (int i = 0; i < WIDTH; i++799k ) { |
53 | 799k | uint64_t n = carry + (uint64_t)b32 * pn[i]; |
54 | 799k | pn[i] = n & 0xffffffff; |
55 | 799k | carry = n >> 32; |
56 | 799k | } |
57 | 99.8k | return *this; |
58 | 99.8k | } |
59 | | |
60 | | template <unsigned int BITS> |
61 | | base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b) |
62 | 4.92M | { |
63 | 4.92M | base_uint<BITS> a; |
64 | 44.3M | for (int j = 0; j < WIDTH; j++39.4M ) { |
65 | 39.4M | uint64_t carry = 0; |
66 | 216M | for (int i = 0; i + j < WIDTH; i++177M ) { |
67 | 177M | uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i]; |
68 | 177M | a.pn[i + j] = n & 0xffffffff; |
69 | 177M | carry = n >> 32; |
70 | 177M | } |
71 | 39.4M | } |
72 | 4.92M | *this = a; |
73 | 4.92M | return *this; |
74 | 4.92M | } base_uint<256u>::operator*=(base_uint<256u> const&) Line | Count | Source | 62 | 4.92M | { | 63 | 4.92M | base_uint<BITS> a; | 64 | 44.3M | for (int j = 0; j < WIDTH; j++39.4M ) { | 65 | 39.4M | uint64_t carry = 0; | 66 | 216M | for (int i = 0; i + j < WIDTH; i++177M ) { | 67 | 177M | uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i]; | 68 | 177M | a.pn[i + j] = n & 0xffffffff; | 69 | 177M | carry = n >> 32; | 70 | 177M | } | 71 | 39.4M | } | 72 | 4.92M | *this = a; | 73 | 4.92M | return *this; | 74 | 4.92M | } |
Unexecuted instantiation: base_uint<6144u>::operator*=(base_uint<6144u> const&) |
75 | | |
76 | | template <unsigned int BITS> |
77 | | base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b) |
78 | 40.5M | { |
79 | 40.5M | base_uint<BITS> div = b; // make a copy, so we can shift. |
80 | 40.5M | base_uint<BITS> num = *this; // make a copy, so we can subtract. |
81 | 40.5M | *this = 0; // the quotient. |
82 | 40.5M | int num_bits = num.bits(); |
83 | 40.5M | int div_bits = div.bits(); |
84 | 40.5M | if (div_bits == 0) |
85 | 0 | throw uint_error("Division by zero"); |
86 | 40.5M | if (div_bits > num_bits) // the result is certainly 0. |
87 | 0 | return *this; |
88 | 40.5M | int shift = num_bits - div_bits; |
89 | 40.5M | div <<= shift; // shift so that div and num align. |
90 | 121M | while (shift >= 0) { |
91 | 81.1M | if (num >= div) { |
92 | 40.5M | num -= div; |
93 | 40.5M | pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result. |
94 | 40.5M | } |
95 | 81.1M | div >>= 1; // shift back. |
96 | 81.1M | shift--; |
97 | 81.1M | } |
98 | | // num now contains the remainder of the division. |
99 | 40.5M | return *this; |
100 | 40.5M | } base_uint<256u>::operator/=(base_uint<256u> const&) Line | Count | Source | 78 | 40.5M | { | 79 | 40.5M | base_uint<BITS> div = b; // make a copy, so we can shift. | 80 | 40.5M | base_uint<BITS> num = *this; // make a copy, so we can subtract. | 81 | 40.5M | *this = 0; // the quotient. | 82 | 40.5M | int num_bits = num.bits(); | 83 | 40.5M | int div_bits = div.bits(); | 84 | 40.5M | if (div_bits == 0) | 85 | 0 | throw uint_error("Division by zero"); | 86 | 40.5M | if (div_bits > num_bits) // the result is certainly 0. | 87 | 0 | return *this; | 88 | 40.5M | int shift = num_bits - div_bits; | 89 | 40.5M | div <<= shift; // shift so that div and num align. | 90 | 121M | while (shift >= 0) { | 91 | 81.1M | if (num >= div) { | 92 | 40.5M | num -= div; | 93 | 40.5M | pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result. | 94 | 40.5M | } | 95 | 81.1M | div >>= 1; // shift back. | 96 | 81.1M | shift--; | 97 | 81.1M | } | 98 | | // num now contains the remainder of the division. | 99 | 40.5M | return *this; | 100 | 40.5M | } |
Unexecuted instantiation: base_uint<6144u>::operator/=(base_uint<6144u> const&) |
101 | | |
102 | | template <unsigned int BITS> |
103 | | int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const |
104 | 82.5G | { |
105 | 661G | for (int i = WIDTH - 1; i >= 0; i--578G ) { |
106 | 660G | if (pn[i] < b.pn[i]) |
107 | 58.0G | return -1; |
108 | 602G | if (pn[i] > b.pn[i]) |
109 | 23.5G | return 1; |
110 | 602G | } |
111 | 993M | return 0; |
112 | 82.5G | } base_uint<256u>::CompareTo(base_uint<256u> const&) const Line | Count | Source | 104 | 82.5G | { | 105 | 661G | for (int i = WIDTH - 1; i >= 0; i--578G ) { | 106 | 660G | if (pn[i] < b.pn[i]) | 107 | 58.0G | return -1; | 108 | 602G | if (pn[i] > b.pn[i]) | 109 | 23.5G | return 1; | 110 | 602G | } | 111 | 993M | return 0; | 112 | 82.5G | } |
Unexecuted instantiation: base_uint<6144u>::CompareTo(base_uint<6144u> const&) const |
113 | | |
114 | | template <unsigned int BITS> |
115 | | bool base_uint<BITS>::EqualTo(uint64_t b) const |
116 | 40.5M | { |
117 | 40.5M | for (int i = WIDTH - 1; i >= 2; i--0 ) { |
118 | 40.5M | if (pn[i]) |
119 | 40.5M | return false; |
120 | 40.5M | } |
121 | 0 | if (pn[1] != (b >> 32)) |
122 | 0 | return false; |
123 | 0 | if (pn[0] != (b & 0xfffffffful)) |
124 | 0 | return false; |
125 | 0 | return true; |
126 | 0 | } |
127 | | |
128 | | template <unsigned int BITS> |
129 | | double base_uint<BITS>::getdouble() const |
130 | 30.2M | { |
131 | 30.2M | double ret = 0.0; |
132 | 30.2M | double fact = 1.0; |
133 | 271M | for (int i = 0; i < WIDTH; i++241M ) { |
134 | 241M | ret += fact * pn[i]; |
135 | 241M | fact *= 4294967296.0; |
136 | 241M | } |
137 | 30.2M | return ret; |
138 | 30.2M | } |
139 | | |
140 | | template <unsigned int BITS> |
141 | | std::string base_uint<BITS>::GetHex() const |
142 | 147k | { |
143 | 147k | base_blob<BITS> b; |
144 | 1.32M | for (int x = 0; x < this->WIDTH; ++x1.18M ) { |
145 | 1.18M | WriteLE32(b.begin() + x*4, this->pn[x]); |
146 | 1.18M | } |
147 | 147k | return b.GetHex(); |
148 | 147k | } |
149 | | |
150 | | template <unsigned int BITS> |
151 | | std::string base_uint<BITS>::ToString() const |
152 | 0 | { |
153 | 0 | return GetHex(); |
154 | 0 | } |
155 | | |
156 | | template <unsigned int BITS> |
157 | | unsigned int base_uint<BITS>::bits() const |
158 | 205M | { |
159 | 205M | for (int pos = WIDTH - 1; pos >= 0; pos--0 ) { |
160 | 205M | if (pn[pos]) { |
161 | 369M | for (int nbits = 31; nbits > 0; nbits--164M ) { |
162 | 369M | if (pn[pos] & 1U << nbits) |
163 | 205M | return 32 * pos + nbits + 1; |
164 | 369M | } |
165 | 0 | return 32 * pos + 1; |
166 | 205M | } |
167 | 205M | } |
168 | 0 | return 0; |
169 | 205M | } base_uint<256u>::bits() const Line | Count | Source | 158 | 205M | { | 159 | 205M | for (int pos = WIDTH - 1; pos >= 0; pos--0 ) { | 160 | 205M | if (pn[pos]) { | 161 | 369M | for (int nbits = 31; nbits > 0; nbits--164M ) { | 162 | 369M | if (pn[pos] & 1U << nbits) | 163 | 205M | return 32 * pos + nbits + 1; | 164 | 369M | } | 165 | 0 | return 32 * pos + 1; | 166 | 205M | } | 167 | 205M | } | 168 | 0 | return 0; | 169 | 205M | } |
Unexecuted instantiation: base_uint<6144u>::bits() const |
170 | | |
171 | | // Explicit instantiations for base_uint<256> |
172 | | template class base_uint<256>; |
173 | | |
174 | | // This implementation directly uses shifts instead of going |
175 | | // through an intermediate MPI representation. |
176 | | arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow) |
177 | 40.5M | { |
178 | 40.5M | int nSize = nCompact >> 24; |
179 | 40.5M | uint32_t nWord = nCompact & 0x007fffff; |
180 | 40.5M | if (nSize <= 3) { |
181 | 0 | nWord >>= 8 * (3 - nSize); |
182 | 0 | *this = nWord; |
183 | 40.5M | } else { |
184 | 40.5M | *this = nWord; |
185 | 40.5M | *this <<= 8 * (nSize - 3); |
186 | 40.5M | } |
187 | 40.5M | if (pfNegative) |
188 | 40.5M | *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0; |
189 | 40.5M | if (pfOverflow) |
190 | 40.5M | *pfOverflow = nWord != 0 && ((nSize > 34) || |
191 | 40.5M | (nWord > 0xff && nSize > 33) || |
192 | 40.5M | (nWord > 0xffff && nSize > 32)); |
193 | 40.5M | return *this; |
194 | 40.5M | } |
195 | | |
196 | | uint32_t arith_uint256::GetCompact(bool fNegative) const |
197 | 123M | { |
198 | 123M | int nSize = CeilDiv(bits(), 8u); |
199 | 123M | uint32_t nCompact = 0; |
200 | 123M | if (nSize <= 3) { |
201 | 0 | nCompact = GetLow64() << 8 * (3 - nSize); |
202 | 123M | } else { |
203 | 123M | arith_uint256 bn = *this >> 8 * (nSize - 3); |
204 | 123M | nCompact = bn.GetLow64(); |
205 | 123M | } |
206 | | // The 0x00800000 bit denotes the sign. |
207 | | // Thus, if it is already set, divide the mantissa by 256 and increase the exponent. |
208 | 123M | if (nCompact & 0x00800000) { |
209 | 0 | nCompact >>= 8; |
210 | 0 | nSize++; |
211 | 0 | } |
212 | 123M | assert((nCompact & ~0x007fffffU) == 0); |
213 | 123M | assert(nSize < 256); |
214 | 123M | nCompact |= nSize << 24; |
215 | 123M | nCompact |= (fNegative && (nCompact & 0x007fffff)0 ? 0x008000000 : 0); |
216 | 123M | return nCompact; |
217 | 123M | } |
218 | | |
219 | | uint256 ArithToUint256(const arith_uint256 &a) |
220 | 0 | { |
221 | 0 | uint256 b; |
222 | 0 | for(int x=0; x<a.WIDTH; ++x) |
223 | 0 | WriteLE32(b.begin() + x*4, a.pn[x]); |
224 | 0 | return b; |
225 | 0 | } |
226 | | arith_uint256 UintToArith256(const uint256 &a) |
227 | 124M | { |
228 | 124M | arith_uint256 b; |
229 | 1.11G | for(int x=0; x<b.WIDTH; ++x993M ) |
230 | 993M | b.pn[x] = ReadLE32(a.begin() + x*4); |
231 | 124M | return b; |
232 | 124M | } |
233 | | |
234 | | // Explicit instantiations for base_uint<6144> (used in test/fuzz/muhash.cpp). |
235 | | template base_uint<6144>& base_uint<6144>::operator*=(const base_uint<6144>& b); |
236 | | template base_uint<6144>& base_uint<6144>::operator/=(const base_uint<6144>& b); |