Coverage Report

Created: 2026-08-05 14:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/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
3.12M
{
17
3.12M
    base_uint<BITS> a(*this);
18
28.1M
    for (int i = 0; i < WIDTH; i++)
19
24.9M
        pn[i] = 0;
20
3.12M
    int k = shift / 32;
21
3.12M
    shift = shift % 32;
22
28.1M
    for (int i = 0; i < WIDTH; i++) {
23
24.9M
        if (i + k + 1 < WIDTH && shift != 0)
24
6.40M
            pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
25
24.9M
        if (i + k < WIDTH)
26
9.69M
            pn[i + k] |= (a.pn[i] << shift);
27
24.9M
    }
28
3.12M
    return *this;
29
3.12M
}
base_uint<256u>::operator<<=(unsigned int)
Line
Count
Source
16
3.12M
{
17
3.12M
    base_uint<BITS> a(*this);
18
28.1M
    for (int i = 0; i < WIDTH; i++)
19
24.9M
        pn[i] = 0;
20
3.12M
    int k = shift / 32;
21
3.12M
    shift = shift % 32;
22
28.1M
    for (int i = 0; i < WIDTH; i++) {
23
24.9M
        if (i + k + 1 < WIDTH && shift != 0)
24
6.40M
            pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
25
24.9M
        if (i + k < WIDTH)
26
9.69M
            pn[i + k] |= (a.pn[i] << shift);
27
24.9M
    }
28
3.12M
    return *this;
29
3.12M
}
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
2.15M
{
34
2.15M
    base_uint<BITS> a(*this);
35
19.4M
    for (int i = 0; i < WIDTH; i++)
36
17.2M
        pn[i] = 0;
37
2.15M
    int k = shift / 32;
38
2.15M
    shift = shift % 32;
39
19.4M
    for (int i = 0; i < WIDTH; i++) {
40
17.2M
        if (i - k - 1 >= 0 && shift != 0)
41
13.1M
            pn[i - k - 1] |= (a.pn[i] << (32 - shift));
42
17.2M
        if (i - k >= 0)
43
15.3M
            pn[i - k] |= (a.pn[i] >> shift);
44
17.2M
    }
45
2.15M
    return *this;
46
2.15M
}
base_uint<256u>::operator>>=(unsigned int)
Line
Count
Source
33
2.15M
{
34
2.15M
    base_uint<BITS> a(*this);
35
19.4M
    for (int i = 0; i < WIDTH; i++)
36
17.2M
        pn[i] = 0;
37
2.15M
    int k = shift / 32;
38
2.15M
    shift = shift % 32;
39
19.4M
    for (int i = 0; i < WIDTH; i++) {
40
17.2M
        if (i - k - 1 >= 0 && shift != 0)
41
13.1M
            pn[i - k - 1] |= (a.pn[i] << (32 - shift));
42
17.2M
        if (i - k >= 0)
43
15.3M
            pn[i - k] |= (a.pn[i] >> shift);
44
17.2M
    }
45
2.15M
    return *this;
46
2.15M
}
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
12.9k
{
51
12.9k
    uint64_t carry = 0;
52
116k
    for (int i = 0; i < WIDTH; i++) {
53
103k
        uint64_t n = carry + (uint64_t)b32 * pn[i];
54
103k
        pn[i] = n & 0xffffffff;
55
103k
        carry = n >> 32;
56
103k
    }
57
12.9k
    return *this;
58
12.9k
}
59
60
template <unsigned int BITS>
61
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
62
63.6k
{
63
63.6k
    base_uint<BITS> a;
64
572k
    for (int j = 0; j < WIDTH; j++) {
65
508k
        uint64_t carry = 0;
66
2.79M
        for (int i = 0; i + j < WIDTH; i++) {
67
2.29M
            uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
68
2.29M
            a.pn[i + j] = n & 0xffffffff;
69
2.29M
            carry = n >> 32;
70
2.29M
        }
71
508k
    }
72
63.6k
    *this = a;
73
63.6k
    return *this;
74
63.6k
}
base_uint<256u>::operator*=(base_uint<256u> const&)
Line
Count
Source
62
63.6k
{
63
63.6k
    base_uint<BITS> a;
64
572k
    for (int j = 0; j < WIDTH; j++) {
65
508k
        uint64_t carry = 0;
66
2.79M
        for (int i = 0; i + j < WIDTH; i++) {
67
2.29M
            uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
68
2.29M
            a.pn[i + j] = n & 0xffffffff;
69
2.29M
            carry = n >> 32;
70
2.29M
        }
71
508k
    }
72
63.6k
    *this = a;
73
63.6k
    return *this;
74
63.6k
}
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
890k
{
79
890k
    base_uint<BITS> div = b;     // make a copy, so we can shift.
80
890k
    base_uint<BITS> num = *this; // make a copy, so we can subtract.
81
890k
    *this = 0;                   // the quotient.
82
890k
    int num_bits = num.bits();
83
890k
    int div_bits = div.bits();
84
890k
    if (div_bits == 0)
85
2
        throw uint_error("Division by zero");
86
890k
    if (div_bits > num_bits) // the result is certainly 0.
87
2
        return *this;
88
890k
    int shift = num_bits - div_bits;
89
890k
    div <<= shift; // shift so that div and num align.
90
2.77M
    while (shift >= 0) {
91
1.88M
        if (num >= div) {
92
905k
            num -= div;
93
905k
            pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
94
905k
        }
95
1.88M
        div >>= 1; // shift back.
96
1.88M
        shift--;
97
1.88M
    }
98
    // num now contains the remainder of the division.
99
890k
    return *this;
100
890k
}
base_uint<256u>::operator/=(base_uint<256u> const&)
Line
Count
Source
78
890k
{
79
890k
    base_uint<BITS> div = b;     // make a copy, so we can shift.
80
890k
    base_uint<BITS> num = *this; // make a copy, so we can subtract.
81
890k
    *this = 0;                   // the quotient.
82
890k
    int num_bits = num.bits();
83
890k
    int div_bits = div.bits();
84
890k
    if (div_bits == 0)
85
2
        throw uint_error("Division by zero");
86
890k
    if (div_bits > num_bits) // the result is certainly 0.
87
2
        return *this;
88
890k
    int shift = num_bits - div_bits;
89
890k
    div <<= shift; // shift so that div and num align.
90
2.77M
    while (shift >= 0) {
91
1.88M
        if (num >= div) {
92
905k
            num -= div;
93
905k
            pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
94
905k
        }
95
1.88M
        div >>= 1; // shift back.
96
1.88M
        shift--;
97
1.88M
    }
98
    // num now contains the remainder of the division.
99
890k
    return *this;
100
890k
}
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
1.09G
{
105
8.58G
    for (int i = WIDTH - 1; i >= 0; i--) {
106
8.57G
        if (pn[i] < b.pn[i])
107
734M
            return -1;
108
7.84G
        if (pn[i] > b.pn[i])
109
356M
            return 1;
110
7.84G
    }
111
5.43M
    return 0;
112
1.09G
}
base_uint<256u>::CompareTo(base_uint<256u> const&) const
Line
Count
Source
104
1.09G
{
105
8.58G
    for (int i = WIDTH - 1; i >= 0; i--) {
106
8.57G
        if (pn[i] < b.pn[i])
107
734M
            return -1;
108
7.84G
        if (pn[i] > b.pn[i])
109
356M
            return 1;
110
7.84G
    }
111
5.43M
    return 0;
112
1.09G
}
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
2.17M
{
117
2.18M
    for (int i = WIDTH - 1; i >= 2; i--) {
118
2.18M
        if (pn[i])
119
2.17M
            return false;
120
2.18M
    }
121
103
    if (pn[1] != (b >> 32))
122
32
        return false;
123
71
    if (pn[0] != (b & 0xfffffffful))
124
32
        return false;
125
39
    return true;
126
71
}
127
128
template <unsigned int BITS>
129
double base_uint<BITS>::getdouble() const
130
135k
{
131
135k
    double ret = 0.0;
132
135k
    double fact = 1.0;
133
1.21M
    for (int i = 0; i < WIDTH; i++) {
134
1.08M
        ret += fact * pn[i];
135
1.08M
        fact *= 4294967296.0;
136
1.08M
    }
137
135k
    return ret;
138
135k
}
139
140
template <unsigned int BITS>
141
std::string base_uint<BITS>::GetHex() const
142
24.5k
{
143
24.5k
    base_blob<BITS> b;
144
221k
    for (int x = 0; x < this->WIDTH; ++x) {
145
196k
        WriteLE32(b.begin() + x*4, this->pn[x]);
146
196k
    }
147
24.5k
    return b.GetHex();
148
24.5k
}
149
150
template <unsigned int BITS>
151
std::string base_uint<BITS>::ToString() const
152
35
{
153
35
    return GetHex();
154
35
}
155
156
template <unsigned int BITS>
157
unsigned int base_uint<BITS>::bits() const
158
2.05M
{
159
2.08M
    for (int pos = WIDTH - 1; pos >= 0; pos--) {
160
2.08M
        if (pn[pos]) {
161
3.28M
            for (int nbits = 31; nbits > 0; nbits--) {
162
3.28M
                if (pn[pos] & 1U << nbits)
163
2.05M
                    return 32 * pos + nbits + 1;
164
3.28M
            }
165
3
            return 32 * pos + 1;
166
2.05M
        }
167
2.08M
    }
168
16
    return 0;
169
2.05M
}
base_uint<256u>::bits() const
Line
Count
Source
158
2.05M
{
159
2.08M
    for (int pos = WIDTH - 1; pos >= 0; pos--) {
160
2.08M
        if (pn[pos]) {
161
3.28M
            for (int nbits = 31; nbits > 0; nbits--) {
162
3.28M
                if (pn[pos] & 1U << nbits)
163
2.05M
                    return 32 * pos + nbits + 1;
164
3.28M
            }
165
3
            return 32 * pos + 1;
166
2.05M
        }
167
2.08M
    }
168
16
    return 0;
169
2.05M
}
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
2.17M
{
178
2.17M
    int nSize = nCompact >> 24;
179
2.17M
    uint32_t nWord = nCompact & 0x007fffff;
180
2.17M
    if (nSize <= 3) {
181
43
        nWord >>= 8 * (3 - nSize);
182
43
        *this = nWord;
183
2.17M
    } else {
184
2.17M
        *this = nWord;
185
2.17M
        *this <<= 8 * (nSize - 3);
186
2.17M
    }
187
2.17M
    if (pfNegative)
188
2.17M
        *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
189
2.17M
    if (pfOverflow)
190
2.17M
        *pfOverflow = nWord != 0 && ((nSize > 34) ||
191
2.17M
                                     (nWord > 0xff && nSize > 33) ||
192
2.17M
                                     (nWord > 0xffff && nSize > 32));
193
2.17M
    return *this;
194
2.17M
}
195
196
uint32_t arith_uint256::GetCompact(bool fNegative) const
197
270k
{
198
270k
    int nSize = CeilDiv(bits(), 8u);
199
270k
    uint32_t nCompact = 0;
200
270k
    if (nSize <= 3) {
201
17
        nCompact = GetLow64() << 8 * (3 - nSize);
202
270k
    } else {
203
270k
        arith_uint256 bn = *this >> 8 * (nSize - 3);
204
270k
        nCompact = bn.GetLow64();
205
270k
    }
206
    // The 0x00800000 bit denotes the sign.
207
    // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
208
270k
    if (nCompact & 0x00800000) {
209
2.42k
        nCompact >>= 8;
210
2.42k
        nSize++;
211
2.42k
    }
212
270k
    assert((nCompact & ~0x007fffffU) == 0);
213
270k
    assert(nSize < 256);
214
270k
    nCompact |= nSize << 24;
215
270k
    nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
216
270k
    return nCompact;
217
270k
}
218
219
uint256 ArithToUint256(const arith_uint256 &a)
220
284k
{
221
284k
    uint256 b;
222
2.55M
    for(int x=0; x<a.WIDTH; ++x)
223
2.27M
        WriteLE32(b.begin() + x*4, a.pn[x]);
224
284k
    return b;
225
284k
}
226
arith_uint256 UintToArith256(const uint256 &a)
227
26.5M
{
228
26.5M
    arith_uint256 b;
229
237M
    for(int x=0; x<b.WIDTH; ++x)
230
210M
        b.pn[x] = ReadLE32(a.begin() + x*4);
231
26.5M
    return b;
232
26.5M
}
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);