Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/arith_uint256.h
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
#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
31.8M
    {
36
286M
        for (int i = 0; i < WIDTH; i++)
  Branch (36:25): [True: 254M, False: 31.8M]
  Branch (36:25): [True: 0, False: 0]
37
254M
            pn[i] = 0;
38
31.8M
    }
base_uint<256u>::base_uint()
Line
Count
Source
35
31.8M
    {
36
286M
        for (int i = 0; i < WIDTH; i++)
  Branch (36:25): [True: 254M, False: 31.8M]
37
254M
            pn[i] = 0;
38
31.8M
    }
Unexecuted instantiation: base_uint<6144u>::base_uint()
39
40
    base_uint(const base_uint& b)
41
128M
    {
42
1.16G
        for (int i = 0; i < WIDTH; i++)
  Branch (42:25): [True: 1.03G, False: 128M]
  Branch (42:25): [True: 0, False: 0]
43
1.03G
            pn[i] = b.pn[i];
44
128M
    }
base_uint<256u>::base_uint(base_uint<256u> const&)
Line
Count
Source
41
128M
    {
42
1.16G
        for (int i = 0; i < WIDTH; i++)
  Branch (42:25): [True: 1.03G, False: 128M]
43
1.03G
            pn[i] = b.pn[i];
44
128M
    }
Unexecuted instantiation: base_uint<6144u>::base_uint(base_uint<6144u> const&)
45
46
    base_uint& operator=(const base_uint& b)
47
13.5M
    {
48
13.5M
        if (this != &b) {
  Branch (48:13): [True: 13.5M, False: 0]
  Branch (48:13): [True: 0, False: 0]
49
122M
            for (int i = 0; i < WIDTH; i++)
  Branch (49:29): [True: 108M, False: 13.5M]
  Branch (49:29): [True: 0, False: 0]
50
108M
                pn[i] = b.pn[i];
51
13.5M
        }
52
13.5M
        return *this;
53
13.5M
    }
base_uint<256u>::operator=(base_uint<256u> const&)
Line
Count
Source
47
13.5M
    {
48
13.5M
        if (this != &b) {
  Branch (48:13): [True: 13.5M, False: 0]
49
122M
            for (int i = 0; i < WIDTH; i++)
  Branch (49:29): [True: 108M, False: 13.5M]
50
108M
                pn[i] = b.pn[i];
51
13.5M
        }
52
13.5M
        return *this;
53
13.5M
    }
Unexecuted instantiation: base_uint<6144u>::operator=(base_uint<6144u> const&)
54
55
    base_uint(uint64_t b)
56
27.2M
    {
57
27.2M
        pn[0] = (unsigned int)b;
58
27.2M
        pn[1] = (unsigned int)(b >> 32);
59
191M
        for (int i = 2; i < WIDTH; i++)
  Branch (59:25): [True: 163M, False: 27.2M]
60
163M
            pn[i] = 0;
61
27.2M
    }
62
63
    base_uint operator~() const
64
6.78M
    {
65
6.78M
        base_uint ret;
66
61.0M
        for (int i = 0; i < WIDTH; i++)
  Branch (66:25): [True: 54.2M, False: 6.78M]
67
54.2M
            ret.pn[i] = ~pn[i];
68
6.78M
        return ret;
69
6.78M
    }
70
71
    base_uint operator-() const
72
9.05M
    {
73
9.05M
        base_uint ret;
74
81.4M
        for (int i = 0; i < WIDTH; i++)
  Branch (74:25): [True: 72.4M, False: 9.05M]
  Branch (74:25): [True: 0, False: 0]
75
72.4M
            ret.pn[i] = ~pn[i];
76
9.05M
        ++ret;
77
9.05M
        return ret;
78
9.05M
    }
base_uint<256u>::operator-() const
Line
Count
Source
72
9.05M
    {
73
9.05M
        base_uint ret;
74
81.4M
        for (int i = 0; i < WIDTH; i++)
  Branch (74:25): [True: 72.4M, False: 9.05M]
75
72.4M
            ret.pn[i] = ~pn[i];
76
9.05M
        ++ret;
77
9.05M
        return ret;
78
9.05M
    }
Unexecuted instantiation: base_uint<6144u>::operator-() const
79
80
    double getdouble() const;
81
82
    base_uint& operator=(uint64_t b)
83
6.78M
    {
84
6.78M
        pn[0] = (unsigned int)b;
85
6.78M
        pn[1] = (unsigned int)(b >> 32);
86
47.4M
        for (int i = 2; i < WIDTH; i++)
  Branch (86:25): [True: 40.7M, False: 6.78M]
  Branch (86:25): [True: 0, False: 0]
87
40.7M
            pn[i] = 0;
88
6.78M
        return *this;
89
6.78M
    }
base_uint<256u>::operator=(unsigned long)
Line
Count
Source
83
6.78M
    {
84
6.78M
        pn[0] = (unsigned int)b;
85
6.78M
        pn[1] = (unsigned int)(b >> 32);
86
47.4M
        for (int i = 2; i < WIDTH; i++)
  Branch (86:25): [True: 40.7M, False: 6.78M]
87
40.7M
            pn[i] = 0;
88
6.78M
        return *this;
89
6.78M
    }
Unexecuted instantiation: base_uint<6144u>::operator=(unsigned long)
90
91
    base_uint& operator^=(const base_uint& b)
92
0
    {
93
0
        for (int i = 0; i < WIDTH; i++)
  Branch (93:25): [True: 0, False: 0]
94
0
            pn[i] ^= b.pn[i];
95
0
        return *this;
96
0
    }
97
98
    base_uint& operator&=(const base_uint& b)
99
0
    {
100
0
        for (int i = 0; i < WIDTH; i++)
  Branch (100:25): [True: 0, False: 0]
101
0
            pn[i] &= b.pn[i];
102
0
        return *this;
103
0
    }
104
105
    base_uint& operator|=(const base_uint& b)
106
0
    {
107
0
        for (int i = 0; i < WIDTH; i++)
  Branch (107:25): [True: 0, False: 0]
108
0
            pn[i] |= b.pn[i];
109
0
        return *this;
110
0
    }
111
112
    base_uint& operator^=(uint64_t b)
113
0
    {
114
0
        pn[0] ^= (unsigned int)b;
115
0
        pn[1] ^= (unsigned int)(b >> 32);
116
0
        return *this;
117
0
    }
118
119
    base_uint& operator|=(uint64_t b)
120
0
    {
121
0
        pn[0] |= (unsigned int)b;
122
0
        pn[1] |= (unsigned int)(b >> 32);
123
0
        return *this;
124
0
    }
125
126
    base_uint& operator<<=(unsigned int shift);
127
    base_uint& operator>>=(unsigned int shift);
128
129
    base_uint& operator+=(const base_uint& b)
130
29.4M
    {
131
29.4M
        uint64_t carry = 0;
132
264M
        for (int i = 0; i < WIDTH; i++)
  Branch (132:25): [True: 235M, False: 29.4M]
  Branch (132:25): [True: 0, False: 0]
133
235M
        {
134
235M
            uint64_t n = carry + pn[i] + b.pn[i];
135
235M
            pn[i] = n & 0xffffffff;
136
235M
            carry = n >> 32;
137
235M
        }
138
29.4M
        return *this;
139
29.4M
    }
base_uint<256u>::operator+=(base_uint<256u> const&)
Line
Count
Source
130
29.4M
    {
131
29.4M
        uint64_t carry = 0;
132
264M
        for (int i = 0; i < WIDTH; i++)
  Branch (132:25): [True: 235M, False: 29.4M]
133
235M
        {
134
235M
            uint64_t n = carry + pn[i] + b.pn[i];
135
235M
            pn[i] = n & 0xffffffff;
136
235M
            carry = n >> 32;
137
235M
        }
138
29.4M
        return *this;
139
29.4M
    }
Unexecuted instantiation: base_uint<6144u>::operator+=(base_uint<6144u> const&)
140
141
    base_uint& operator-=(const base_uint& b)
142
9.05M
    {
143
9.05M
        *this += -b;
144
9.05M
        return *this;
145
9.05M
    }
base_uint<256u>::operator-=(base_uint<256u> const&)
Line
Count
Source
142
9.05M
    {
143
9.05M
        *this += -b;
144
9.05M
        return *this;
145
9.05M
    }
Unexecuted instantiation: base_uint<6144u>::operator-=(base_uint<6144u> const&)
146
147
    base_uint& operator+=(uint64_t b64)
148
0
    {
149
0
        base_uint b;
150
0
        b = b64;
151
0
        *this += b;
152
0
        return *this;
153
0
    }
154
155
    base_uint& operator-=(uint64_t b64)
156
0
    {
157
0
        base_uint b;
158
0
        b = b64;
159
0
        *this += -b;
160
0
        return *this;
161
0
    }
162
163
    base_uint& operator*=(uint32_t b32);
164
    base_uint& operator*=(const base_uint& b);
165
    base_uint& operator/=(const base_uint& b);
166
167
    base_uint& operator++()
168
9.05M
    {
169
        // prefix operator
170
9.05M
        int i = 0;
171
9.05M
        while (i < WIDTH && ++pn[i] == 0)
  Branch (171:16): [True: 9.05M, False: 0]
  Branch (171:29): [True: 0, False: 9.05M]
  Branch (171:16): [True: 0, False: 0]
  Branch (171:29): [True: 0, False: 0]
172
0
            i++;
173
9.05M
        return *this;
174
9.05M
    }
base_uint<256u>::operator++()
Line
Count
Source
168
9.05M
    {
169
        // prefix operator
170
9.05M
        int i = 0;
171
9.05M
        while (i < WIDTH && ++pn[i] == 0)
  Branch (171:16): [True: 9.05M, False: 0]
  Branch (171:29): [True: 0, False: 9.05M]
172
0
            i++;
173
9.05M
        return *this;
174
9.05M
    }
Unexecuted instantiation: base_uint<6144u>::operator++()
175
176
    base_uint operator++(int)
177
0
    {
178
        // postfix operator
179
0
        const base_uint ret = *this;
180
0
        ++(*this);
181
0
        return ret;
182
0
    }
183
184
    base_uint& operator--()
185
0
    {
186
        // prefix operator
187
0
        int i = 0;
188
0
        while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
  Branch (188:16): [True: 0, False: 0]
  Branch (188:29): [True: 0, False: 0]
189
0
            i++;
190
0
        return *this;
191
0
    }
192
193
    base_uint operator--(int)
194
0
    {
195
        // postfix operator
196
0
        const base_uint ret = *this;
197
0
        --(*this);
198
0
        return ret;
199
0
    }
200
201
    /** Numeric ordering (unlike \ref base_blob::Compare) */
202
    int CompareTo(const base_uint& b) const;
203
    bool EqualTo(uint64_t b) const;
204
205
18.0M
    friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
206
2.26M
    friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
207
2.26M
    friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
208
6.78M
    friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
209
    friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
210
    friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
211
    friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
212
2.24M
    friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
213
    friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
214
8.40k
    friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
215
    friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
216
4.23G
    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
216
4.23G
    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&)
217
6.79M
    friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
218
219
    /** Hex encoding of the number (with the most significant digits first). */
220
    std::string GetHex() const;
221
    std::string ToString() const;
222
223
    unsigned int size() const
224
0
    {
225
0
        return sizeof(pn);
226
0
    }
227
228
    /**
229
     * Returns the position of the highest bit set plus one, or zero if the
230
     * value is zero.
231
     */
232
    unsigned int bits() const;
233
234
    uint64_t GetLow64() const
235
2.24M
    {
236
2.24M
        static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
237
2.24M
        return pn[0] | (uint64_t)pn[1] << 32;
238
2.24M
    }
239
};
240
241
/** 256-bit unsigned big integer. */
242
class arith_uint256 : public base_uint<256> {
243
public:
244
13.7M
    arith_uint256() = default;
245
15.8M
    arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
246
11.3M
    arith_uint256(uint64_t b) : base_uint<256>(b) {}
247
248
    /**
249
     * The "compact" format is a representation of a whole
250
     * number N using an unsigned 32bit number similar to a
251
     * floating point format.
252
     * The most significant 8 bits are the unsigned exponent of base 256.
253
     * This exponent can be thought of as "number of bytes of N".
254
     * The lower 23 bits are the mantissa.
255
     * Bit number 24 (0x800000) represents the sign of N.
256
     * N = (-1^sign) * mantissa * 256^(exponent-3)
257
     *
258
     * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
259
     * MPI uses the most significant bit of the first byte as sign.
260
     * Thus 0x1234560000 is compact (0x05123456)
261
     * and  0xc0de000000 is compact (0x0600c0de)
262
     *
263
     * Bitcoin only uses this "compact" format for encoding difficulty
264
     * targets, which are unsigned 256bit quantities.  Thus, all the
265
     * complexities of the sign bit and using base 256 are probably an
266
     * implementation accident.
267
     */
268
    arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
269
    uint32_t GetCompact(bool fNegative = false) const;
270
271
    friend uint256 ArithToUint256(const arith_uint256 &);
272
    friend arith_uint256 UintToArith256(const uint256 &);
273
};
274
275
uint256 ArithToUint256(const arith_uint256 &);
276
arith_uint256 UintToArith256(const uint256 &);
277
278
extern template class base_uint<256>;
279
280
#endif // BITCOIN_ARITH_UINT256_H