Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/hash.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_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 <support/cleanse.h>
17
#include <uint256.h>
18
19
#include <string>
20
#include <vector>
21
22
/** A BIP32 chain code. Cleansed on destruction. */
23
class ChainCode : public base_blob<256> {
24
public:
25
1.20M
    constexpr ChainCode() = default;
26
0
    constexpr explicit ChainCode(std::span<const unsigned char> vch) : base_blob<256>(vch) {}
27
0
    constexpr explicit ChainCode(const base_blob<256>& b) : base_blob<256>(b) {}
28
18.9M
    ~ChainCode() { memory_cleanse(data(), size()); }
29
};
30
31
/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
32
class CHash256 {
33
private:
34
    CSHA256 sha;
35
public:
36
    static constexpr size_t OUTPUT_SIZE{CSHA256::OUTPUT_SIZE};
37
38
1.55M
    void Finalize(std::span<unsigned char> output) {
39
1.55M
        assert(output.size() == OUTPUT_SIZE);
40
1.55M
        unsigned char buf[CSHA256::OUTPUT_SIZE];
41
1.55M
        sha.Finalize(buf);
42
1.55M
        sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
43
1.55M
    }
44
45
2.69M
    CHash256& Write(std::span<const unsigned char> input) {
46
2.69M
        sha.Write(input.data(), input.size());
47
2.69M
        return *this;
48
2.69M
    }
49
50
132k
    CHash256& Reset() {
51
132k
        sha.Reset();
52
132k
        return *this;
53
132k
    }
54
};
55
56
/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
57
class CHash160 {
58
private:
59
    CSHA256 sha;
60
public:
61
    static constexpr size_t OUTPUT_SIZE{CRIPEMD160::OUTPUT_SIZE};
62
63
6.50M
    void Finalize(std::span<unsigned char> output) {
64
6.50M
        assert(output.size() == OUTPUT_SIZE);
65
6.50M
        unsigned char buf[CSHA256::OUTPUT_SIZE];
66
6.50M
        sha.Finalize(buf);
67
6.50M
        CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
68
6.50M
    }
69
70
6.50M
    CHash160& Write(std::span<const unsigned char> input) {
71
6.50M
        sha.Write(input.data(), input.size());
72
6.50M
        return *this;
73
6.50M
    }
74
75
0
    CHash160& Reset() {
76
0
        sha.Reset();
77
0
        return *this;
78
0
    }
79
};
80
81
/** Compute the 256-bit hash of an object. */
82
template<typename T>
83
inline uint256 Hash(const T& in1)
84
347k
{
85
347k
    uint256 result;
86
347k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
347k
    return result;
88
347k
}
uint256 Hash<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
84
3.79k
{
85
3.79k
    uint256 result;
86
3.79k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
3.79k
    return result;
88
3.79k
}
uint256 Hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Line
Count
Source
84
276
{
85
276
    uint256 result;
86
276
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
276
    return result;
88
276
}
uint256 Hash<DataStream>(DataStream const&)
Line
Count
Source
84
1
{
85
1
    uint256 result;
86
1
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
1
    return result;
88
1
}
uint256 Hash<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&)
Line
Count
Source
84
313k
{
85
313k
    uint256 result;
86
313k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
313k
    return result;
88
313k
}
uint256 Hash<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&)
Line
Count
Source
84
29.4k
{
85
29.4k
    uint256 result;
86
29.4k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
29.4k
    return result;
88
29.4k
}
89
90
/** Compute the 256-bit hash of the concatenation of two objects. */
91
template<typename T1, typename T2>
92
928k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
928k
    uint256 result;
94
928k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
928k
    return result;
96
928k
}
uint256 Hash<uint256, uint256>(uint256 const&, uint256 const&)
Line
Count
Source
92
915k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
915k
    uint256 result;
94
915k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
915k
    return result;
96
915k
}
uint256 Hash<CPubKey, std::vector<unsigned char, secure_allocator<unsigned char>>>(CPubKey const&, std::vector<unsigned char, secure_allocator<unsigned char>> const&)
Line
Count
Source
92
7.43k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
7.43k
    uint256 result;
94
7.43k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
7.43k
    return result;
96
7.43k
}
uint256 Hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char [8]>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned char const (&) [8])
Line
Count
Source
92
5.11k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
5.11k
    uint256 result;
94
5.11k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
5.11k
    return result;
96
5.11k
}
97
98
/** Compute the 160-bit hash an object. */
99
template<typename T1>
100
inline uint160 Hash160(const T1& in1)
101
6.34M
{
102
6.34M
    uint160 result;
103
6.34M
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
6.34M
    return result;
105
6.34M
}
uint160 Hash160<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
101
6.00M
{
102
6.00M
    uint160 result;
103
6.00M
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
6.00M
    return result;
105
6.00M
}
uint160 Hash160<XOnlyPubKey>(XOnlyPubKey const&)
Line
Count
Source
101
20.9k
{
102
20.9k
    uint160 result;
103
20.9k
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
20.9k
    return result;
105
20.9k
}
uint160 Hash160<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&)
Line
Count
Source
101
2
{
102
2
    uint160 result;
103
2
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
2
    return result;
105
2
}
uint160 Hash160<CPubKey>(CPubKey const&)
Line
Count
Source
101
2
{
102
2
    uint160 result;
103
2
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
2
    return result;
105
2
}
uint160 Hash160<CScript>(CScript const&)
Line
Count
Source
101
322k
{
102
322k
    uint160 result;
103
322k
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
322k
    return result;
105
322k
}
106
107
/** A writer stream (for serialization) that computes a 256-bit hash. */
108
class HashWriter
109
{
110
private:
111
    CSHA256 ctx;
112
113
public:
114
    void write(std::span<const std::byte> src)
115
319M
    {
116
319M
        ctx.Write(UCharCast(src.data()), src.size());
117
319M
    }
118
119
    /** Compute the double-SHA256 hash of all data written to this object.
120
     *
121
     * Invalidates this object.
122
     */
123
32.8M
    uint256 GetHash() {
124
32.8M
        uint256 result;
125
32.8M
        ctx.Finalize(result.begin());
126
32.8M
        ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
127
32.8M
        return result;
128
32.8M
    }
129
130
    /** Compute the SHA256 hash of all data written to this object.
131
     *
132
     * Invalidates this object.
133
     */
134
1.50M
    uint256 GetSHA256() {
135
1.50M
        uint256 result;
136
1.50M
        ctx.Finalize(result.begin());
137
1.50M
        return result;
138
1.50M
    }
139
140
    /**
141
     * Returns the first 64 bits from the resulting hash.
142
     */
143
225k
    inline uint64_t GetCheapHash() {
144
225k
        uint256 result = GetHash();
145
225k
        return ReadLE64(result.begin());
146
225k
    }
147
148
    template <typename T>
149
    HashWriter& operator<<(const T& obj)
150
165M
    {
151
165M
        ::Serialize(*this, obj);
152
165M
        return *this;
153
165M
    }
HashWriter& HashWriter::operator<<<int>(int const&)
Line
Count
Source
150
442k
    {
151
442k
        ::Serialize(*this, obj);
152
442k
        return *this;
153
442k
    }
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CMutableTransaction>>(ParamsWrapper<TransactionSerParams, CMutableTransaction> const&)
Line
Count
Source
150
50.0k
    {
151
50.0k
        ::Serialize(*this, obj);
152
50.0k
        return *this;
153
50.0k
    }
HashWriter& HashWriter::operator<<<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
150
1.00M
    {
151
1.00M
        ::Serialize(*this, obj);
152
1.00M
        return *this;
153
1.00M
    }
HashWriter& HashWriter::operator<<<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&)
Line
Count
Source
150
270k
    {
151
270k
        ::Serialize(*this, obj);
152
270k
        return *this;
153
270k
    }
HashWriter& HashWriter::operator<<<std::vector<bool, std::allocator<bool>>>(std::vector<bool, std::allocator<bool>> const&)
Line
Count
Source
150
1
    {
151
1
        ::Serialize(*this, obj);
152
1
        return *this;
153
1
    }
HashWriter& HashWriter::operator<<<transaction_identifier<true>>(transaction_identifier<true> const&)
Line
Count
Source
150
888
    {
151
888
        ::Serialize(*this, obj);
152
888
        return *this;
153
888
    }
HashWriter& HashWriter::operator<<<std::span<unsigned char const, 32ul>>(std::span<unsigned char const, 32ul> const&)
Line
Count
Source
150
83.7M
    {
151
83.7M
        ::Serialize(*this, obj);
152
83.7M
        return *this;
153
83.7M
    }
HashWriter& HashWriter::operator<<<transaction_identifier<false>>(transaction_identifier<false> const&)
Line
Count
Source
150
3
    {
151
3
        ::Serialize(*this, obj);
152
3
        return *this;
153
3
    }
HashWriter& HashWriter::operator<<<uint256>(uint256 const&)
Line
Count
Source
150
2.43M
    {
151
2.43M
        ::Serialize(*this, obj);
152
2.43M
        return *this;
153
2.43M
    }
HashWriter& HashWriter::operator<<<unsigned long>(unsigned long const&)
Line
Count
Source
150
47.0k
    {
151
47.0k
        ::Serialize(*this, obj);
152
47.0k
        return *this;
153
47.0k
    }
HashWriter& HashWriter::operator<<<unsigned char>(unsigned char const&)
Line
Count
Source
150
1.40M
    {
151
1.40M
        ::Serialize(*this, obj);
152
1.40M
        return *this;
153
1.40M
    }
HashWriter& HashWriter::operator<<<COutPoint>(COutPoint const&)
Line
Count
Source
150
20.5M
    {
151
20.5M
        ::Serialize(*this, obj);
152
20.5M
        return *this;
153
20.5M
    }
HashWriter& HashWriter::operator<<<unsigned int>(unsigned int const&)
Line
Count
Source
150
8.26M
    {
151
8.26M
        ::Serialize(*this, obj);
152
8.26M
        return *this;
153
8.26M
    }
HashWriter& HashWriter::operator<<<CTxOut>(CTxOut const&)
Line
Count
Source
150
13.9M
    {
151
13.9M
        ::Serialize(*this, obj);
152
13.9M
        return *this;
153
13.9M
    }
HashWriter& HashWriter::operator<<<CBlockUndo>(CBlockUndo const&)
Line
Count
Source
150
101k
    {
151
101k
        ::Serialize(*this, obj);
152
101k
        return *this;
153
101k
    }
HashWriter& HashWriter::operator<<<Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&> const&)
Line
Count
Source
150
69.4k
    {
151
69.4k
        ::Serialize(*this, obj);
152
69.4k
        return *this;
153
69.4k
    }
HashWriter& HashWriter::operator<<<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&)
Line
Count
Source
150
9.03k
    {
151
9.03k
        ::Serialize(*this, obj);
152
9.03k
        return *this;
153
9.03k
    }
HashWriter& HashWriter::operator<<<Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&> const&)
Line
Count
Source
150
60.4k
    {
151
60.4k
        ::Serialize(*this, obj);
152
60.4k
        return *this;
153
60.4k
    }
HashWriter& HashWriter::operator<<<CBlockHeader>(CBlockHeader const&)
Line
Count
Source
150
30.0M
    {
151
30.0M
        ::Serialize(*this, obj);
152
30.0M
        return *this;
153
30.0M
    }
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CMutableTransaction const>>(ParamsWrapper<TransactionSerParams, CMutableTransaction const> const&)
Line
Count
Source
150
661k
    {
151
661k
        ::Serialize(*this, obj);
152
661k
        return *this;
153
661k
    }
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CTransaction const>>(ParamsWrapper<TransactionSerParams, CTransaction const> const&)
Line
Count
Source
150
1.72M
    {
151
1.72M
        ::Serialize(*this, obj);
152
1.72M
        return *this;
153
1.72M
    }
HashWriter& HashWriter::operator<<<long>(long const&)
Line
Count
Source
150
191k
    {
151
191k
        ::Serialize(*this, obj);
152
191k
        return *this;
153
191k
    }
HashWriter& HashWriter::operator<<<CScript>(CScript const&)
Line
Count
Source
150
191k
    {
151
191k
        ::Serialize(*this, obj);
152
191k
        return *this;
153
191k
    }
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CTransaction> const&)
Line
Count
Source
150
41.2k
    {
151
41.2k
        ::Serialize(*this, obj);
152
41.2k
        return *this;
153
41.2k
    }
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction> const&)
Line
Count
Source
150
85.5k
    {
151
85.5k
        ::Serialize(*this, obj);
152
85.5k
        return *this;
153
85.5k
    }
HashWriter& HashWriter::operator<<<CompactSizeWriter>(CompactSizeWriter const&)
Line
Count
Source
150
163k
    {
151
163k
        ::Serialize(*this, obj);
152
163k
        return *this;
153
163k
    }
HashWriter& HashWriter::operator<<<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Line
Count
Source
150
52
    {
151
52
        ::Serialize(*this, obj);
152
52
        return *this;
153
52
    }
HashWriter& HashWriter::operator<<<CPubKey>(CPubKey const&)
Line
Count
Source
150
870
    {
151
870
        ::Serialize(*this, obj);
152
870
        return *this;
153
870
    }
HashWriter& HashWriter::operator<<<std::span<std::byte const, 18446744073709551615ul>>(std::span<std::byte const, 18446744073709551615ul> const&)
Line
Count
Source
150
26
    {
151
26
        ::Serialize(*this, obj);
152
26
        return *this;
153
26
    }
HashWriter& HashWriter::operator<<<unsigned char [384]>(unsigned char const (&) [384])
Line
Count
Source
150
4.33k
    {
151
4.33k
        ::Serialize(*this, obj);
152
4.33k
        return *this;
153
4.33k
    }
154
};
155
156
/** Reads data from an underlying stream, while hashing the read data. */
157
template <typename Source>
158
class HashVerifier : public HashWriter
159
{
160
private:
161
    Source& m_source;
162
163
public:
164
34.4k
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
HashVerifier<DataStream>::HashVerifier(DataStream&)
Line
Count
Source
164
3
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
HashVerifier<AutoFile>::HashVerifier(AutoFile&)
Line
Count
Source
164
635
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
HashVerifier<BufferedReader<AutoFile>>::HashVerifier(BufferedReader<AutoFile>&)
Line
Count
Source
164
33.8k
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
165
166
    void read(std::span<std::byte> dst)
167
1.17M
    {
168
1.17M
        m_source.read(dst);
169
1.17M
        this->write(dst);
170
1.17M
    }
HashVerifier<DataStream>::read(std::span<std::byte, 18446744073709551615ul>)
Line
Count
Source
167
1.08k
    {
168
1.08k
        m_source.read(dst);
169
1.08k
        this->write(dst);
170
1.08k
    }
HashVerifier<AutoFile>::read(std::span<std::byte, 18446744073709551615ul>)
Line
Count
Source
167
886k
    {
168
886k
        m_source.read(dst);
169
886k
        this->write(dst);
170
886k
    }
HashVerifier<BufferedReader<AutoFile>>::read(std::span<std::byte, 18446744073709551615ul>)
Line
Count
Source
167
286k
    {
168
286k
        m_source.read(dst);
169
286k
        this->write(dst);
170
286k
    }
171
172
    void ignore(size_t num_bytes)
173
0
    {
174
0
        std::byte data[1024];
175
0
        while (num_bytes > 0) {
176
0
            size_t now = std::min<size_t>(num_bytes, 1024);
177
0
            read({data, now});
178
0
            num_bytes -= now;
179
0
        }
180
0
    }
Unexecuted instantiation: HashVerifier<AutoFile>::ignore(unsigned long)
Unexecuted instantiation: HashVerifier<DataStream>::ignore(unsigned long)
Unexecuted instantiation: HashVerifier<BufferedReader<AutoFile>>::ignore(unsigned long)
181
182
    template <typename T>
183
    HashVerifier<Source>& operator>>(T&& obj)
184
126k
    {
185
126k
        ::Unserialize(*this, obj);
186
126k
        return *this;
187
126k
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
Line
Count
Source
184
1
    {
185
1
        ::Unserialize(*this, obj);
186
1
        return *this;
187
1
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><std::array<unsigned char, 4ul>&>(std::array<unsigned char, 4ul>&)
Line
Count
Source
184
2
    {
185
2
        ::Unserialize(*this, obj);
186
2
        return *this;
187
2
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><AddrMan&>(AddrMan&)
Line
Count
Source
184
2
    {
185
2
        ::Unserialize(*this, obj);
186
2
        return *this;
187
2
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><std::array<unsigned char, 4ul>&>(std::array<unsigned char, 4ul>&)
Line
Count
Source
184
635
    {
185
635
        ::Unserialize(*this, obj);
186
635
        return *this;
187
635
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><AddrMan&>(AddrMan&)
Line
Count
Source
184
606
    {
185
606
        ::Unserialize(*this, obj);
186
606
        return *this;
187
606
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>&>(ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>&)
Line
Count
Source
184
28
    {
185
28
        ::Unserialize(*this, obj);
186
28
        return *this;
187
28
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>>(Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>&&)
Line
Count
Source
184
606
    {
185
606
        ::Unserialize(*this, obj);
186
606
        return *this;
187
606
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>>(Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>&&)
Line
Count
Source
184
2
    {
185
2
        ::Unserialize(*this, obj);
186
2
        return *this;
187
2
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><CBlockUndo&>(CBlockUndo&)
Line
Count
Source
184
33.8k
    {
185
33.8k
        ::Unserialize(*this, obj);
186
33.8k
        return *this;
187
33.8k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>&&)
Line
Count
Source
184
30.3k
    {
185
30.3k
        ::Unserialize(*this, obj);
186
30.3k
        return *this;
187
30.3k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>&&)
Line
Count
Source
184
30.3k
    {
185
30.3k
        ::Unserialize(*this, obj);
186
30.3k
        return *this;
187
30.3k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul>&&)
Line
Count
Source
184
30.3k
    {
185
30.3k
        ::Unserialize(*this, obj);
186
30.3k
        return *this;
187
30.3k
    }
188
};
189
190
/** Writes data to an underlying source stream, while hashing the written data. */
191
template <typename Source>
192
class HashedSourceWriter : public HashWriter
193
{
194
private:
195
    Source& m_source;
196
197
public:
198
1.61k
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
HashedSourceWriter<DataStream>::HashedSourceWriter(DataStream&)
Line
Count
Source
198
1
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
HashedSourceWriter<AutoFile>::HashedSourceWriter(AutoFile&)
Line
Count
Source
198
1.61k
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
199
200
    void write(std::span<const std::byte> src)
201
2.28M
    {
202
2.28M
        m_source.write(src);
203
2.28M
        HashWriter::write(src);
204
2.28M
    }
HashedSourceWriter<DataStream>::write(std::span<std::byte const, 18446744073709551615ul>)
Line
Count
Source
201
2
    {
202
2
        m_source.write(src);
203
2
        HashWriter::write(src);
204
2
    }
HashedSourceWriter<AutoFile>::write(std::span<std::byte const, 18446744073709551615ul>)
Line
Count
Source
201
2.28M
    {
202
2.28M
        m_source.write(src);
203
2.28M
        HashWriter::write(src);
204
2.28M
    }
205
206
    template <typename T>
207
    HashedSourceWriter& operator<<(const T& obj)
208
3.23k
    {
209
3.23k
        ::Serialize(*this, obj);
210
3.23k
        return *this;
211
3.23k
    }
HashedSourceWriter<DataStream>& HashedSourceWriter<DataStream>::operator<<<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Line
Count
Source
208
1
    {
209
1
        ::Serialize(*this, obj);
210
1
        return *this;
211
1
    }
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<std::array<unsigned char, 4ul>>(std::array<unsigned char, 4ul> const&)
Line
Count
Source
208
1.61k
    {
209
1.61k
        ::Serialize(*this, obj);
210
1.61k
        return *this;
211
1.61k
    }
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<AddrMan>(AddrMan const&)
Line
Count
Source
208
1.58k
    {
209
1.58k
        ::Serialize(*this, obj);
210
1.58k
        return *this;
211
1.58k
    }
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>> const>>(ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>> const> const&)
Line
Count
Source
208
35
    {
209
35
        ::Serialize(*this, obj);
210
35
        return *this;
211
35
    }
212
};
213
214
/** Single-SHA256 a 32-byte input (represented as uint256). */
215
[[nodiscard]] uint256 SHA256Uint256(const uint256& input);
216
217
unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash);
218
219
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
220
221
/** Return a HashWriter primed for tagged hashes (as specified in BIP 340).
222
 *
223
 * The returned object will have SHA256(tag) written to it twice (= 64 bytes).
224
 * A tagged hash can be computed by feeding the message into this object, and
225
 * then calling HashWriter::GetSHA256().
226
 */
227
HashWriter TaggedHash(const std::string& tag);
228
229
/** Compute the 160-bit RIPEMD-160 hash of an array. */
230
inline uint160 RIPEMD160(std::span<const unsigned char> data)
231
2.36k
{
232
2.36k
    uint160 result;
233
2.36k
    CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
234
2.36k
    return result;
235
2.36k
}
236
237
#endif // BITCOIN_HASH_H