Remove the GeneralHash trait#4085
Conversation
|
🚨 API BREAKING CHANGE DETECTED To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section. |
022a5c7 to
7038ca3
Compare
|
In case its not obvious, the regression test in patch one is to prove that the change to |
|
🚨 API BREAKING CHANGE DETECTED To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section. |
7038ca3 to
caac660
Compare
|
🚨 API BREAKING CHANGE DETECTED To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section. |
| /// A hashing engine which bytes can be serialized into. | ||
| pub trait HashEngine: Clone { | ||
| /// The `Hash` type returned when finalizing this engine. | ||
| type Hash: Hash; |
There was a problem hiding this comment.
If you're going to mess with the traits here it should be GeneralHash (but I think it shouldn't be required anyway).
There was a problem hiding this comment.
GeneralHash is folded into Hash and deleted here.
As for why do we need the bound -- I tried removing it and it broke the HmacEngine which wants to be able to finalize its inner engine (obtaining a HashEngine::Hash type) and then call .as_ref to convert it to bytes so that it can iterate over it and do some xor magic.
I think we should leave this be and revisit this in a followup, since it's a nontrivial change.
There was a problem hiding this comment.
IIRC I've originally suggested AsRef<[u8]> bound which should work for the vast majority of use cases (but I wouldn't mind also having Borrow, Copy and equality traits).
There was a problem hiding this comment.
Can we merge as is and follow up? I did try various things yesterday and couldn't get anything nicer to work.
There was a problem hiding this comment.
I could be wrong because every experiment requires loads of boring changes but I definitely tried AsRef<[u8]> trait bound.
There was a problem hiding this comment.
I'm not really sure if we should merge it in this state. The code before was not horribly broken, so clean history has value and the changes I have in mind touch a significant chunk of code, I think.
There was a problem hiding this comment.
I think we should merge it. This gets rid of the GeneralHash trait and greatly reduces the amount of stuff we need to think about (we have two interacting traits, Hash and HashEngine, instead of three, and it seems like we're moving toward also getting rid of Hash.)
Even if this means that we have a series of big diffs, conceptually it'll be easier to follow removing the big GeneralHash trait in one go, then reducing or removing Hash in another go.
There was a problem hiding this comment.
OK, I think I'll be able to review properly tomorrow.
There was a problem hiding this comment.
Crap, so many things slipping by
|
caac660 looks great! Started local CI but it might be many hours before it finishes, since today there are many 5+-commit PRs for some reason. To do in followups:
|
I think it should be possible to remove it entirely. |
|
Promoted to #4094 |
|
I got 18 hours into this and was 1/3 done. I stopped the build to defragment my nix store and my btrfs extents and will restart in a bit. The timing here is pretty bad ... I am using btrfs over SATA on a spinning disk drive for my Nix store. I have a new PCIe drive with ext4 which should be 10-1000x faster, but I can't cut over to it when I'm remote ... and I left town yesterday for a couple weeks. I shouldn't have kept procrastinating on it.. |
|
We can just queue up drafts on top of this for any |
|
Compliing with features |
|
In ed6fa5a (the very first commit): Your new regression test doesn't compile (yet). If you cd into |
|
CI should not pass then, I'll investigate. Thanks |
|
@tcharding it does compile by the last commit, so I'm not too surprised that CI passes since it tests only the tip. It looks like you added the regression then reordered the commits but didn't tweak it, or something. |
|
Oh woops, cheers. Will fix. |
caac660 to
820a1b7
Compare
|
🚨 API BREAKING CHANGE DETECTED To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section. |
Add a simple regression test prior to patching the `sha256::Hash::hash_again` function.
Add an associated const `Hash` to the `HashEngine` trait. Also add a `finalize` method that converts the engine to the associated hash. For now just use the existent `from_engine` stuff. We can refactor later.
We would like to do away with the `GeneralHash` trait. Currently we bound `Hmac` and `HmacEngine` on it but this is unnecessary now that we have added `HashEngine::finalize` and `HashEngine::Hash`. Bound the `HmacEngine` on `HashEngine` (which has an associated `Hash` type returned by `finilalize`). Bound `Hmac` type on `T::Hash` where `T` is `HashEngine`. Includes some minor shortening of local variable names around hmac engine usage. Note this means that `Hmac` no longer implements `GeneralHash`.
Add a standalone `hash` function that is a drop in replacement for `GeneralHash::hash`. Do not add it to `hmac` - this is in parity with the current code because `Hmac` does not implement `GeneralHash::hash`. Use the new function in `bitcoin` removing all occurrences of `GeneralHash` from `bitcoin`. In `hashes` replace usage of `GeneralHash::hash` with the new `hash` function.
Add a standalone `hash_byte_chunks` function that is a drop in replacement for `GeneralHash::hash_byte_chunks`. Do not add it to `hmac` - this is in parity with the current code because `Hmac` does not implement `GeneralHash::hash_byte_chunks`.
We would like to remove the `GeneralHash` trait but we want to keep the `hash_reader` functionality. Add a stand alone function (when `hashes` is enabled) `hash_reader`. Remove the extension trait.
This import is not needed. Interesting that the linter does not catch it.
Now that we are able to unambiguously go from a hash engine to its associated hash type there is no longer any need for the `GeneralHash` trait. Please note that IMO this concept of a general hash type as opposed to one where one can hash arbitrary data still exists in the codebase - it is implicitly in the `hash_newtype` macro. Remove the `GeneralHash` trait.
820a1b7 to
95ad91c
Compare
|
🚨 API BREAKING CHANGE DETECTED To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section. |
|
Rebased. Also did a couple more changes making local variable more terse like I did already in other places before hand - I think. |
|
Note to self: Soon as this lands rebase #4017. |
| } | ||
| engine.finalize() | ||
| } | ||
|
|
There was a problem hiding this comment.
I'd like to see this commit dropped if you can be bothered.
There was a problem hiding this comment.
I guess you mean 2b6ef31.
Let's promote this to an issue (or just a comment on #4205). I'm 8 hours into a 9-hour local CI run on this.
I agree it'd be better if this function hadn't existed to begin with, but we've gotta deprecate some copies anyway in #4205 so it won't be too bad to also delete this one.
There was a problem hiding this comment.
OK, if the CI succeeds and no other problem will be found let's keep it. But it the PR will need to be modified please drop it.
LOL, didn't notice this was already 1h ago :D
| mod benches { | ||
| use test::Bencher; | ||
|
|
||
| use crate::{hash160, GeneralHash as _, Hash as _, HashEngine}; |
There was a problem hiding this comment.
I believe that the reason linter doesn't catch it is that we're calling a function defined on it that's also inherent function and the linter doesn't know they behave the same.
|
|
||
| fn from_engine(e: Self::Engine) -> Hash<$($gen),*> { Self::from_engine(e) } | ||
| } | ||
|
|
There was a problem hiding this comment.
Th commit message: "concept of a general hash type as opposed to one where one can hash arbitrary data still exists in the codebase"
is funny, arbitrary data = general hash type, I think you meant "as opposed to hash that only hashes specific kinds of data". And yes, general hash type exists as a concept but its' expressed with HashEngine instead.
There was a problem hiding this comment.
In 95ad91c:
Agreed, the commit message should be improved. It's the last commit in the PR so it'd be cheap for me to re-run if @tcharding wants to change it. But IMO it's not a big enough deal to hold up the PR over.
The GeneralHash trait was removed (see CHANGELOG rust-bitcoin#4085), but internal_macros.rs still referenced it in macro docs. Updated hash_trait_impls! and general_hash_type! documentation to reflect actual current requirements
31a07f1 docs(hashes): replace deprecated GeneralHash references (Bashmunta) Pull request description: The GeneralHash trait was removed (hashes/CHANGELOG.md #4085), but internal_macros.rs still referenced it in docs. Updated hash_trait_impls! and general_hash_type! documentation to reflect actual current requirements ACKs for top commit: apoelstra: ACK 31a07f1; successfully ran local tests tcharding: ACK 31a07f1 Tree-SHA512: 8d407a05767f0209f447d0f3923aeb4f5528c144c6a234b35721412d9e50ba8b24a950ff09fadaf6603ce63c75ed14c1b85480caf516db08b879c4e6378c8cbf
This is the done as part of #4051.
Requires some surgery on the
HmacandHkdftypes as well as a few other patches to maintain the logic that is currently provided by the trait. Final patch is a pure red diff - enjoy.