fuzz coverage

Coverage Report

Created: 2025-06-01 19:34

/Users/eugenesiegel/btc/bitcoin/src/hash.h
Line
Count
Source (jump to first uncovered line)
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_HASH_H
7
#define BITCOIN_HASH_H
8
9
#include <attributes.h>
10
#include <crypto/common.h>
11
#include <crypto/ripemd160.h>
12
#include <crypto/sha256.h>
13
#include <prevector.h>
14
#include <serialize.h>
15
#include <span.h>
16
#include <uint256.h>
17
18
#include <string>
19
#include <vector>
20
21
typedef uint256 ChainCode;
22
23
/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
24
class CHash256 {
25
private:
26
    CSHA256 sha;
27
public:
28
    static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
29
30
21.8M
    void Finalize(std::span<unsigned char> output) {
31
21.8M
        assert(output.size() == OUTPUT_SIZE);
32
21.8M
        unsigned char buf[CSHA256::OUTPUT_SIZE];
33
21.8M
        sha.Finalize(buf);
34
21.8M
        sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
35
21.8M
    }
36
37
41.6M
    CHash256& Write(std::span<const unsigned char> input) {
38
41.6M
        sha.Write(input.data(), input.size());
39
41.6M
        return *this;
40
41.6M
    }
41
42
861k
    CHash256& Reset() {
43
861k
        sha.Reset();
44
861k
        return *this;
45
861k
    }
46
};
47
48
/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
49
class CHash160 {
50
private:
51
    CSHA256 sha;
52
public:
53
    static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
54
55
0
    void Finalize(std::span<unsigned char> output) {
56
0
        assert(output.size() == OUTPUT_SIZE);
57
0
        unsigned char buf[CSHA256::OUTPUT_SIZE];
58
0
        sha.Finalize(buf);
59
0
        CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
60
0
    }
61
62
0
    CHash160& Write(std::span<const unsigned char> input) {
63
0
        sha.Write(input.data(), input.size());
64
0
        return *this;
65
0
    }
66
67
0
    CHash160& Reset() {
68
0
        sha.Reset();
69
0
        return *this;
70
0
    }
71
};
72
73
/** Compute the 256-bit hash of an object. */
74
template<typename T>
75
inline uint256 Hash(const T& in1)
76
1.14M
{
77
1.14M
    uint256 result;
78
1.14M
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
1.14M
    return result;
80
1.14M
}
Unexecuted instantiation: _Z4HashINSt3__14spanIKhLm18446744073709551615EEEE7uint256RKT_
_Z4HashINSt3__16vectorIhNS0_9allocatorIhEEEEE7uint256RKT_
Line
Count
Source
76
1.14M
{
77
1.14M
    uint256 result;
78
1.14M
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
1.14M
    return result;
80
1.14M
}
Unexecuted instantiation: _Z4HashINSt3__14spanIhLm18446744073709551615EEEE7uint256RKT_
81
82
/** Compute the 256-bit hash of the concatenation of two objects. */
83
template<typename T1, typename T2>
84
0
inline uint256 Hash(const T1& in1, const T2& in2) {
85
0
    uint256 result;
86
0
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
87
0
    return result;
88
0
}
Unexecuted instantiation: _Z4HashINSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEA8_hE7uint256RKT_RKT0_
Unexecuted instantiation: _Z4HashI7uint256S0_ES0_RKT_RKT0_
89
90
/** Compute the 160-bit hash an object. */
91
template<typename T1>
92
inline uint160 Hash160(const T1& in1)
93
0
{
94
0
    uint160 result;
95
0
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96
0
    return result;
97
0
}
Unexecuted instantiation: _Z7Hash160INSt3__14spanIKhLm18446744073709551615EEEE7uint160RKT_
Unexecuted instantiation: _Z7Hash160INSt3__16vectorIhNS0_9allocatorIhEEEEE7uint160RKT_
Unexecuted instantiation: _Z7Hash160I11XOnlyPubKeyE7uint160RKT_
Unexecuted instantiation: _Z7Hash160I7CPubKeyE7uint160RKT_
Unexecuted instantiation: _Z7Hash160I7CScriptE7uint160RKT_
98
99
/** A writer stream (for serialization) that computes a 256-bit hash. */
100
class HashWriter
101
{
102
private:
103
    CSHA256 ctx;
104
105
public:
106
    void write(std::span<const std::byte> src)
107
1.41G
    {
108
1.41G
        ctx.Write(UCharCast(src.data()), src.size());
109
1.41G
    }
110
111
    /** Compute the double-SHA256 hash of all data written to this object.
112
     *
113
     * Invalidates this object.
114
     */
115
173M
    uint256 GetHash() {
116
173M
        uint256 result;
117
173M
        ctx.Finalize(result.begin());
118
173M
        ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
119
173M
        return result;
120
173M
    }
121
122
    /** Compute the SHA256 hash of all data written to this object.
123
     *
124
     * Invalidates this object.
125
     */
126
0
    uint256 GetSHA256() {
127
0
        uint256 result;
128
0
        ctx.Finalize(result.begin());
129
0
        return result;
130
0
    }
131
132
    /**
133
     * Returns the first 64 bits from the resulting hash.
134
     */
135
0
    inline uint64_t GetCheapHash() {
136
0
        uint256 result = GetHash();
137
0
        return ReadLE64(result.begin());
138
0
    }
139
140
    template <typename T>
141
    HashWriter& operator<<(const T& obj)
142
438M
    {
143
438M
        ::Serialize(*this, obj);
144
438M
        return *this;
145
438M
    }
Unexecuted instantiation: _ZN10HashWriterlsIjEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsINSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEERS_RKT_
_ZN10HashWriterlsINSt3__16vectorIhNS1_9allocatorIhEEEEEERS_RKT_
Line
Count
Source
142
49.9k
    {
143
49.9k
        ::Serialize(*this, obj);
144
49.9k
        return *this;
145
49.9k
    }
Unexecuted instantiation: _ZN10HashWriterlsINSt3__14spanIcLm18446744073709551615EEEEERS_RKT_
_ZN10HashWriterlsI7uint256EERS_RKT_
Line
Count
Source
142
9.99M
    {
143
9.99M
        ::Serialize(*this, obj);
144
9.99M
        return *this;
145
9.99M
    }
_ZN10HashWriterlsINSt3__14spanIKhLm32EEEEERS_RKT_
Line
Count
Source
142
254M
    {
143
254M
        ::Serialize(*this, obj);
144
254M
        return *this;
145
254M
    }
Unexecuted instantiation: _ZN10HashWriterlsIyEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsIhEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsIiEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsINSt3__16vectorIbNS1_9allocatorIbEEEEEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsI9COutPointEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsI6CTxOutEERS_RKT_
_ZN10HashWriterlsI10CBlockUndoEERS_RKT_
Line
Count
Source
142
9.99M
    {
143
9.99M
        ::Serialize(*this, obj);
144
9.99M
        return *this;
145
9.99M
    }
Unexecuted instantiation: _ZN10HashWriterlsI7WrapperI15VarIntFormatterIL10VarIntMode0EERyEEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsINSt3__14spanIhLm18446744073709551615EEEEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsINSt3__14spanIKhLm18446744073709551615EEEEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsI22transaction_identifierILb1EEEERS_RKT_
_ZN10HashWriterlsI12CBlockHeaderEERS_RKT_
Line
Count
Source
142
122M
    {
143
122M
        ::Serialize(*this, obj);
144
122M
        return *this;
145
122M
    }
Unexecuted instantiation: _ZN10HashWriterlsI13ParamsWrapperI20TransactionSerParamsK19CMutableTransactionEEERS_RKT_
_ZN10HashWriterlsI13ParamsWrapperI20TransactionSerParamsK12CTransactionEEERS_RKT_
Line
Count
Source
142
41.3M
    {
143
41.3M
        ::Serialize(*this, obj);
144
41.3M
        return *this;
145
41.3M
    }
Unexecuted instantiation: _ZN10HashWriterlsIxEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsI7CScriptEERS_RKT_
Unexecuted instantiation: interpreter.cpp:_ZN10HashWriterlsIN12_GLOBAL__N_131CTransactionSignatureSerializerI12CTransactionEEEERS_RKT_
Unexecuted instantiation: interpreter.cpp:_ZN10HashWriterlsIN12_GLOBAL__N_131CTransactionSignatureSerializerI19CMutableTransactionEEEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsI17CompactSizeWriterEERS_RKT_
Unexecuted instantiation: _ZN10HashWriterlsIA384_hEERS_RKT_
146
};
147
148
/** Reads data from an underlying stream, while hashing the read data. */
149
template <typename Source>
150
class HashVerifier : public HashWriter
151
{
152
private:
153
    Source& m_source;
154
155
public:
156
55
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
Unexecuted instantiation: _ZN12HashVerifierI10DataStreamEC2ERS0_
Unexecuted instantiation: _ZN12HashVerifierI8AutoFileEC2ERS0_
_ZN12HashVerifierI14BufferedReaderI8AutoFileEEC2ERS2_
Line
Count
Source
156
55
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
157
158
    void read(std::span<std::byte> dst)
159
55
    {
160
55
        m_source.read(dst);
161
55
        this->write(dst);
162
55
    }
Unexecuted instantiation: _ZN12HashVerifierI10DataStreamE4readENSt3__14spanISt4byteLm18446744073709551615EEE
Unexecuted instantiation: _ZN12HashVerifierI8AutoFileE4readENSt3__14spanISt4byteLm18446744073709551615EEE
_ZN12HashVerifierI14BufferedReaderI8AutoFileEE4readENSt3__14spanISt4byteLm18446744073709551615EEE
Line
Count
Source
159
55
    {
160
55
        m_source.read(dst);
161
55
        this->write(dst);
162
55
    }
163
164
    void ignore(size_t num_bytes)
165
0
    {
166
0
        std::byte data[1024];
167
0
        while (num_bytes > 0) {
168
0
            size_t now = std::min<size_t>(num_bytes, 1024);
169
0
            read({data, now});
170
0
            num_bytes -= now;
171
0
        }
172
0
    }
Unexecuted instantiation: _ZN12HashVerifierI8AutoFileE6ignoreEm
Unexecuted instantiation: _ZN12HashVerifierI10DataStreamE6ignoreEm
Unexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEE6ignoreEm
173
174
    template <typename T>
175
    HashVerifier<Source>& operator>>(T&& obj)
176
55
    {
177
55
        ::Unserialize(*this, obj);
178
55
        return *this;
179
55
    }
Unexecuted instantiation: _ZN12HashVerifierI10DataStreamErsIRNSt3__15arrayIhLm4EEEEERS1_OT_
Unexecuted instantiation: _ZN12HashVerifierI10DataStreamErsIR7AddrManEERS1_OT_
Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsIRNSt3__15arrayIhLm4EEEEERS1_OT_
Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsIR7AddrManEERS1_OT_
Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsIR13ParamsWrapperIN8CAddress9SerParamsENSt3__16vectorIS4_NS6_9allocatorIS4_EEEEEEERS1_OT_
Unexecuted instantiation: _ZN12HashVerifierI8AutoFileErsI7WrapperI19CustomUintFormatterILi1ELb0EERN11AddrManImpl6FormatEEEERS1_OT_
Unexecuted instantiation: _ZN12HashVerifierI10DataStreamErsI7WrapperI19CustomUintFormatterILi1ELb0EERN11AddrManImpl6FormatEEEERS1_OT_
_ZN12HashVerifierI14BufferedReaderI8AutoFileEErsIR10CBlockUndoEERS3_OT_
Line
Count
Source
176
55
    {
177
55
        ::Unserialize(*this, obj);
178
55
        return *this;
179
55
    }
Unexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEErsI7WrapperI15VarIntFormatterIL10VarIntMode0EERyEEERS3_OT_
Unexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEErsI7WrapperI15VarIntFormatterIL10VarIntMode0EERjEEERS3_OT_
Unexecuted instantiation: _ZN12HashVerifierI14BufferedReaderI8AutoFileEErsINSt3__14spanIhLm18446744073709551615EEEEERS3_OT_
180
};
181
182
/** Writes data to an underlying source stream, while hashing the written data. */
183
template <typename Source>
184
class HashedSourceWriter : public HashWriter
185
{
186
private:
187
    Source& m_source;
188
189
public:
190
0
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
191
192
    void write(std::span<const std::byte> src)
193
0
    {
194
0
        m_source.write(src);
195
0
        HashWriter::write(src);
196
0
    }
197
198
    template <typename T>
199
    HashedSourceWriter& operator<<(const T& obj)
200
0
    {
201
0
        ::Serialize(*this, obj);
202
0
        return *this;
203
0
    }
Unexecuted instantiation: _ZN18HashedSourceWriterI8AutoFileElsINSt3__15arrayIhLm4EEEEERS1_RKT_
Unexecuted instantiation: _ZN18HashedSourceWriterI8AutoFileElsI7AddrManEERS1_RKT_
Unexecuted instantiation: _ZN18HashedSourceWriterI8AutoFileElsI13ParamsWrapperIN8CAddress9SerParamsEKNSt3__16vectorIS4_NS6_9allocatorIS4_EEEEEEERS1_RKT_
204
};
205
206
/** Single-SHA256 a 32-byte input (represented as uint256). */
207
[[nodiscard]] uint256 SHA256Uint256(const uint256& input);
208
209
unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash);
210
211
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
212
213
/** Return a HashWriter primed for tagged hashes (as specified in BIP 340).
214
 *
215
 * The returned object will have SHA256(tag) written to it twice (= 64 bytes).
216
 * A tagged hash can be computed by feeding the message into this object, and
217
 * then calling HashWriter::GetSHA256().
218
 */
219
HashWriter TaggedHash(const std::string& tag);
220
221
/** Compute the 160-bit RIPEMD-160 hash of an array. */
222
inline uint160 RIPEMD160(std::span<const unsigned char> data)
223
0
{
224
0
    uint160 result;
225
0
    CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
226
0
    return result;
227
0
}
228
229
#endif // BITCOIN_HASH_H