Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-03-24 13:57

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