Skip to content

Conversation

theStack
Copy link
Contributor

@theStack theStack commented Jan 31, 2023

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:

bitcoin/src/hash.h

Lines 74 to 89 in b5868f4

/** Compute the 256-bit hash of an object. */
template<typename T>
inline uint256 Hash(const T& in1)
{
uint256 result;
CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
return result;
}
/** Compute the 256-bit hash of the concatenation of two objects. */
template<typename T1, typename T2>
inline uint256 Hash(const T1& in1, const T2& in2) {
uint256 result;
CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
return result;
}

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.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 31, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK john-moffett, stickies-v, MarcoFalke

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26390 (index: Compare deserialized block hash with the block hash from the blockindex by kcalvinalvin)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@john-moffett john-moffett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 87f11ef

It does make it far more readable. I looked and couldn't find any other suitable places to use the helpers.

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 87f11ef

Avoid duplication, easier to read, can't see behaviour change.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review ACK 87f11ef 😬

Show signature

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK 87f11ef47fea31d51bcc3f5df68f78fb28e3d8dd  😬
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUj9lQv9HytY6oVQ1voN8ClRAJIVVgNODDZQbZV2TzpietMli+PyOukp1S4vyXCx
+vz3mPAwrp0mw64GxwnLyc7UoLw9CVG3b2lzbvUnAvPGYMBAY7wxbWH6HMqeyKPI
XtKQyA8lvp5Fo++4Cn59lWtQejlMsvHHQA3jakmkPwDmDsDbptBETcfgfOqrTILC
wCBr4R6/4ml5UZ4ziIrnxzXmzzjc58wHuCld26HcqLlkIl1P6QBkiouybXfjwlH1
q5IWW6v0zEH01ocw6FCLyWwZyXkiGGh8DWGwUIF7WnKQ4sKifPa1o26eE3IIPY82
+u7IjkJSdgz8tWAYeViAq5mAZGgV0+OrArkMzBhgDLsGbd+/2bhjhGjhF+EEJBto
kZWgo7A73RTDkmRv+xaXCCD+OtmgAmc+eMH0kwQiEVtDVvlhV1Bou7PUA91VK3Va
Nc/2iQmwrPCrDZxsSgdgXJ6sZB12fLOX1ii/r4ti5OLxNWN/L56jh6nb9V3uaWRu
jWfVWwuU
=mACN
-----END PGP SIGNATURE-----

@maflcko maflcko merged commit 8fc3bcf into bitcoin:master Feb 1, 2023
@theStack theStack deleted the 202301-refactor-use_double_sha256_helper branch February 1, 2023 15:03
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 1, 2023
…alculations

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 bitcoin#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
@bitcoin bitcoin locked and limited conversation to collaborators Feb 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants