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