Commit Graph

5931 Commits

Author SHA1 Message Date
Tobin C. Harding 43ae9d7516
primitives: Hide script error internals
As part of the 1.0 effort and forward maintainability hide the internals
of the two error types in the `script` module. Add getters to get at the
invalid size.
2025-02-26 11:05:07 +11:00
Tobin C. Harding 2d8227f091
Hide relative locktime error internals
As part of the 1.0 effort and forward maintainability hide the internals
of the two error types in the `relative` locktime module. Doing so
allows us to remove the `non_exhaustive` attribute. Add getters to get
at the error innards.
2025-02-26 11:04:58 +11:00
merge-script aebda6e516
Merge rust-bitcoin/rust-bitcoin#4104: Automated daily update to rustc (to nightly-2025-02-21)
ca8bd531d6 Automated update to Github CI to rustc nightly-2025-02-21 (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 ca8bd531d6

Tree-SHA512: 39f9ee068cdafde4d50cebfbf9fd3b96377d12e0c98a059e835f61bfbe57f77f4e84a629082dc38aa40aabb190e66dbd95ab4ba6423f816796534fe28f89e6f8
2025-02-26 10:50:36 +11:00
Tobin C. Harding 814685e551
Macroise the NumOpResult tests
Macroise the tests improving coverage along the way.

Includes a TODO to remind us to do `Neg` more fully.
2025-02-25 20:46:01 +00:00
Andrew Poelstra a44a9d31f6
Add a few impls to the result macro
Add a few missing impls to the `impl_op_for_references` macro.

Includes a minor whitespace change so that traits are grouped together.
2025-02-25 20:45:56 +00:00
Andrew Poelstra 353c23fa01
units: pull generic op impls on NumOpResult into macro 2025-02-25 20:44:30 +00:00
Andrew Poelstra 21ac5aefe0
units: extend op reference macro to handle generics and where clauses
This is a bit ugly and requires that we put our where-clauses in
parentheses because the macro_rules parser sucks, but it allows us to
move the blanket-impls on NumOpResult into the macro.

This commit moves one instance and updates the macro; the next commits
will change the rest.
2025-02-25 20:31:45 +00:00
Andrew Poelstra ad9564895b
units: replace a gazillion more macro calls with new macro
Looks like a large diff but if you run

    git show --color-moved-ws=allow-indentation-change

you will see that it's 100% moves (though moves of code into the
reference macro). May be easier to just look at src/amount/result.rs
after this; it's pretty short now.
2025-02-25 20:31:45 +00:00
Andrew Poelstra 0dc7f6cebd
units: rearrange a bit of code to prep the next commit
The next commit changes a lot of code, but almost entirely by moving and
indenting it. We try to do the moves here ahead of time, so it the diff
for the next commit will be just deletions and indentations.
2025-02-25 20:31:45 +00:00
Andrew Poelstra a358e79a85
units: allow multiple invocations in impl_op_for_references macro
This is not too complicated a change to support and it will reduce the
noise in the following commits a fair bit.
2025-02-25 20:31:45 +00:00
Andrew Poelstra 2da332e04a
units: introduce impl_op_for_references and use it in three places
This macro can generally handle a lot of different cases where we
implement "the same trait but on references". We introduce it here and
use it in two places. We will use it in many more, but I wanted to make
the diff small on this commit, which introduces the actual macro code
and might take a bit of reading to understand.

You may want to use --color-moved-ws=allow-indentation-change to review
this, and the next commit.

The next set of changes will mechanically delete other macros that are
made redundant by this.
2025-02-25 20:31:45 +00:00
Tobin C. Harding c90559de8e
Derive Copy for NumOpResult
The `NumOpResult` type is way more ergonomic to use if it derives
`Copy`. This restricts the `NumOpResult` to being `Copy` as well.

This does restrict what we can include in the error type in the future.

Derive Copy for `NumOpResult` and `NumOpResult`.
2025-02-25 20:31:45 +00:00
merge-script e80ce4a89c
Merge rust-bitcoin/rust-bitcoin#4113: Fix `is_invalid_use_of_sighash_single()` incompatibility with Bitcoin Core
7ab2f5be40 Add test for sighash_single_bug incompatility fix (Liu-Cheng Xu)
5d38073afb Fix `is_invalid_use_of_sighash_single()` incompatibility with Bitcoin Core (Liu-Cheng Xu)

Pull request description:

  Close https://github.com/rust-bitcoin/rust-bitcoin/issues/4112

ACKs for top commit:
  tcharding:
    ACK 7ab2f5be40
  Kixunil:
    ACK 7ab2f5be40
  apoelstra:
    ACK 7ab2f5be4076c22e60eefcaa943444808eae3e3f; successfully ran local tests

Tree-SHA512: d47143d188653d3e845951e64e9b410fdbdac8e51906f33532b8d71519f0bd1454a46135dfdd6073a6d1ced9854dc3e13f3c35de60b7fdd45c22ef37f9a0fc75
2025-02-25 17:36:12 +00:00
costcould 083a33af85 chore: remove redundant words in CONTRIBUTING.md
Signed-off-by: costcould <fliter@myyahoo.com>
2025-02-26 00:13:07 +08:00
leopardracer 83bd83385e
Update sighash.rs 2025-02-25 12:10:51 +02:00
leopardracer fc4ea87429
Update transaction.rs 2025-02-25 12:10:23 +02:00
leopardracer 0e70e85a1a
Update key.rs 2025-02-25 12:03:35 +02:00
leopardracer f7c28ab44d
Update input_string.rs 2025-02-25 12:03:03 +02:00
Tobin C. Harding 2d6ee57d9c
Grab missing changelog
We release `v0.32.4` and `v0.32.5` already but forgot to merge the
changelog entries back into master.

Grab the missing changelog entries from the `0.32.x` release branch.
2025-02-25 19:35:43 +11:00
Liu-Cheng Xu 7ab2f5be40 Add test for sighash_single_bug incompatility fix 2025-02-25 10:56:29 +08:00
Liu-Cheng Xu 5d38073afb Fix `is_invalid_use_of_sighash_single()` incompatibility with Bitcoin Core 2025-02-25 10:54:47 +08:00
merge-script 6c286e32d4
Merge rust-bitcoin/rust-bitcoin#4089: Script improvements
5680b4e870 Refer to `Script{Buf}` as `Self` where relevant (Martin Habovstiak)
ce55dd5b70 Make `ScriptHash` & `WScriptHash` obey sanity rule (Martin Habovstiak)
9ec9adc71d Add a note about Electrum's script hashes (Martin Habovstiak)
82f553aada Expose `ScriptBuf`'s `capacity` (Martin Habovstiak)
6b9d439dc1 Remove stale FIXME comments (Martin Habovstiak)
0567e6fe1d Put `#[inline]` on most `Script{Buf}` methods (Martin Habovstiak)
b7e2af1b6b Implement `Arbitrary` for `&'a Script` (Martin Habovstiak)
bca2864084 Move `Deref{Mut}` from common module to `owned` (Martin Habovstiak)
3b15e900f0 Add `const` to some `Script` methods (Martin Habovstiak)
277223da6a Make `Script` and `ScriptBuf` obey sanity rules (Martin Habovstiak)

Pull request description:

  This implements various improvements related to `Script`. Please refer to the individual commits for details.

  This is a part of #4059

ACKs for top commit:
  tcharding:
    ACK 5680b4e870
  apoelstra:
    ACK 5680b4e870ba3b7340432256c24d37d2b6ead15a; successfully ran local tests

Tree-SHA512: 5daa8bf6c0b439a579d31d23944077e4a7fa89e14052003d2b81c745f225147f8f6f693d068e0567830027cefea7dda2516596da632bc817199352fa29af0a9b
2025-02-24 21:50:48 +00:00
Martin Habovstiak a8168c3f81 Add `taproot_leaf_script` methood to `Witness`
We already have `tapscript` method on `Witness` which is broken because
it doesn't check that the leaf script is a tapscript, however that
behavior might have been intended by some consumers who want to inspect
the script independent of the version. To resolve the confusion, we're
going to add a new method that returns both the leaf script and, to
avoid forgetting version check, also the leaf version.

This doesn't touch the `tapscript` method yet to make backporting of
this commit easier. It's also worth noting that leaf script is often
used together with version. To make passing them around easier it'd be
helpful to use a separate type. Thus this also adds a public POD type
containing the script and the version. In anticipation of if being
usable in different APIs it's also generic over the script type.
Similarly to the `tapscript` method, this also only adds the type and
doesn't change other functions to use it yet. Only the newly added
`taproot_leaf_script` method uses it now.

This is a part of #4073
2025-02-24 18:33:55 +01:00
Martin Habovstiak 59f21a291f Add a test case checking `taproot_control_block`
The previous commit fixed a bug when `taproot_control_block` returned
`Some` on key-spends. This adds a test case for it which succeeds when
applied after the previous commit and fails if applied before it.
2025-02-24 18:33:31 +01:00
Martin Habovstiak e810ecff7c Fix key/script spend detection in `Witness`
The `taproot_control_block` did not properly detect whether it deals
with script spend or key spend. As a result, if key spend with annex was
used it'd return the first element (the signature) as if it was a
control block.

Further, the conditions identifying which kind of spend it was were
repeated multiple times but behaved subtly differently making only
`taproot_control_block` buggy but the other places confusing.

To resolve these issues this change adds a `P2TrSpend` enum that
represents a parsed witness and has a single method doing all the
parsing. The other methods can then be trivially implemented by matching
on that type. This way only one place needs to be verified and the
parsing code is more readable since it uses one big `match` to handle
all possibilities.

The downside of this is a potential perf impact if the parsing code
doesn't get inlined since the common parsing code has to shuffle around
data that the caller is not intersted in. I don't think this will be a
problem but if it will I suppose it will be solvable (e.g. by using
`#[inline(always)]`).

The enum also looks somewhat nice and perhaps downstream consumers could
make use of it. This change does not expose it yet but is written such
that after exposing it the API would be (mostly) idiomatic.

Closes #4097
2025-02-24 18:33:13 +01:00
merge-script 5a0d5d7b79
Merge rust-bitcoin/rust-bitcoin#4105: Automated nightly rustfmt (2025-02-23)
0596867048 2025-02-23 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

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

Tree-SHA512: 84a3b7c79939c1bcd0c3e30b117e0f681923a8c0825ee8852845abc0ec0677f3a6033c6133eba7742b9bb4e67d635614be5cd2d983d07c5866396142a8d8c3bc
2025-02-24 17:31:28 +00:00
merge-script 81deddd0a9
Merge rust-bitcoin/rust-bitcoin#4099: Make transaction::Version field private
cb270eae8e Make transaction::Version field private (jrakibi)
6c69b66b0d Use Version constant (jrakibi)

Pull request description:

  This commit addresses #4041 by making the `transaction::Version` field private.

  This forces people to either use the associated constants (`Version::ONE/TWO/THREE`) or the `non_standard`/`from_consensus` methods for any other transaction version.

  This aligns with our approach to `block::Version`

ACKs for top commit:
  tcharding:
    ACK cb270eae8e
  Kixunil:
    ACK cb270eae8e

Tree-SHA512: dcf5b50dfeda04e56fec350acd735dcb7099989b552afce4261d559a8a846c0eb3705369ad635ef9bbbfb2373d203a2c3641178925de6685426aa91245db9a8c
2025-02-24 16:37:16 +00:00
merge-script 76a511dfb2
Merge rust-bitcoin/rust-bitcoin#4107: sighash: Document `witness_script` in p2wsh
82a8aefc53 sighash: Document witness_script in p2wsh (Jose Storopoli)

Pull request description:

  A note suggestion for the `p2wsh_signature_hash` in the same vein as the one in the "sister" function `p2wpkh_signature_hash`: 33f5e5a6ac/bitcoin/src/crypto/sighash.rs (L863-L867)

  Any better wordings are welcomed. Not married to my sentences.

ACKs for top commit:
  luisschwab:
    ACK 82a8aefc53
  Kixunil:
    ACK 82a8aefc53

Tree-SHA512: 1ba5f9b7c287b5c1494ba9ab03c146ff99fa8be791397b61ff4609a8dbab5be5c1376d9f401b56ec4a94196eec5a96ba140965a5a8178938a2c293994e71dbfe
2025-02-24 15:28:18 +00:00
Jose Storopoli 82a8aefc53
sighash: Document witness_script in p2wsh 2025-02-24 08:15:42 -03:00
jrakibi cb270eae8e Make transaction::Version field private
This commit addresses #4041 by making the transaction::Version field private

Changes:
- Make the `Version` field private with `pub(crate)`
- Rename `non_standard` to `maybe_non_standard` for clarity since it accepts both standard and non-standard versions
- Add `#[inline]` attributes to small, frequently used methods:
  - `as_u32`
  - `maybe_non_standard`

Users now must use either:
- Constants (`Version::ONE/TWO/THREE`) for standard versions
- `maybe_non_standard` method for any version (standard or non-standard)
2025-02-24 06:54:21 +07:00
merge-script 0157dc21c3
Merge rust-bitcoin/rust-bitcoin#4088: Abstract out "debug-print hex fields" using WrapDebug
4eb3177fab Abstract out "debug-print hex fields" using WrapDebug (Erick Cestari)

Pull request description:

  This pr introduces `WrapDebug` in `internals` and updates `Witness` debug implementation to use it. The previous `DebugElements` struct has been removed in favor of an ad-hoc closure inside `WrapDebug`, which formats witness elements as a debug list of hex-encoded values.

  By abstracting out the "debug-print hex fields" pattern, we reduce code duplication and improve maintainability.

  Closes #4074

ACKs for top commit:
  Kixunil:
    ACK 4eb3177fab
  apoelstra:
    ACK 4eb3177fab0655457a8a6d1556fd7eba54459250; successfully ran local tests

Tree-SHA512: 533ab10a8ca2be891c0f9e374bdb990cff941e332414a345ba6573d03cca19595a840d818eb24947c8b8bf88512532eed395adbc0dd3a7bb66ffb4cc641cbf21
2025-02-23 17:45:38 +00:00
merge-script 38e9bf841e
Merge rust-bitcoin/rust-bitcoin#4084: documentation: update instructions for building lock files
948647a9dd update documentation for changing lockfiles; change script to support mac nanaive cp function (jeremiah)

Pull request description:

  1. Updates `CONTRIBUTING.md` with latest instructions on using `just` to create new lock files. See discussion [here](https://github.com/rust-bitcoin/rust-bitcoin/pull/4045/files).
  2. For the command `cp`,  `--force` is not an option on the Mac. Changed to `-f` which as far as I can tell is equivalent.

  ```
  ➜  rust-bitcoin git:(jr_update_contrib) ✗ cp --force
  cp: illegal option -- -
  ```

ACKs for top commit:
  tcharding:
    ACK 948647a9dd
  Kixunil:
    ACK 948647a9dd
  apoelstra:
    ACK 948647a9dd95bedbb8c34f0b9548e859bfc723b1; successfully ran local tests

Tree-SHA512: a9486ec323b15a9cfa78cd70332e153881537ec8fd26e6e23f8b61ed2272251271299723e9aa390542044588357ec072eb1559cd5e32cb707757f58361a6fb05
2025-02-23 14:39:44 +00:00
Fmt Bot 0596867048 2025-02-23 automated rustfmt nightly 2025-02-23 01:21:34 +00:00
jrakibi 6c69b66b0d Use Version constant
Replace Version(2) with predefined Version::TWO constant
2025-02-23 04:19:48 +07:00
Update Nightly Rustc Bot ca8bd531d6 Automated update to Github CI to rustc nightly-2025-02-21 2025-02-22 01:38:55 +00:00
merge-script d1171381bc
Merge rust-bitcoin/rust-bitcoin#4095: Automated weekly update to rustc stable (to 1.85.0)
2349e5be73 Automated update to Github CI to rustc stable-1.85.0 (Update Stable Rustc Bot)

Pull request description:

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

ACKs for top commit:
  tcharding:
    ACK 2349e5be73

Tree-SHA512: 49d78997ea252b1a98844fe6ab7a98b798e464c6a99f7346e13611c567c7bb8abea618de00dd92fb945e84dc01d8a44d10d659ca3ef175460a534288e35e6b0e
2025-02-21 15:46:16 +11:00
Update Stable Rustc Bot 2349e5be73 Automated update to Github CI to rustc stable-1.85.0 2025-02-21 00:56:08 +00:00
Martin Habovstiak 5680b4e870 Refer to `Script{Buf}` as `Self` where relevant
Using `Self` instead of specific type name can make some refactorings
easier.
2025-02-20 19:30:07 +01:00
Martin Habovstiak ce55dd5b70 Make `ScriptHash` & `WScriptHash` obey sanity rule
These were re-implementing hashing after the check rather than calling
the `_unchecked` method, so this replaces the manual implementation with
the method.
2025-02-20 18:10:53 +01:00
Martin Habovstiak 9ec9adc71d Add a note about Electrum's script hashes
The Electrum protocol uses hashes of `script_pubkey` that might look
similar to the ones we have in the crate and could be confused. This
change notes that to hopefully avoid the confusion.

Resolves https://github.com/rust-bitcoin/rust-bitcoin/discussions/3997
2025-02-20 18:01:46 +01:00
Martin Habovstiak 82f553aada Expose `ScriptBuf`'s `capacity`
There are already several methods referring to capacity but none to
retrieve it. Those methods also promise certain behavior that mandates
having *a* capacity field inside the struct, so no changes in layout
will ever remove it. Thus it's OK to expose the field.

Aside from exposing the field, this also fixes up the tests to obey the
sanity rules.
2025-02-20 16:30:31 +01:00
Martin Habovstiak 6b9d439dc1 Remove stale FIXME comments
These comments said that the modules should be private but they already
are. Also, the internals of the newtypes became private a few commits
ago.
2025-02-20 16:23:26 +01:00
Martin Habovstiak 0567e6fe1d Put `#[inline]` on most `Script{Buf}` methods
These methods are either newtype casts that should compile to no-ops or
directly calling into some other function with at most some pointer
adjustments. As such making them `#[inline]` is definitely benefitial.
There are also methods that check length and then call some other
function. These are also worth inlining since the length could be known
at compile time and the check could be eliminated.
2025-02-20 16:18:21 +01:00
Martin Habovstiak b7e2af1b6b Implement `Arbitrary` for `&'a Script`
The `Arbitrary` trait allows zero-copy implementations for borrowed
types, so this adds the implementation for borrowed `Script`.
2025-02-20 16:02:22 +01:00
Martin Habovstiak bca2864084 Move `Deref{Mut}` from common module to `owned`
We have several trait implementations for `Script` and `ScriptBuf` in a
common module so that it's easy to verify that they are same but `Deref`
and `DerefMut` should *not* be implemented for `Script` so having them
in the common module is not helpful. This moves them to the appropriate
`Owned` module.
2025-02-20 16:00:10 +01:00
Martin Habovstiak 3b15e900f0 Add `const` to some `Script` methods
We're confident these methods will never perform syscalls, so it's fine
to add `const` to them.
2025-02-20 15:49:46 +01:00
Martin Habovstiak 277223da6a Make `Script` and `ScriptBuf` obey sanity rules
The newtype sanity rules (a name I came up with):
* Newtypes should have at most one constructor that directly references
  the inner field.
* Newtypes should have at most three accessor methods that directly
  reference the ineer field: one for owned access, the second for
  borrowed and the third for mutably borrowed.
* All other methods should use the methods above to perform operations
  on the newtype and not directly access the fields.

This commit makes `Script` and `ScriptBuf` obey these except for
`reserve` and `reserve_exact` since we don't have `as_mut_vec` method.
As a side effect it also adds `const` to `ScriptBuf::from_bytes`.
2025-02-20 15:35:17 +01:00
Erick Cestari 4eb3177fab Abstract out "debug-print hex fields" using WrapDebug
This commit introduces `WrapDebug` in `internals` and updates `Witness`
debug implementation to use it. The previous `DebugElements` struct has
been removed in favor of an ad-hoc closure inside `WrapDebug`, which
formats witness elements as a debug list of hex-encoded values.

By abstracting out the "debug-print hex fields" pattern, we reduce
code duplication and improve maintainability.
2025-02-20 10:56:12 -03:00
jeremiah 948647a9dd update documentation for changing lockfiles; change script to support mac nanaive cp function 2025-02-19 18:26:12 -05:00
merge-script e487618503
Merge rust-bitcoin/rust-bitcoin#4071: chacha20: add benchmarks
e41653d188 add chacha20 benchmarks (jeremiah)

Pull request description:

  Add benchmarks for chacha20. These could be useful to understand future improvements.

  I copied the same 10, 1k and 64k sizes as used in the sha256 benchmark. Some of the lines changed here, such as in `Config.toml`, are a little mysterious to me as I am new to rust, so please check.

  ```
  ➜  chacha20_poly1305 git:(jr_chachabench) RUSTFLAGS='--cfg=bench' cargo +nightly bench
  ...
  test benches::chacha20_10  ... bench:         188.67 ns/iter (+/- 2.70) = 53 MB/s
  test benches::chacha20_1k  ... bench:       2,885.83 ns/iter (+/- 90.06) = 354 MB/s
  test benches::chacha20_64k ... bench:     186,493.88 ns/iter (+/- 5,074.53) = 351 MB/s
  ```

ACKs for top commit:
  apoelstra:
    ACK e41653d18838e0d6e157db475b7b034b2456c924; successfully ran local tests; nice!
  Kixunil:
    ACK e41653d188

Tree-SHA512: fa37a4b6842ab78edc32726b22410d9769c1f03e1bc3488517e1e59e17388cabb63e58eb6ea6f112cb27ff1015296f356275ddee12d261c848050e98e3c6eed6
2025-02-19 19:35:32 +00:00