Commit Graph

19 Commits

Author SHA1 Message Date
Fmt Bot b8910e201e 2025-03-30 automated rustfmt nightly 2025-03-30 01:27:51 +00:00
Martin Habovstiak e8a42d5851 Unify/reduce usage of `unsafe`
Since the introduction of `Script` `unsafe` started slowly creeping in
as more types with similar semantics were added. The `unsafe` in these
cases is just for trivial conversions between various pointer-like
types. As such, it's possible to move these into a single macro that
takes care of the conversions at one place and avoid repeating the same
`unsafe` code in the codebase. This decreases the cost of audits which
now only need to happen in `internals`, focuses any changes to happen in
that single macro and decreases the chance that we will mess up
similarly to the recent `try_into().expect()` issue (but this time with
UB rather than panic).

The new macro accepts syntax very similar to the already-existing struct
declarations with these differences:

* The struct MUST NOT have `#[repr(transparent)]` - it's added by the
  macro
* If the struct uses `PhantomData` it must be the first field and the
  real data must be the second field (to allow unsized types).
* The struct must be immediately followed by an impl block containing at
  least on conversion function.
* If the struct has generics the impl block has to use the same names of
  generics.
* The conversion functions don't have bodies (similarly to required
  trait methods) and have a fixed set of allowed signatures.
* Underscore (`_`) must be used in place of the inner type in the
  conversion function parameters.

The existing code can simply call the macro with simple changes and get
the same behavior without any direct use of `unsafe`. This change
already calls the macro for all relevant existing types. There are still
some usages left unrelated to the macro, except one additional
conversion in reverse direction on `Script`. It could be moved as well
but since it's on a single place so far it's not really required.
2025-03-27 20:15:37 +01:00
Martin Habovstiak bc6da1fe07 Swap around the fields in `sha256t::Hash`
There's a restriction that for structs containing unsized types the
unsized type has to be the last field. `sha256t::Hash` is not an unsized
type but we are going to introduce a macro that will assume this order
to work equally well with both sized and unsized types. Thus we swap it
upfront here.
2025-03-24 13:22:15 +01:00
Martin Habovstiak 8ee088df74 Make `sha256t` obey sanity rules
The comparison functions were accessing the fields directly, so this
changes them to use methods instead.
2025-03-24 13:17:49 +01:00
Tobin C. Harding 2b6ef31469
hashes: Add hash_byte_chunks function to modules
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`.
2025-03-06 11:47:29 +11:00
Tobin C. Harding d3846895d7
hashes: Add hash function to modules
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.
2025-03-06 11:47:29 +11:00
Tobin C. Harding e1bac7da55
Bound HmacEngine on HashEngine
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`.
2025-03-06 11:47:26 +11:00
Tobin C. Harding ab63b7a0ff
Add Hash type and finalize method to HashEngine
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.
2025-03-06 11:43:51 +11:00
Fmt Bot 166e37161a 2025-03-02 automated rustfmt nightly 2025-03-02 01:22:29 +00:00
ndungudedan 25707da14a
hashes: Remove Clone trait bound from Tag
Implementors of the Tag trait had to use the #[derive(Clone)] attribute.
This change eliminates this need by removing the Clone trait
bound from the Tag trait.
2025-02-26 22:13:53 +03:00
Tobin C. Harding f61e93ccf1
Properly deprecate Hash::from_slice
The `hashes::error::FromSliceError` error is only returned from
`from_slice`. We attempted to deprecate this function but it seems we
only did half a job at it.

- deprecate _all_ instances of the method/function
- deprecate the error type
2025-02-18 19:27:30 +11:00
Jamil Lambert, PhD e7c6564d38
Add missing hex feature gate
`hex` was previously enabled by `alloc`, now it is optional add the
missing feature gate to tests.
2025-02-14 11:44:55 +00:00
Tobin C. Harding fd4586eaae
Invert dependency between io and hashes
Currently in order to release `hashes v1.0` we need to 1.0 `io` as well.
For multiple reasons, many out of our control, the `io` crate may not
stabalise any time soon.

Instead we can invert the dependency between the two crates.

This is an ingenious idea, props to Kixunil for coming up with it.

Notes

- `io` does not currently re-export the `hashes` crate.
- This work highlights that we cannot call `hash_reader` on a siphash.
- The `Hmac::hash_reader` uses the default key which may not be obvious.

Signed-off-by: Tobin C. Harding <me@tobin.cc>
2025-02-11 09:17:21 +11:00
Tobin C. Harding 6002ccdc56
Add a tagged sha256t hash engine
We would like it if two different pre-tagged engines were considered
different types so it is not possible to mix them up.

Add a new `sha256t::HashEngine<T>` where `T` is a tag the same as on
`sha256t::Hash<T>`.
2025-02-08 13:27:30 +11:00
Tobin C. Harding a0211906fe
sha256t: Remove standalone from_engine function
This function is meant to be used in conjunction with the
`general_hash_type` macro but the `sha256t` module does not use that
macro.

Inline the function. Internal change only.
2025-02-08 13:27:30 +11:00
Tobin C. Harding 5ce8781162
Remove the Tag::engine method
Now we have an associated const we can do away with the `engine` trait
method all together. Users can call `Hash<FooTag>::engine` instead. This
is better because its an API more similar to the other hash types and
therefor easier to discover and remember.
2025-02-08 13:27:30 +11:00
Tobin C. Harding ba6425947f
hashes: Use associated cost for pre-tagging
Instead of requiring users of the `Tag` trait to implement the `engine`
method we can have an associated const and provide an `engine` method
with a default implementation.

Use an associated const for the pre-tagged hash engine. Fro now keep the
`engine` trait method but have a default impl that returns the const. We
will remove it as a separate patch to assist review.
2025-02-08 13:27:30 +11:00
Tobin C. Harding 613fddc82b
Delete deprecated sha256t_hash_newtype macro
This macro is a maintenance burden. We would like to put a tag on the
hash engine but doing so would require breaking this macro anyway so
lets just delete it.
2025-02-08 13:27:30 +11:00
Tobin C. Harding 1bfd1e071a
hashes: Make module subdirectories
In preparation for removing a bunch of macros move all the modules to
`<mod>/mod.rs`.

Do so by running the following shell:

```bash
for mod in hash160 ripemd160 sha1 sha256 sha256d sha256t \
sha384 sha512 sha512_256 siphash24 hkdf hmac; do
mkdir $mod
mv "$mod.rs" "$mod/mod.rs"
done
```

Internal change only.
2025-02-06 07:10:02 +11:00
Renamed from hashes/src/sha256t.rs (Browse further)