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

Make sha256t_hash_newtype! evocative of the output. #1773

Merged
merged 3 commits into from Apr 3, 2023

Conversation

Kixunil
Copy link
Collaborator

@Kixunil Kixunil commented Apr 2, 2023

The Rust API guidelines state that macros should be evocative of the
output, which is a sensible recommendation. We already had this for
hash_newtype! macro but didn't for sha256t version.

This changes the macro to have this syntax:

sha256t_hash_newtype! {
    // Order of these structs is fixed.
    /// Optional documentation details here. Summary is auto-generated.
    /*pub*/ struct Tag = raw(MIDSTATE_BYTES, LEN);

    /// Documentation here
    #[hash_newtype(forward)] // optional, default is backward
    /*pub*/ struct HashType(/* attributes allowed here */ _);
}

Closes #1427

Depends on #1769

How do you like the syntax? Is weird struct Foo = bar(..); acceptable?

Computing hashes in const fn is useful for esily creating tags for
`sha256t`. This adds `const fn` implementation for `sha256::Hash` and
the algorithm for computing midstate of tagged hash in `const` context.

Part of rust-bitcoin#1427
The Rust API guidelines state that macros should be evocative of the
output, which is a sensible recommendation. We already had this for
`hash_newtype!` macro but didn't for sha256t version.

This changes the macro to have this syntax:

```rust
sha256t_hash_newtype! {
    // Order of these structs is fixed.
    /// Optional documentation details here. Summary is auto-generated.
    /*pub*/ struct Tag = raw(MIDSTATE_BYTES, LEN);

    /// Documentation here
    #[hash_newtype(forward)] // optional, default is backward
    /*pub*/ struct HashType(/* attributes allowed here */ _);
}
```

Closes rust-bitcoin#1427
Previous changes enabled passing the string used as a tag into
`sha256t_hash_newtype!` macro rather than hard-coding midstate. This
commit takes advantage of it and replaces the hard-coded values with
compile-time executed (`const`) hashing.
Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 91f45a2

@tcharding
Copy link
Member

Cool.

@tcharding
Copy link
Member

I think #1758 will fix the CI fail.

@apoelstra
Copy link
Member

apoelstra commented Apr 3, 2023

Looks good to me. I kinda think they should be renamed as

  • from_str to iv_from_bip340_tag; I would also make this take a &[u8] rather than &str and then prefix the strings with b, but it's up to you.
  • hash_bytes should just be removed since AFAICT it's not used, and BIP-340 only descirbes using strings as tags
  • raw to iv or maybe iv_from_bytes

But I'm not gonna hold up the PR over these.

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 91f45a2

@apoelstra apoelstra merged commit 350b413 into rust-bitcoin:master Apr 3, 2023
23 of 24 checks passed
@Kixunil Kixunil deleted the evocative-sha256t branch April 4, 2023 06:24
@Kixunil
Copy link
Collaborator Author

Kixunil commented Apr 4, 2023

LOL, you merged it already. OK I guess. :)

Regarding naming, all of them are IVs, so telling what's specific about them seems better. Mentioning BIP-340 sounds good. Byte version was added just in case something comes up in the future.

Maybe just bip340() and raw?

@apoelstra
Copy link
Member

@Kixunil bip340 and raw sound great to me!

Yes, yes, sorry for going ahead and merging -- the PR seemed reasonably large and conflict-prone, and the naming unimportant enough that it could get slipped, that I figured it'd be nicer to do the renaming (if we decided to) in a different PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose an API to create tagged hashes from string?
3 participants