fuzz coverage

Coverage Report

Created: 2025-10-29 15:27

/Users/eugenesiegel/btc/bitcoin/src/arith_uint256.h
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
#ifndef BITCOIN_ARITH_UINT256_H
7
#define BITCOIN_ARITH_UINT256_H
8
9
#include <compare>
10
#include <cstdint>
11
#include <cstring>
12
#include <limits>
13
#include <stdexcept>
14
#include <string>
15
16
class uint256;
17
18
class uint_error : public std::runtime_error {
19
public:
20
0
    explicit uint_error(const std::string& str) : std::runtime_error(str) {}
21
};
22
23
/** Template base class for unsigned big integers. */
24
template<unsigned int BITS>
25
class base_uint
26
{
27
protected:
28
    static_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");
29
    static constexpr int WIDTH = BITS / 32;
30
    /** Big integer represented with 32-bit digits, least-significant first. */
31
    uint32_t pn[WIDTH];
32
public:
33
34
    base_uint()
35
71.0M
    {
36
639M
        for (int i = 0; i < WIDTH; 
i++568M
)
37
568M
            pn[i] = 0;
38
71.0M
    }
_ZN9base_uintILj256EEC2Ev
Line
Count
Source
35
71.0M
    {
36
639M
        for (int i = 0; i < WIDTH; 
i++568M
)
37
568M
            pn[i] = 0;
38
71.0M
    }
Unexecuted instantiation: _ZN9base_uintILj6144EEC2Ev
39
40
    base_uint(const base_uint& b) = default;
41
    base_uint& operator=(const base_uint& b) = default;
42
43
    base_uint(uint64_t b)
44
49.3M
    {
45
49.3M
        pn[0] = (unsigned int)b;
46
49.3M
        pn[1] = (unsigned int)(b >> 32);
47
345M
        for (int i = 2; i < WIDTH; 
i++295M
)
48
295M
            pn[i] = 0;
49
49.3M
    }
_ZN9base_uintILj256EEC2Ey
Line
Count
Source
44
49.3M
    {
45
49.3M
        pn[0] = (unsigned int)b;
46
49.3M
        pn[1] = (unsigned int)(b >> 32);
47
345M
        for (int i = 2; i < WIDTH; 
i++295M
)
48
295M
            pn[i] = 0;
49
49.3M
    }
Unexecuted instantiation: _ZN9base_uintILj6144EEC2Ey
50
51
    base_uint operator~() const
52
14.0M
    {
53
14.0M
        base_uint ret;
54
126M
        for (int i = 0; i < WIDTH; 
i++112M
)
55
112M
            ret.pn[i] = ~pn[i];
56
14.0M
        return ret;
57
14.0M
    }
58
59
    base_uint operator-() const
60
15.8M
    {
61
15.8M
        base_uint ret;
62
142M
        for (int i = 0; i < WIDTH; 
i++126M
)
63
126M
            ret.pn[i] = ~pn[i];
64
15.8M
        ++ret;
65
15.8M
        return ret;
66
15.8M
    }
_ZNK9base_uintILj256EEngEv
Line
Count
Source
60
15.8M
    {
61
15.8M
        base_uint ret;
62
142M
        for (int i = 0; i < WIDTH; 
i++126M
)
63
126M
            ret.pn[i] = ~pn[i];
64
15.8M
        ++ret;
65
15.8M
        return ret;
66
15.8M
    }
Unexecuted instantiation: _ZNK9base_uintILj6144EEngEv
67
68
    double getdouble() const;
69
70
    base_uint& operator=(uint64_t b)
71
14.0M
    {
72
14.0M
        pn[0] = (unsigned int)b;
73
14.0M
        pn[1] = (unsigned int)(b >> 32);
74
98.3M
        for (int i = 2; i < WIDTH; 
i++84.2M
)
75
84.2M
            pn[i] = 0;
76
14.0M
        return *this;
77
14.0M
    }
_ZN9base_uintILj256EEaSEy
Line
Count
Source
71
14.0M
    {
72
14.0M
        pn[0] = (unsigned int)b;
73
14.0M
        pn[1] = (unsigned int)(b >> 32);
74
98.3M
        for (int i = 2; i < WIDTH; 
i++84.2M
)
75
84.2M
            pn[i] = 0;
76
14.0M
        return *this;
77
14.0M
    }
Unexecuted instantiation: _ZN9base_uintILj6144EEaSEy
78
79
    base_uint& operator^=(const base_uint& b)
80
0
    {
81
0
        for (int i = 0; i < WIDTH; i++)
82
0
            pn[i] ^= b.pn[i];
83
0
        return *this;
84
0
    }
85
86
    base_uint& operator&=(const base_uint& b)
87
0
    {
88
0
        for (int i = 0; i < WIDTH; i++)
89
0
            pn[i] &= b.pn[i];
90
0
        return *this;
91
0
    }
92
93
    base_uint& operator|=(const base_uint& b)
94
0
    {
95
0
        for (int i = 0; i < WIDTH; i++)
96
0
            pn[i] |= b.pn[i];
97
0
        return *this;
98
0
    }
99
100
    base_uint& operator^=(uint64_t b)
101
0
    {
102
0
        pn[0] ^= (unsigned int)b;
103
0
        pn[1] ^= (unsigned int)(b >> 32);
104
0
        return *this;
105
0
    }
106
107
    base_uint& operator|=(uint64_t b)
108
0
    {
109
0
        pn[0] |= (unsigned int)b;
110
0
        pn[1] |= (unsigned int)(b >> 32);
111
0
        return *this;
112
0
    }
113
114
    base_uint& operator<<=(unsigned int shift);
115
    base_uint& operator>>=(unsigned int shift);
116
117
    base_uint& operator+=(const base_uint& b)
118
57.9M
    {
119
57.9M
        uint64_t carry = 0;
120
521M
        for (int i = 0; i < WIDTH; 
i++463M
)
121
463M
        {
122
463M
            uint64_t n = carry + pn[i] + b.pn[i];
123
463M
            pn[i] = n & 0xffffffff;
124
463M
            carry = n >> 32;
125
463M
        }
126
57.9M
        return *this;
127
57.9M
    }
_ZN9base_uintILj256EEpLERKS0_
Line
Count
Source
118
57.9M
    {
119
57.9M
        uint64_t carry = 0;
120
521M
        for (int i = 0; i < WIDTH; 
i++463M
)
121
463M
        {
122
463M
            uint64_t n = carry + pn[i] + b.pn[i];
123
463M
            pn[i] = n & 0xffffffff;
124
463M
            carry = n >> 32;
125
463M
        }
126
57.9M
        return *this;
127
57.9M
    }
Unexecuted instantiation: _ZN9base_uintILj6144EEpLERKS0_
128
129
    base_uint& operator-=(const base_uint& b)
130
15.8M
    {
131
15.8M
        *this += -b;
132
15.8M
        return *this;
133
15.8M
    }
_ZN9base_uintILj256EEmIERKS0_
Line
Count
Source
130
15.8M
    {
131
15.8M
        *this += -b;
132
15.8M
        return *this;
133
15.8M
    }
Unexecuted instantiation: _ZN9base_uintILj6144EEmIERKS0_
134
135
    base_uint& operator+=(uint64_t b64)
136
0
    {
137
0
        base_uint b;
138
0
        b = b64;
139
0
        *this += b;
140
0
        return *this;
141
0
    }
142
143
    base_uint& operator-=(uint64_t b64)
144
0
    {
145
0
        base_uint b;
146
0
        b = b64;
147
0
        *this += -b;
148
0
        return *this;
149
0
    }
150
151
    base_uint& operator*=(uint32_t b32);
152
    base_uint& operator*=(const base_uint& b);
153
    base_uint& operator/=(const base_uint& b);
154
155
    base_uint& operator++()
156
15.8M
    {
157
        // prefix operator
158
15.8M
        int i = 0;
159
15.8M
        while (i < WIDTH && ++pn[i] == 0)
160
0
            i++;
161
15.8M
        return *this;
162
15.8M
    }
_ZN9base_uintILj256EEppEv
Line
Count
Source
156
15.8M
    {
157
        // prefix operator
158
15.8M
        int i = 0;
159
15.8M
        while (i < WIDTH && ++pn[i] == 0)
160
0
            i++;
161
15.8M
        return *this;
162
15.8M
    }
Unexecuted instantiation: _ZN9base_uintILj6144EEppEv
163
164
    base_uint operator++(int)
165
0
    {
166
        // postfix operator
167
0
        const base_uint ret = *this;
168
0
        ++(*this);
169
0
        return ret;
170
0
    }
171
172
    base_uint& operator--()
173
0
    {
174
        // prefix operator
175
0
        int i = 0;
176
0
        while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
177
0
            i++;
178
0
        return *this;
179
0
    }
180
181
    base_uint operator--(int)
182
0
    {
183
        // postfix operator
184
0
        const base_uint ret = *this;
185
0
        --(*this);
186
0
        return ret;
187
0
    }
188
189
    /** Numeric ordering (unlike \ref base_blob::Compare) */
190
    int CompareTo(const base_uint& b) const;
191
    bool EqualTo(uint64_t b) const;
192
193
40.3M
    friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
194
1.80M
    friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
195
1.80M
    friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
196
14.0M
    friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
197
    friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
198
    friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
199
    friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
200
353k
    friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
201
0
    friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
202
10.3k
    friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
203
0
    friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
Unexecuted instantiation: _ZeqRK9base_uintILj256EES2_
Unexecuted instantiation: _ZeqRK9base_uintILj6144EES2_
204
3.33G
    friend inline std::strong_ordering operator<=>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <=> 0; }
_ZssRK9base_uintILj256EES2_
Line
Count
Source
204
3.33G
    friend inline std::strong_ordering operator<=>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <=> 0; }
Unexecuted instantiation: _ZssRK9base_uintILj6144EES2_
205
14.0M
    friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
206
207
    /** Hex encoding of the number (with the most significant digits first). */
208
    std::string GetHex() const;
209
    std::string ToString() const;
210
211
    unsigned int size() const
212
0
    {
213
0
        return sizeof(pn);
214
0
    }
215
216
    /**
217
     * Returns the position of the highest bit set plus one, or zero if the
218
     * value is zero.
219
     */
220
    unsigned int bits() const;
221
222
    uint64_t GetLow64() const
223
353k
    {
224
353k
        static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
225
353k
        return pn[0] | (uint64_t)pn[1] << 32;
226
353k
    }
227
};
228
229
/** 256-bit unsigned big integer. */
230
class arith_uint256 : public base_uint<256> {
231
public:
232
39.3M
    arith_uint256() = default;
233
28.9M
    arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
234
17.7M
    arith_uint256(uint64_t b) : base_uint<256>(b) {}
235
236
    /**
237
     * The "compact" format is a representation of a whole
238
     * number N using an unsigned 32bit number similar to a
239
     * floating point format.
240
     * The most significant 8 bits are the unsigned exponent of base 256.
241
     * This exponent can be thought of as "number of bytes of N".
242
     * The lower 23 bits are the mantissa.
243
     * Bit number 24 (0x800000) represents the sign of N.
244
     * N = (-1^sign) * mantissa * 256^(exponent-3)
245
     *
246
     * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
247
     * MPI uses the most significant bit of the first byte as sign.
248
     * Thus 0x1234560000 is compact (0x05123456)
249
     * and  0xc0de000000 is compact (0x0600c0de)
250
     *
251
     * Bitcoin only uses this "compact" format for encoding difficulty
252
     * targets, which are unsigned 256bit quantities.  Thus, all the
253
     * complexities of the sign bit and using base 256 are probably an
254
     * implementation accident.
255
     */
256
    arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
257
    uint32_t GetCompact(bool fNegative = false) const;
258
259
    friend uint256 ArithToUint256(const arith_uint256 &);
260
    friend arith_uint256 UintToArith256(const uint256 &);
261
};
262
263
// Keeping the trivially copyable property is beneficial for performance
264
static_assert(std::is_trivially_copyable_v<arith_uint256>);
265
266
uint256 ArithToUint256(const arith_uint256 &);
267
arith_uint256 UintToArith256(const uint256 &);
268
269
extern template class base_uint<256>;
270
271
#endif // BITCOIN_ARITH_UINT256_H