Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the sha256t::Hash type #2717

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Apr 25, 2024

I pulled the concept in this out of #2708 and attempted to reduce the scope.

In bitcoin we never use the sha256t::Hash type directly but only as an underlying generic type that holds the tagging logic. To get tagged hash types we currently use the sha256t_hash_newtype macro.

Assuming other users implement tagged hashing as we do in rust-bitcoin then the sha256t::Hash type currently serves only as a type to implement Tag::engine on - but as long as there exists a way to get the pre-tagged engine then we do not actually need the type.

If we assume all users will use a provided macro to define tagged hashes then we have two options. This PR implements the first option and #2719 demonstrates the second.

  1. In the NewTypeHash::engine function return a pre-tagged sha256::HashEngine.

  2. Have callers of the macro define a new engine type as well as a new hash type, then we can put the tagging logic in the Default implementation of the new engine.

Remove the sha256t::Hash type, and the Tag trait. Modify the sha256_hash_newtype macro to more closely mimic hash_newtype! and just implement the tagging logic in the engine function.

Add unit tests to make sure we correctly handle the forward/backward attribute. rust-bitcoin includes test already that verify this patch.

@tcharding tcharding changed the title Remove the sha256t::Hash type Remove the sha256t::Hash type Apr 25, 2024
@github-actions github-actions bot added C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate labels Apr 25, 2024
@coveralls
Copy link

coveralls commented Apr 25, 2024

Pull Request Test Coverage Report for Build 8826416322

Details

  • 41 of 47 (87.23%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.07%) to 83.203%

Changes Missing Coverage Covered Lines Changed/Added Lines %
bitcoin/src/psbt/mod.rs 0 1 0.0%
hashes/src/sha256t.rs 32 37 86.49%
Totals Coverage Status
Change from base Build 8801402712: 0.07%
Covered Lines: 19205
Relevant Lines: 23082

💛 - Coveralls

@tcharding tcharding force-pushed the 04-25-tagged-hashes-rm-generic branch 2 times, most recently from 1c84f5b to 743b61e Compare April 25, 2024 02:50
In `bitcoin` we never use the `sha256t::Hash` type directly but only as
an underlying generic type that holds the tagging logic. To get tagged
hash types we currently use the `sha256t_hash_newtype` macro.

Assuming other users implement tagged hashing as we do in `rust-bitcoin`
then the `sha256t::Hash` type currently serves only as a type to
implement `Tag::engine` on - but as long as there exists a way to get
the pre-tagged engine then we do not actually need the type.

If we assume all users will use a provided macro to define tagged hashes
then we have two options. This PR implements the first option.

1. In the `NewTypeHash::engine` function return a pre-tagged
  `sha256::HashEngine`.

2. Have callers of the macro define a new engine type as well as a new
hash type, then we can put the tagging logic in the `Default`
implementation of the new engine.

Remove the `sha256t::Hash` type, and the `Tag` trait. Modify the
`sha256_hash_newtype` macro to match and just implement the tagging
logic in the `engine` function.

Add unit tests to make sure we correctly handle the forward/backward
attribute. `rust-bitcoin` includes test already that verify this patch.
@@ -78,6 +78,7 @@ const UTXO_3: P2trUtxo = P2trUtxo {
use std::collections::BTreeMap;
use std::str::FromStr;

use bitcoin::hashes::Hash;
Copy link
Member

Choose a reason for hiding this comment

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

In 2c73f7e:

Can you add as _ here?

}

impl_message_from_hash!(TapSighash);
Copy link
Member

Choose a reason for hiding this comment

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

In 2c73f7e:

Why does this go away?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a mistake, right there. I had a feeling something was wrong with the Message::from_digest instead of Message::from changes. And I knew you'd catch it in review. Sorry to lean on you like that, I've literally done various combinations of all these hashes changes like 10 times over the last two weeks.

@tcharding tcharding marked this pull request as draft April 30, 2024 05:45
@tcharding tcharding closed this Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants