Coverage Report

Created: 2025-03-18 19:34

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