Commit Graph

42 Commits

Author SHA1 Message Date
Tobin C. Harding 12f261c009
hashes: Re-order from_byte_array
The `hashes` crate has a bunch of similar types defined by a bunch of
similar macros and impl blocks, all of which makes it difficult to tell
exactly what is implemented where. In an effort to make the code easier
to read order the `from_byte_array` constructor in the same place across
the crate. Note also we typically put constructors up the top, also
`from_byte_array` is the likely most used constructor so put it first.

FWIW I discovered this while polishing the HTML docs.

Internal change only.
2024-10-16 12:22:42 +11:00
Tobin C. Harding c11587d60d
hashes: Rename hash_type macro
Conceptually (and using traits) we split the hashes into "general"
hash types and more restricted hash types (`Hash`). Also we observe that
the `hash_type` macro defines all the inherent functions name
identically to the `GeneralHash` trait methods.

Rename the trait to describe better what it does.

Internal change only.
2024-10-16 12:22:42 +11:00
Tobin C. Harding 62617cf9ac
hashes: Move from_engine function to other macro
The `from_engine` function is associated with a general hash type but we
are defining it in the `hash_type` macro which holds nothing but
functions associated with the `Hash` trait. By "associated" I mean
methods on the type as opposed to trait methods.

In preparation for re-naming the `hash_type` macro move the
`from_engine` function there. Requires duplicating the code in the
`siphash` impl block, this is as expected because the `siphash` requires
a custom implementation of the general hashing functionality.

Internal change only.
2024-10-16 12:22:22 +11:00
Tobin C. Harding 2cc944578d
Fully deprecate Hash::from_slice
We had an initial go at this but we didn't do the `Hash` trait method.
In order to do so we need to hack the serde code a fair bit, note the
public visitor types.
2024-10-15 07:56:05 +11:00
Tobin C. Harding 3b7ba4f977
Remove the SliceIndex implementation from hash types
If folk really want to index into a hash they can us `as_byte_array`
then index that.

Includes a bump to the version number of `hashes` to `v0.15.0` - this
is because otherwise `secp` won't build since we are breaking an API
that is used in the current release of secp.

Fix: #3115
2024-10-02 10:18:45 +10:00
Tobin C. Harding 58704c2eff
Remove schemars all together
We introduced schemars as a personal favor to a user, and it broke our
CI repeatedly but eventually it seemed like it was stable (mainly, our
MSRV caught up with its MSRV) so we just let it slide. In the end having
schemars on hashes but nowhere else in the rust-bitcoin ecosystem did
not prove that useful.

Remove schemars all together.

Fix: #3393
2024-09-23 06:57:00 +10:00
Liam Aharon 25d906d936
Use UFCS in macros 2024-09-14 09:46:57 +02:00
Tobin C. Harding ae93e226e3
Remove hashes io feature
Currently we only get `std::io::Write` impls when the `bitcoin-io`
dependency is used. This is overly restrictive, it would be nice to have
`std::io::Write` imlps even without the `bitcoin-io` dependency.

Copy the logic out of the `bitcoin_io::impl_write` macro into `hashes`
but feature gate it differently.

Call the new macro inside `hash_type` (and in `hmac`), remove the
`impls` module, and move the tests to the integration test directory.

Remove the `io` feature from `hashes`, now if users enable `std` they
get `std::io::Write` impls and if they enable `bitcoin-io` they get
`bitcoin_io::Write` impls as well.
2024-09-09 06:37:49 +10:00
Tobin C. Harding d72f730211
hashes: Use $crate in internal macros
These are only called from within the crate but it is still more correct
to use `$crate` and saves this from biting us later if we copy the code
someplace else.

Internal change only.
2024-09-06 09:20:10 +10:00
merge-script 9233eb2fa3
Merge rust-bitcoin/rust-bitcoin#3275: Enforce that `Hash::Bytes` is an array
be13397570 Make hmac & hkdf more robust against buggy `Hash` (Martin Habovstiak)
94c0614bda Enforce that `Hash::Bytes` is an array (Martin Habovstiak)

Pull request description:

  This makes sure `Hash::Bytes` is an array. We've discussed this somewhere but I don't remember where.

  I'm not sure if the second commit is actually valuable but hopefully shouldn't make things worse.

ACKs for top commit:
  apoelstra:
    ACK be13397570 successfully ran local tests; yep, this looks like an improvement. Agreed that the second commit has questionable value but doe not make things worse
  tcharding:
    ACK be13397570

Tree-SHA512: 0fed982084f0f98927c2b4a275cec81cb4bbc0efbf01551a0a4a8b6b39a4504830243ee8d55a5c0418d81b5d4babc7b22332dbacc0609ced8fada84d2961ae71
2024-09-02 00:42:39 +00:00
Martin Habovstiak 94c0614bda Enforce that `Hash::Bytes` is an array
In the future we would like to guarantee the correctness of `LEN` which
is currently not entirely possible, so this at least adds a sealed trait
enforcing the `Bytes` type to be an array. Consumers concerned about the
validity of the length can access the `LEN` constant on `Bytes` instead
to get the correct length of the array.
2024-08-30 06:16:44 +02:00
Antoni Spaanderman a14cdaf859
don't enable std by default when testing
- make tests no_std compatible by adding imports to alloc or std
- feature gate tests behind the 'alloc' feature if they use anything
  from 'alloc' (like the `format!` macro)
- schemars feature enables alloc
2024-08-26 21:08:23 +02:00
Fmt Bot fbf7f41875 2024-08-25 automated rustfmt nightly 2024-08-25 01:14:09 +00:00
Martin Habovstiak 1a91492204 Clean up the siphash mess
Previously we had removed `Default` impl on `siphash24::HashEngine` by
reimplementing the type manually. This was a really bad idea as it
inevitably led to API differences that broke the build which we didn't
notice because of unrelated bug. It should've just split the macro from
the start as was suggested but it was claimed to be difficult, I don't
think was the case as can be seen by this PR.

This commit does what the previous one should've done: it renames the
macro to have `_no_default` suffix, creates another one of the original
name that calls into `_no_default` one and moves anything related to
`Default`. This cleanly ensures all previous hashes stay the same while
siphash gets `Default` removed. This also removes all now-conflicting
impls from `siphash24` module which makes the module almost identical to
what it looked like before the change. The only differences are removed
`Default`/`new`, fixes in tests and recent rename of `as_u64` to
`to_u64`.
2024-08-23 05:54:41 +02:00
Tobin C. Harding 191897f9ea
Manually format
Run `rustfmt` and manually fix the places where comments are moved to
the wrong place.
2024-08-08 09:38:45 +10:00
merge-script 075ab9d3e0
Merge rust-bitcoin/rust-bitcoin#3127: hashes: Call through to trait methods
975f22f399 hashes: Call through to trait methods (Tobin C. Harding)

Pull request description:

  Currently we have duplicate code in inherent functions that also occurs in the default implementation of the `GeneralHash` trait methods, this is unnecessary because we can call through to the trait methods.

ACKs for top commit:
  Kixunil:
    ACK 975f22f399
  apoelstra:
    ACK 975f22f399 successfully ran local tests

Tree-SHA512: 74d8905a20d75536abf477dd2226e3cb12d8bd7330b1769e520840df1538362c6cbec6a976dfeb771797732b1f9259ee4f1970cadb69eca67b8b9bbe956ceeca
2024-08-06 16:58:59 +00:00
Tobin C. Harding 0a045d87ea
hashes: Add a new hash_reader function
Add a function `hash_reader` that uses the `BufRead` trait to read
bytes directly into the hash engine.

Add the functionality to:

- as a trait method in the `GeneralHash` trait with default implementation
- as inherent functions to all the hash types

Close: #3050
2024-08-06 10:38:57 +10:00
Tobin C. Harding 975f22f399
hashes: Call through to trait methods
Currently we have duplicate code in inherent functions that also occurs
in the default implementation of the `GeneralHash` trait methods, this
is unnecessary because we can call through to the trait methods.
2024-08-06 10:28:03 +10:00
Tobin C. Harding 360d1fb1bb
hashes: Use $crate in macro
Depending on types being in scope when calling macros is bad practice
but we have mistakenly done so in `internal_macros` when using the
`FromSliceError`.

Use `$crate::FromSliceError` in the macro and remove import statements.
2024-07-12 12:55:22 +10:00
Andrew Poelstra 73dcc79763
hashes: split Hash trait into two 2024-06-24 13:28:54 +00:00
Andrew Poelstra 8869f35a69
hashes: drop the `all_zeros` method on arbitrary hashes
Manually implement it for Wtxid, Txid and BlockHash, where the all-zero
"hash" has a consensus meaning. But in general we should not be
implementing this method unless we have a good reason to do so. It can
be emulated or implemeted in terms of from_byte_array.

The use of Wtxid::all_zeros is obscure and specific enough that I am
tempted to drop it. But for txid and blockhash, the 0 hash appears in
actual blockdata and we should keep it.

All other uses of all_zeros were either in test code or in places where
the specific hash was not important and [u8; 32] was a more appropriate
type.
2024-06-22 15:09:28 +00:00
Fmt Bot 4745b55cae 2024-06-16 automated rustfmt nightly 2024-06-16 01:09:27 +00:00
Tobin C. Harding 6b7d02e5ae
Add inherent functions to hashes
Currently we have a trait `Hash` that is required for `Hmac`, `Hkdf`,
and other use cases. However, it is unegonomic for users who just want
to do a simple hash to have to import the trait.

Add inherent functions to all hash types including those created with
the new wrapper type macros.

This patch introduces some duplicate code but we are trying to make
progress in the hashes API re-write. We can come back and de-dublicate
later.

Includes making `to_byte_array`,`from_byte_array`, `as_byte_array`, and
`all_zeros` const where easily possible.
2024-06-14 10:17:00 +10:00
Tobin C. Harding ce1db3ea26
hashes: Move non-trait functions
The `hash_trait_impls` macro currently adds an impl block for `Hash` -
this is not what the docs say since and `impl Hash` block is nothing
to do with traits.

Move the impl block and add a duplicate of the functions to the
`sha256t::Hash` type.

This is a refactor, no API or logic changes. Note that wrapper types
currently do net get these functions - that will be
discussed/implemented separately.
2024-06-11 13:03:09 +10:00
Tobin C. Harding 71bb86232b
hashes: Do not import str
Depending on things being in scope for macros to use is bad form,
using the fully qualified path is the correct way.

Do not import `str` instead use the fully qualified path to the `core`
re-export.

Use fully qualified instead.
2024-04-06 06:49:07 +11:00
Tobin C. Harding 4bfb466bb9
Upgrade hex dependency
Upgrade to the new `hex v0.2.0` release.
2024-03-10 10:35:01 +11:00
Tobin C. Harding f0558e8eb9
Use fmt_hex_exact
Currently we have two functions for displaying forwards and backwards and
we also have `fmt_hex_exact`. I do not know why we added the functions.

In the latest version of hex we do not have the ability to construct a
`DisplayArray` type so we have to use `fmt_hex_exact`.

Make the change now, separate from the `hex` upgrade, to assist review.
2024-03-10 10:35:01 +11:00
Steven Roose aa6e5cd342
Use full path in all macro usage of Result 2024-01-18 13:20:19 +00:00
Tobin C. Harding 75c490c60f
hashes: Remove default features from schemars dep
We are trying to get rid of the `serde_derive` dependency from our
dependency graph.

Stop using default features for the `schemars` dependency which includes
`schemars_derive` which depends on `serder_derive`.

Manually implement `schemars::JsonSchema` instead of deriving it.
2023-11-20 15:18:33 +11:00
Tobin C. Harding 2268b44911
Depend on hex-conservative
We have just released the `hex-conservative` crate, we can now use it.

Do the following:

- Depend on `hex-conservative` in `bitcoin` and `hashes`
- Re-export `hex-conservative` as `hex` from both crate roots.
- Remove all the old hex code from `hashes`
- Fix all the import statements (makes up the bulk of the lines changed
  in this patch)
2023-07-21 10:59:46 +10:00
Tobin C. Harding 06afd52a12
Improve hashes::Error
We are trying to make error types stable on the way to v1.0

The current `hashes::Error` is a "general" enum error type with a single
variant, better to use a struct and make the error usecase specific.

Improve the `hashes::Error` by doing:

- Make it a struct
- Rename to `FromSliceError`
- Move it to the crate root (remove `error` module)

Includes usage in `bitcoin`.
2023-05-25 13:25:13 +10:00
Tobin C. Harding 99673ab5c4
hashes: Introduce SPDX license identifiers
Whether or not every file needs an explicit license comment is out of
scope for this patch; in the `bitcoin` crate we use SPDX identifiers
because they are a single line with no loss of "benefit" over any longer
form.

Use SPDX identifiers in `hashes`. Drop the mention of re-licensing code
from Apache to CC0-1 (because the original code was written by Andrew
as well as the copied code then if the argument ever comes up it can be
easily countered).
2023-05-01 09:26:36 +10:00
junderw 91863c8a78
Feature (hashes): Add from_bytes_ref and from_bytes_mut to all Hash types 2023-03-28 18:27:41 -07:00
Tobin C. Harding 913575ac91
hashes: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
2023-03-21 08:33:24 +11:00
Tobin C. Harding 161273b209
Re-name hash inner/byte methods
Currently we have an associated type on hash types `Inner` with
accompanying methods `into_inner`, `from_inner`, `as_inner`. Also, we
provide a way to create new wrapped hash types. The use of 'inner'
becomes ambiguous with the addition of wrapped types because the inner
could be the inner hash type or the `Inner` byte array of the inner
wrapped hash type.

In an effort to make the API more clear and uniform do the following:

- Rename `Inner` -> `Bytes`
- Rename `*_inner` -> `*_byte_array`
- Rename the inner hash to/from methods to `*_raw_hash`

Correct method prefix `into_` -> `to_` because theses methods convert
owned `Copy` types.

Add the trait Bound `Copy` to the `Bytes` type because we rely on this
trait bound for the conversion methods to be correctly named according
to convention.

Because of the dependency hole created by `secp256k1` this patch changes
the secp dependency to a git tag dependency that includes changes to the
hashes calls required so that we can get green lights on CI in this
repo.
2023-02-27 14:23:58 +11:00
Andrew Poelstra 562400a7bc
Merge rust-bitcoin/rust-bitcoin#1593: hashes: Implement AsRef for fixed size arrays
a121e19e94 hashes: Implement AsRef for fixed size arrays (Tobin C. Harding)

Pull request description:

  Implement `AsRef<[u8; X]>` for hash types including wrapped hash types. Doing so means at times the compiler can no longer infer the type because we have `AsRef<[u8]` implemented also but we can use `into_inner` and `as_inner` to get the inner array if needed.

  Fix: #1462

  ## Note

  This touches code that will likely be changed by #1577 and when we do #1491 but I believe its a step forward.

ACKs for top commit:
  arturomf94:
    ACK [`a121e19`](a121e19e94)
  apoelstra:
    ACK a121e19e94
  Kixunil:
    ACK a121e19e94

Tree-SHA512: 257c44826c7649db25bb3a6f023f68b2f17b70c546a056afad044bc8a16bf61f654c3846222505aaf5e6f9a0ad1d2113272d61317b407d0ac83702e41060a1ee
2023-02-17 13:24:57 +00:00
Tobin C. Harding a308e1e2ea
Remove FromHex for all types except Vec and array
Remove `FromHex` from hash and script types

- Remove the `FromHex` implementation from hash types and `ScriptBuf`
- Remove the `FromStr` implementation from `ScriptBuf` because it does not
  roundtrip with `Display`.
- Implement a method `from_hex` on `ScriptBuf`.
- Implement `FromStr` on hash types using a fixed size array.

This leaves `FromHex` implementations only on `Vec` and fixed size arrays.
2023-02-01 08:26:46 +11:00
Tobin C. Harding 5ab5c264d2
Use fully qualified path in macro
It is easier to maintain code if macros use the fully qualified path to
types and functions instead of relying on code that uses the macro to
import said type or function.
2023-02-01 08:22:46 +11:00
Tobin C. Harding 3344cf6be2
Favour $reverse instead of $reversed
Currently we have two things that are the same but use different
identifiers, use `$reverse` instead of `$reversed`.
2023-02-01 08:22:46 +11:00
Tobin C. Harding a121e19e94
hashes: Implement AsRef for fixed size arrays
Implement `AsRef<[u8; X]>` for hash types including wrapped hash types.
Doing so means at times the compiler can no longer infer the type because we have
`AsRef<[u8]` implemented also but we can use `into_inner` and `as_inner`
to get the inner array if needed.
2023-01-26 11:00:57 +11:00
Martin Habovstiak 3e520f9094 Use hex from internals rather than hashes
`bitcoin-internals` contains a more performant implementation of hex
encoding than what `bitcoin_hashes` uses internally. This switches the
implementations for formatting trait implementations as a step towards
moving over completely.

The public macros are also changed to delegate to inner type which is
technically a breaking change but we will break the API anyway and the
consuers should only call the macro on the actual hash newtypes where
the inner types already have the appropriate implementations.

Apart from removing reliance on internal hex from public API this
reduces duplicated code generated and compiled. E.g. if you created 10
hash newtypes of SHA256 the formatting implementation would be
instantiated 11 times despite being the same.

To do all this some other changes were required to the hex
infrastructure. Mainly modifying `put_bytes` to accept iterator (so that
`iter().rev()` can be used) and adding a new `DisplayArray` type. The
iterator idea was invented by Tobin C. Harding, this commit just adds a
bound check and generalizes over `u8` and `&u8` returning iterators.

While it may seem that `DisplayByteSlice` would suffice it'd create and
initialize a large array even for small arrays wasting performance.
Knowing the exact length `DisplayArray` fixes this.

Another part of refactoring is changing from returning `impl Display` to
return `impl LowerHex + UpperHex`. This makes selecting casing less
annoying since the consumer no longer needs to import `Case` without
cluttering the API with convenience methods.
2022-12-22 10:44:27 +01:00
Tobin C. Harding b9643bf3e9 Import bitcoin_hashes crate into hashes
We would like to bring the `bitcoin_hashes` crate into the
`rust-bitcoin` repository.

Import `bitcoin_hashes` into `rust-bitocin/hashes`, doing so looses all
the commit history from the original crate but if we archive the
original repository then the history will be preserved. We maintain the
same version number obviously and in the changelog we note the change of
repository.

Commit hash that was tip of `bitcoin_hashes` at time of import:

 commit 54c16249e06cc6b7870c7fc07d90f489d82647c7

Includes making `embedded` and `fuzzing` per-crate i.e., move them into
`bitcoin` as hashes includes these also.

NOTE: Does _not_ enable fuzzing for `hashes` in CI.

Notes on CI:

Attempts to merge in the github actions from the hashes crate however reduces
coverage by not running hashes tests for beta toolchain. Some additional
work could be done to improve the CI to increase efficiency without
reducing coverage. Leaving for another day.
2022-11-08 08:58:09 +11:00