Coverage Report

Created: 2025-09-08 17:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/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_UINT256_H
7
#define BITCOIN_UINT256_H
8
9
#include <crypto/common.h>
10
#include <span.h>
11
#include <util/strencodings.h>
12
#include <util/string.h>
13
14
#include <algorithm>
15
#include <array>
16
#include <cassert>
17
#include <cstdint>
18
#include <cstring>
19
#include <optional>
20
#include <string>
21
#include <string_view>
22
23
/** Template base class for fixed-sized opaque blobs. */
24
template<unsigned int BITS>
25
class base_blob
26
{
27
protected:
28
    static constexpr int WIDTH = BITS / 8;
29
    static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
30
    std::array<uint8_t, WIDTH> m_data;
31
    static_assert(WIDTH == sizeof(m_data), "Sanity check");
32
33
public:
34
    /* construct 0 value by default */
35
3.07G
    constexpr base_blob() : m_data() {}
_ZN9base_blobILj256EEC2Ev
Line
Count
Source
35
3.03G
    constexpr base_blob() : m_data() {}
_ZN9base_blobILj160EEC2Ev
Line
Count
Source
35
40.6M
    constexpr base_blob() : m_data() {}
36
37
    /* constructor for constants between 1 and 255 */
38
38.0k
    constexpr explicit base_blob(uint8_t v) : m_data{v} {}
39
40
    constexpr explicit base_blob(std::span<const unsigned char> vch)
41
6.74M
    {
42
6.74M
        assert(vch.size() == WIDTH);
43
6.74M
        std::copy(vch.begin(), vch.end(), m_data.begin());
44
6.74M
    }
_ZN9base_blobILj256EEC2ESt4spanIKhLm18446744073709551615EE
Line
Count
Source
41
5.68M
    {
42
5.68M
        assert(vch.size() == WIDTH);
43
5.68M
        std::copy(vch.begin(), vch.end(), m_data.begin());
44
5.68M
    }
_ZN9base_blobILj160EEC2ESt4spanIKhLm18446744073709551615EE
Line
Count
Source
41
1.05M
    {
42
1.05M
        assert(vch.size() == WIDTH);
43
1.05M
        std::copy(vch.begin(), vch.end(), m_data.begin());
44
1.05M
    }
45
46
    consteval explicit base_blob(std::string_view hex_str);
47
48
    constexpr bool IsNull() const
49
53.0M
    {
50
374M
        return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
51
374M
            return val == 0;
52
374M
        });
_ZZNK9base_blobILj256EE6IsNullEvENKUlhE_clEh
Line
Count
Source
50
373M
        return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
51
373M
            return val == 0;
52
373M
        });
_ZZNK9base_blobILj160EE6IsNullEvENKUlhE_clEh
Line
Count
Source
50
347k
        return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
51
347k
            return val == 0;
52
347k
        });
53
53.0M
    }
_ZNK9base_blobILj256EE6IsNullEv
Line
Count
Source
49
52.9M
    {
50
52.9M
        return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
51
52.9M
            return val == 0;
52
52.9M
        });
53
52.9M
    }
_ZNK9base_blobILj160EE6IsNullEv
Line
Count
Source
49
18.5k
    {
50
18.5k
        return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
51
18.5k
            return val == 0;
52
18.5k
        });
53
18.5k
    }
54
55
    constexpr void SetNull()
56
24.7M
    {
57
24.7M
        std::fill(m_data.begin(), m_data.end(), 0);
58
24.7M
    }
_ZN9base_blobILj256EE7SetNullEv
Line
Count
Source
56
24.7M
    {
57
24.7M
        std::fill(m_data.begin(), m_data.end(), 0);
58
24.7M
    }
_ZN9base_blobILj160EE7SetNullEv
Line
Count
Source
56
24.0k
    {
57
24.0k
        std::fill(m_data.begin(), m_data.end(), 0);
58
24.0k
    }
59
60
    /** Lexicographic ordering
61
     * @note Does NOT match the ordering on the corresponding \ref
62
     *       base_uint::CompareTo, which starts comparing from the end.
63
     */
64
7.07G
    constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
_ZNK9base_blobILj256EE7CompareERKS0_
Line
Count
Source
64
6.96G
    constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
_ZNK9base_blobILj160EE7CompareERKS0_
Line
Count
Source
64
115M
    constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
65
66
31.9M
    friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
_ZeqRK9base_blobILj256EES2_
Line
Count
Source
66
31.8M
    friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
_ZeqRK9base_blobILj160EES2_
Line
Count
Source
66
87.2k
    friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
67
4.48M
    friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
_ZneRK9base_blobILj256EES2_
Line
Count
Source
67
4.48M
    friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
Unexecuted instantiation: _ZneRK9base_blobILj160EES2_
68
319M
    friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
_ZltRK9base_blobILj256EES2_
Line
Count
Source
68
203M
    friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
_ZltRK9base_blobILj160EES2_
Line
Count
Source
68
115M
    friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
69
70
    /** @name Hex representation
71
     *
72
     * The hex representation used by GetHex(), ToString(), and FromHex()
73
     * is unusual, since it shows bytes of the base_blob in reverse order.
74
     * For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is represented
75
     * as "78563412" instead of the more typical "12345678" representation
76
     * that would be shown in a hex editor or used by typical
77
     * byte-array / hex conversion functions like python's bytes.hex() and
78
     * bytes.fromhex().
79
     *
80
     * The nice thing about the reverse-byte representation, even though it is
81
     * unusual, is that if a blob contains an arithmetic number in little endian
82
     * format (with least significant bytes first, and most significant bytes
83
     * last), the GetHex() output will match the way the number would normally
84
     * be written in base-16 (with most significant digits first and least
85
     * significant digits last).
86
     *
87
     * This means, for example, that ArithToUint256(num).GetHex() can be used to
88
     * display an arith_uint256 num value as a number, because
89
     * ArithToUint256() converts the number to a blob in little-endian format,
90
     * so the arith_uint256 class doesn't need to have its own number parsing
91
     * and formatting functions.
92
     *
93
     * @{*/
94
    std::string GetHex() const;
95
    std::string ToString() const;
96
    /**@}*/
97
98
106M
    constexpr const unsigned char* data() const { return m_data.data(); }
_ZNK9base_blobILj160EE4dataEv
Line
Count
Source
98
64.5k
    constexpr const unsigned char* data() const { return m_data.data(); }
_ZNK9base_blobILj256EE4dataEv
Line
Count
Source
98
106M
    constexpr const unsigned char* data() const { return m_data.data(); }
99
87.2M
    constexpr unsigned char* data() { return m_data.data(); }
_ZN9base_blobILj160EE4dataEv
Line
Count
Source
99
26.4M
    constexpr unsigned char* data() { return m_data.data(); }
_ZN9base_blobILj256EE4dataEv
Line
Count
Source
99
60.7M
    constexpr unsigned char* data() { return m_data.data(); }
100
101
730M
    constexpr unsigned char* begin() { return m_data.data(); }
_ZN9base_blobILj160EE5beginEv
Line
Count
Source
101
13.0M
    constexpr unsigned char* begin() { return m_data.data(); }
_ZN9base_blobILj256EE5beginEv
Line
Count
Source
101
717M
    constexpr unsigned char* begin() { return m_data.data(); }
102
19.9k
    constexpr unsigned char* end() { return m_data.data() + WIDTH; }
_ZN9base_blobILj160EE3endEv
Line
Count
Source
102
19.0k
    constexpr unsigned char* end() { return m_data.data() + WIDTH; }
_ZN9base_blobILj256EE3endEv
Line
Count
Source
102
884
    constexpr unsigned char* end() { return m_data.data() + WIDTH; }
103
104
43.6M
    constexpr const unsigned char* begin() const { return m_data.data(); }
_ZNK9base_blobILj160EE5beginEv
Line
Count
Source
104
1.62M
    constexpr const unsigned char* begin() const { return m_data.data(); }
_ZNK9base_blobILj256EE5beginEv
Line
Count
Source
104
42.0M
    constexpr const unsigned char* begin() const { return m_data.data(); }
105
8.84M
    constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
_ZNK9base_blobILj160EE3endEv
Line
Count
Source
105
1.62M
    constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
_ZNK9base_blobILj256EE3endEv
Line
Count
Source
105
7.22M
    constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
106
107
186M
    static constexpr unsigned int size() { return WIDTH; }
_ZN9base_blobILj160EE4sizeEv
Line
Count
Source
107
26.5M
    static constexpr unsigned int size() { return WIDTH; }
_ZN9base_blobILj256EE4sizeEv
Line
Count
Source
107
160M
    static constexpr unsigned int size() { return WIDTH; }
108
109
4.24G
    constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
110
111
    template<typename Stream>
112
    void Serialize(Stream& s) const
113
11.4G
    {
114
11.4G
        s << std::span(m_data);
115
11.4G
    }
_ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR12SizeComputer20TransactionSerParamsEEEvRT_
Line
Count
Source
113
11.1G
    {
114
11.1G
        s << std::span(m_data);
115
11.1G
    }
_ZNK9base_blobILj256EE9SerializeI10DataStreamEEvRT_
Line
Count
Source
113
26.3M
    {
114
26.3M
        s << std::span(m_data);
115
26.3M
    }
_ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR10DataStream20TransactionSerParamsEEEvRT_
Line
Count
Source
113
1.46M
    {
114
1.46M
        s << std::span(m_data);
115
1.46M
    }
_ZNK9base_blobILj256EE9SerializeI12SizeComputerEEvRT_
Line
Count
Source
113
91.5k
    {
114
91.5k
        s << std::span(m_data);
115
91.5k
    }
_ZNK9base_blobILj256EE9SerializeI12VectorWriterEEvRT_
Line
Count
Source
113
457k
    {
114
457k
        s << std::span(m_data);
115
457k
    }
_ZNK9base_blobILj160EE9SerializeI10DataStreamEEvRT_
Line
Count
Source
113
520
    {
114
520
        s << std::span(m_data);
115
520
    }
_ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR12VectorWriter20TransactionSerParamsEEEvRT_
Line
Count
Source
113
1.20M
    {
114
1.20M
        s << std::span(m_data);
115
1.20M
    }
_ZNK9base_blobILj256EE9SerializeI12ParamsStreamIRS2_IR12VectorWriter20TransactionSerParamsES5_EEEvRT_
Line
Count
Source
113
7.67k
    {
114
7.67k
        s << std::span(m_data);
115
7.67k
    }
_ZNK9base_blobILj256EE9SerializeI8AutoFileEEvRT_
Line
Count
Source
113
110k
    {
114
110k
        s << std::span(m_data);
115
110k
    }
_ZNK9base_blobILj256EE9SerializeI10HashWriterEEvRT_
Line
Count
Source
113
185M
    {
114
185M
        s << std::span(m_data);
115
185M
    }
Unexecuted instantiation: _ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR18HashedSourceWriterI8AutoFileEN8CAddress9SerParamsEEEEvRT_
_ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR10DataStreamN8CAddress9SerParamsEEEEvRT_
Line
Count
Source
113
9.18k
    {
114
9.18k
        s << std::span(m_data);
115
9.18k
    }
_ZNK9base_blobILj256EE9SerializeI14BufferedWriterI8AutoFileEEEvRT_
Line
Count
Source
113
227k
    {
114
227k
        s << std::span(m_data);
115
227k
    }
_ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR14BufferedWriterI8AutoFileE20TransactionSerParamsEEEvRT_
Line
Count
Source
113
771k
    {
114
771k
        s << std::span(m_data);
115
771k
    }
Unexecuted instantiation: _ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR8AutoFile20TransactionSerParamsEEEvRT_
_ZNK9base_blobILj256EE9SerializeI12ParamsStreamIR10HashWriter20TransactionSerParamsEEEvRT_
Line
Count
Source
113
111M
    {
114
111M
        s << std::span(m_data);
115
111M
    }
116
117
    template<typename Stream>
118
    void Unserialize(Stream& s)
119
32.0M
    {
120
32.0M
        s.read(MakeWritableByteSpan(m_data));
121
32.0M
    }
_ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR10DataStream20TransactionSerParamsEEEvRT_
Line
Count
Source
119
6.63M
    {
120
6.63M
        s.read(MakeWritableByteSpan(m_data));
121
6.63M
    }
_ZN9base_blobILj256EE11UnserializeI10DataStreamEEvRT_
Line
Count
Source
119
22.3M
    {
120
22.3M
        s.read(MakeWritableByteSpan(m_data));
121
22.3M
    }
_ZN9base_blobILj256EE11UnserializeI10SpanReaderEEvRT_
Line
Count
Source
119
80.7k
    {
120
80.7k
        s.read(MakeWritableByteSpan(m_data));
121
80.7k
    }
_ZN9base_blobILj160EE11UnserializeI10DataStreamEEvRT_
Line
Count
Source
119
528
    {
120
528
        s.read(MakeWritableByteSpan(m_data));
121
528
    }
_ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR10SpanReader20TransactionSerParamsEEEvRT_
Line
Count
Source
119
32.9k
    {
120
32.9k
        s.read(MakeWritableByteSpan(m_data));
121
32.9k
    }
_ZN9base_blobILj256EE11UnserializeI8AutoFileEEvRT_
Line
Count
Source
119
478k
    {
120
478k
        s.read(MakeWritableByteSpan(m_data));
121
478k
    }
Unexecuted instantiation: _ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR8AutoFileN8CAddress9SerParamsEEEEvRT_
Unexecuted instantiation: _ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR12HashVerifierI8AutoFileEN8CAddress9SerParamsEEEEvRT_
_ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR10DataStreamN8CAddress9SerParamsEEEEvRT_
Line
Count
Source
119
6.87k
    {
120
6.87k
        s.read(MakeWritableByteSpan(m_data));
121
6.87k
    }
_ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR12HashVerifierI10DataStreamEN8CAddress9SerParamsEEEEvRT_
Line
Count
Source
119
2.38k
    {
120
2.38k
        s.read(MakeWritableByteSpan(m_data));
121
2.38k
    }
Unexecuted instantiation: _ZN9base_blobILj256EE11UnserializeI14BufferedReaderI8AutoFileEEEvRT_
_ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR8AutoFile20TransactionSerParamsEEEvRT_
Line
Count
Source
119
1.06M
    {
120
1.06M
        s.read(MakeWritableByteSpan(m_data));
121
1.06M
    }
_ZN9base_blobILj256EE11UnserializeI12BufferedFileEEvRT_
Line
Count
Source
119
885k
    {
120
885k
        s.read(MakeWritableByteSpan(m_data));
121
885k
    }
_ZN9base_blobILj256EE11UnserializeI12ParamsStreamIR12BufferedFile20TransactionSerParamsEEEvRT_
Line
Count
Source
119
519k
    {
120
519k
        s.read(MakeWritableByteSpan(m_data));
121
519k
    }
122
};
123
124
template <unsigned int BITS>
125
consteval base_blob<BITS>::base_blob(std::string_view hex_str)
126
{
127
    if (hex_str.length() != m_data.size() * 2) throw "Hex string must fit exactly";
128
    auto str_it = hex_str.rbegin();
129
    for (auto& elem : m_data) {
130
        auto lo = util::ConstevalHexDigit(*(str_it++));
131
        elem = (util::ConstevalHexDigit(*(str_it++)) << 4) | lo;
132
    }
133
}
134
135
namespace detail {
136
/**
137
 * Writes the hex string (in reverse byte order) into a new uintN_t object
138
 * and only returns a value iff all of the checks pass:
139
 *   - Input length is uintN_t::size()*2
140
 *   - All characters are hex
141
 */
142
template <class uintN_t>
143
std::optional<uintN_t> FromHex(std::string_view str)
144
9.62k
{
145
9.62k
    if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
146
884
    uintN_t rv;
147
884
    unsigned char* p1 = rv.begin();
148
884
    unsigned char* pend = rv.end();
149
884
    size_t digits = str.size();
150
29.1k
    while (digits > 0 && p1 < pend) {
151
28.2k
        *p1 = ::HexDigit(str[--digits]);
152
28.2k
        if (digits > 0) {
153
28.2k
            *p1 |= ((unsigned char)::HexDigit(str[--digits]) << 4);
154
28.2k
            p1++;
155
28.2k
        }
156
28.2k
    }
157
884
    return rv;
158
9.62k
}
Unexecuted instantiation: _ZN6detail7FromHexI7uint160EESt8optionalIT_ESt17basic_string_viewIcSt11char_traitsIcEE
_ZN6detail7FromHexI7uint256EESt8optionalIT_ESt17basic_string_viewIcSt11char_traitsIcEE
Line
Count
Source
144
9.62k
{
145
9.62k
    if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
146
884
    uintN_t rv;
147
884
    unsigned char* p1 = rv.begin();
148
884
    unsigned char* pend = rv.end();
149
884
    size_t digits = str.size();
150
29.1k
    while (digits > 0 && p1 < pend) {
151
28.2k
        *p1 = ::HexDigit(str[--digits]);
152
28.2k
        if (digits > 0) {
153
28.2k
            *p1 |= ((unsigned char)::HexDigit(str[--digits]) << 4);
154
28.2k
            p1++;
155
28.2k
        }
156
28.2k
    }
157
884
    return rv;
158
9.62k
}
159
/**
160
 * @brief Like FromHex(std::string_view str), but allows an "0x" prefix
161
 *        and pads the input with leading zeroes if it is shorter than
162
 *        the expected length of uintN_t::size()*2.
163
 *
164
 *        Designed to be used when dealing with user input.
165
 */
166
template <class uintN_t>
167
std::optional<uintN_t> FromUserHex(std::string_view input)
168
454
{
169
454
    input = util::RemovePrefixView(input, "0x");
170
454
    constexpr auto expected_size{uintN_t::size() * 2};
171
454
    if (input.size() < expected_size) {
172
83
        auto padded = std::string(expected_size, '0');
173
83
        std::copy(input.begin(), input.end(), padded.begin() + expected_size - input.size());
174
83
        return FromHex<uintN_t>(padded);
175
83
    }
176
371
    return FromHex<uintN_t>(input);
177
454
}
178
} // namespace detail
179
180
/** 160-bit opaque blob.
181
 * @note This type is called uint160 for historical reasons only. It is an opaque
182
 * blob of 160 bits and has no integer operations.
183
 */
184
class uint160 : public base_blob<160> {
185
public:
186
0
    static std::optional<uint160> FromHex(std::string_view str) { return detail::FromHex<uint160>(str); }
187
40.6M
    constexpr uint160() = default;
188
1.05M
    constexpr explicit uint160(std::span<const unsigned char> vch) : base_blob<160>(vch) {}
189
};
190
191
/** 256-bit opaque blob.
192
 * @note This type is called uint256 for historical reasons only. It is an
193
 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
194
 * those are required.
195
 */
196
class uint256 : public base_blob<256> {
197
public:
198
9.16k
    static std::optional<uint256> FromHex(std::string_view str) { return detail::FromHex<uint256>(str); }
199
454
    static std::optional<uint256> FromUserHex(std::string_view str) { return detail::FromUserHex<uint256>(str); }
200
3.03G
    constexpr uint256() = default;
201
0
    consteval explicit uint256(std::string_view hex_str) : base_blob<256>(hex_str) {}
202
38.0k
    constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
203
5.68M
    constexpr explicit uint256(std::span<const unsigned char> vch) : base_blob<256>(vch) {}
204
    static const uint256 ZERO;
205
    static const uint256 ONE;
206
};
207
208
#endif // BITCOIN_UINT256_H