Coverage Report

Created: 2025-06-10 13:21

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-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
13.5M
{
16
13.5M
    base_uint<BITS> a(*this);
17
122M
    for (int i = 0; i < WIDTH; i++)
  Branch (17:21): [True: 108M, False: 13.5M]
  Branch (17:21): [True: 0, False: 0]
18
108M
        pn[i] = 0;
19
13.5M
    int k = shift / 32;
20
13.5M
    shift = shift % 32;
21
122M
    for (int i = 0; i < WIDTH; i++) {
  Branch (21:21): [True: 108M, False: 13.5M]
  Branch (21:21): [True: 0, False: 0]
22
108M
        if (i + k + 1 < WIDTH && shift != 0)
  Branch (22:13): [True: 47.4M, False: 61.1M]
  Branch (22:34): [True: 47.4M, False: 0]
  Branch (22:13): [True: 0, False: 0]
  Branch (22:34): [True: 0, False: 0]
23
47.4M
            pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
24
108M
        if (i + k < WIDTH)
  Branch (24:13): [True: 61.0M, False: 47.5M]
  Branch (24:13): [True: 0, False: 0]
25
61.0M
            pn[i + k] |= (a.pn[i] << shift);
26
108M
    }
27
13.5M
    return *this;
28
13.5M
}
base_uint<256u>::operator<<=(unsigned int)
Line
Count
Source
15
13.5M
{
16
13.5M
    base_uint<BITS> a(*this);
17
122M
    for (int i = 0; i < WIDTH; i++)
  Branch (17:21): [True: 108M, False: 13.5M]
18
108M
        pn[i] = 0;
19
13.5M
    int k = shift / 32;
20
13.5M
    shift = shift % 32;
21
122M
    for (int i = 0; i < WIDTH; i++) {
  Branch (21:21): [True: 108M, False: 13.5M]
22
108M
        if (i + k + 1 < WIDTH && shift != 0)
  Branch (22:13): [True: 47.4M, False: 61.1M]
  Branch (22:34): [True: 47.4M, False: 0]
23
47.4M
            pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
24
108M
        if (i + k < WIDTH)
  Branch (24:13): [True: 61.0M, False: 47.5M]
25
61.0M
            pn[i + k] |= (a.pn[i] << shift);
26
108M
    }
27
13.5M
    return *this;
28
13.5M
}
Unexecuted instantiation: base_uint<6144u>::operator<<=(unsigned int)
29
30
template <unsigned int BITS>
31
base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift)
32
15.8M
{
33
15.8M
    base_uint<BITS> a(*this);
34
142M
    for (int i = 0; i < WIDTH; i++)
  Branch (34:21): [True: 126M, False: 15.8M]
  Branch (34:21): [True: 0, False: 0]
35
126M
        pn[i] = 0;
36
15.8M
    int k = shift / 32;
37
15.8M
    shift = shift % 32;
38
142M
    for (int i = 0; i < WIDTH; i++) {
  Branch (38:21): [True: 126M, False: 15.8M]
  Branch (38:21): [True: 0, False: 0]
39
126M
        if (i - k - 1 >= 0 && shift != 0)
  Branch (39:13): [True: 95.0M, False: 31.5M]
  Branch (39:31): [True: 95.0M, False: 0]
  Branch (39:13): [True: 0, False: 0]
  Branch (39:31): [True: 0, False: 0]
40
95.0M
            pn[i - k - 1] |= (a.pn[i] << (32 - shift));
41
126M
        if (i - k >= 0)
  Branch (41:13): [True: 110M, False: 15.6M]
  Branch (41:13): [True: 0, False: 0]
42
110M
            pn[i - k] |= (a.pn[i] >> shift);
43
126M
    }
44
15.8M
    return *this;
45
15.8M
}
base_uint<256u>::operator>>=(unsigned int)
Line
Count
Source
32
15.8M
{
33
15.8M
    base_uint<BITS> a(*this);
34
142M
    for (int i = 0; i < WIDTH; i++)
  Branch (34:21): [True: 126M, False: 15.8M]
35
126M
        pn[i] = 0;
36
15.8M
    int k = shift / 32;
37
15.8M
    shift = shift % 32;
38
142M
    for (int i = 0; i < WIDTH; i++) {
  Branch (38:21): [True: 126M, False: 15.8M]
39
126M
        if (i - k - 1 >= 0 && shift != 0)
  Branch (39:13): [True: 95.0M, False: 31.5M]
  Branch (39:31): [True: 95.0M, False: 0]
40
95.0M
            pn[i - k - 1] |= (a.pn[i] << (32 - shift));
41
126M
        if (i - k >= 0)
  Branch (41:13): [True: 110M, False: 15.6M]
42
110M
            pn[i - k] |= (a.pn[i] >> shift);
43
126M
    }
44
15.8M
    return *this;
45
15.8M
}
Unexecuted instantiation: base_uint<6144u>::operator>>=(unsigned int)
46
47
template <unsigned int BITS>
48
base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
49
8.40k
{
50
8.40k
    uint64_t carry = 0;
51
75.6k
    for (int i = 0; i < WIDTH; i++) {
  Branch (51:21): [True: 67.2k, False: 8.40k]
52
67.2k
        uint64_t n = carry + (uint64_t)b32 * pn[i];
53
67.2k
        pn[i] = n & 0xffffffff;
54
67.2k
        carry = n >> 32;
55
67.2k
    }
56
8.40k
    return *this;
57
8.40k
}
58
59
template <unsigned int BITS>
60
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
61
2.26M
{
62
2.26M
    base_uint<BITS> a;
63
20.3M
    for (int j = 0; j < WIDTH; j++) {
  Branch (63:21): [True: 18.1M, False: 2.26M]
  Branch (63:21): [True: 0, False: 0]
64
18.1M
        uint64_t carry = 0;
65
99.6M
        for (int i = 0; i + j < WIDTH; i++) {
  Branch (65:25): [True: 81.4M, False: 18.1M]
  Branch (65:25): [True: 0, False: 0]
66
81.4M
            uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
67
81.4M
            a.pn[i + j] = n & 0xffffffff;
68
81.4M
            carry = n >> 32;
69
81.4M
        }
70
18.1M
    }
71
2.26M
    *this = a;
72
2.26M
    return *this;
73
2.26M
}
base_uint<256u>::operator*=(base_uint<256u> const&)
Line
Count
Source
61
2.26M
{
62
2.26M
    base_uint<BITS> a;
63
20.3M
    for (int j = 0; j < WIDTH; j++) {
  Branch (63:21): [True: 18.1M, False: 2.26M]
64
18.1M
        uint64_t carry = 0;
65
99.6M
        for (int i = 0; i + j < WIDTH; i++) {
  Branch (65:25): [True: 81.4M, False: 18.1M]
66
81.4M
            uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
67
81.4M
            a.pn[i + j] = n & 0xffffffff;
68
81.4M
            carry = n >> 32;
69
81.4M
        }
70
18.1M
    }
71
2.26M
    *this = a;
72
2.26M
    return *this;
73
2.26M
}
Unexecuted instantiation: base_uint<6144u>::operator*=(base_uint<6144u> const&)
74
75
template <unsigned int BITS>
76
base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b)
77
6.78M
{
78
6.78M
    base_uint<BITS> div = b;     // make a copy, so we can shift.
79
6.78M
    base_uint<BITS> num = *this; // make a copy, so we can subtract.
80
6.78M
    *this = 0;                   // the quotient.
81
6.78M
    int num_bits = num.bits();
82
6.78M
    int div_bits = div.bits();
83
6.78M
    if (div_bits == 0)
  Branch (83:9): [True: 0, False: 6.78M]
  Branch (83:9): [True: 0, False: 0]
84
0
        throw uint_error("Division by zero");
85
6.78M
    if (div_bits > num_bits) // the result is certainly 0.
  Branch (85:9): [True: 87, False: 6.78M]
  Branch (85:9): [True: 0, False: 0]
86
87
        return *this;
87
6.78M
    int shift = num_bits - div_bits;
88
6.78M
    div <<= shift; // shift so that div and num align.
89
20.3M
    while (shift >= 0) {
  Branch (89:12): [True: 13.5M, False: 6.78M]
  Branch (89:12): [True: 0, False: 0]
90
13.5M
        if (num >= div) {
  Branch (90:13): [True: 6.78M, False: 6.78M]
  Branch (90:13): [True: 0, False: 0]
91
6.78M
            num -= div;
92
6.78M
            pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
93
6.78M
        }
94
13.5M
        div >>= 1; // shift back.
95
13.5M
        shift--;
96
13.5M
    }
97
    // num now contains the remainder of the division.
98
6.78M
    return *this;
99
6.78M
}
base_uint<256u>::operator/=(base_uint<256u> const&)
Line
Count
Source
77
6.78M
{
78
6.78M
    base_uint<BITS> div = b;     // make a copy, so we can shift.
79
6.78M
    base_uint<BITS> num = *this; // make a copy, so we can subtract.
80
6.78M
    *this = 0;                   // the quotient.
81
6.78M
    int num_bits = num.bits();
82
6.78M
    int div_bits = div.bits();
83
6.78M
    if (div_bits == 0)
  Branch (83:9): [True: 0, False: 6.78M]
84
0
        throw uint_error("Division by zero");
85
6.78M
    if (div_bits > num_bits) // the result is certainly 0.
  Branch (85:9): [True: 87, False: 6.78M]
86
87
        return *this;
87
6.78M
    int shift = num_bits - div_bits;
88
6.78M
    div <<= shift; // shift so that div and num align.
89
20.3M
    while (shift >= 0) {
  Branch (89:12): [True: 13.5M, False: 6.78M]
90
13.5M
        if (num >= div) {
  Branch (90:13): [True: 6.78M, False: 6.78M]
91
6.78M
            num -= div;
92
6.78M
            pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
93
6.78M
        }
94
13.5M
        div >>= 1; // shift back.
95
13.5M
        shift--;
96
13.5M
    }
97
    // num now contains the remainder of the division.
98
6.78M
    return *this;
99
6.78M
}
Unexecuted instantiation: base_uint<6144u>::operator/=(base_uint<6144u> const&)
100
101
template <unsigned int BITS>
102
int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const
103
4.23G
{
104
33.8G
    for (int i = WIDTH - 1; i >= 0; i--) {
  Branch (104:29): [True: 33.7G, False: 83.2M]
  Branch (104:29): [True: 0, False: 0]
105
33.7G
        if (pn[i] < b.pn[i])
  Branch (105:13): [True: 2.74G, False: 31.0G]
  Branch (105:13): [True: 0, False: 0]
106
2.74G
            return -1;
107
31.0G
        if (pn[i] > b.pn[i])
  Branch (107:13): [True: 1.40G, False: 29.6G]
  Branch (107:13): [True: 0, False: 0]
108
1.40G
            return 1;
109
31.0G
    }
110
83.2M
    return 0;
111
4.23G
}
base_uint<256u>::CompareTo(base_uint<256u> const&) const
Line
Count
Source
103
4.23G
{
104
33.8G
    for (int i = WIDTH - 1; i >= 0; i--) {
  Branch (104:29): [True: 33.7G, False: 83.2M]
105
33.7G
        if (pn[i] < b.pn[i])
  Branch (105:13): [True: 2.74G, False: 31.0G]
106
2.74G
            return -1;
107
31.0G
        if (pn[i] > b.pn[i])
  Branch (107:13): [True: 1.40G, False: 29.6G]
108
1.40G
            return 1;
109
31.0G
    }
110
83.2M
    return 0;
111
4.23G
}
Unexecuted instantiation: base_uint<6144u>::CompareTo(base_uint<6144u> const&) const
112
113
template <unsigned int BITS>
114
bool base_uint<BITS>::EqualTo(uint64_t b) const
115
6.79M
{
116
6.79M
    for (int i = WIDTH - 1; i >= 2; i--) {
  Branch (116:29): [True: 6.79M, False: 0]
117
6.79M
        if (pn[i])
  Branch (117:13): [True: 6.79M, False: 0]
118
6.79M
            return false;
119
6.79M
    }
120
0
    if (pn[1] != (b >> 32))
  Branch (120:9): [True: 0, False: 0]
121
0
        return false;
122
0
    if (pn[0] != (b & 0xfffffffful))
  Branch (122:9): [True: 0, False: 0]
123
0
        return false;
124
0
    return true;
125
0
}
126
127
template <unsigned int BITS>
128
double base_uint<BITS>::getdouble() const
129
2.24M
{
130
2.24M
    double ret = 0.0;
131
2.24M
    double fact = 1.0;
132
20.2M
    for (int i = 0; i < WIDTH; i++) {
  Branch (132:21): [True: 17.9M, False: 2.24M]
133
17.9M
        ret += fact * pn[i];
134
17.9M
        fact *= 4294967296.0;
135
17.9M
    }
136
2.24M
    return ret;
137
2.24M
}
138
139
template <unsigned int BITS>
140
std::string base_uint<BITS>::GetHex() const
141
22.1k
{
142
22.1k
    base_blob<BITS> b;
143
199k
    for (int x = 0; x < this->WIDTH; ++x) {
  Branch (143:21): [True: 177k, False: 22.1k]
144
177k
        WriteLE32(b.begin() + x*4, this->pn[x]);
145
177k
    }
146
22.1k
    return b.GetHex();
147
22.1k
}
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
15.8M
{
158
15.8M
    for (int pos = WIDTH - 1; pos >= 0; pos--) {
  Branch (158:31): [True: 15.8M, False: 174]
  Branch (158:31): [True: 0, False: 0]
159
15.8M
        if (pn[pos]) {
  Branch (159:13): [True: 15.8M, False: 12.5k]
  Branch (159:13): [True: 0, False: 0]
160
24.8M
            for (int nbits = 31; nbits > 0; nbits--) {
  Branch (160:34): [True: 24.8M, False: 0]
  Branch (160:34): [True: 0, False: 0]
161
24.8M
                if (pn[pos] & 1U << nbits)
  Branch (161:21): [True: 15.8M, False: 9.06M]
  Branch (161:21): [True: 0, False: 0]
162
15.8M
                    return 32 * pos + nbits + 1;
163
24.8M
            }
164
0
            return 32 * pos + 1;
165
15.8M
        }
166
15.8M
    }
167
174
    return 0;
168
15.8M
}
base_uint<256u>::bits() const
Line
Count
Source
157
15.8M
{
158
15.8M
    for (int pos = WIDTH - 1; pos >= 0; pos--) {
  Branch (158:31): [True: 15.8M, False: 174]
159
15.8M
        if (pn[pos]) {
  Branch (159:13): [True: 15.8M, False: 12.5k]
160
24.8M
            for (int nbits = 31; nbits > 0; nbits--) {
  Branch (160:34): [True: 24.8M, False: 0]
161
24.8M
                if (pn[pos] & 1U << nbits)
  Branch (161:21): [True: 15.8M, False: 9.06M]
162
15.8M
                    return 32 * pos + nbits + 1;
163
24.8M
            }
164
0
            return 32 * pos + 1;
165
15.8M
        }
166
15.8M
    }
167
174
    return 0;
168
15.8M
}
Unexecuted instantiation: base_uint<6144u>::bits() const
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
6.79M
{
177
6.79M
    int nSize = nCompact >> 24;
178
6.79M
    uint32_t nWord = nCompact & 0x007fffff;
179
6.79M
    if (nSize <= 3) {
  Branch (179:9): [True: 0, False: 6.79M]
180
0
        nWord >>= 8 * (3 - nSize);
181
0
        *this = nWord;
182
6.79M
    } else {
183
6.79M
        *this = nWord;
184
6.79M
        *this <<= 8 * (nSize - 3);
185
6.79M
    }
186
6.79M
    if (pfNegative)
  Branch (186:9): [True: 6.79M, False: 0]
187
6.79M
        *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
  Branch (187:23): [True: 6.79M, False: 0]
  Branch (187:37): [True: 0, False: 6.79M]
188
6.79M
    if (pfOverflow)
  Branch (188:9): [True: 6.79M, False: 0]
189
6.79M
        *pfOverflow = nWord != 0 && ((nSize > 34) ||
  Branch (189:23): [True: 6.79M, False: 0]
  Branch (189:38): [True: 0, False: 6.79M]
190
6.79M
                                     (nWord > 0xff && nSize > 33) ||
  Branch (190:39): [True: 6.79M, False: 0]
  Branch (190:55): [True: 0, False: 6.79M]
191
6.79M
                                     (nWord > 0xffff && nSize > 32));
  Branch (191:39): [True: 6.79M, False: 0]
  Branch (191:57): [True: 0, False: 6.79M]
192
6.79M
    return *this;
193
6.79M
}
194
195
uint32_t arith_uint256::GetCompact(bool fNegative) const
196
2.24M
{
197
2.24M
    int nSize = (bits() + 7) / 8;
198
2.24M
    uint32_t nCompact = 0;
199
2.24M
    if (nSize <= 3) {
  Branch (199:9): [True: 0, False: 2.24M]
200
0
        nCompact = GetLow64() << 8 * (3 - nSize);
201
2.24M
    } else {
202
2.24M
        arith_uint256 bn = *this >> 8 * (nSize - 3);
203
2.24M
        nCompact = bn.GetLow64();
204
2.24M
    }
205
    // The 0x00800000 bit denotes the sign.
206
    // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
207
2.24M
    if (nCompact & 0x00800000) {
  Branch (207:9): [True: 0, False: 2.24M]
208
0
        nCompact >>= 8;
209
0
        nSize++;
210
0
    }
211
2.24M
    assert((nCompact & ~0x007fffffU) == 0);
  Branch (211:5): [True: 2.24M, False: 0]
212
2.24M
    assert(nSize < 256);
  Branch (212:5): [True: 2.24M, False: 0]
213
2.24M
    nCompact |= nSize << 24;
214
2.24M
    nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
  Branch (214:18): [True: 0, False: 2.24M]
  Branch (214:31): [True: 0, False: 0]
215
2.24M
    return nCompact;
216
2.24M
}
217
218
uint256 ArithToUint256(const arith_uint256 &a)
219
11.0k
{
220
11.0k
    uint256 b;
221
99.8k
    for(int x=0; x<a.WIDTH; ++x)
  Branch (221:18): [True: 88.7k, False: 11.0k]
222
88.7k
        WriteLE32(b.begin() + x*4, a.pn[x]);
223
11.0k
    return b;
224
11.0k
}
225
arith_uint256 UintToArith256(const uint256 &a)
226
2.27M
{
227
2.27M
    arith_uint256 b;
228
20.4M
    for(int x=0; x<b.WIDTH; ++x)
  Branch (228:18): [True: 18.2M, False: 2.27M]
229
18.2M
        b.pn[x] = ReadLE32(a.begin() + x*4);
230
2.27M
    return b;
231
2.27M
}
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);