Coverage Report

Created: 2026-10-07 13:27

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.54M
    void Finalize(std::span<unsigned char> output) {
39
1.54M
        assert(output.size() == OUTPUT_SIZE);
40
1.54M
        unsigned char buf[CSHA256::OUTPUT_SIZE];
41
1.54M
        sha.Finalize(buf);
42
1.54M
        sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
43
1.54M
    }
44
45
2.68M
    CHash256& Write(std::span<const unsigned char> input) {
46
2.68M
        sha.Write(input.data(), input.size());
47
2.68M
        return *this;
48
2.68M
    }
49
50
134k
    CHash256& Reset() {
51
134k
        sha.Reset();
52
134k
        return *this;
53
134k
    }
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.53M
    void Finalize(std::span<unsigned char> output) {
64
6.53M
        assert(output.size() == OUTPUT_SIZE);
65
6.53M
        unsigned char buf[CSHA256::OUTPUT_SIZE];
66
6.53M
        sha.Finalize(buf);
67
6.53M
        CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
68
6.53M
    }
69
70
6.53M
    CHash160& Write(std::span<const unsigned char> input) {
71
6.53M
        sha.Write(input.data(), input.size());
72
6.53M
        return *this;
73
6.53M
    }
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
338k
{
85
338k
    uint256 result;
86
338k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
338k
    return result;
88
338k
}
uint256 Hash<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
84
4.34k
{
85
4.34k
    uint256 result;
86
4.34k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
4.34k
    return result;
88
4.34k
}
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
303k
{
85
303k
    uint256 result;
86
303k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
303k
    return result;
88
303k
}
uint256 Hash<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&)
Line
Count
Source
84
30.1k
{
85
30.1k
    uint256 result;
86
30.1k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
87
30.1k
    return result;
88
30.1k
}
89
90
/** Compute the 256-bit hash of the concatenation of two objects. */
91
template<typename T1, typename T2>
92
925k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
925k
    uint256 result;
94
925k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
925k
    return result;
96
925k
}
uint256 Hash<uint256, uint256>(uint256 const&, uint256 const&)
Line
Count
Source
92
912k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
912k
    uint256 result;
94
912k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
912k
    return result;
96
912k
}
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.76k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
7.76k
    uint256 result;
94
7.76k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
7.76k
    return result;
96
7.76k
}
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.60k
inline uint256 Hash(const T1& in1, const T2& in2) {
93
5.60k
    uint256 result;
94
5.60k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
95
5.60k
    return result;
96
5.60k
}
97
98
/** Compute the 160-bit hash an object. */
99
template<typename T1>
100
inline uint160 Hash160(const T1& in1)
101
6.37M
{
102
6.37M
    uint160 result;
103
6.37M
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
6.37M
    return result;
105
6.37M
}
uint160 Hash160<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
101
6.02M
{
102
6.02M
    uint160 result;
103
6.02M
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
6.02M
    return result;
105
6.02M
}
uint160 Hash160<XOnlyPubKey>(XOnlyPubKey const&)
Line
Count
Source
101
21.0k
{
102
21.0k
    uint160 result;
103
21.0k
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
21.0k
    return result;
105
21.0k
}
uint160 Hash160<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&)
Line
Count
Source
101
79
{
102
79
    uint160 result;
103
79
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
79
    return result;
105
79
}
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
325k
{
102
325k
    uint160 result;
103
325k
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
104
325k
    return result;
105
325k
}
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
439M
    {
116
439M
        ctx.Write(UCharCast(src.data()), src.size());
117
439M
    }
118
119
    /** Compute the double-SHA256 hash of all data written to this object.
120
     *
121
     * Invalidates this object.
122
     */
123
61.2M
    uint256 GetHash() {
124
61.2M
        uint256 result;
125
61.2M
        ctx.Finalize(result.begin());
126
61.2M
        ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
127
61.2M
        return result;
128
61.2M
    }
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
252M
    {
151
252M
        ::Serialize(*this, obj);
152
252M
        return *this;
153
252M
    }
HashWriter& HashWriter::operator<<<int>(int const&)
Line
Count
Source
150
444k
    {
151
444k
        ::Serialize(*this, obj);
152
444k
        return *this;
153
444k
    }
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
140M
    {
151
140M
        ::Serialize(*this, obj);
152
140M
        return *this;
153
140M
    }
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.41M
    {
151
2.41M
        ::Serialize(*this, obj);
152
2.41M
        return *this;
153
2.41M
    }
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.37M
    {
151
1.37M
        ::Serialize(*this, obj);
152
1.37M
        return *this;
153
1.37M
    }
HashWriter& HashWriter::operator<<<COutPoint>(COutPoint const&)
Line
Count
Source
150
20.6M
    {
151
20.6M
        ::Serialize(*this, obj);
152
20.6M
        return *this;
153
20.6M
    }
HashWriter& HashWriter::operator<<<unsigned int>(unsigned int const&)
Line
Count
Source
150
8.27M
    {
151
8.27M
        ::Serialize(*this, obj);
152
8.27M
        return *this;
153
8.27M
    }
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.9k
    {
151
69.9k
        ::Serialize(*this, obj);
152
69.9k
        return *this;
153
69.9k
    }
HashWriter& HashWriter::operator<<<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&)
Line
Count
Source
150
9.10k
    {
151
9.10k
        ::Serialize(*this, obj);
152
9.10k
        return *this;
153
9.10k
    }
HashWriter& HashWriter::operator<<<Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&> const&)
Line
Count
Source
150
60.8k
    {
151
60.8k
        ::Serialize(*this, obj);
152
60.8k
        return *this;
153
60.8k
    }
HashWriter& HashWriter::operator<<<CBlockHeader>(CBlockHeader const&)
Line
Count
Source
150
59.7M
    {
151
59.7M
        ::Serialize(*this, obj);
152
59.7M
        return *this;
153
59.7M
    }
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.73M
    {
151
1.73M
        ::Serialize(*this, obj);
152
1.73M
        return *this;
153
1.73M
    }
HashWriter& HashWriter::operator<<<long>(long const&)
Line
Count
Source
150
217k
    {
151
217k
        ::Serialize(*this, obj);
152
217k
        return *this;
153
217k
    }
HashWriter& HashWriter::operator<<<CScript>(CScript const&)
Line
Count
Source
150
217k
    {
151
217k
        ::Serialize(*this, obj);
152
217k
        return *this;
153
217k
    }
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CTransaction> const&)
Line
Count
Source
150
41.3k
    {
151
41.3k
        ::Serialize(*this, obj);
152
41.3k
        return *this;
153
41.3k
    }
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction> const&)
Line
Count
Source
150
86.5k
    {
151
86.5k
        ::Serialize(*this, obj);
152
86.5k
        return *this;
153
86.5k
    }
HashWriter& HashWriter::operator<<<CompactSizeWriter>(CompactSizeWriter const&)
Line
Count
Source
150
165k
    {
151
165k
        ::Serialize(*this, obj);
152
165k
        return *this;
153
165k
    }
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
12
    {
151
12
        ::Serialize(*this, obj);
152
12
        return *this;
153
12
    }
HashWriter& HashWriter::operator<<<unsigned char [384]>(unsigned char const (&) [384])
Line
Count
Source
150
4.31k
    {
151
4.31k
        ::Serialize(*this, obj);
152
4.31k
        return *this;
153
4.31k
    }
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.5k
    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
666
    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.20M
    {
168
1.20M
        m_source.read(dst);
169
1.20M
        this->write(dst);
170
1.20M
    }
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
914k
    {
168
914k
        m_source.read(dst);
169
914k
        this->write(dst);
170
914k
    }
HashVerifier<BufferedReader<AutoFile>>::read(std::span<std::byte, 18446744073709551615ul>)
Line
Count
Source
167
293k
    {
168
293k
        m_source.read(dst);
169
293k
        this->write(dst);
170
293k
    }
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
129k
    {
185
129k
        ::Unserialize(*this, obj);
186
129k
        return *this;
187
129k
    }
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
666
    {
185
666
        ::Unserialize(*this, obj);
186
666
        return *this;
187
666
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><AddrMan&>(AddrMan&)
Line
Count
Source
184
634
    {
185
634
        ::Unserialize(*this, obj);
186
634
        return *this;
187
634
    }
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
31
    {
185
31
        ::Unserialize(*this, obj);
186
31
        return *this;
187
31
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>>(Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>&&)
Line
Count
Source
184
634
    {
185
634
        ::Unserialize(*this, obj);
186
634
        return *this;
187
634
    }
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
31.0k
    {
185
31.0k
        ::Unserialize(*this, obj);
186
31.0k
        return *this;
187
31.0k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>&&)
Line
Count
Source
184
31.0k
    {
185
31.0k
        ::Unserialize(*this, obj);
186
31.0k
        return *this;
187
31.0k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul>&&)
Line
Count
Source
184
31.0k
    {
185
31.0k
        ::Unserialize(*this, obj);
186
31.0k
        return *this;
187
31.0k
    }
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.66k
    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.66k
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
199
200
    void write(std::span<const std::byte> src)
201
2.32M
    {
202
2.32M
        m_source.write(src);
203
2.32M
        HashWriter::write(src);
204
2.32M
    }
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.32M
    {
202
2.32M
        m_source.write(src);
203
2.32M
        HashWriter::write(src);
204
2.32M
    }
205
206
    template <typename T>
207
    HashedSourceWriter& operator<<(const T& obj)
208
3.32k
    {
209
3.32k
        ::Serialize(*this, obj);
210
3.32k
        return *this;
211
3.32k
    }
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.66k
    {
209
1.66k
        ::Serialize(*this, obj);
210
1.66k
        return *this;
211
1.66k
    }
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<AddrMan>(AddrMan const&)
Line
Count
Source
208
1.62k
    {
209
1.62k
        ::Serialize(*this, obj);
210
1.62k
        return *this;
211
1.62k
    }
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
39
    {
209
39
        ::Serialize(*this, obj);
210
39
        return *this;
211
39
    }
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.44k
{
232
2.44k
    uint160 result;
233
2.44k
    CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
234
2.44k
    return result;
235
2.44k
}
236
237
#endif // BITCOIN_HASH_H