Skip to content

Commit 65ff12c

Browse files
kwvgPastaPastaPasta
authored andcommitted
merge bitcoin#19326: Simplify hash.h interface using Spans
includes: - 2a2182c - 77c5073 completion of partial merge done in dash#4164 as 56f1b2d (blocker bitcoin#17938 was merged in dash#5246 as 00802bb)
1 parent 7ddf2ff commit 65ff12c

18 files changed

+41
-61
lines changed

src/base58.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
136136
{
137137
// add 4-byte hash check to the end
138138
std::vector<unsigned char> vch(input.begin(), input.end());
139-
uint256 hash = Hash(vch.begin(), vch.end());
139+
uint256 hash = Hash(vch);
140140
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
141141
return EncodeBase58(vch);
142142
}
@@ -149,7 +149,7 @@ bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int
149149
return false;
150150
}
151151
// re-calculate the checksum, ensure it matches the included 4-byte checksum
152-
uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4);
152+
uint256 hash = Hash(MakeSpan(vchRet).first(vchRet.size() - 4));
153153
if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
154154
vchRet.clear();
155155
return false;

src/flat-database.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CFlatDB
4747
ssObj << strMagicMessage; // specific magic message for this type of object
4848
ssObj << Params().MessageStart(); // network specific magic number
4949
ssObj << objToSave;
50-
uint256 hash = Hash(ssObj.begin(), ssObj.end());
50+
uint256 hash = Hash(ssObj);
5151
ssObj << hash;
5252

5353
// open output file, and associate with CAutoFile
@@ -109,7 +109,7 @@ class CFlatDB
109109
CDataStream ssObj(vchData, SER_DISK, CLIENT_VERSION);
110110

111111
// verify stored checksum matches input data
112-
uint256 hashTmp = Hash(ssObj.begin(), ssObj.end());
112+
uint256 hashTmp = Hash(ssObj);
113113
if (hashIn != hashTmp)
114114
{
115115
error("%s: Checksum mismatch, data corrupted", __func__);

src/hash.h

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,52 +84,31 @@ class CHash160 {
8484
};
8585

8686
/** Compute the 256-bit hash of an object. */
87-
template<typename T1>
88-
inline uint256 Hash(const T1 pbegin, const T1 pend)
87+
template<typename T>
88+
inline uint256 Hash(const T& in1)
8989
{
90-
static const unsigned char pblank[1] = {};
9190
uint256 result;
92-
CHash256().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
93-
.Finalize(result);
91+
CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
9492
return result;
9593
}
9694

9795
/** Compute the 256-bit hash of the concatenation of two objects. */
9896
template<typename T1, typename T2>
99-
inline uint256 Hash(const T1 p1begin, const T1 p1end,
100-
const T2 p2begin, const T2 p2end) {
101-
static const unsigned char pblank[1] = {};
97+
inline uint256 Hash(const T1& in1, const T2& in2) {
10298
uint256 result;
103-
CHash256().Write({p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])})
104-
.Write({p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])})
105-
.Finalize(result);
99+
CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
106100
return result;
107101
}
108102

109103
/** Compute the 160-bit hash an object. */
110104
template<typename T1>
111-
inline uint160 Hash160(const T1 pbegin, const T1 pend)
105+
inline uint160 Hash160(const T1& in1)
112106
{
113-
static unsigned char pblank[1] = {};
114107
uint160 result;
115-
CHash160().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
116-
.Finalize(result);
108+
CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
117109
return result;
118110
}
119111

120-
/** Compute the 160-bit hash of a vector. */
121-
inline uint160 Hash160(const std::vector<unsigned char>& vch)
122-
{
123-
return Hash160(vch.begin(), vch.end());
124-
}
125-
126-
/** Compute the 160-bit hash of a vector. */
127-
template<unsigned int N>
128-
inline uint160 Hash160(const prevector<N, unsigned char>& vch)
129-
{
130-
return Hash160(vch.begin(), vch.end());
131-
}
132-
133112
/** A writer stream (for serialization) that computes a 256-bit hash. */
134113
class CHashWriter
135114
{

src/hdchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ SecureVector CHDChain::GetSeed() const
153153
uint256 CHDChain::GetSeedHash()
154154
{
155155
LOCK(cs);
156-
return Hash(vchSeed.begin(), vchSeed.end());
156+
return Hash(vchSeed);
157157
}
158158

159159
void CHDChain::DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_t nChildIndex, CExtKey& extKeyRet, KeyOriginInfo& key_origin)

src/merkleblock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::ve
8484
else
8585
right = left;
8686
// combine subhashes
87-
return Hash(left.begin(), left.end(), right.begin(), right.end());
87+
return Hash(left, right);
8888
}
8989
}
9090

@@ -140,7 +140,7 @@ uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, uns
140140
right = left;
141141
}
142142
// and combine them before returning
143-
return Hash(left.begin(), left.end(), right.begin(), right.end());
143+
return Hash(left, right);
144144
}
145145
}
146146

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ std::optional<CNetMessage> V1TransportDeserializer::GetMessage(int64_t time, uin
850850

851851
void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) {
852852
// create dbl-sha256 checksum
853-
uint256 hash = Hash(msg.data.begin(), msg.data.end());
853+
uint256 hash = Hash(msg.data);
854854

855855
// create header
856856
CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), msg.data.size());

src/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ std::vector<unsigned char> CNetAddr::GetAddrBytes() const
830830

831831
uint64_t CNetAddr::GetHash() const
832832
{
833-
uint256 hash = Hash(m_addr.begin(), m_addr.end());
833+
uint256 hash = Hash(m_addr);
834834
uint64_t nRet;
835835
memcpy(&nRet, &hash, sizeof(nRet));
836836
return nRet;

src/pubkey.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ class CPubKey
157157
//! Get the KeyID of this public key (hash of its serialization)
158158
CKeyID GetID() const
159159
{
160-
return CKeyID(Hash160(vch, vch + size()));
160+
return CKeyID(Hash160(MakeSpan(vch).first(size())));
161161
}
162162

163163
//! Get the 256-bit hash of this public key.
164164
uint256 GetHash() const
165165
{
166-
return Hash(vch, vch + size());
166+
return Hash(MakeSpan(vch).first(size()));
167167
}
168168

169169
/*

src/script/standard.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ typedef std::vector<unsigned char> valtype;
1313
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
1414
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
1515

16-
CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in.begin(), in.end())) {}
16+
CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
1717
CScriptID::CScriptID(const ScriptHash& in) : BaseHash(static_cast<uint160>(in)) {}
1818

19-
ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in.begin(), in.end())) {}
19+
ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in)) {}
2020
ScriptHash::ScriptHash(const CScriptID& in) : BaseHash(static_cast<uint160>(in)) {}
2121

2222
PKHash::PKHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
@@ -242,7 +242,6 @@ CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
242242
return script;
243243
}
244244

245-
246245
bool IsValidDestination(const CTxDestination& dest) {
247246
return dest.index() != 0;
248247
}

src/test/fuzz/crypto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ FUZZ_TARGET(crypto)
6262
(void)sha512.Write(data.data(), data.size());
6363
(void)sip_hasher.Write(data.data(), data.size());
6464

65+
(void)Hash(data);
6566
(void)Hash160(data);
66-
(void)Hash160(data.begin(), data.end());
6767
(void)sha512.Size();
6868
},
6969
[&] {

0 commit comments

Comments
 (0)