Skip to content

Commit 8fc3bcf

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#27010: refactor: use Hash helpers for double-SHA256 calculations
87f11ef refactor: use `Hash` helper for double-SHA256 calculations (Sebastian Falbesoner) Pull request description: We have two helper templates `Hash(const T& in1)` and `Hash(const T& in1, const T& in2)` available for calculating the double-SHA256 hash of one object or two concatenated objects, respectively: https://github.com/bitcoin/bitcoin/blob/b5868f4b1f884e8d6612f34ca4005fe3a992053d/src/hash.h#L74-L89 This PR uses them in order to increase readability and simplify the code. As in #15294 (which inspired this PR, doing the same for RIPEMD160), the helper is not utilized in validation.cpp and script/interpreter.cpp to avoid touching consensus-relevant code. ACKs for top commit: john-moffett: ACK 87f11ef stickies-v: ACK 87f11ef MarcoFalke: review ACK 87f11ef 😬 Tree-SHA512: 11d7e3d00c89685107784010fbffb33ccafb4d1b6a76c4dceb937b29bb234ef4d54581b16bd0737c8d2994a90cf4fe10a9738c7cc5b6d085c6a819f06176dab9
2 parents 22ccf4e + 87f11ef commit 8fc3bcf

File tree

5 files changed

+8
-23
lines changed

5 files changed

+8
-23
lines changed

src/blockfilter.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,21 +247,10 @@ bool BlockFilter::BuildParams(GCSFilter::Params& params) const
247247

248248
uint256 BlockFilter::GetHash() const
249249
{
250-
const std::vector<unsigned char>& data = GetEncodedFilter();
251-
252-
uint256 result;
253-
CHash256().Write(data).Finalize(result);
254-
return result;
250+
return Hash(GetEncodedFilter());
255251
}
256252

257253
uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
258254
{
259-
const uint256& filter_hash = GetHash();
260-
261-
uint256 result;
262-
CHash256()
263-
.Write(filter_hash)
264-
.Write(prev_header)
265-
.Finalize(result);
266-
return result;
255+
return Hash(GetHash(), prev_header);
267256
}

src/index/blockfilterindex.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256&
157157
std::vector<uint8_t> encoded_filter;
158158
try {
159159
filein >> block_hash >> encoded_filter;
160-
uint256 result;
161-
CHash256().Write(encoded_filter).Finalize(result);
162-
if (result != hash) return error("Checksum mismatch in filter decode.");
160+
if (Hash(encoded_filter) != hash) return error("Checksum mismatch in filter decode.");
163161
filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
164162
}
165163
catch (const std::exception& e) {

src/key.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
245245
unsigned char rnd[8];
246246
std::string str = "Bitcoin key verification\n";
247247
GetRandBytes(rnd);
248-
uint256 hash;
249-
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
248+
uint256 hash{Hash(str, rnd)};
250249
std::vector<unsigned char> vchSig;
251250
Sign(hash, vchSig);
252251
return pubkey.Verify(hash, vchSig);

src/test/key_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ BOOST_AUTO_TEST_CASE(key_key_negation)
205205
unsigned char rnd[8];
206206
std::string str = "Bitcoin key verification\n";
207207
GetRandBytes(rnd);
208-
uint256 hash;
209-
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
208+
uint256 hash{Hash(str, rnd)};
210209

211210
// import the static test key
212211
CKey key = DecodeSecret(strSecret1C);

src/test/merkle_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
6060
}
6161
}
6262
mutated |= (inner[level] == h);
63-
CHash256().Write(inner[level]).Write(h).Finalize(h);
63+
h = Hash(inner[level], h);
6464
}
6565
// Store the resulting hash at inner position level.
6666
inner[level] = h;
@@ -86,7 +86,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
8686
if (pbranch && matchh) {
8787
pbranch->push_back(h);
8888
}
89-
CHash256().Write(h).Write(h).Finalize(h);
89+
h = Hash(h, h);
9090
// Increment count to the value it would have if two entries at this
9191
// level had existed.
9292
count += ((uint32_t{1}) << level);
@@ -101,7 +101,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
101101
matchh = true;
102102
}
103103
}
104-
CHash256().Write(inner[level]).Write(h).Finalize(h);
104+
h = Hash(inner[level], h);
105105
level++;
106106
}
107107
}

0 commit comments

Comments
 (0)