Commit Graph

6323 Commits

Author SHA1 Message Date
Tobin C. Harding e6f7b26d80
Use _unchecked in amount const types
We are about to start enforcing the MAX_MONEY invariant. Doing so will
change constructors to return an error type.

In preparation use the `_unchecked` constructor for all the consts.

Internal change only, no logic changes.
2025-03-11 05:32:07 +11:00
Andrew Poelstra ef0af8d62e
Use sat/ssat constructors throughout tests
There is an as yet unresolved discussion about the unchecked amount
constructor. In an effort to focus the amount of changes required later
and also to make the `tests` module uniform use the `sat` and `ssat`
constructor functions everywhere.

Internal change only, no logic changes.
2025-03-11 05:32:06 +11:00
Tobin C. Harding 8ecdc7c275
Use den_ prefix for local Denomination variable
Throughout the `amount::tests` module we use `sat` and `ssat` as aliases
to amount constructors but in on test we use them as `Denomination`
variables. To assist clarity and so we can introduce uniform usage of
the constructor aliases change the variable names to use the `den_`
prefix.

Internal change only, no logic changes.
2025-03-11 05:32:06 +11:00
Tobin C. Harding 938461cc65
psbt: Use Amount::ZERO in unit test
We have a const for this, use it.

Internal change only.
2025-03-11 05:32:06 +11:00
merge-script 540038df90
Merge rust-bitcoin/rust-bitcoin#4216: Replace underflow with overflow in doc comments
a273814d23 Replace underflow with overflow in doc comments (yancy)
2f897e2109 Remove warning section (yancy)

Pull request description:

  The use of underflow is misleading.  Adding one to MAX and subtracting one from MIN are both considered an overflow.

  Note I tried to keep to 80 column line length so a paragraph needed some shuffling.

  closes https://github.com/rust-bitcoin/rust-bitcoin/issues/4187

ACKs for top commit:
  Kixunil:
    ACK a273814d23
  tcharding:
    ACK a273814d23
  apoelstra:
    ACK a273814d23f7f804f60bc260e6939a75ed9f1790; successfully ran local tests

Tree-SHA512: 3d1fd3a6c3e11694d189086b6f9e14d55b912497dca8bac2153cc54afbbbee527cae354e60781f8b61591b13aa85fb9e490cea2e7aed3dd9d3e4335502ffb84b
2025-03-10 16:26:38 +00:00
merge-script 3bb1a7acea
Merge rust-bitcoin/rust-bitcoin#4223: primitives: Feature gate import
0428554585 primitives: Feature gate import (Tobin C. Harding)

Pull request description:

  Feature gate the `Infallible` import. Found with `clippy`.

ACKs for top commit:
  apoelstra:
    ACK 0428554585de668e2a59fa61e44b473f4085d717; successfully ran local tests

Tree-SHA512: 23bf7d62d81af87b67395ddee251cef82140895be76026c43c774997c29f2ed2707bf42e82d0a155a5cff83412538cb2ed5251f10c0e0cb746434a8d7db8e554
2025-03-10 15:26:57 +00:00
Erick Cestari 8f74b823ab Add validation for private key format and master key constraints
This commit adds additional validation checks when decoding extended private keys:

1. Verifies that byte 45 is zero as required by BIP-32 specification
2. For master keys (depth=0), ensures parent fingerprint is zero
3. For master keys (depth=0), ensures child number is zero

These checks improve security by rejecting malformed keys that could
potentially lead to unexpected behavior. Added corresponding error types
and unit tests to verify each validation rule.
2025-03-10 10:22:41 -03:00
AM 199f57849a Remove references to cfg(mutate) from lint allow - no longer allowed
Also fix incorrect spelling of honggfuzz
2025-03-10 12:45:57 +08:00
AM a65d1d8b95 docs: Update README to replace use of mutagen with cargo-mutants 2025-03-10 12:17:04 +08:00
RiceChuan a824b79206 docs: The quotation marks are incorrect.
Signed-off-by: RiceChuan <lc582041246@gmail.com>
2025-03-10 12:10:12 +08:00
wgyt 08ced04d12 fix typos 2025-03-10 09:04:59 +08:00
merge-script 694d926ed0
Merge rust-bitcoin/rust-bitcoin#4209: Fix fuzz targets
ddb6dd520e Move module out of fuzz_target directory (Jamil Lambert, PhD)

Pull request description:

  #4185 introduced a new file `fuzz/fuzz_targets/bitcoin/fuzz_utils.rs` which is not a valid fuzz target.  This causes the daily fuzz workflow to fail on `verify-execution`.

  Move the module to the `src/` directory. Create a `lib.rs` file.

ACKs for top commit:
  brunoerg:
    ACK ddb6dd520e
  tcharding:
    ACK ddb6dd520e

Tree-SHA512: da05ab92cbb934ee90c2dc11b9edbb95e6ae66cbbeec1481ff2e2d225b43c59a7bf75a22578ca3f62c9fdda2ad195159e8a8d45529948382369a85c8718954b3
2025-03-09 15:29:24 +00:00
merge-script aefcf2bb02
Merge rust-bitcoin/rust-bitcoin#4178: primitives: Enable pedantic lints
df500e9b71 primitives: Enable pedantic lints (Tobin C. Harding)

Pull request description:

  Draft to check the subjective ones please, then I'll squash.

ACKs for top commit:
  apoelstra:
    ACK df500e9b71187fe658da76adafdb3300a51de2ef; successfully ran local tests

Tree-SHA512: 8cc8c9b369a63c1b2b26461e288a818e3b74e0f9b7359c964c1650028d3161db1d79369c74f18e79958873bf4d223ee72fa481708600f0297d79377d97a84dda
2025-03-09 13:36:07 +00:00
Tobin C. Harding 0428554585
primitives: Feature gate import
Feature gate the `Infallible` import. Found with `clippy`.
2025-03-09 05:57:03 +11:00
merge-script b1d7c0a0a2
Merge rust-bitcoin/rust-bitcoin#4220: Automated daily update to rustc (to nightly-2025-03-06)
02a9efddc4 Automated update to Github CI to rustc nightly-2025-03-06 (Update Nightly Rustc Bot)

Pull request description:

  Automated update to Github CI workflow `rust.yml` by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  tcharding:
    ACK 02a9efddc4

Tree-SHA512: 747376061e7cc8a3a44be33996aafd3b2866c95a71325946b54770354888adc9a5711a38e6766744ba9da9154c7a8b2deb3f8ca3b103fddec2988b839a278c2b
2025-03-09 05:47:51 +11:00
yancy a273814d23 Replace underflow with overflow in doc comments
The use of underflow is misleading.  Adding one to MAX and
subtracting one from MIN are both considered an overflow.
2025-03-08 10:41:20 -06:00
yancy 2f897e2109 Remove warning section
Since monadic handling has been introduced, panics have been replaced
with return errors.  Therefore this section is no longer applicable.
2025-03-08 10:41:20 -06:00
merge-script 86266d2dad
Merge rust-bitcoin/rust-bitcoin#4161: Improve ops for amount types
0a9f14f7b0 Implement Div by amount for amount types (Tobin C. Harding)
b57bfb9bc5 Add missing Mul impls for amount types (Tobin C. Harding)
501c9ab89e Test amount ops that involve an integer (Tobin C. Harding)
851080d3b1 Add more add/sub tests (Tobin C. Harding)
47923957b1 Improve add/sub tests for amount types (Tobin C. Harding)
8bb9ce3e47 Add tests for amount op int (Tobin C. Harding)

Pull request description:

  Improve the test coverage and add missing implementations of math operations for the amount types.

  Along the way close #4030.

ACKs for top commit:
  apoelstra:
    ACK 0a9f14f7b036c5232449d058fb6d425c8376d87a; successfully ran local tests; nice!

Tree-SHA512: f303b2a90b5bb9e77091e047f8325821a5c89f52dfe242d849968dba0d097d3868d444009c2c05b9d7c0e91fa2ce6898cdc4733977699ca4b1ae226562878cdf
2025-03-08 15:12:51 +00:00
merge-script eee3505d2b
Merge rust-bitcoin/rust-bitcoin#4214: Fix some comments
2aac5a1f81 Fix some comments (NinaLua)

Pull request description:

  I fixed some typos in the comments, please review it.

ACKs for top commit:
  Kixunil:
    ACK 2aac5a1f81
  apoelstra:
    ACK 2aac5a1f81a9bb217c4dfb7e45b96188ea60e35b; successfully ran local tests

Tree-SHA512: 50a55451b166189e8ca3d2725ed7bb8ff95a8f1ebef0296c0003414871f1b211e6ffcc3b7225302dd3d6760bfc3f65cf8ed730327ceab60cd55b868ccb0cea9a
2025-03-08 14:20:59 +00:00
merge-script 8a7c295412
Merge rust-bitcoin/rust-bitcoin#4212: Add `fee_rate::serde` re-export
d1c758f5a4 Add fee_rate::serde re-export (Tobin C. Harding)

Pull request description:

  When we added the `fee_rate::serde` module we forgot to re-export it. This is needed so downstream can do specify serde attributes on struct fields.

  ```rust
      #[serde(with = "bitcoin::fee_rate::serde::as_sat_per_kwu")]
      rate: FeeRate,
  ```

ACKs for top commit:
  Kixunil:
    ACK d1c758f5a4
  apoelstra:
    ACK d1c758f5a472a4a67cf9c7afa9ef9c0d793a2e16; successfully ran local tests

Tree-SHA512: 6e6f7879d8a0dab59d79f0e41dd5f9f791b72dfb5a1583d0c87ec04216c0a9c0e5c4fb328b93f5298af47b56d898f48717b1641f51295314423e6a569b4677fe
2025-03-08 05:01:52 +00:00
merge-script f7b1810b15
Merge rust-bitcoin/rust-bitcoin#4182: Replace uses of `chunks_exact` with `as_chunks`
a013700527 Replace uses of `chunks_exact` with `as_chunks` (Martin Habovstiak)

Pull request description:

  This is now ready for review.

  In the past we've been using `chunks_exact` because const generics were unstable but then, when they were stabilized we didn't use `as_chunks` (or `array_chunks`) since they were unstable. But the instability was only because Rust devs don't know how to handle `0` being passed in. The function is perfectly implementable on stable. (With a tiny, easy-to-understand `unsafe` block.) `core` doesn't want to make a decision for all other crates yet but we can make it for our own crates because we know that we simply never pass zero. (And even if we did, we could just change the decision.)

  It also turns out there's a hack to simulate `const {}` block in our MSRV, so we can make compilation fail early.

  This commit adds an extension trait to internals to provide the methods, so we no longer have to use `chunks_exact`. It also cleans up the code quite nicely.

  Previous unresolved question, leaving for reference:

  > One issue with this change is that the names collide which could lead to hard error in future Rust versions. How do we solve it?
  > * ignore and just backport the fix once that actually happens
  > * rename the methods to something reasonable (e.g. `as_array_chunks`) - this risks that they'll rename the methods to the same thing by accident and it'll break anyway
  > * rename the methods to something silly (`bitcoin_as_chunks`) - yeah, the risk above is not there but then we have silly-looking code.

  We've decide to just rename the methods to something that won't possibly collide.

ACKs for top commit:
  tcharding:
    ACK a013700527
  apoelstra:
    ACK a01370052715b6733f07011f28944105493bda63; successfully ran local tests; nice!

Tree-SHA512: cc3359518f97e510da5ee9a33495e26c338bfc3e4162aaffcc72ed9c7daad0daf5e9ca3d23bce50877b0d3881792e98e28d21174a4426bb01281f12285ce08d1
2025-03-08 03:58:10 +00:00
merge-script a959df3edf
Merge rust-bitcoin/rust-bitcoin#4197: Take `spent` closure by value in `count_witness_sigops`
ae0ba6c135 Take spent closure by value in count_witness_sigops and count_p2sh_sigops (jrakibi)

Pull request description:

  This fixes #4141

  Changed `count_witness_sigops` to take the `spent` closure by value instead of `&mut`
  This removes the need for `&mut` when calling the function while still allowing mutable closure to be passed when needed

ACKs for top commit:
  Kixunil:
    ACK ae0ba6c135
  tcharding:
    ACK ae0ba6c135
  apoelstra:
    ACK ae0ba6c1356505697fc5e841741ac488538e3407; successfully ran local tests

Tree-SHA512: 76c5c98994b00412d0d371c07e3e83538f21754129a67889c66e1299e0453defaecb82bd4305297f772d65b042045d3579eaac14f8ea59419bf26b8b0d2ac84f
2025-03-08 01:25:53 +00:00
Update Nightly Rustc Bot 02a9efddc4 Automated update to Github CI to rustc nightly-2025-03-06 2025-03-08 01:18:46 +00:00
Tobin C. Harding c707b959b7
Rename timestamp module to time
We just re-named `Timestamp` to `BlockTime`. We have a `units::block`
module but it currently holds abstractions (`BlockHeight` and
`BlockInterval`) that are not onchain abstractions and therefore
somewhat different from the `BlockTime`. Instead of making `block` a
block 'utils' module instead re-name the `timestamp` module to `time`.
2025-03-08 08:30:10 +11:00
Tobin C. Harding e2dee4900f
Re-name Timestamp to BlockTime
We just added a `Timestamp` type without knowing that there was a push
by OpenTimestamps to also create a timestamp and that our new type may
lead to confusion. Our timestamp is explicitly for the `time` field in a
block so we can call it `BlockTime`. This name change makes the module
name stale but we will change that in a following patch to ease review.
2025-03-08 08:28:46 +11:00
merge-script 1a18ff582c
Merge rust-bitcoin/rust-bitcoin#4127: units: Prevent casting pub enums as ints
97453ef9bc units: Prevent casting pub enums as ints (Tobin C. Harding)

Pull request description:

  A public enum with simple variants gets an automatic integer variant that can be cast by library consumers. This puts a unnecessary maintenance burden upon us because we cannot then add variants in the middle of others.

  Add a hidden variant to the single public non-error enum in `units`.

ACKs for top commit:
  Kixunil:
    ACK 97453ef9bc
  apoelstra:
    ACK 97453ef9bc2b99a67252419ff015f13679df7312; successfully ran local tests

Tree-SHA512: 2515152107fb21a2dbdef9b46308fef6bd45f4a9719da7a39149b3bdbce6a93dc0f98e112ac246eb32dbe4df1210d5e6328c26ea8678e3da15276e893b39cc9c
2025-03-07 17:32:54 +00:00
merge-script 5581c49e0f
Merge rust-bitcoin/rust-bitcoin#4085: Remove the `GeneralHash` trait
95ad91cdb6 hashes: Remove the GeneralHash trait (Tobin C. Harding)
6426e59c63 Remove unused trait import (Tobin C. Harding)
791501eabc io: Use function in place of GeneralHashExt (Tobin C. Harding)
2b6ef31469 hashes: Add hash_byte_chunks function to modules (Tobin C. Harding)
d3846895d7 hashes: Add hash function to modules (Tobin C. Harding)
e1bac7da55 Bound HmacEngine on HashEngine (Tobin C. Harding)
ab63b7a0ff Add Hash type and finalize method to HashEngine (Tobin C. Harding)
84623ffaf9 Add hash_again regression test (Tobin C. Harding)

Pull request description:

  This is the done as part of #4051.

  Requires some surgery on the `Hmac` and `Hkdf` types 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.

ACKs for top commit:
  Kixunil:
    ACK 95ad91cdb6
  apoelstra:
    ACK 95ad91cdb64c8870d3eb992090bab7a70d1369b9; successfully ran local tests

Tree-SHA512: bfd215de51c115144c6f5b3430b17dad5d770a8c876fe3775af2828ec620a1f4e4155e63bb18dac244a82c3840413a615d55a0fef91b6949d3de319aa5bb8c2f
2025-03-07 16:35:09 +00:00
Jamil Lambert, PhD ddb6dd520e
Move module out of fuzz_target directory
The new module `fuzz_utils` in the `fuzz_targets/` directory causes
`verify-execution` to fail.

Move the module to the `src/` directory. Create a `lib.rs` file.
2025-03-07 15:47:39 +00:00
NinaLua 2aac5a1f81 Fix some comments
Signed-off-by: NinaLua <iturf@sina.cn>
2025-03-07 17:03:58 +08:00
merge-script 845daaee16
Merge rust-bitcoin/rust-bitcoin#4199: ci: Update GitHub workflows to use ubuntu-24.04 instead of ubuntu-latest
2a6cbda304 ci: Update GitHub workflows to use ubuntu-24.04 instead of ubuntu-latest (Erick Cestari)

Pull request description:

  This PR updates all GitHub workflow files to use `ubuntu-24.04` instead of `ubuntu-latest`.

  This change improves CI reliability by:
  - Pinning to a specific Ubuntu version instead of a floating reference
  - Preventing unexpected CI failures when GitHub updates the `ubuntu-latest` alias
  - Ensuring consistent build environments and reproducible CI results
  - Allowing for controlled migration to newer Ubuntu versions when ready

  The modification affects all workflow files in the `.github/workflows/` directory and the fuzz generation script.

  Context:

  > Yeah, I prefer the explicit version (and would prefer changing the other `-latest` ones to use the explicit version too). But I don't feel strongly.

  _Originally posted by apoelstra in https://github.com/rust-bitcoin/rust-bitcoin/pull/4188#discussion_r1980549662_

   > I think an explicit version would be a bit better. It wouldn't suddenly and unexpectedly change and if GH does something like this again, it'll be a few years into the future. Also an explicit version is nicer for troubleshooting.

  _Originally posted by Kixunil in https://github.com/rust-bitcoin/rust-bitcoin/pull/4188#discussion_r1979872413_

ACKs for top commit:
  Kixunil:
    ACK 2a6cbda304
  tcharding:
    ACK 2a6cbda304

Tree-SHA512: da8c483ddeb8839c4bd5449185ca51881c966ab010c8df9fd17970d621343fceb37d38562b20e86cf4a8ed10f752afdb4dde388db45ee22ecb26e83a67b2204f
2025-03-07 03:54:36 +00:00
Tobin C. Harding d1c758f5a4
Add fee_rate::serde re-export
When we added the `fee_rate::serde` module we forgot to re-export it.
This is needed so downstream can do specify serde attributes on struct
fields.

```rust
    #[serde(with = "bitcoin::fee_rate::serde::as_sat_per_kwu")]
    rate: FeeRate,
```
2025-03-07 14:02:40 +11:00
merge-script 9d7190e581
Merge rust-bitcoin/rust-bitcoin#4190: Kill mutants in primitives and units
90d909becc Kill mutants in primitives and units (Shing Him Ng)

Pull request description:

  This kills 15 mutants found with the mutants workflow. Ran `cargo mutants` locally to confirm

  Closes #4156
  Closes #4106

ACKs for top commit:
  jamillambert:
    ACK 90d909becc
  tcharding:
    ACK 90d909becc
  apoelstra:
    ACK 90d909becc4638c03003845154e9cc1eb5f3ad41; successfully ran local tests
  Kixunil:
    ACK 90d909becc

Tree-SHA512: e5c95a1c4054cf1c60c940ea605eec84dffcbff292f9c7c4d96813c6389e807c318f6c5f8f69ceeb9ffcab3c3e45aa0d5a8fda7335d540c6f070aab92bae7a0f
2025-03-07 02:53:55 +00:00
merge-script 12b35e734e
Merge rust-bitcoin/rust-bitcoin#4198: Minor: fix typo
1bb3d6a371 Minor: fix typo (Peter Todd)

Pull request description:

ACKs for top commit:
  tcharding:
    ACK 1bb3d6a371
  Kixunil:
    ACK 1bb3d6a371
  apoelstra:
    ACK 1bb3d6a371d6a09b3d27e1aaec0f40f4f12e5c3b; successfully ran local tests

Tree-SHA512: 4387859970fef92232a925d014ca0cfdfb0ffc9f4ea3b956a87aee21a717bacb48e7416001f92018db14e1d3c4548619a1f87235f42cab8dc9d02881df7231d0
2025-03-07 01:32:33 +00:00
merge-script 631ea1a2a9
Merge rust-bitcoin/rust-bitcoin#4126: Add additional re-exports
5da506b506 Add additional re-exports (Tobin C. Harding)

Pull request description:

  As we do for other types add two new alias' at the crate root of `primitives` and mirror it in `bitcoin`:

  - `BlockVersion`
  - `TransactionVersion`

ACKs for top commit:
  apoelstra:
    ACK 5da506b506aa41b88aa7e8ecdffdcc0478ec72b6; successfully ran local tests
  Kixunil:
    ACK 5da506b506

Tree-SHA512: 5f91e3aae87b1128256b528d20d9aab562bf054131ed8028e0db789e2b487863ad4c89aaca080d0fbcf760dd160815a1843046c5fab0ed1483230fdedc66402e
2025-03-06 23:13:42 +00:00
merge-script fa8eaf0257
Merge rust-bitcoin/rust-bitcoin#4092: Add `Timestamp` newtype
b3f122b399 Add Timestamp newtype (Tobin C. Harding)

Pull request description:

  Bitcoin block headers have a timestamp. Currently we are using a `u32`. While this functions correctly it gives the compiler no chance to enforce type safety.

  Add a `Timestamp` newtype that is a thin wrapper around a `u32`. Document it and test the API surface in `api.rs`.

ACKs for top commit:
  apoelstra:
    ACK b3f122b3996c1a73479be2f95b7f2ae642c9c56f; successfully ran local tests
  Kixunil:
    ACK b3f122b399

Tree-SHA512: 6f4a4a588bc836243ae28f3d36be6c0ae264cb2b7a0061277910b107d05e5ca0e679497d2890208f5d8ec148f37bf263bcd0b0410f9e5e6370d8e763ff30b78a
2025-03-06 19:34:11 +00:00
merge-script febce17eff
Merge rust-bitcoin/rust-bitcoin#4186: Clean up Witness API
Pull request description:

  Enhance Witness struct element access methods:

  - Rename `nth()` to `get()` for clearer slice-like element retrieval
  - Introduce `get_back()` method for flexible reverse indexing
  - Remove redundant `second_to_last()` and `third_to_last()` methods
  - Add `#[track_caller]` to index implementation for better error tracking
  - Update all references to use new method names
  - Improve documentation with usage examples

  The changes provide a more intuitive and consistent approach to accessing witness elements.

  Close #4098

ACKs for top commit:
  Kixunil:
    ACK 3ca3218c23
  tcharding:
    ACK 3ca3218c23
  apoelstra:
    ACK 3ca3218c236c63a9b006047524e2b47e310f07d9; successfully ran local tests

Tree-SHA512: 163e7457f3fe5141373e27a6df5fe1da6f2f05f02e877ef96243510d030d832c0fa86ade781e015a3c392f004651170b60438a83d330f1059457e5ade6478af7
2025-03-06 18:43:27 +00:00
Martin Habovstiak a013700527 Replace uses of `chunks_exact` with `as_chunks`
In the past we've been using `chunks_exact` because const generics were
unstable but then, when they were stabilized we didn't use `as_chunks`
(or `array_chunks`) since they were unstable. But the instability was
only because Rust devs don't know how to handle `0` being passed in. The
function is perfectly implementable on stable. (With a tiny,
easy-to-understand `unsafe` block.) `core` doesn't want to make a
decision for all other crates yet but we can make it for our own crates
because we know that we simply never pass zero. (And even if we did, we
could just change the decision.)

It also turns out there's a hack to simulate `const {}` block in our
MSRV, so we can make compilation fail early.

This commit adds an extension trait to internals to provide the methods,
so we no longer have to use `chunks_exact`. It also cleans up the code
quite nicely.
2025-03-06 19:02:08 +01:00
merge-script a27a6b9592
Merge rust-bitcoin/rust-bitcoin#4201: Make `hex` in `internals` optional
d1d483491f Make `hex` in `internals` optional (Martin Habovstiak)

Pull request description:

  The `hex` crate is not always desirable - e.g. when the consumer wants to work with raw data only. We already had this optional in `hashes` but if `hashes` is going to depend on `internals` it would break this property.

  This change makes `hash` optional, since it's easy: there's just one struct that depends on it.

ACKs for top commit:
  tcharding:
    ACK d1d483491f
  apoelstra:
    ACK d1d483491f5f0181e60778e557fadd99b44b5d30; successfully ran local tests; nice

Tree-SHA512: c20091e6febb49b9114273c280580ddcdafc91893f3c73288b374f51990f09f035a044806dd26777148f2e4341ad082ab05f1b49f8ceb3bcd24eb210ffa5fd5f
2025-03-06 16:50:12 +00:00
merge-script 181b7482ca
Merge rust-bitcoin/rust-bitcoin#4174: Improve weight
2434afc40d Make Weigth::from_kwu const (Tobin C. Harding)
1fe984552e Improve Weight rustdocs (Tobin C. Harding)

Pull request description:

  Do two minor improvements to the `weight` module. Docs and add `const`.

ACKs for top commit:
  apoelstra:
    ACK 2434afc40dadd213f8695c5ce91d397c03f337be; successfully ran local tests
  Kixunil:
    ACK 2434afc40d

Tree-SHA512: e3e9653d5fcd060c27a2313e642d7b96f51b9342953505a30a9748cb7f0c19a87bcb1faadb1b07ecc770aaec45496e5a750ac48e3e9141e379c554f0875df6a1
2025-03-06 15:58:59 +00:00
jrakibi ae0ba6c135 Take spent closure by value in count_witness_sigops and count_p2sh_sigops
This fixes issue #4141
Change count_witness_sigops and count_p2sh_sigops to take the spent
closure by value instead of &mut

- Changed both functions to accept S as a value (FnMut) instead of &mut S
- Removes need to annotate &mut when calling the function
2025-03-06 16:38:32 +07:00
Tobin C. Harding 95ad91cdb6
hashes: Remove the GeneralHash trait
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.
2025-03-06 11:47:29 +11:00
Tobin C. Harding 6426e59c63
Remove unused trait import
This import is not needed. Interesting that the linter does not catch
it.
2025-03-06 11:47:29 +11:00
Tobin C. Harding 791501eabc
io: Use function in place of GeneralHashExt
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.
2025-03-06 11:47:29 +11: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
Tobin C. Harding 84623ffaf9
Add hash_again regression test
Add a simple regression test prior to patching the
`sha256::Hash::hash_again` function.
2025-03-06 11:43:51 +11:00
Tobin C. Harding 97453ef9bc
units: Prevent casting pub enums as ints
A public enum with simple variants gets an automatic integer variant
that can be cast by library consumers. This puts a unnecessary
maintenance burden upon us because we cannot then add variants in the
middle of others.

Add a hidden variant to the single public non-error enum in `units`.
2025-03-06 11:34:02 +11:00
Tobin C. Harding df500e9b71
primitives: Enable pedantic lints
Enable all the pedantic lints and fix warnings.

Notable items:

- `enum_glob_used` import types with a single character alias
- `doc_markdown`: add a whitelist that includes SegWit and OpenSSL
2025-03-06 10:58:17 +11:00