Coverage Report

Created: 2024-09-11 06:39

/src/cryptofuzz/modules/bitcoin/uint256.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2020 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 <assert.h>
10
#include <cstring>
11
#include <stdint.h>
12
#include <string>
13
#include <vector>
14
15
/** Template base class for fixed-sized opaque blobs. */
16
template<unsigned int BITS>
17
class base_blob
18
{
19
protected:
20
    static constexpr int WIDTH = BITS / 8;
21
    uint8_t m_data[WIDTH];
22
public:
23
    /* construct 0 value by default */
24
3.23G
    constexpr base_blob() : m_data() {}
25
26
    /* constructor for constants between 1 and 255 */
27
    constexpr explicit base_blob(uint8_t v) : m_data{v} {}
28
29
    explicit base_blob(const std::vector<unsigned char>& vch);
30
31
    bool IsNull() const
32
    {
33
        for (int i = 0; i < WIDTH; i++)
34
            if (m_data[i] != 0)
35
                return false;
36
        return true;
37
    }
38
39
    void SetNull()
40
    {
41
        memset(m_data, 0, sizeof(m_data));
42
    }
43
44
    inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }
45
46
    friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
47
    friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
48
    friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
49
50
    std::string GetHex() const;
51
    void SetHex(const char* psz);
52
    void SetHex(const std::string& str);
53
    std::string ToString() const;
54
55
    const unsigned char* data() const { return m_data; }
56
    unsigned char* data() { return m_data; }
57
58
    unsigned char* begin()
59
606M
    {
60
606M
        return &m_data[0];
61
606M
    }
62
63
    unsigned char* end()
64
    {
65
        return &m_data[WIDTH];
66
    }
67
68
    const unsigned char* begin() const
69
33.2M
    {
70
33.2M
        return &m_data[0];
71
33.2M
    }
72
73
    const unsigned char* end() const
74
    {
75
        return &m_data[WIDTH];
76
    }
77
78
    unsigned int size() const
79
    {
80
        return sizeof(m_data);
81
    }
82
83
    uint64_t GetUint64(int pos) const
84
1.06G
    {
85
1.06G
        const uint8_t* ptr = m_data + pos * 8;
86
1.06G
        return ((uint64_t)ptr[0]) | \
87
1.06G
               ((uint64_t)ptr[1]) << 8 | \
88
1.06G
               ((uint64_t)ptr[2]) << 16 | \
89
1.06G
               ((uint64_t)ptr[3]) << 24 | \
90
1.06G
               ((uint64_t)ptr[4]) << 32 | \
91
1.06G
               ((uint64_t)ptr[5]) << 40 | \
92
1.06G
               ((uint64_t)ptr[6]) << 48 | \
93
1.06G
               ((uint64_t)ptr[7]) << 56;
94
1.06G
    }
95
96
    template<typename Stream>
97
    void Serialize(Stream& s) const
98
    {
99
        s.write((char*)m_data, sizeof(m_data));
100
    }
101
102
    template<typename Stream>
103
    void Unserialize(Stream& s)
104
    {
105
        s.read((char*)m_data, sizeof(m_data));
106
    }
107
};
108
109
/** 160-bit opaque blob.
110
 * @note This type is called uint160 for historical reasons only. It is an opaque
111
 * blob of 160 bits and has no integer operations.
112
 */
113
class uint160 : public base_blob<160> {
114
public:
115
    constexpr uint160() {}
116
0
    explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
117
};
118
119
/** 256-bit opaque blob.
120
 * @note This type is called uint256 for historical reasons only. It is an
121
 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
122
 * those are required.
123
 */
124
class uint256 : public base_blob<256> {
125
public:
126
3.23G
    constexpr uint256() {}
127
    constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
128
3
    explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
129
    static const uint256 ZERO;
130
    static const uint256 ONE;
131
};
132
133
/* uint256 from const char *.
134
 * This is a separate function because the constructor uint256(const char*) can result
135
 * in dangerously catching uint256(0).
136
 */
137
inline uint256 uint256S(const char *str)
138
6.32k
{
139
6.32k
    uint256 rv;
140
6.32k
    rv.SetHex(str);
141
6.32k
    return rv;
142
6.32k
}
143
/* uint256 from std::string.
144
 * This is a separate function because the constructor uint256(const std::string &str) can result
145
 * in dangerously catching uint256(0) via std::string(const char*).
146
 */
147
inline uint256 uint256S(const std::string& str)
148
0
{
149
0
    uint256 rv;
150
0
    rv.SetHex(str);
151
0
    return rv;
152
0
}
153
154
#endif // BITCOIN_UINT256_H