mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 18:53:23 -03:00
Merge bitcoin/bitcoin#27010: refactor: use Hash
helpers for double-SHA256 calculations
87f11ef47f
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:b5868f4b1f/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: ACK87f11ef47f
stickies-v: ACK87f11ef47f
MarcoFalke: review ACK87f11ef47f
😬 Tree-SHA512: 11d7e3d00c89685107784010fbffb33ccafb4d1b6a76c4dceb937b29bb234ef4d54581b16bd0737c8d2994a90cf4fe10a9738c7cc5b6d085c6a819f06176dab9
This commit is contained in:
commit
8fc3bcf93d
5 changed files with 8 additions and 23 deletions
|
@ -247,21 +247,10 @@ bool BlockFilter::BuildParams(GCSFilter::Params& params) const
|
||||||
|
|
||||||
uint256 BlockFilter::GetHash() const
|
uint256 BlockFilter::GetHash() const
|
||||||
{
|
{
|
||||||
const std::vector<unsigned char>& data = GetEncodedFilter();
|
return Hash(GetEncodedFilter());
|
||||||
|
|
||||||
uint256 result;
|
|
||||||
CHash256().Write(data).Finalize(result);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
|
uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
|
||||||
{
|
{
|
||||||
const uint256& filter_hash = GetHash();
|
return Hash(GetHash(), prev_header);
|
||||||
|
|
||||||
uint256 result;
|
|
||||||
CHash256()
|
|
||||||
.Write(filter_hash)
|
|
||||||
.Write(prev_header)
|
|
||||||
.Finalize(result);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,9 +157,7 @@ bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256&
|
||||||
std::vector<uint8_t> encoded_filter;
|
std::vector<uint8_t> encoded_filter;
|
||||||
try {
|
try {
|
||||||
filein >> block_hash >> encoded_filter;
|
filein >> block_hash >> encoded_filter;
|
||||||
uint256 result;
|
if (Hash(encoded_filter) != hash) return error("Checksum mismatch in filter decode.");
|
||||||
CHash256().Write(encoded_filter).Finalize(result);
|
|
||||||
if (result != hash) return error("Checksum mismatch in filter decode.");
|
|
||||||
filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
|
filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
|
|
|
@ -245,8 +245,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
|
||||||
unsigned char rnd[8];
|
unsigned char rnd[8];
|
||||||
std::string str = "Bitcoin key verification\n";
|
std::string str = "Bitcoin key verification\n";
|
||||||
GetRandBytes(rnd);
|
GetRandBytes(rnd);
|
||||||
uint256 hash;
|
uint256 hash{Hash(str, rnd)};
|
||||||
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
|
|
||||||
std::vector<unsigned char> vchSig;
|
std::vector<unsigned char> vchSig;
|
||||||
Sign(hash, vchSig);
|
Sign(hash, vchSig);
|
||||||
return pubkey.Verify(hash, vchSig);
|
return pubkey.Verify(hash, vchSig);
|
||||||
|
|
|
@ -205,8 +205,7 @@ BOOST_AUTO_TEST_CASE(key_key_negation)
|
||||||
unsigned char rnd[8];
|
unsigned char rnd[8];
|
||||||
std::string str = "Bitcoin key verification\n";
|
std::string str = "Bitcoin key verification\n";
|
||||||
GetRandBytes(rnd);
|
GetRandBytes(rnd);
|
||||||
uint256 hash;
|
uint256 hash{Hash(str, rnd)};
|
||||||
CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
|
|
||||||
|
|
||||||
// import the static test key
|
// import the static test key
|
||||||
CKey key = DecodeSecret(strSecret1C);
|
CKey key = DecodeSecret(strSecret1C);
|
||||||
|
|
|
@ -60,7 +60,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mutated |= (inner[level] == h);
|
mutated |= (inner[level] == h);
|
||||||
CHash256().Write(inner[level]).Write(h).Finalize(h);
|
h = Hash(inner[level], h);
|
||||||
}
|
}
|
||||||
// Store the resulting hash at inner position level.
|
// Store the resulting hash at inner position level.
|
||||||
inner[level] = h;
|
inner[level] = h;
|
||||||
|
@ -86,7 +86,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
|
||||||
if (pbranch && matchh) {
|
if (pbranch && matchh) {
|
||||||
pbranch->push_back(h);
|
pbranch->push_back(h);
|
||||||
}
|
}
|
||||||
CHash256().Write(h).Write(h).Finalize(h);
|
h = Hash(h, h);
|
||||||
// Increment count to the value it would have if two entries at this
|
// Increment count to the value it would have if two entries at this
|
||||||
// level had existed.
|
// level had existed.
|
||||||
count += ((uint32_t{1}) << level);
|
count += ((uint32_t{1}) << level);
|
||||||
|
@ -101,7 +101,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
|
||||||
matchh = true;
|
matchh = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CHash256().Write(inner[level]).Write(h).Finalize(h);
|
h = Hash(inner[level], h);
|
||||||
level++;
|
level++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue