Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/arith_uint256.h
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
#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
2
    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
33
public:
34
    constexpr base_uint()
35
47.7M
    {
36
429M
        for (int i = 0; i < WIDTH; i++)
37
381M
            pn[i] = 0;
38
47.7M
    }
base_uint<256u>::base_uint()
Line
Count
Source
35
47.7M
    {
36
429M
        for (int i = 0; i < WIDTH; i++)
37
381M
            pn[i] = 0;
38
47.7M
    }
Unexecuted instantiation: base_uint<6144u>::base_uint()
39
40
    base_uint(const base_uint& b) = default;
41
    base_uint& operator=(const base_uint& b) = default;
42
43
    constexpr base_uint(uint64_t b)
44
5.74M
    {
45
5.74M
        pn[0] = (unsigned int)b;
46
5.74M
        pn[1] = (unsigned int)(b >> 32);
47
40.2M
        for (int i = 2; i < WIDTH; i++)
48
34.4M
            pn[i] = 0;
49
5.74M
    }
50
51
    base_uint operator~() const
52
1.01M
    {
53
1.01M
        base_uint ret;
54
9.09M
        for (int i = 0; i < WIDTH; i++)
55
8.08M
            ret.pn[i] = ~pn[i];
56
1.01M
        return ret;
57
1.01M
    }
58
59
    base_uint operator-() const
60
1.09M
    {
61
1.09M
        base_uint ret;
62
9.83M
        for (int i = 0; i < WIDTH; i++)
63
8.73M
            ret.pn[i] = ~pn[i];
64
1.09M
        ++ret;
65
1.09M
        return ret;
66
1.09M
    }
base_uint<256u>::operator-() const
Line
Count
Source
60
1.09M
    {
61
1.09M
        base_uint ret;
62
9.83M
        for (int i = 0; i < WIDTH; i++)
63
8.73M
            ret.pn[i] = ~pn[i];
64
1.09M
        ++ret;
65
1.09M
        return ret;
66
1.09M
    }
Unexecuted instantiation: base_uint<6144u>::operator-() const
67
68
    double getdouble() const;
69
70
    base_uint& operator=(uint64_t b)
71
1.01M
    {
72
1.01M
        pn[0] = (unsigned int)b;
73
1.01M
        pn[1] = (unsigned int)(b >> 32);
74
7.11M
        for (int i = 2; i < WIDTH; i++)
75
6.10M
            pn[i] = 0;
76
1.01M
        return *this;
77
1.01M
    }
base_uint<256u>::operator=(unsigned long)
Line
Count
Source
71
1.01M
    {
72
1.01M
        pn[0] = (unsigned int)b;
73
1.01M
        pn[1] = (unsigned int)(b >> 32);
74
7.11M
        for (int i = 2; i < WIDTH; i++)
75
6.10M
            pn[i] = 0;
76
1.01M
        return *this;
77
1.01M
    }
Unexecuted instantiation: base_uint<6144u>::operator=(unsigned long)
78
79
    base_uint& operator^=(const base_uint& b)
80
534
    {
81
4.80k
        for (int i = 0; i < WIDTH; i++)
82
4.27k
            pn[i] ^= b.pn[i];
83
534
        return *this;
84
534
    }
85
86
    base_uint& operator&=(const base_uint& b)
87
18
    {
88
162
        for (int i = 0; i < WIDTH; i++)
89
144
            pn[i] &= b.pn[i];
90
18
        return *this;
91
18
    }
92
93
    base_uint& operator|=(const base_uint& b)
94
274
    {
95
2.46k
        for (int i = 0; i < WIDTH; i++)
96
2.19k
            pn[i] |= b.pn[i];
97
274
        return *this;
98
274
    }
99
100
    base_uint& operator^=(uint64_t b)
101
2
    {
102
2
        pn[0] ^= (unsigned int)b;
103
2
        pn[1] ^= (unsigned int)(b >> 32);
104
2
        return *this;
105
2
    }
106
107
    base_uint& operator|=(uint64_t b)
108
2
    {
109
2
        pn[0] |= (unsigned int)b;
110
2
        pn[1] |= (unsigned int)(b >> 32);
111
2
        return *this;
112
2
    }
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
4.13M
    {
119
4.13M
        uint64_t carry = 0;
120
37.1M
        for (int i = 0; i < WIDTH; i++)
121
33.0M
        {
122
33.0M
            uint64_t n = carry + pn[i] + b.pn[i];
123
33.0M
            pn[i] = n & 0xffffffff;
124
33.0M
            carry = n >> 32;
125
33.0M
        }
126
4.13M
        return *this;
127
4.13M
    }
base_uint<256u>::operator+=(base_uint<256u> const&)
Line
Count
Source
118
4.13M
    {
119
4.13M
        uint64_t carry = 0;
120
37.1M
        for (int i = 0; i < WIDTH; i++)
121
33.0M
        {
122
33.0M
            uint64_t n = carry + pn[i] + b.pn[i];
123
33.0M
            pn[i] = n & 0xffffffff;
124
33.0M
            carry = n >> 32;
125
33.0M
        }
126
4.13M
        return *this;
127
4.13M
    }
Unexecuted instantiation: base_uint<6144u>::operator+=(base_uint<6144u> const&)
128
129
    base_uint& operator-=(const base_uint& b)
130
1.09M
    {
131
1.09M
        *this += -b;
132
1.09M
        return *this;
133
1.09M
    }
base_uint<256u>::operator-=(base_uint<256u> const&)
Line
Count
Source
130
1.09M
    {
131
1.09M
        *this += -b;
132
1.09M
        return *this;
133
1.09M
    }
Unexecuted instantiation: base_uint<6144u>::operator-=(base_uint<6144u> const&)
134
135
    base_uint& operator+=(uint64_t b64)
136
4.93k
    {
137
4.93k
        base_uint b;
138
4.93k
        b = b64;
139
4.93k
        *this += b;
140
4.93k
        return *this;
141
4.93k
    }
142
143
    base_uint& operator-=(uint64_t b64)
144
1
    {
145
1
        base_uint b;
146
1
        b = b64;
147
1
        *this += -b;
148
1
        return *this;
149
1
    }
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
1.09M
    {
157
        // prefix operator
158
1.09M
        int i = 0;
159
1.10M
        while (i < WIDTH && ++pn[i] == 0)
160
10.1k
            i++;
161
1.09M
        return *this;
162
1.09M
    }
base_uint<256u>::operator++()
Line
Count
Source
156
1.09M
    {
157
        // prefix operator
158
1.09M
        int i = 0;
159
1.10M
        while (i < WIDTH && ++pn[i] == 0)
160
10.1k
            i++;
161
1.09M
        return *this;
162
1.09M
    }
Unexecuted instantiation: base_uint<6144u>::operator++()
163
164
    base_uint operator++(int)
165
255
    {
166
        // postfix operator
167
255
        const base_uint ret = *this;
168
255
        ++(*this);
169
255
        return ret;
170
255
    }
171
172
    base_uint& operator--()
173
511
    {
174
        // prefix operator
175
511
        int i = 0;
176
2.30k
        while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
177
1.79k
            i++;
178
511
        return *this;
179
511
    }
180
181
    base_uint operator--(int)
182
255
    {
183
        // postfix operator
184
255
        const base_uint ret = *this;
185
255
        --(*this);
186
255
        return ret;
187
255
    }
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
2.41M
    friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
194
64.8k
    friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
195
60.3k
    friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
196
1.01M
    friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
197
13
    friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
198
13
    friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
199
529
    friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
200
260k
    friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
201
54.4k
    friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
202
12.9k
    friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
203
8.30k
    friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
204
948M
    friend inline std::strong_ordering operator<=>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <=> 0; }
operator<=>(base_uint<256u> const&, base_uint<256u> const&)
Line
Count
Source
204
948M
    friend inline std::strong_ordering operator<=>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <=> 0; }
Unexecuted instantiation: operator<=>(base_uint<6144u> const&, base_uint<6144u> const&)
205
3.24M
    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
4
    {
213
4
        return sizeof(pn);
214
4
    }
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
412k
    {
224
412k
        static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
225
412k
        return pn[0] | (uint64_t)pn[1] << 32;
226
412k
    }
227
};
228
229
/** 256-bit unsigned big integer. */
230
class arith_uint256 : public base_uint<256>
231
{
232
public:
233
45.5M
    constexpr arith_uint256() = default;
234
1.70M
    constexpr arith_uint256(const base_uint& b) : base_uint(b) {}
235
3.57M
    constexpr arith_uint256(uint64_t b) : base_uint(b) {}
236
237
    /**
238
     * The "compact" format is a representation of a whole
239
     * number N using an unsigned 32bit number similar to a
240
     * floating point format.
241
     * The most significant 8 bits are the unsigned exponent of base 256.
242
     * This exponent can be thought of as "number of bytes of N".
243
     * The lower 23 bits are the mantissa.
244
     * Bit number 24 (0x800000) represents the sign of N.
245
     * N = (-1^sign) * mantissa * 256^(exponent-3)
246
     *
247
     * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
248
     * MPI uses the most significant bit of the first byte as sign.
249
     * Thus 0x1234560000 is compact (0x05123456)
250
     * and  0xc0de000000 is compact (0x0600c0de)
251
     *
252
     * Bitcoin only uses this "compact" format for encoding difficulty
253
     * targets, which are unsigned 256bit quantities.  Thus, all the
254
     * complexities of the sign bit and using base 256 are probably an
255
     * implementation accident.
256
     */
257
    arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
258
    uint32_t GetCompact(bool fNegative = false) const;
259
260
    friend uint256 ArithToUint256(const arith_uint256 &);
261
    friend arith_uint256 UintToArith256(const uint256 &);
262
};
263
264
// Keeping the trivially copyable property is beneficial for performance
265
static_assert(std::is_trivially_copyable_v<arith_uint256>);
266
267
uint256 ArithToUint256(const arith_uint256 &);
268
arith_uint256 UintToArith256(const uint256 &);
269
270
extern template class base_uint<256>;
271
272
#endif // BITCOIN_ARITH_UINT256_H