/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 | 3.29M | { |
16 | 3.29M | base_uint<BITS> a(*this); |
17 | 29.6M | for (int i = 0; i < WIDTH; i++26.3M ) |
18 | 26.3M | pn[i] = 0; |
19 | 3.29M | int k = shift / 32; |
20 | 3.29M | shift = shift % 32; |
21 | 29.6M | for (int i = 0; i < WIDTH; i++26.3M ) { |
22 | 26.3M | if (i + k + 1 < WIDTH && shift != 011.5M ) |
23 | 11.5M | pn[i + k + 1] |= (a.pn[i] >> (32 - shift)); |
24 | 26.3M | if (i + k < WIDTH) |
25 | 14.8M | pn[i + k] |= (a.pn[i] << shift); |
26 | 26.3M | } |
27 | 3.29M | return *this; |
28 | 3.29M | } _ZN9base_uintILj256EElSEj Line | Count | Source | 15 | 3.29M | { | 16 | 3.29M | base_uint<BITS> a(*this); | 17 | 29.6M | for (int i = 0; i < WIDTH; i++26.3M ) | 18 | 26.3M | pn[i] = 0; | 19 | 3.29M | int k = shift / 32; | 20 | 3.29M | shift = shift % 32; | 21 | 29.6M | for (int i = 0; i < WIDTH; i++26.3M ) { | 22 | 26.3M | if (i + k + 1 < WIDTH && shift != 011.5M ) | 23 | 11.5M | pn[i + k + 1] |= (a.pn[i] >> (32 - shift)); | 24 | 26.3M | if (i + k < WIDTH) | 25 | 14.8M | pn[i + k] |= (a.pn[i] << shift); | 26 | 26.3M | } | 27 | 3.29M | return *this; | 28 | 3.29M | } |
Unexecuted instantiation: _ZN9base_uintILj6144EElSEj |
29 | | |
30 | | template <unsigned int BITS> |
31 | | base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift) |
32 | 3.35M | { |
33 | 3.35M | base_uint<BITS> a(*this); |
34 | 30.1M | for (int i = 0; i < WIDTH; i++26.8M ) |
35 | 26.8M | pn[i] = 0; |
36 | 3.35M | int k = shift / 32; |
37 | 3.35M | shift = shift % 32; |
38 | 30.1M | for (int i = 0; i < WIDTH; i++26.8M ) { |
39 | 26.8M | if (i - k - 1 >= 0 && shift != 023.0M ) |
40 | 23.0M | pn[i - k - 1] |= (a.pn[i] << (32 - shift)); |
41 | 26.8M | if (i - k >= 0) |
42 | 26.4M | pn[i - k] |= (a.pn[i] >> shift); |
43 | 26.8M | } |
44 | 3.35M | return *this; |
45 | 3.35M | } _ZN9base_uintILj256EErSEj Line | Count | Source | 32 | 3.35M | { | 33 | 3.35M | base_uint<BITS> a(*this); | 34 | 30.1M | for (int i = 0; i < WIDTH; i++26.8M ) | 35 | 26.8M | pn[i] = 0; | 36 | 3.35M | int k = shift / 32; | 37 | 3.35M | shift = shift % 32; | 38 | 30.1M | for (int i = 0; i < WIDTH; i++26.8M ) { | 39 | 26.8M | if (i - k - 1 >= 0 && shift != 023.0M ) | 40 | 23.0M | pn[i - k - 1] |= (a.pn[i] << (32 - shift)); | 41 | 26.8M | if (i - k >= 0) | 42 | 26.4M | pn[i - k] |= (a.pn[i] >> shift); | 43 | 26.8M | } | 44 | 3.35M | return *this; | 45 | 3.35M | } |
Unexecuted instantiation: _ZN9base_uintILj6144EErSEj |
46 | | |
47 | | template <unsigned int BITS> |
48 | | base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32) |
49 | 888 | { |
50 | 888 | uint64_t carry = 0; |
51 | 7.99k | for (int i = 0; i < WIDTH; i++7.10k ) { |
52 | 7.10k | uint64_t n = carry + (uint64_t)b32 * pn[i]; |
53 | 7.10k | pn[i] = n & 0xffffffff; |
54 | 7.10k | carry = n >> 32; |
55 | 7.10k | } |
56 | 888 | return *this; |
57 | 888 | } |
58 | | |
59 | | template <unsigned int BITS> |
60 | | base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b) |
61 | 89.5k | { |
62 | 89.5k | base_uint<BITS> a; |
63 | 805k | for (int j = 0; j < WIDTH; j++716k ) { |
64 | 716k | uint64_t carry = 0; |
65 | 3.93M | for (int i = 0; i + j < WIDTH; i++3.22M ) { |
66 | 3.22M | uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i]; |
67 | 3.22M | a.pn[i + j] = n & 0xffffffff; |
68 | 3.22M | carry = n >> 32; |
69 | 3.22M | } |
70 | 716k | } |
71 | 89.5k | *this = a; |
72 | 89.5k | return *this; |
73 | 89.5k | } _ZN9base_uintILj256EEmLERKS0_ Line | Count | Source | 61 | 89.5k | { | 62 | 89.5k | base_uint<BITS> a; | 63 | 805k | for (int j = 0; j < WIDTH; j++716k ) { | 64 | 716k | uint64_t carry = 0; | 65 | 3.93M | for (int i = 0; i + j < WIDTH; i++3.22M ) { | 66 | 3.22M | uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i]; | 67 | 3.22M | a.pn[i + j] = n & 0xffffffff; | 68 | 3.22M | carry = n >> 32; | 69 | 3.22M | } | 70 | 716k | } | 71 | 89.5k | *this = a; | 72 | 89.5k | return *this; | 73 | 89.5k | } |
Unexecuted instantiation: _ZN9base_uintILj6144EEmLERKS0_ |
74 | | |
75 | | template <unsigned int BITS> |
76 | | base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b) |
77 | 1.64M | { |
78 | 1.64M | base_uint<BITS> div = b; // make a copy, so we can shift. |
79 | 1.64M | base_uint<BITS> num = *this; // make a copy, so we can subtract. |
80 | 1.64M | *this = 0; // the quotient. |
81 | 1.64M | int num_bits = num.bits(); |
82 | 1.64M | int div_bits = div.bits(); |
83 | 1.64M | if (div_bits == 0) |
84 | 0 | throw uint_error("Division by zero"); |
85 | 1.64M | if (div_bits > num_bits) // the result is certainly 0. |
86 | 0 | return *this; |
87 | 1.64M | int shift = num_bits - div_bits; |
88 | 1.64M | div <<= shift; // shift so that div and num align. |
89 | 4.94M | while (shift >= 0) { |
90 | 3.29M | if (num >= div) { |
91 | 1.64M | num -= div; |
92 | 1.64M | pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result. |
93 | 1.64M | } |
94 | 3.29M | div >>= 1; // shift back. |
95 | 3.29M | shift--; |
96 | 3.29M | } |
97 | | // num now contains the remainder of the division. |
98 | 1.64M | return *this; |
99 | 1.64M | } _ZN9base_uintILj256EEdVERKS0_ Line | Count | Source | 77 | 1.64M | { | 78 | 1.64M | base_uint<BITS> div = b; // make a copy, so we can shift. | 79 | 1.64M | base_uint<BITS> num = *this; // make a copy, so we can subtract. | 80 | 1.64M | *this = 0; // the quotient. | 81 | 1.64M | int num_bits = num.bits(); | 82 | 1.64M | int div_bits = div.bits(); | 83 | 1.64M | if (div_bits == 0) | 84 | 0 | throw uint_error("Division by zero"); | 85 | 1.64M | if (div_bits > num_bits) // the result is certainly 0. | 86 | 0 | return *this; | 87 | 1.64M | int shift = num_bits - div_bits; | 88 | 1.64M | div <<= shift; // shift so that div and num align. | 89 | 4.94M | while (shift >= 0) { | 90 | 3.29M | if (num >= div) { | 91 | 1.64M | num -= div; | 92 | 1.64M | pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result. | 93 | 1.64M | } | 94 | 3.29M | div >>= 1; // shift back. | 95 | 3.29M | shift--; | 96 | 3.29M | } | 97 | | // num now contains the remainder of the division. | 98 | 1.64M | return *this; | 99 | 1.64M | } |
Unexecuted instantiation: _ZN9base_uintILj6144EEdVERKS0_ |
100 | | |
101 | | template <unsigned int BITS> |
102 | | int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const |
103 | 203M | { |
104 | 1.60G | for (int i = WIDTH - 1; i >= 0; i--1.39G ) { |
105 | 1.60G | if (pn[i] < b.pn[i]) |
106 | 154M | return -1; |
107 | 1.44G | if (pn[i] > b.pn[i]) |
108 | 47.4M | return 1; |
109 | 1.44G | } |
110 | 1.08M | return 0; |
111 | 203M | } _ZNK9base_uintILj256EE9CompareToERKS0_ Line | Count | Source | 103 | 203M | { | 104 | 1.60G | for (int i = WIDTH - 1; i >= 0; i--1.39G ) { | 105 | 1.60G | if (pn[i] < b.pn[i]) | 106 | 154M | return -1; | 107 | 1.44G | if (pn[i] > b.pn[i]) | 108 | 47.4M | return 1; | 109 | 1.44G | } | 110 | 1.08M | return 0; | 111 | 203M | } |
Unexecuted instantiation: _ZNK9base_uintILj6144EE9CompareToERKS0_ |
112 | | |
113 | | template <unsigned int BITS> |
114 | | bool base_uint<BITS>::EqualTo(uint64_t b) const |
115 | 1.64M | { |
116 | 1.64M | for (int i = WIDTH - 1; i >= 2; i--0 ) { |
117 | 1.64M | if (pn[i]) |
118 | 1.64M | return false; |
119 | 1.64M | } |
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 | 1.95k | { |
130 | 1.95k | double ret = 0.0; |
131 | 1.95k | double fact = 1.0; |
132 | 17.5k | for (int i = 0; i < WIDTH; i++15.6k ) { |
133 | 15.6k | ret += fact * pn[i]; |
134 | 15.6k | fact *= 4294967296.0; |
135 | 15.6k | } |
136 | 1.95k | return ret; |
137 | 1.95k | } |
138 | | |
139 | | template <unsigned int BITS> |
140 | | std::string base_uint<BITS>::GetHex() const |
141 | 7.28k | { |
142 | 7.28k | base_blob<BITS> b; |
143 | 65.5k | for (int x = 0; x < this->WIDTH; ++x58.2k ) { |
144 | 58.2k | WriteLE32(b.begin() + x*4, this->pn[x]); |
145 | 58.2k | } |
146 | 7.28k | return b.GetHex(); |
147 | 7.28k | } |
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 | 3.35M | { |
158 | 3.35M | for (int pos = WIDTH - 1; pos >= 0; pos--0 ) { |
159 | 3.35M | if (pn[pos]) { |
160 | 5.05M | for (int nbits = 31; nbits > 0; nbits--1.70M ) { |
161 | 5.05M | if (pn[pos] & 1U << nbits) |
162 | 3.35M | return 32 * pos + nbits + 1; |
163 | 5.05M | } |
164 | 0 | return 32 * pos + 1; |
165 | 3.35M | } |
166 | 3.35M | } |
167 | 0 | return 0; |
168 | 3.35M | } _ZNK9base_uintILj256EE4bitsEv Line | Count | Source | 157 | 3.35M | { | 158 | 3.35M | for (int pos = WIDTH - 1; pos >= 0; pos--0 ) { | 159 | 3.35M | if (pn[pos]) { | 160 | 5.05M | for (int nbits = 31; nbits > 0; nbits--1.70M ) { | 161 | 5.05M | if (pn[pos] & 1U << nbits) | 162 | 3.35M | return 32 * pos + nbits + 1; | 163 | 5.05M | } | 164 | 0 | return 32 * pos + 1; | 165 | 3.35M | } | 166 | 3.35M | } | 167 | 0 | return 0; | 168 | 3.35M | } |
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 | 1.64M | { |
177 | 1.64M | int nSize = nCompact >> 24; |
178 | 1.64M | uint32_t nWord = nCompact & 0x007fffff; |
179 | 1.64M | if (nSize <= 3) { |
180 | 0 | nWord >>= 8 * (3 - nSize); |
181 | 0 | *this = nWord; |
182 | 1.64M | } else { |
183 | 1.64M | *this = nWord; |
184 | 1.64M | *this <<= 8 * (nSize - 3); |
185 | 1.64M | } |
186 | 1.64M | if (pfNegative) |
187 | 1.64M | *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0; |
188 | 1.64M | if (pfOverflow) |
189 | 1.64M | *pfOverflow = nWord != 0 && ((nSize > 34) || |
190 | 1.64M | (nWord > 0xff && nSize > 33) || |
191 | 1.64M | (nWord > 0xffff && nSize > 32)); |
192 | 1.64M | return *this; |
193 | 1.64M | } |
194 | | |
195 | | uint32_t arith_uint256::GetCompact(bool fNegative) const |
196 | 50.4k | { |
197 | 50.4k | int nSize = (bits() + 7) / 8; |
198 | 50.4k | uint32_t nCompact = 0; |
199 | 50.4k | if (nSize <= 3) { |
200 | 0 | nCompact = GetLow64() << 8 * (3 - nSize); |
201 | 50.4k | } else { |
202 | 50.4k | arith_uint256 bn = *this >> 8 * (nSize - 3); |
203 | 50.4k | nCompact = bn.GetLow64(); |
204 | 50.4k | } |
205 | | // The 0x00800000 bit denotes the sign. |
206 | | // Thus, if it is already set, divide the mantissa by 256 and increase the exponent. |
207 | 50.4k | if (nCompact & 0x00800000) { |
208 | 0 | nCompact >>= 8; |
209 | 0 | nSize++; |
210 | 0 | } |
211 | 50.4k | assert((nCompact & ~0x007fffffU) == 0); |
212 | 50.4k | assert(nSize < 256); |
213 | 50.4k | nCompact |= nSize << 24; |
214 | 50.4k | nCompact |= (fNegative && (nCompact & 0x007fffff)0 ? 0x008000000 : 0); |
215 | 50.4k | return nCompact; |
216 | 50.4k | } |
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 | 65.0k | { |
227 | 65.0k | arith_uint256 b; |
228 | 585k | for(int x=0; x<b.WIDTH; ++x520k ) |
229 | 520k | b.pn[x] = ReadLE32(a.begin() + x*4); |
230 | 65.0k | return b; |
231 | 65.0k | } |
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); |