Commit Graph

74 Commits

Author SHA1 Message Date
Martin Habovstiak 06f1f027ab Make `hash_newtype` evocative of the output
The API guidelines say macro input should be evocative of the output.
`hash_newtype` didn't have this property.

This change makes it look exactly like the resulting struct, `$len`
parameter was removed since it's not needed, reversing is controlled
using an attribute. The macro is also better documented and ready to be
extended in the future.

The tagged SHA256 newtype is not yet modified because it has a more
complicated input parameters.

Closes #1648
2023-02-22 14:35:50 +01:00
Martin Habovstiak b018f3e90b Remove the `$len` argument from `hash_newtype`
Now that the `$len` argument is no longer used, remove it completely.
2023-02-22 14:35:14 +01:00
Tobin C. Harding be7b3754a9
Rename schnorr module to taproot
"schnorr" is a dirty word; the current `schnorr` module defines a
`Signature` that includes a sighash type, this sighash type is a bitcoin
specific construct related to taproot. Therefore the `Signature` is
better named `taproot::Signature`. Note also that the usage of `schnorr`
in `secp256k1` is probably justified because the
`secp256::schnorr::Signature` is just doing the crypto.

While we are at it, update docs and error messages to use "taproot"
instead of "schnorr". Also change function names and identifiers that
use "schnorr".
2023-02-20 12:58:09 +11:00
Tobin C. Harding 9f39e872bc
Rename SchnorrSighashType to TapSighashType
As we did for `SchnorrSighash`, rename the `SchnorrSighashType` to
`TapSighashType`.
2023-02-20 12:58:09 +11:00
Tobin C. Harding f5c26693c5
Make match arms more terse
Add function local import statements so we can make the match arms more
terse.

Refactor only, no logic changes.
2023-02-20 12:58:09 +11:00
Tobin C. Harding 40c246743b
Split Sighash into LegacySighash and SegwitV0Sighash
Currently we have `TapSighash` that is used for taproot sighashes but
for non-taproot sighashes we use `hash_types::Sighash`. We can improve
the API by creating a `LegacySighash`, and `SegwitV0Sighash`.

Copy the original `Sighash` macro calls to create the two new types in
the `sighash` module.

While we are at it, put the `TapSighash` and `TapSighashTag` into the
`sighash` module also.
2023-02-20 12:58:09 +11:00
Tobin C. Harding 98130f49f1
Rename TapSighashHash to TapSighash
The TapSighash is the taproot sighash, no need to append `Hash` to the
identifier.
2023-02-20 12:58:08 +11:00
Tobin C. Harding 7e4da3c0ab
Move taproot keys to the keys module
We have a keys module, taproot keys should live in there.
2023-02-20 12:58:05 +11:00
Tobin C. Harding c5fe315a93
Move sighash to crypto module
There is never any use for the `sighash` module unless one is signing,
which requires the `crypto` module. The `sighash` module should
therefore live in the `crypto` module. This is not an API breaking
change because we reexport it at the crate root.
2023-02-20 12:56:34 +11:00
Martin Habovstiak bcd95fa036 Add a newtype for script-pushable slice
The code previously contained undocumented panic when trying to push
slice. This change solves it by adding a newtype that guarantees limited
length.
2023-02-18 16:35:25 +01:00
Martin Habovstiak 8fedbcbf13 Add `ecdsa::SerializedSignature`
`Signature` only supported serialization into `Vec` which required a
heap allocation as well as prevented statically proving maximum length.

Adding a specialized type that holds a byte array and size solves this.
The solution is very similar to `secp256k1::ecdsa::SerializedSignature`.
The difference is that serialized signature in this crate contains
sighash bytes flag while in `secp256k1` it doesn't.
2023-02-18 12:31:07 +01:00
Tobin C. Harding 277e8e96bd
Add KeyPair import to rustdoc example
Recently, and bizarrely, a PR merged that broke `cargo test --doc`.

Add an import for `KeyPair` to the `schnorr` rustdoc example.
2023-02-02 12:22:47 +11:00
Tobin C. Harding facaefc49c Add conversions for TweakedKeyPair -> TweakedPublicKey
It is trivially possible to get `TweakedPublicKey` from a
`TweakedKeyPair`, add conversion methods for doing so.
2023-01-31 17:21:56 +11:00
Tobin C. Harding 2407f241e4 Remove sep256k1 path from Parity
The `Parity` type is unambiguous, no need to use the `secp256k1` path
all the time just import the type.

Refactor only, no logic changes.
2023-01-31 17:21:56 +11:00
Tobin C. Harding 877f9af364 Add new hex parse error variant
Recently we used an error type that holds only one expected hex string
length when parsing but for `PublicKey`s we have two (66 and 130). Add a
new error variant to express the error. Requires adding a variant to
`bip32` for the same thing.

Fix: #1281
2023-01-28 07:53:47 +11:00
Tobin C. Harding 1d3d5a9c5b
Take Into<secp256k1::PublicKey> in PublicKey constructors
We can make the API more ergonomic by taking a generic argument that
implements `Into<secp256k1::PublicKey>` in the `bitcoin::PublicKey`
constructors.

The only thing than this is useful for is passing in `KeyPair` and the
`From` implementation already exists. Add a unit test to verify.

Fix: #1453
2023-01-23 14:46:53 +11:00
Tobin C. Harding b13a76407b
keys: Clean up test imports
Clean up the test import statements in the `keys` module. Done in
preparation for adding a unit test.
2023-01-23 14:46:53 +11:00
Andrew Poelstra 649bf023af
Merge rust-bitcoin/rust-bitcoin#1537: Export the DisplayHex trait from within prelude
70fe07f1ce Export the DisplayHex trait from within prelude (Tobin C. Harding)

Pull request description:

  We use `internals::hex::display::DisplayHex` in many places, we can improve ergonomics of the `internals` crate by re-exporting it from the `prelude` module.

ACKs for top commit:
  Kixunil:
    ACK 70fe07f1ce
  apoelstra:
    ACK 70fe07f1ce

Tree-SHA512: 96a89135cb0b829b7b5926a3b344f78e178b5b48e772a69da5133fab6d2e14e7b7bbaa56b7a417a5c1a64337546a1c7bac32307d3a1f27aa199ed61f590902bf
2023-01-16 15:21:33 +00:00
Tobin C. Harding 70fe07f1ce
Export the DisplayHex trait from within prelude
We use `internals::hex::display::DisplayHex` in many places, we can
improve ergonomics of the `internals` crate by re-exporting it from the
`prelude` module.
2023-01-10 09:56:41 +11:00
sanket1729 5ff2635585 Rename TapBranchHash -> TapNodeHash 2023-01-09 12:42:46 -08:00
Martin Habovstiak 1b0988833a Remove `ToHex`
The `ToHex` trait was replaced by either simple `Display`/`LowerHex`
where appropriate or `DisplayHex` from `bitcoin_internals` which is
faster.

This change replaces the usages and removes the trait.
2023-01-07 19:50:03 +01:00
Tobin C. Harding 2780e6cdaa Move base58 module to crate root
In preparation for removing the `util` module move the `base58` module
to the crate root. This is likely not the final resting place for this
module but it is a step in the right direction.

Includes addition of rustfmt attribute to skip formatting the digits
array. No other changes to the `base58` module.
2022-12-01 09:56:33 +11:00
Tobin C. Harding db5c8fe61c Move the taproot module to crate root
We are trying to flatten the `util` module. The `taproot` module can
live in the crate root. If/when we create a `crypto` module/crate we may
wish to pull some stuff out of this module but for now moving it gets us
closer to removing `util` without making the directory structure any
worse.

Includes adding rustfmt attributes to skip formatting of macros.
2022-11-30 12:03:39 +11:00
Tobin C. Harding 2df51dae15 Create crypto module
Done as part of flattening util.

Currently in `util` module we have a bunch of modules that provide
cryptography related functionality.

Create a `crypto` module and move into it the following:

- ecdsa
- schnorr
- key

To improve uniformity and ergonomics, do the following re-names while we
are at it:

- EcdsaSig -> ecdsa::Signature
- SchnorrSig -> schnorr::Signature
- EcdsaSigError -> ecdsa::Error
- SchnorrSigError -> schnorr::Error
- InvalidSchnorrSigSize -> InvalidSignatureSize  (this is an error enum variant)
2022-11-22 14:09:33 +11:00