Commit Graph

4714 Commits

Author SHA1 Message Date
Martin Habovstiak 5a461545c7 Separate private `Script` methods
These will need a private extension trait and to make the review easier,
we separate them.
2024-08-13 13:13:29 +02:00
Martin Habovstiak 27adc09e9f Generalize fn params in `define_extension_trait`
The macro was trying to "parse" the parameters of functions defined in
extension trait. This was not needed and it was causing problems around
the `self` parameter. In this commit we change the macro to just pass
the parameters through.
2024-08-13 13:13:10 +02:00
Tobin C. Harding 2ec901fd63
Move the CompactTarget type to primitives
Potentially the whole `pow` module will move to `primitives` but this
is not possible easily right now. However, we would like to be able to
move the `BlockHash` and `block::Header` types over to `primitives`
and doing so requires the `CompactTarget` to be there.

Move the `CompactTarget` type to `primitives` and re-export it from the
`primitives` crate root.

Note also, we re-export the type publicly from `bitcoin::pow`.
2024-08-13 05:29:22 +10:00
Tobin C. Harding a00bd7cc4d
Introduce CompactTargetExt trait
In preparation for moving the `CompactTarget` type to `primitives`
introduce an extension trait for code that will be left behind in
`bitcoin`.
2024-08-13 05:26:59 +10:00
yancy b8067da934 Parse MSRV minor version number from env 2024-08-12 14:24:31 -05:00
Tobin C. Harding 100ce03643
Run cargo +nightly fmt
No manual changes.
2024-08-13 05:23:35 +10:00
Tobin C. Harding 9c4a629659
Wrap CompactTarget impl block in temporary module
`rustfmt` is unable to format macro calls so instead we wrap the impl
blocks in a module to enable formatting in the next commit.
2024-08-13 05:23:31 +10:00
Tobin C. Harding 578143c09e
Separate CompactTarget impl blocks
In preparation for adding an `CompactTargetExt` trait move the
primitives methods to a separate impl block.

Refactor only, no logic changes.
2024-08-13 05:23:26 +10:00
Tobin C. Harding 22d5646f7b
Stop using CompactTarget inner field
In preparation for moving the `CompactTarget` type to `primitives` stop
using the inner field in code that will stay behind in the
`bitcoin::pow` module.
2024-08-13 05:23:10 +10:00
Tobin C. Harding 244d7dbe6c
Remove generic test impl
In preparation for moving the `CompactTarget` to `primitives` remove the
generic `Into` impl and explicitly implement for just the `From` impls
that the `pow` unit tests use.

Test code only.
2024-08-13 05:23:10 +10:00
Tobin C. Harding 3d85ee3a02
primitives: Fix alloc feature
We have an `alloc` feature but we are unconditionally using
`extern crate alloc`, this is broken - clearly we need to add a `no-std`
build for `primitives` in CI.

Feature gate the `alloc` crate.

While we are at it just pull types in from `alloc` in the `prelude` - I
have no idea why we do not do this in `bitcoin`?
2024-08-13 05:23:10 +10:00
merge-script 5d8f4b2492
Merge rust-bitcoin/rust-bitcoin#3156: impl IndexMut for PushBytes (already has Index) and fix/add docs
7c8601a696 implement IndexMut for PushBytes (Antoni Spaanderman)
71d760b3f2 Add and fix documentation on PushBytes's functions (Antoni Spaanderman)

Pull request description:

ACKs for top commit:
  Kixunil:
    ACK 7c8601a696
  tcharding:
    ACK 7c8601a696
  apoelstra:
    ACK 7c8601a696 successfully ran local tests

Tree-SHA512: a600dcda638cc727c2a31917e76c2e2f6da57b7044d571a345a919f1c471e0d30b82326492716eacfd9b3ce65c4484430bdd0f2d8ebbd220107cd9259a9faaa6
2024-08-12 13:30:01 +00:00
Tobin C. Harding f5a9f58fbf
Merge rust-bitcoin/rust-bitcoin#3159: Automated daily update to rustc (to nightly-2024-08-11)
dcce7b969b Automated update to Github CI to rustc nightly-2024-08-11 (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 dcce7b969b

Tree-SHA512: bb87ffd521111e555692658aca0e8047638850de7d3c3f7191fe548619e48f2707a8a45df5f1c8a55ec0ae822850efa2fcf24b27898ba3054a63862b7d971b82
2024-08-12 20:01:33 +10:00
Update Nightly Rustc Bot dcce7b969b Automated update to Github CI to rustc nightly-2024-08-11 2024-08-12 01:37:45 +00:00
Antoni Spaanderman 7c8601a696
implement IndexMut for PushBytes 2024-08-11 21:57:20 +02:00
Antoni Spaanderman 71d760b3f2
Add and fix documentation on PushBytes's functions 2024-08-11 21:55:15 +02:00
Martin Habovstiak fcc3cb03f0 Support non-doc attrs in extension trait macro
The `define_extension_trait` macro originally didn't support `#[inline]`
or other attributes for simplicity. We still want them so this commit
adds basic support for it. It adds the `doc` attributes to trait
*definition* only and adds all other attributes to the *implementation*
only. This should support `#[inline]` and other attributes. The downside
is it doesn't support adding non-doc attributes to trait *definition*
but I can't think of any relevant ones that we would want and we can
find a solution later if we do.
2024-08-11 14:57:47 +02:00
Tobin C. Harding ca1735f24c Separate POD methods
We want to move `Script` as a plain old data type to `primitives`.

Step 1: Move the methods that will go to `primitives` to a separate impl
block.
2024-08-11 14:40:08 +02:00
merge-script 6dcb6f33fb
Merge rust-bitcoin/rust-bitcoin#3118: Automated weekly update to rustc stable (to 1.80.1)
78f091fb3f Automated update to Github CI to rustc stable-1.80.1 (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 78f091fb3f

Tree-SHA512: c95cd7a0abf4b47474cdf4b52b7f18d672c62e7e2bb397659c12214688bd0ab7a67ec35935ada3b7ba6867ac48a8867c63976d5b87b5efd2b567207d1034ea3d
2024-08-10 08:01:09 +10:00
Update Stable Rustc Bot 78f091fb3f Automated update to Github CI to rustc stable-1.80.1 2024-08-09 00:50:11 +00:00
merge-script c22aea50ec
Merge rust-bitcoin/rust-bitcoin#3144: githooks: remove unnecessary shellcheck disables
a0febf7eed githooks: remove unnecessary shellcheck disables (Jose Storopoli)

Pull request description:

  We don't need to worry about nested quoting in SC2046.

  Thanks to Kixunil in https://github.com/rust-bitcoin/rust-secp256k1/pull/697 for pointing that out.

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

Tree-SHA512: 83cdcc55c7e7922c05f5e78305086595a96745f34ea68ee99ed1c08c97683bd45d3c62e8d8b4bcba6b0572c9c65178ff4e21abd7178e2e8b0c9e42f4b25508fb
2024-08-08 22:45:32 +00:00
merge-script 8ac0e60d84
Merge rust-bitcoin/rust-bitcoin#3140: primitives: Fix alloc feature
3b5bcd0983 primitives: Fix alloc feature (Tobin C. Harding)

Pull request description:

  We have an `alloc` feature but we are unconditionally using `extern crate alloc`, this is broken - clearly we need to add a `no-std` build for `primitives` in CI.

  Feature gate the `alloc` crate.

  While we are at it just pull types in from `alloc` in the `prelude` - I have no idea why we do not do this in `bitcoin`?

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

Tree-SHA512: f042dfb368dc0bde16237764a0dd09def7eeeb59291a5dbb1b5bd5f1dcd49b3adf66c12508c3be4f0ff48b52cd31cbb20edea68d9d55760e99c506ac5b114781
2024-08-08 21:18:09 +00:00
merge-script bee044fbb9
Merge rust-bitcoin/rust-bitcoin#3137: De-sugar `self`
34e8212594 Replace &self with self: &Self (Tobin C. Harding)

Pull request description:

  `foo(&self)` is syntax sugar for `foo(self: &Self)`.

  The `define_extension_trait` is currently large, ugly, and not that expressive. If we use `self: &Self` then the macro is greatly simplified.

  (Also consuming version `self: Self`)

  De-sugar only, no logic changes.

ACKs for top commit:
  apoelstra:
    ACK 34e8212594 successfully ran local tests; lol this looks so much better
  Kixunil:
    ACK 34e8212594

Tree-SHA512: 7ec81bee99ede328d73a661c9e683a774ba14404ceb89ecb06765bedddf04dc9721672775b9ad3a9e3356d510c76681848f24ce4392a59d53216d23e6a27d9ad
2024-08-08 19:55:07 +00:00
merge-script c59b9e3d1a
Merge rust-bitcoin/rust-bitcoin#2929: Introduce `ToU64` trait
579b76b7cb Introduce ToU64 conversion trait (Tobin C. Harding)

Pull request description:

  The idea for this was pulled out of Steven's work in #2133

  We already explicitly do not support 16 bit machines.

  Also, because Rust supports `u182`s one cannot infallibly convert from a `usize` to a `u64`. This is unergonomic and results in a ton of casts.

  We can instead limit our code to running only on machines where `usize` is less that or equal to 64 bits then the infallible conversion is possible.

  Since 128 bit machines are not a thing yet this does not in reality introduce any limitations on the library.

  Add a "private" trait to the `internals` crate to do infallible conversion to a `u64` from `usize`.

  Implement it for all unsigned integers smaller than `u64` as well so we have the option to use the trait instead of `u32::from(foo)`.

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

Tree-SHA512: 2eaddfff995987a346e052386c6dfef3510e4732e674e3a2cfab60ee391b4cce1bf7ba4fb2dfd4926f8203d7251eea2198ccb61f0b40332e624c88fda4fa7f48
2024-08-08 19:05:23 +00:00
merge-script 5a53cfe81f
Merge rust-bitcoin/rust-bitcoin#3138: Manually format
191897f9ea Manually format (Tobin C. Harding)

Pull request description:

  Run `rustfmt` and manually fix the places where comments are moved to the wrong place.

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

Tree-SHA512: f977ff373d1d410012734208c090bfcd8f9dbda414d0b19400acf8f552df481b4a2bc20d77c61538895a6fb66197be13cbdadf74956d67fd4d055b99ba8ab356
2024-08-08 17:36:36 +00:00
merge-script 74a5959479
Merge rust-bitcoin/rust-bitcoin#3139: Remove build cfg for versions less than MSRV
abe7b3f202 Remove build cfg for versions less than MSRV (Tobin C. Harding)

Pull request description:

  Recently we upgraded the MSRV but forgot to remove the Rust version specific `cfg`s.

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

Tree-SHA512: eabfdeb3217a5af8eae69e6f3589044f71b649c23b411525fb0401c2c3866dcd4c64f22ef927765f12584c223186ff850a60e71ee065476d39d5d557c5807e92
2024-08-08 15:24:05 +00:00
Jose Storopoli a0febf7eed
githooks: remove unnecessary shellcheck disables 2024-08-08 07:06:00 -03:00
Tobin C. Harding 579b76b7cb
Introduce ToU64 conversion trait
We already explicitly do not support 16 bit machines.

Also, because Rust supports `u182`s one cannot infallibly convert from a
`usize` to a `u64`. This is unergonomic and results in a ton of casts.

We can instead limit our code to running only on machines where `usize`
is less that or equal to 64 bits then the infallible conversion is
possible.

Since 128 bit machines are not a thing yet this does not in reality
introduce any limitations on the library.

Add a "private" trait to the `internals` crate to do infallible
conversion to a `u64` from `usize`.

Implement it for all unsigned integers smaller than `u64` as well so
we have the option to use the trait instead of `u32::from(foo)`.
2024-08-08 15:32:40 +10:00
merge-script 5dd20ddfb0
Merge rust-bitcoin/rust-bitcoin#3142: Automated daily update to rustc (to nightly-2024-08-07)
108047ce96 Automated update to Github CI to rustc nightly-2024-08-07 (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 108047ce96

Tree-SHA512: 70114c69b98ff4d769af3ca37097bbbffb6f0b4e429ba7fe4d8a13786e3a70ec5e68c92cd4c05781e5ad8a3096d2151befbb312d06f74cf5ea74d104b4ebf313
2024-08-08 13:34:33 +10:00
Tobin C. Harding abe7b3f202
Remove build cfg for versions less than MSRV
Recently we upgraded the MSRV but forgot to remove the Rust version
specific `cfg`s.
2024-08-08 12:03:06 +10:00
Update Nightly Rustc Bot 108047ce96 Automated update to Github CI to rustc nightly-2024-08-07 2024-08-08 01:34:58 +00:00
Tobin C. Harding 3b5bcd0983
primitives: Fix alloc feature
We have an `alloc` feature but we are unconditionally using
`extern crate alloc`, this is broken - clearly we need to add a `no-std`
build for `primitives` in CI.

Feature gate the `alloc` crate.

While we are at it just pull types in from `alloc` in the `prelude` - I
have no idea why we do not do this in `bitcoin`?
2024-08-08 10:57:38 +10: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
Tobin C. Harding 34e8212594
Replace &self with self: &Self
`foo(&self)` is syntax sugar for `foo(self: &Self)`.

The `define_extension_trait` is currently large, ugly, and not that
expressive. If we use `self: &Self` then the macro is greatly
simplified.

De-sugar only, no logic changes.
2024-08-08 09:17:47 +10:00
merge-script 000661360e
Merge rust-bitcoin/rust-bitcoin#3120: base58: Remove or document casts
4b66a479b0 base58: Use u32 instead of usize (Tobin C. Harding)
3f8cf1b335 base58: Use from and document cast (Tobin C. Harding)
121b435a9b base58: Use from to cast u8 (Tobin C. Harding)

Pull request description:

  Done as part of #2941.

  Remove or document casts in the `base58` crate. Two separate patches because to assist review, with the hope that reviewers are able to just read the diff without going to the crate's code.

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

Tree-SHA512: e120e844af6e41eb29cc1538a9bad21a4e3ea8b27b90b7221a09eb5112093c72248ba3d72cd91d950ea8420032cb2e66790c24f9e15af7f2c0554f6cac3854c0
2024-08-07 02:12:24 +00:00
Tobin C. Harding 4b66a479b0
base58: Use u32 instead of usize
The `carry` variable is used as a value not as an array index so we
should use a `u32` - this is inline with other usage in the crate.
2024-08-07 05:33:31 +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
merge-script d214dc7b09
Merge rust-bitcoin/rust-bitcoin#3077: hashes: Add a new `hash_reader` function
0a045d87ea hashes: Add a new hash_reader function (Tobin C. Harding)

Pull request description:

  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

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

Tree-SHA512: 225f1d72f7a6119313d36422a3f7dc026ddcd27de9c8712c5734ea6056bb21e4857814761dbf2383a7a87fa82573ffc2097f67d08a0785a93e691c1745d0db8c
2024-08-06 15:26:45 +00:00
merge-script 2dac88b416
Merge rust-bitcoin/rust-bitcoin#3126: Remove catch all pattern
6836de9ee6 Remove catch all pattern (Tobin C. Harding)

Pull request description:

  The `PushBytes` type enforces len is less than 0x100000000 so we do not need to panic in a catch all pattern after explicitly matching against less than 0x100000000.

  Refactor only because of the invariant on `PushBytes` - no logic changes.

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

Tree-SHA512: a7cdb31683a8c00eecbdd0879886bec48133f9029f899b5279f1f5294ef40320592db196bfcafdeef4507636fc785a7ab87879b25b6d1b4905ae573b545f1ff4
2024-08-06 13:06:20 +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
Tobin C. Harding 6836de9ee6
Remove catch all pattern
The `PushBytes` type enforces len is less than 0x100000000 so we do
not need to panic in a catch all pattern after explicitly matching
against less than 0x100000000.

Refactor only because of the invariant on `PushBytes` - no logic
changes.
2024-08-06 10:23:47 +10:00
merge-script 5cca2f271d
Merge rust-bitcoin/rust-bitcoin#3122: Introduce helper function name policy
84df3438ca Fix markdown list items (Tobin C. Harding)
0a45c68cf8 Introduce helper function name policy (Tobin C. Harding)

Pull request description:

  As much as it hurts the C hacker inside me we have settled on using `_internal` to mark private function names that clash with a public function of the same name.

  Introduce a policy section and rename one instance, I did not grep the codebase looking for other violations.

  This came up because I had to look at what `_inner` implied when reading the function name somewhere else.

ACKs for top commit:
  storopoli:
    ACK 84df3438ca
  apoelstra:
    ACK 84df3438ca successfully ran local tests; sgtm. should probably update rust-miniscript which uses a `real_` prefix

Tree-SHA512: aece73fac54c4f34bdba72c08721586eb6d76dc9191442bbca43301f2af3d3b3e3217383f1ad20752fa7654e7f5d09927d1a4b72602aa064e76d550b5d6e81bd
2024-08-05 23:11:05 +00:00
merge-script 865fdf78dd
Merge rust-bitcoin/rust-bitcoin#3121: bip158: Improve casting
ea9f6e8f97 Cast after calling min (Tobin C. Harding)
342fe18ad0 Use From in map_to_range (Tobin C. Harding)
d9331794f1 bip158: Fix typo (Tobin C. Harding)

Pull request description:

  Patch 1 is a typo fix, other two are cast improvements. Done as part of #2941.

  Leaves a cast to `u64` in there, will be removed once we get to the `ToU64` stuff. One other remaining cast in the module is obviously bit twiddling.

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

Tree-SHA512: 92fa6225621f6c1eea4864472a5b9c60cb244e62cdfbee4074067da43b8f2bd4e3ed1a249ba40b994cad824e97585f3140747b03677ef1596c80be3007c94a7c
2024-08-05 20:58:29 +00:00
Tobin C. Harding 3f8cf1b335
base58: Use from and document cast
Done in an effort to reduce the cognitive load of reading the loop.

The base68 decode and encode algorithm uses a `u32` intentionally for
multiplication and a cast to `u8` intentionally when carrying.

Use `From` where possible and document the cast.
2024-08-06 05:49:30 +10:00
Tobin C. Harding c28d5d5964
Merge rust-bitcoin/rust-bitcoin#3125: Automated daily update to rustc (to nightly-2024-08-04)
a90c8b1929 Automated update to Github CI to rustc nightly-2024-08-04 (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 a90c8b1929

Tree-SHA512: 272f60a156cdfa21799af43282655804e4a5723c55976bf742aa0e1fbe1bfc4127225ecc9b965a69a0efb54a21b8c32cdbc48d4ba802181685d2fdb46c7c2f3a
2024-08-06 05:41:47 +10:00
Tobin C. Harding 84df3438ca
Fix markdown list items
Fix list items to use capital letters because the list items are
sentences (have trailing full stop already).

Also, use a long single line because it is [subjectively] easier to read
the list.
2024-08-06 04:18:23 +10:00
Tobin C. Harding 0a45c68cf8
Introduce helper function name policy
As much as it hurts the C hacker inside me we have settled on using
`_internal` to mark private function names that clash with a public
function of the same name.

Introduce a policy section and rename one instance, I did not grep the
codebase looking for other violations.

This came up because I had to look at what `_inner` implied when reading
the function name somewhere else.
2024-08-06 04:17:28 +10:00