Commit Graph

257 Commits

Author SHA1 Message Date
Jamil Lambert, PhD a76d76eca1
Change `T::from_str(s)` to `s.parse::<T>()`
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in the main codebase, not including
examples, rustdocs, and in the test module.

`use std::str::FromStr;` has been removed where this change makes
it unnecessary.
2024-08-27 17:31:00 +01:00
Fmt Bot fbf7f41875 2024-08-25 automated rustfmt nightly 2024-08-25 01:14:09 +00:00
merge-script 6f335b011c
Merge rust-bitcoin/rust-bitcoin#3223: Use TBD in deprecated attribute
a2be82c0c9 Use TBD in deprecated attribute (Tobin C. Harding)

Pull request description:

  Our `release` job checks for 'TBD', I can't remember exactly why but I thought we introduced `0.0.0-NEXT-RELEASE` because CI was failing when we used TBD - clearly this is not the case now because we have a bunch of `TBD`s in the code base.

  Change all the instances of `0.0.0-NEXT-RELEASE` to be `TBD`.

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

Tree-SHA512: b383cc4095484291a7b4dca593ad5e017e3a9de9bfae9d6e9447ae36da32aa1c0d1fd593f49fd52c04db5ca5cdbaae8b30a772f792df13542f0a157a86295746
2024-08-24 02:55:37 +00:00
Tobin C. Harding a2be82c0c9
Use TBD in deprecated attribute
Our `release` job checks for 'TBD', I can't remember exactly why but I
thought we introduced `0.0.0-NEXT-RELEASE` because CI was failing when
we used TBD - clearly this is not the case now because we have a bunch
of `TBD`s in the code base.

Change all the instances of `0.0.0-NEXT-RELEASE` to be `TBD`.
2024-08-23 14:49:57 +10:00
Martin Habovstiak 6e5bd473a6 Improve siphash's `as_u64` -> `to_u64` rename
The previous change was just a dumb rename with no deprecation and it
also kept the `self` type which should be taken by value since the hash
is `Copy`. This improves on it by adding a deprecated method of the
original name and changing the type to be `self` instead of `&self`.
2024-08-23 05:54:41 +02: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
merge-script 722a7239df
Merge rust-bitcoin/rust-bitcoin#3214: Panic in const context
60b3cabb41 Panic in const context (Tobin C. Harding)

Pull request description:

  Now that our MSRV is past 1.57 we can panic in const contexts.

  Fix: #2427

ACKs for top commit:
  Kixunil:
    ACK 60b3cabb41
  apoelstra:
    ACK 60b3cabb41 successfully ran local tests

Tree-SHA512: 705a8b7d52a11826e6084684706cb7e01dfaa554e4e369739e64e64263537b0c8c0e518b04e96249ecdeea1f22b534594ffd2a89e17ebba85b369d896e820239
2024-08-22 16:32:12 +00:00
merge-script 8f851967a2
Merge rust-bitcoin/rust-bitcoin#3184: Reduce API surface of tagged wrapped hash types
c97389596b Remove stale docs from sha256t_hash_newtype (Tobin C. Harding)
39f7dcb816 Reduce API surface of tagged wrapped hash types (Tobin C. Harding)

Pull request description:

  Recently we made it so that wrapper types created with `hash_newtype` were not general purpose hash types i.e., one could not easily hash arbitrary data into them. We would like to do the same for tagged wrapped hash types.

  In `hashes` do:

  - Create a new macro `sha256_tag` that does just the tag/engine stuff out of the `sha256t_hash_newtype` macro.
  - Deprecate the `sha256t_hash_newtype` macro.

  In `bitcoin` do:

  - Use a combination of `sha256_tag` and `hash_newtype` to create tagged wrapped hash types.

  Note that we do not add private helper functions `engine` and `from_engine` to the tagged wrapper types as we do for legacy/segwit in `sighash`. Can be done later if wanted/needed.

  Fix: #3135

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

Tree-SHA512: d937a8eac1a77298231f946f9dfbc2f7739af8da00f2075b0b54803b4111c0cec810bc6564515153769193056cf102a9c954e216664f055b249d4a6153b14bca
2024-08-22 14:42:58 +00:00
Tobin C. Harding 60b3cabb41
Panic in const context
Now that our MSRV is past 1.57 we can panic in const contexts.

Fix: #2427
2024-08-22 17:37:17 +10:00
Tobin C. Harding c97389596b
Remove stale docs from sha256t_hash_newtype
We removed the repetition a while ago from the macro but left the docs
in there - remove them.
2024-08-22 16:31:58 +10:00
Tobin C. Harding 39f7dcb816
Reduce API surface of tagged wrapped hash types
Recently we made it so that wrapper types created with `hash_newtype`
were not general purpose hash types i.e., one could not easily hash
arbitrary data into them. We would like to do the same for tagged
wrapped hash types.

In `hashes` do:

- Create a new macro `sha256t_tag` that does just the tag/engine stuff
out of the `sha256t_hash_newtype` macro.
- Deprecate the `sha256t_hash_newtype` macro.

In `bitcoin` do:

- Use a combination of `sha256t_tag` and `hash_newtype` to create tagged
wrapped hash types.

Note that we do not add private helper functions `engine` and
`from_engine` to the tagged wrapper types as we do for legacy/segwit in
`sighash`. Can be done later if wanted/needed.
2024-08-22 10:07:58 +10:00
Tobin C. Harding aed61bd2d4
Implement FromStr and serde impls for siphash
The `serde` impls and `FromStr` are missing from `siphash`, add them.
2024-08-21 16:00:13 +10:00
Tobin C. Harding f8846101ae
siphash: Make functions inherent
In recent work making functions on hash types inherent it seems we
missed `siphash`. Add inherent getters/setters to the `siphash::Hash`
type and call through to them from the `Hash` trait impl.
2024-08-21 16:00:13 +10:00
Tobin C. Harding 321d82ca53
hashes: Pin in extra_test
Note:

- The `extra_test.sh` script runs for all toolchains.
- The `schemars` crate does not use the repo lock files.
- We need to pin some deps when building the `schemars` test.

Pin in the `extra_test.sh` script, and mention it in the docs so the
docs don't go stale next MSRV update.

This was previously unnoticed because of the `run_task` bug.

ref: rust-bitcoin/rust-bitcoin-maintainer-tools#10
2024-08-21 15:31:28 +10:00
Tobin C. Harding 42c7617a46
Fix shchemars test
In #3010 we added a `length` field to the `sha256::Midstate` which broke
the `schemars` test but we did not notice because of the current bug [0]
in the `run_task` CI script.

[0] https://github.com/rust-bitcoin/rust-bitcoin-maintainer-tools/issues/10
2024-08-21 15:31:28 +10:00
Tobin C. Harding b6fda6517c
Implement JsonSchema for siphash::Hash
We lost this impl at some stage in the last few weeks and did not notice
because of a bug in `run_task` [0].

Implement `JsonSchema` for `siphash` as we do for all the other hash
types.

[0] https://github.com/rust-bitcoin/rust-bitcoin-maintainer-tools/issues/10
2024-08-21 15:31:28 +10:00
Tobin C. Harding 1af6ff4394
hashes: Feature gate hash_reader unit test
The `hash_reader` function is only available when `bitcoin-io` is
enabled - it should be feature gated.
2024-08-21 15:31:28 +10:00
Tobin C. Harding 5230d3309c
Remove hash_reader from sha256t_hash_newtype
The `hash_reader` function is new and unreleased, it should never have
been put into the `sha256t_hash_newtype` macro, and its broken.
2024-08-21 15:31:28 +10:00
leichak 2756b7fd7a Removed unneeded usages of vec! macro 2024-08-19 10:12:09 +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 50e3465bde
Merge rust-bitcoin/rust-bitcoin#3129: hashes:: Rename const_hash functions
e7762e0612 hashes:: Rename const_hash functions (Tobin C. Harding)

Pull request description:

  There are a number of issues with the two `const_hash` functions in the `sha256` module:

  - The two `const_hash` functions in the `sha256` module differ slightly, one finalizes the hash and one is for computing the midstate.
  - They are inefficient and provided for usage for const context only.

  Fix both issues by renaming the functions as discussed in #3075.

  Close: #3075

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

Tree-SHA512: 2b765bbbaa596d060a555495582b24175f660bf630de489cf0e0199f1c589f13f46dde5c9735bffece10a1ff116a70472f821df66c62a97fffb424f16e5568f9
2024-08-06 19:15:46 +00: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 e7762e0612
hashes:: Rename const_hash functions
There are a number of issues with the two `const_hash` functions in the
`sha256` module:

- The two `const_hash` functions in the `sha256` module differ slightly,
  one finalizes the hash and one is for computing the midstate.
- They are inefficient and provided for usage for const context only.

Fix both issues by renaming the functions as discussed in #3075.

Close: #3075
2024-08-06 12:27:15 +10: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
Shing Him Ng 5a91719755 Rename Siphash::as_u64 to Siphash::to_u64 2024-08-02 18:20:23 -05:00
Nick Johnson 2969b032f9 Push up the Default bound on HashEngine
* The Default bound only makes sense for unkeyed hash functions which
can fire up a new engine without a key. Keyed hash functions, like
SipHash24 or Poly1305 require a secret key to be initialized and
should not implement a default engine generator.
* SipHash24 tests updated to the previous default key "0".
2024-07-31 13:13:51 -07:00
merge-script 15b87606bf
Merge rust-bitcoin/rust-bitcoin#3100: Bump MSRV to 1.63
c72069e921 Bump MSRV to 1.63 (Martin Habovstiak)

Pull request description:

  The version 1.63 satisfies our requirements for MSRV and provides significant benefits so this commit bumps it. This commit also starts using some advantages of the new MSRV, namely namespaced features, weak dependencies and the ability to use trait bounds in `const` context.

  This however does not yet migrade the `rand-std` feature because that requires a release of `secp256k1` with the same kind of change - bumping MSRV to 1.63 and removing `rand-std` in favor of weak dependency. (Accompanying PR to secp256k1: https://github.com/rust-bitcoin/rust-secp256k1/pull/709 )

  Suggested plan:

  * merge both PRs
  * at some point release `hashes` and `secp256k`
  * remove `rand-std` from `bitcoin`
  * release the rest of the crates

ACKs for top commit:
  apoelstra:
    ACK c72069e921
  tcharding:
    ACK c72069e921

Tree-SHA512: 0b301ef8145f01967318d3ed1c738d33e6cf9e44f835f3762122b460a536f926916dbd6ea39d6f80b4f95402cd845e924401e75427dbb0731ca5b12b4fa6915e
2024-07-28 21:11:28 +00:00
Martin Habovstiak c72069e921 Bump MSRV to 1.63
The version 1.63 satisfies our requirements for MSRV and provides
significant benefits so this commit bumps it. This commit also starts
using some advantages of the new MSRV, namely namespaced features, weak
dependencies and the ability to use trait bounds in `const` context.

This however does not yet migrade the `rand-std` feature because that
requires a release of `secp256k1` with the same kind of change - bumping
MSRV to 1.63 and removing `rand-std` in favor of weak dependency.
2024-07-27 07:24:32 +02:00
Tobin C. Harding 98fe6179db
Use "unfinalized" in header of Midstate
The midstate has not been finalized [0], so use the term in the struct
header.

FTR I don't know _exactly_ what "finalized" means in the context of
sha256 hashing (or hashing in general). This change came from a review
suggestion and we have other mentions of "finalized" in the code.
2024-07-18 06:16:46 +10:00
Tobin C. Harding 86de586898
Use const to construct Midstate
As a bit more of an example of how to use the `sha256::Midstate` use a
`static` in one of the unit tests.
2024-07-18 06:16:46 +10:00
Tobin C. Harding dcb18bfa7a
Add length to sha256::Midstate
In a `HashEngine` the `length` field represents number of bytes
input into the hash engine.

Note also:

> the midstate bytes are only updated when the compression function is
run, which only happens every 64 bytes.

Currently our midstate API allows extracting the midstate after any
amount of input bytes, this is probably not what users want.

Note also that most users should not be using the midstate API anyways.

With all this in mind, add a private `length` field to the `Midstate`
struct and enforce an invariant that it is modulo 64.

Add a single const `Midstate` constructor that panics if the invariant
is violated. The `Midstate` is niche enough that panic is acceptable.

Remove the `from_slice`, `from_byte_array`, and `to_byte_array`
functions because they no longer make sense. Keep `AsRef<[u8]>` for
cheap access to the midstate's inner byte slice.

Note change to `Debug`: `bytes` field now does not include the `0x`
prefix because `as_hex` because of the use of `debug_struct`.

Enjoy nice warm fuzzy feeling from hacking on crypto code.
2024-07-18 06:16:45 +10:00
Tobin C. Harding db0502d3cd
Use third person in rustdoc
As is convention in this repo use the third person when describing the
`sha256::HashEngine::from_midstate` function.
2024-07-18 06:13:26 +10:00
Tobin C. Harding 34dd95f909
Debug Midstate forwards
Done in preparation for adding a `length` field to `Midstate` and also
in preparation for removing the `Display` implementation (will be
justified in the patch that does it).

Currently in the `Debug` impl of `Midstate` we are calling through to
`Display` using the alternate form of printing, we can use `as_hex` to
achieve almost the same thing. Note that in `LowerHex` we use the
`fmt_hex_exact` macro that allows us to reverse the iterator however
when we later attempt to use `f.debug_struct` we cannot use the macro.

Elect to change the current behaviour to `Debug` forwards, shown by the
change to the regression test.
2024-07-18 06:13:24 +10:00
Tobin C. Harding 1d0e70b1da
Add regression test for Midstate debug output
In preparation for patching the `Debug` implementation of `Midstate` and
a regression test.
2024-07-18 06:12:53 +10:00
Tobin C. Harding ca823945fc
Manually implement AsRef (remove Borrow)
Currently we are using a macro to implement `AsRef` and `Borrow` for
`sha256::Midstate`.

In preparation for adding a length field to the `Midstate` remove the
implementation of `Borrow` but keep `AsRef`.

API breaking change.
2024-07-17 08:47:38 +10:00
Tobin C. Harding 7dc68b62e9
Remove serde from sha256::Midstate
The `sha256::Midstate` is a niche use case type, there is no real reason
we need to support serialization/deserialization. If people really want
this they can just get the byte array and serialize it themselves.

API breaking change.
2024-07-17 08:47:38 +10:00
Tobin C. Harding 07e8e5d3a6
Stop using macro for Midstate
In preparation for changing the `sha256::Midstate` internals stop using
the `arr_newtype_fmt_impl` macro and implement the `fmt` traits
manually.

In doing so, remove the `DISPLAY_BACKWARDS` const but keep the current
behaviour of displaying the midstate backwards.
2024-07-17 08:47:38 +10:00
Tobin C. Harding 37b54dd54c
Move from_midstate function
Move the `sha256::HashEngine::from_midstate` function to be in the same
impl block as `midstate`.

Refactor only, no logic change.
2024-07-17 08:47:38 +10:00
Tobin C. Harding 9efe4cea9d
Move impl block under struct
Put the impl block for `Midstate` under the struct, as is customary.

(Note the diff shows moving some other code around the impl block not
the impl block itself.)

Code move only.
2024-07-17 08:47:38 +10:00
Tobin C. Harding 5941008733
Import sha256t in docs builds
Explicitly import `sha256t` in docs builds and remove explicit link
target. This patch is code churn on its own but the `sha256t` module
will be used again in proceeding patches, done separately to reduce the
size/complexity of proceeding patches.
2024-07-17 08:47:37 +10:00
merge-script 8eb5d8fad0
Merge rust-bitcoin/rust-bitcoin#3019: Fix error messages
2169b75bba Use lower case error messages (Jamil Lambert, PhD)

Pull request description:

  Error messages should be lower case, except for proper nouns and variable names.  These have all been changed.

  ~~They should also state what went wrong.  Some expect error messages were positive, giving the correct behaviour or correct input.  These have been changed so that they are now negative, i.e. saying what went wrong.~~

  EDIT: After further discussion it was decided not to change the expect messages.

ACKs for top commit:
  Kixunil:
    ACK 2169b75bba
  tcharding:
    ACK 2169b75bba

Tree-SHA512: 92442c869e0141532425f6fca5195fd319b65026f68c4230a65ad70253565d98931b2b44ee202975c307280525c505147e272297dc81207312e40c43d007021c
2024-07-16 15:30:26 +00:00
merge-script c50796c238
Merge rust-bitcoin/rust-bitcoin#2981: Remove to/from/as_raw_hash functions
2b56f763d0 hashes: Remove to/from/as_raw_hash (Tobin C. Harding)

Pull request description:

  In an effort to shrink the API of `hashes` remove the `from_raw_hash`, `to_raw_hash`, and `as_raw_hash` inherent functions from types created with the `hash_newtype` macro.

  There are a few reasons why this is favourable:

  - It allows stable crates to use the macro and not expose unstable `hashes` types in their API.
  - It makes types created with the macro less "general" in the sense that its more obscure to just hash any data into them. This allows us to write cleaner APIs in `rust-bitcoin`.

ACKs for top commit:
  Kixunil:
    ACK 2b56f763d0
  apoelstra:
    ACK 2b56f763d0

Tree-SHA512: 3d73aa8250dd775994623c9201dd819256acf2ec82526b3537da74c9e19c2ac5e8bba358a2171f7b02342804cb6b4d5ac4dca88d912b3d46d14e3bc35dd5cb91
2024-07-15 13:53:37 +00:00
Jamil Lambert, PhD 2169b75bba Use lower case error messages
Error messages should start with a lower case character unless it is a
proper noun.

This has been changed everywhere.
2024-07-15 09:25:08 +01:00
Tobin C. Harding 2b56f763d0
hashes: Remove to/from/as_raw_hash
In an effort to shrink the API of `hashes` remove the `from_raw_hash`,
`to_raw_hash`, and `as_raw_hash` inherent functions from types created
with the `hash_newtype` macro.

There are a few reasons why this is favourable:

- It allows stable crates to use the macro and not expose unstable
`hashes` types in their API.
- It makes types created with the macro less "general" in the sense that
its more obscure to just hash any data into them. This allows us to
write cleaner APIs in `rust-bitcoin`.
2024-07-14 05:03:36 +10:00
Tobin C. Harding 8dcecfc144
Remove midstate from the GeneralHash and HashEngine traits
Midstates are not generic objects; they don't have universal
cryptographic properties and if you are using them you should be using a
specific midstate type. Therefore it shouldn't be part of `GeneralHash` or
`HashEngine`. Furthermore, in practice it seems like `sha2` midstates are
the only ones that anybody uses, at least in bitcoin.

Remove the midstate stuff from the `GeneralHash` and `HashEngine`
traits. Keep the `midstate` functionality as inherent functions if it is
used internally. Keep the functionality on `sha256` as inherent public
functions.
2024-07-13 07:59:33 +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
Tobin C. Harding 51010777bf
hashes: Strongly type the hash160::HashEngine
Currently we are using a type alias for the `hash160::HashEngine`.

Type alias' allow for potential mixing of types, a `hash160::HashEngine`
struct can better serve our users with not much additional complexity or
maintenance burden.

As we did for the `sha256d::HashEngine`, add a new wrapper type
`hash160::HashEngine` that replaces the current type alias.
2024-07-09 13:30:20 +10:00
Tobin C. Harding d5dd54a489
hashes: Strongly type the sha256d::HashEngine
Currently we are using a type alias for the `sha256d::HashEngine`.

Type alias' allow for potential mixing of types, a `sha256d::HashEngine`
struct can better serve our users with not much additional complexity or
maintenance burden.
2024-07-09 13:21:11 +10:00
Tobin C. Harding a7422a779c
hashes: Add const hash engine constructors
Add `const` constructors to all hash engines. Call through to
`Self::new` in `default` impls on `HashEngine`.
2024-07-09 13:17:16 +10:00