Coverage Report

Created: 2026-07-14 18:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
1.63M
{
17
1.63M
    base_uint<BITS> a(*this);
18
14.6M
    for (int i = 0; i < WIDTH; i++)
  Branch (18:21): [True: 13.0M, False: 1.63M]
  Branch (18:21): [True: 0, False: 0]
19
13.0M
        pn[i] = 0;
20
1.63M
    int k = shift / 32;
21
1.63M
    shift = shift % 32;
22
14.6M
    for (int i = 0; i < WIDTH; i++) {
  Branch (22:21): [True: 13.0M, False: 1.63M]
  Branch (22:21): [True: 0, False: 0]
23
13.0M
        if (i + k + 1 < WIDTH && shift != 0)
  Branch (23:13): [True: 5.71M, False: 7.34M]
  Branch (23:34): [True: 5.71M, False: 0]
  Branch (23:13): [True: 0, False: 0]
  Branch (23:34): [True: 0, False: 0]
24
5.71M
            pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
25
13.0M
        if (i + k < WIDTH)
  Branch (25:13): [True: 7.35M, False: 5.71M]
  Branch (25:13): [True: 0, False: 0]
26
7.35M
            pn[i + k] |= (a.pn[i] << shift);
27
13.0M
    }
28
1.63M
    return *this;
29
1.63M
}
base_uint<256u>::operator<<=(unsigned int)
Line
Count
Source
16
1.63M
{
17
1.63M
    base_uint<BITS> a(*this);
18
14.6M
    for (int i = 0; i < WIDTH; i++)
  Branch (18:21): [True: 13.0M, False: 1.63M]
19
13.0M
        pn[i] = 0;
20
1.63M
    int k = shift / 32;
21
1.63M
    shift = shift % 32;
22
14.6M
    for (int i = 0; i < WIDTH; i++) {
  Branch (22:21): [True: 13.0M, False: 1.63M]
23
13.0M
        if (i + k + 1 < WIDTH && shift != 0)
  Branch (23:13): [True: 5.71M, False: 7.34M]
  Branch (23:34): [True: 5.71M, False: 0]
24
5.71M
            pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
25
13.0M
        if (i + k < WIDTH)
  Branch (25:13): [True: 7.35M, False: 5.71M]
26
7.35M
            pn[i + k] |= (a.pn[i] << shift);
27
13.0M
    }
28
1.63M
    return *this;
29
1.63M
}
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
1.76M
{
34
1.76M
    base_uint<BITS> a(*this);
35
15.9M
    for (int i = 0; i < WIDTH; i++)
  Branch (35:21): [True: 14.1M, False: 1.76M]
  Branch (35:21): [True: 0, False: 0]
36
14.1M
        pn[i] = 0;
37
1.76M
    int k = shift / 32;
38
1.76M
    shift = shift % 32;
39
15.9M
    for (int i = 0; i < WIDTH; i++) {
  Branch (39:21): [True: 14.1M, False: 1.76M]
  Branch (39:21): [True: 0, False: 0]
40
14.1M
        if (i - k - 1 >= 0 && shift != 0)
  Branch (40:13): [True: 11.4M, False: 2.67M]
  Branch (40:31): [True: 11.4M, False: 0]
  Branch (40:13): [True: 0, False: 0]
  Branch (40:31): [True: 0, False: 0]
41
11.4M
            pn[i - k - 1] |= (a.pn[i] << (32 - shift));
42
14.1M
        if (i - k >= 0)
  Branch (42:13): [True: 13.2M, False: 902k]
  Branch (42:13): [True: 0, False: 0]
43
13.2M
            pn[i - k] |= (a.pn[i] >> shift);
44
14.1M
    }
45
1.76M
    return *this;
46
1.76M
}
base_uint<256u>::operator>>=(unsigned int)
Line
Count
Source
33
1.76M
{
34
1.76M
    base_uint<BITS> a(*this);
35
15.9M
    for (int i = 0; i < WIDTH; i++)
  Branch (35:21): [True: 14.1M, False: 1.76M]
36
14.1M
        pn[i] = 0;
37
1.76M
    int k = shift / 32;
38
1.76M
    shift = shift % 32;
39
15.9M
    for (int i = 0; i < WIDTH; i++) {
  Branch (39:21): [True: 14.1M, False: 1.76M]
40
14.1M
        if (i - k - 1 >= 0 && shift != 0)
  Branch (40:13): [True: 11.4M, False: 2.67M]
  Branch (40:31): [True: 11.4M, False: 0]
41
11.4M
            pn[i - k - 1] |= (a.pn[i] << (32 - shift));
42
14.1M
        if (i - k >= 0)
  Branch (42:13): [True: 13.2M, False: 902k]
43
13.2M
            pn[i - k] |= (a.pn[i] >> shift);
44
14.1M
    }
45
1.76M
    return *this;
46
1.76M
}
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
48.7k
{
51
48.7k
    uint64_t carry = 0;
52
438k
    for (int i = 0; i < WIDTH; i++) {
  Branch (52:21): [True: 389k, False: 48.7k]
53
389k
        uint64_t n = carry + (uint64_t)b32 * pn[i];
54
389k
        pn[i] = n & 0xffffffff;
55
389k
        carry = n >> 32;
56
389k
    }
57
48.7k
    return *this;
58
48.7k
}
59
60
template <unsigned int BITS>
61
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
62
330k
{
63
330k
    base_uint<BITS> a;
64
2.97M
    for (int j = 0; j < WIDTH; j++) {
  Branch (64:21): [True: 2.64M, False: 330k]
  Branch (64:21): [True: 0, False: 0]
65
2.64M
        uint64_t carry = 0;
66
14.5M
        for (int i = 0; i + j < WIDTH; i++) {
  Branch (66:25): [True: 11.8M, False: 2.64M]
  Branch (66:25): [True: 0, False: 0]
67
11.8M
            uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
68
11.8M
            a.pn[i + j] = n & 0xffffffff;
69
11.8M
            carry = n >> 32;
70
11.8M
        }
71
2.64M
    }
72
330k
    *this = a;
73
330k
    return *this;
74
330k
}
base_uint<256u>::operator*=(base_uint<256u> const&)
Line
Count
Source
62
330k
{
63
330k
    base_uint<BITS> a;
64
2.97M
    for (int j = 0; j < WIDTH; j++) {
  Branch (64:21): [True: 2.64M, False: 330k]
65
2.64M
        uint64_t carry = 0;
66
14.5M
        for (int i = 0; i + j < WIDTH; i++) {
  Branch (66:25): [True: 11.8M, False: 2.64M]
67
11.8M
            uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
68
11.8M
            a.pn[i + j] = n & 0xffffffff;
69
11.8M
            carry = n >> 32;
70
11.8M
        }
71
2.64M
    }
72
330k
    *this = a;
73
330k
    return *this;
74
330k
}
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
817k
{
79
817k
    base_uint<BITS> div = b;     // make a copy, so we can shift.
80
817k
    base_uint<BITS> num = *this; // make a copy, so we can subtract.
81
817k
    *this = 0;                   // the quotient.
82
817k
    int num_bits = num.bits();
83
817k
    int div_bits = div.bits();
84
817k
    if (div_bits == 0)
  Branch (84:9): [True: 0, False: 817k]
  Branch (84:9): [True: 0, False: 0]
85
0
        throw uint_error("Division by zero");
86
817k
    if (div_bits > num_bits) // the result is certainly 0.
  Branch (86:9): [True: 508, False: 816k]
  Branch (86:9): [True: 0, False: 0]
87
508
        return *this;
88
816k
    int shift = num_bits - div_bits;
89
816k
    div <<= shift; // shift so that div and num align.
90
2.45M
    while (shift >= 0) {
  Branch (90:12): [True: 1.63M, False: 816k]
  Branch (90:12): [True: 0, False: 0]
91
1.63M
        if (num >= div) {
  Branch (91:13): [True: 818k, False: 820k]
  Branch (91:13): [True: 0, False: 0]
92
818k
            num -= div;
93
818k
            pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
94
818k
        }
95
1.63M
        div >>= 1; // shift back.
96
1.63M
        shift--;
97
1.63M
    }
98
    // num now contains the remainder of the division.
99
816k
    return *this;
100
817k
}
base_uint<256u>::operator/=(base_uint<256u> const&)
Line
Count
Source
78
817k
{
79
817k
    base_uint<BITS> div = b;     // make a copy, so we can shift.
80
817k
    base_uint<BITS> num = *this; // make a copy, so we can subtract.
81
817k
    *this = 0;                   // the quotient.
82
817k
    int num_bits = num.bits();
83
817k
    int div_bits = div.bits();
84
817k
    if (div_bits == 0)
  Branch (84:9): [True: 0, False: 817k]
85
0
        throw uint_error("Division by zero");
86
817k
    if (div_bits > num_bits) // the result is certainly 0.
  Branch (86:9): [True: 508, False: 816k]
87
508
        return *this;
88
816k
    int shift = num_bits - div_bits;
89
816k
    div <<= shift; // shift so that div and num align.
90
2.45M
    while (shift >= 0) {
  Branch (90:12): [True: 1.63M, False: 816k]
91
1.63M
        if (num >= div) {
  Branch (91:13): [True: 818k, False: 820k]
92
818k
            num -= div;
93
818k
            pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
94
818k
        }
95
1.63M
        div >>= 1; // shift back.
96
1.63M
        shift--;
97
1.63M
    }
98
    // num now contains the remainder of the division.
99
816k
    return *this;
100
817k
}
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.00G
{
105
8.30G
    for (int i = WIDTH - 1; i >= 0; i--) {
  Branch (105:29): [True: 8.06G, False: 239M]
  Branch (105:29): [True: 0, False: 0]
106
8.06G
        if (pn[i] < b.pn[i])
  Branch (106:13): [True: 482M, False: 7.58G]
  Branch (106:13): [True: 0, False: 0]
107
482M
            return -1;
108
7.58G
        if (pn[i] > b.pn[i])
  Branch (108:13): [True: 287M, False: 7.29G]
  Branch (108:13): [True: 0, False: 0]
109
287M
            return 1;
110
7.58G
    }
111
239M
    return 0;
112
1.00G
}
base_uint<256u>::CompareTo(base_uint<256u> const&) const
Line
Count
Source
104
1.00G
{
105
8.30G
    for (int i = WIDTH - 1; i >= 0; i--) {
  Branch (105:29): [True: 8.06G, False: 239M]
106
8.06G
        if (pn[i] < b.pn[i])
  Branch (106:13): [True: 482M, False: 7.58G]
107
482M
            return -1;
108
7.58G
        if (pn[i] > b.pn[i])
  Branch (108:13): [True: 287M, False: 7.29G]
109
287M
            return 1;
110
7.58G
    }
111
239M
    return 0;
112
1.00G
}
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
816k
{
117
816k
    for (int i = WIDTH - 1; i >= 2; i--) {
  Branch (117:29): [True: 816k, False: 0]
118
816k
        if (pn[i])
  Branch (118:13): [True: 816k, False: 0]
119
816k
            return false;
120
816k
    }
121
0
    if (pn[1] != (b >> 32))
  Branch (121:9): [True: 0, False: 0]
122
0
        return false;
123
0
    if (pn[0] != (b & 0xfffffffful))
  Branch (123:9): [True: 0, False: 0]
124
0
        return false;
125
0
    return true;
126
0
}
127
128
template <unsigned int BITS>
129
double base_uint<BITS>::getdouble() const
130
118k
{
131
118k
    double ret = 0.0;
132
118k
    double fact = 1.0;
133
1.06M
    for (int i = 0; i < WIDTH; i++) {
  Branch (133:21): [True: 944k, False: 118k]
134
944k
        ret += fact * pn[i];
135
944k
        fact *= 4294967296.0;
136
944k
    }
137
118k
    return ret;
138
118k
}
139
140
template <unsigned int BITS>
141
std::string base_uint<BITS>::GetHex() const
142
27
{
143
27
    base_blob<BITS> b;
144
243
    for (int x = 0; x < this->WIDTH; ++x) {
  Branch (144:21): [True: 216, False: 27]
145
216
        WriteLE32(b.begin() + x*4, this->pn[x]);
146
216
    }
147
27
    return b.GetHex();
148
27
}
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
1.76M
{
159
1.78M
    for (int pos = WIDTH - 1; pos >= 0; pos--) {
  Branch (159:31): [True: 1.78M, False: 1.01k]
  Branch (159:31): [True: 0, False: 0]
160
1.78M
        if (pn[pos]) {
  Branch (160:13): [True: 1.76M, False: 24.8k]
  Branch (160:13): [True: 0, False: 0]
161
2.76M
            for (int nbits = 31; nbits > 0; nbits--) {
  Branch (161:34): [True: 2.76M, False: 0]
  Branch (161:34): [True: 0, False: 0]
162
2.76M
                if (pn[pos] & 1U << nbits)
  Branch (162:21): [True: 1.76M, False: 1.00M]
  Branch (162:21): [True: 0, False: 0]
163
1.76M
                    return 32 * pos + nbits + 1;
164
2.76M
            }
165
0
            return 32 * pos + 1;
166
1.76M
        }
167
1.78M
    }
168
1.01k
    return 0;
169
1.76M
}
base_uint<256u>::bits() const
Line
Count
Source
158
1.76M
{
159
1.78M
    for (int pos = WIDTH - 1; pos >= 0; pos--) {
  Branch (159:31): [True: 1.78M, False: 1.01k]
160
1.78M
        if (pn[pos]) {
  Branch (160:13): [True: 1.76M, False: 24.8k]
161
2.76M
            for (int nbits = 31; nbits > 0; nbits--) {
  Branch (161:34): [True: 2.76M, False: 0]
162
2.76M
                if (pn[pos] & 1U << nbits)
  Branch (162:21): [True: 1.76M, False: 1.00M]
163
1.76M
                    return 32 * pos + nbits + 1;
164
2.76M
            }
165
0
            return 32 * pos + 1;
166
1.76M
        }
167
1.78M
    }
168
1.01k
    return 0;
169
1.76M
}
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
816k
{
178
816k
    int nSize = nCompact >> 24;
179
816k
    uint32_t nWord = nCompact & 0x007fffff;
180
816k
    if (nSize <= 3) {
  Branch (180:9): [True: 0, False: 816k]
181
0
        nWord >>= 8 * (3 - nSize);
182
0
        *this = nWord;
183
816k
    } else {
184
816k
        *this = nWord;
185
816k
        *this <<= 8 * (nSize - 3);
186
816k
    }
187
816k
    if (pfNegative)
  Branch (187:9): [True: 816k, False: 0]
188
816k
        *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
  Branch (188:23): [True: 816k, False: 0]
  Branch (188:37): [True: 0, False: 816k]
189
816k
    if (pfOverflow)
  Branch (189:9): [True: 816k, False: 0]
190
816k
        *pfOverflow = nWord != 0 && ((nSize > 34) ||
  Branch (190:23): [True: 816k, False: 0]
  Branch (190:38): [True: 0, False: 816k]
191
816k
                                     (nWord > 0xff && nSize > 33) ||
  Branch (191:39): [True: 816k, False: 0]
  Branch (191:55): [True: 0, False: 816k]
192
816k
                                     (nWord > 0xffff && nSize > 32));
  Branch (192:39): [True: 816k, False: 0]
  Branch (192:57): [True: 0, False: 816k]
193
816k
    return *this;
194
816k
}
195
196
uint32_t arith_uint256::GetCompact(bool fNegative) const
197
128k
{
198
128k
    int nSize = CeilDiv(bits(), 8u);
199
128k
    uint32_t nCompact = 0;
200
128k
    if (nSize <= 3) {
  Branch (200:9): [True: 0, False: 128k]
201
0
        nCompact = GetLow64() << 8 * (3 - nSize);
202
128k
    } else {
203
128k
        arith_uint256 bn = *this >> 8 * (nSize - 3);
204
128k
        nCompact = bn.GetLow64();
205
128k
    }
206
    // The 0x00800000 bit denotes the sign.
207
    // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
208
128k
    if (nCompact & 0x00800000) {
  Branch (208:9): [True: 0, False: 128k]
209
0
        nCompact >>= 8;
210
0
        nSize++;
211
0
    }
212
128k
    assert((nCompact & ~0x007fffffU) == 0);
  Branch (212:5): [True: 128k, False: 0]
213
128k
    assert(nSize < 256);
  Branch (213:5): [True: 128k, False: 0]
214
128k
    nCompact |= nSize << 24;
215
128k
    nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
  Branch (215:18): [True: 0, False: 128k]
  Branch (215:31): [True: 0, False: 0]
216
128k
    return nCompact;
217
128k
}
218
219
uint256 ArithToUint256(const arith_uint256 &a)
220
0
{
221
0
    uint256 b;
222
0
    for(int x=0; x<a.WIDTH; ++x)
  Branch (222:18): [True: 0, False: 0]
223
0
        WriteLE32(b.begin() + x*4, a.pn[x]);
224
0
    return b;
225
0
}
226
arith_uint256 UintToArith256(const uint256 &a)
227
129k
{
228
129k
    arith_uint256 b;
229
1.16M
    for(int x=0; x<b.WIDTH; ++x)
  Branch (229:18): [True: 1.03M, False: 129k]
230
1.03M
        b.pn[x] = ReadLE32(a.begin() + x*4);
231
129k
    return b;
232
129k
}
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);