Commit Graph

628 Commits

Author SHA1 Message Date
KaFai Choi e80de8b1ee
add nano and pico BTC to Donomination enum 2022-01-11 19:23:45 +07:00
KaFai Choi 9835736ef5
wrap u8 and LeafVersion in backticks and square bracket in doc 2022-01-10 15:09:44 +07:00
sanket1729 d82afc6ef5
Merge rust-bitcoin/rust-bitcoin#761: Taproot trivial post-merge fixups
7f06e91a93 LowerHex and UpperHex implementations for LeafVersion (Dr Maxim Orlovsky)
6a3f3aabaf Inverse alternative formatting for LeafVersion type (Dr Maxim Orlovsky)
bec6694233 Fix docs on error conditions in LeafVersion::from_consensus (Dr Maxim Orlovsky)
7c28b47451 LowerHex and UpperHex implementations for FutureLeafVersion (Dr Maxim Orlovsky)

Pull request description:

  Trivial post-merge fixups from review comments in #718

ACKs for top commit:
  Kixunil:
    ACK 7f06e91a93
  sanket1729:
    ACK 7f06e91a93

Tree-SHA512: d94c4bd3d0b466287c8965103f74ecaba185d14c13b6c3f37d9fbe194343b3fc902fd2c7716554ad01fe28ff89cda933df199b7e8388a3fa6097028caf62522b
2022-01-10 04:18:48 +05:30
sanket1729 476eed7f2f
Merge rust-bitcoin/rust-bitcoin#590: Taproot: BIP32 extended keys using Scep256k1 keys instead of bitcoin ECDSA
cf0c48cc86 Improve Debug for PrivateKey (Dr Maxim Orlovsky)
b65a6ae49b Test for extended private key keypair generation  f5875a (Dr Maxim Orlovsky)
e6a3d603c9 BIP32 extended key `to_ecdsa()` and `to_schnorr()` methods (Dr Maxim Orlovsky)
b72f56c4ae BIP32 extended keys are using Scep256k1 keys instead of bitcoin ECDSA (Dr Maxim Orlovsky)

Pull request description:

  This is third step required to introduce Schnorr key support according to #588. This PR starts API-breaking changes and is follow-up to non-API breaking #589, which is already merged.

  PR rationale: BIP32 does not support uncompressed keys and using type with compression flag was a mistake

ACKs for top commit:
  apoelstra:
    ACK cf0c48cc86
  sanket1729:
    ACK cf0c48cc86. #757 might need rework after this

Tree-SHA512: 6356a65004e7517256bacbf9aaeb69a22fd8536b341e567c5c4e819288e1105d083fe12ac0641404c407c97acf039bdc525f8e02b1b594a6cdda90106f3b1bdc
2022-01-10 03:46:05 +05:30
Dr Maxim Orlovsky 7f06e91a93 LowerHex and UpperHex implementations for LeafVersion 2022-01-09 20:52:38 +01:00
Dr Maxim Orlovsky 6a3f3aabaf Inverse alternative formatting for LeafVersion type 2022-01-09 20:50:22 +01:00
Dr Maxim Orlovsky bec6694233 Fix docs on error conditions in LeafVersion::from_consensus 2022-01-09 20:48:00 +01:00
Dr Maxim Orlovsky 7c28b47451 LowerHex and UpperHex implementations for FutureLeafVersion 2022-01-09 20:46:51 +01:00
Andrew Poelstra 8e9f99b620
Merge rust-bitcoin/rust-bitcoin#718: Converting LeafVersion into an enum
ef8a3a839e Introduce FutureLeafVersion (Dr Maxim Orlovsky)
b028385a72 Improve docs in LeafVersion (Dr Maxim Orlovsky)
839c022f29 Make serde for LeafVersion to have byte representation (Dr Maxim Orlovsky)
67b8db05a8 Converting LeafVersion into an enum (Dr Maxim Orlovsky)
2405417432 Use TAPROOT_ANNEX_PREFIX in sighash module (Dr Maxim Orlovsky)

Pull request description:

  The original `LeafVersion` implementation was just a newtype around `u8`. I think that having enum explicitly listing consensus script implementation rules may be more beneficial in terms of both code readibility and future use of multiple script types, where `LeafVersion` may operate as a context object provided to `Script` to specify interpretation rules for particular op codes.

ACKs for top commit:
  Kixunil:
    ACK ef8a3a839e
  sanket1729:
    crACK ef8a3a839e. Waiting a day to let others complete review before merging.
  apoelstra:
    ACK ef8a3a839e

Tree-SHA512: 3356d2b9b00cf904edfece26d26ffbc646ba74446cc23ec4b2b4026ed50861285802f077226e30ba8fed466f68f8e8556c729ce48cb38581b1d95a02a6fde9cf
2022-01-09 15:26:05 +00:00
Dr Maxim Orlovsky cf0c48cc86 Improve Debug for PrivateKey 2022-01-09 07:17:10 +01:00
Dr Maxim Orlovsky b65a6ae49b Test for extended private key keypair generation f5875a 2022-01-09 07:17:06 +01:00
Dr Maxim Orlovsky e6a3d603c9 BIP32 extended key `to_ecdsa()` and `to_schnorr()` methods 2022-01-09 07:17:02 +01:00
Dr Maxim Orlovsky b72f56c4ae BIP32 extended keys are using Scep256k1 keys instead of bitcoin ECDSA
According to #588, BIP32 does not support uncompressed keys and using type with compression flag is a mistake
2022-01-09 07:16:49 +01:00
Dr Maxim Orlovsky ef8a3a839e Introduce FutureLeafVersion 2022-01-08 23:40:21 +01:00
Dr Maxim Orlovsky 14ace92666 Fix SchnorrSig type references in PSBT serialization macros 2022-01-08 16:29:45 +01:00
Dr Maxim Orlovsky b028385a72 Improve docs in LeafVersion 2022-01-07 22:06:17 +01:00
Dr Maxim Orlovsky 839c022f29 Make serde for LeafVersion to have byte representation 2022-01-07 22:04:41 +01:00
Dr Maxim Orlovsky 2b530000d3 Use EcdsaSig in PSBT partial signatures instead of Vec<u8> 2022-01-07 21:57:42 +01:00
Dr Maxim Orlovsky 141dbbd1b9 Add serde impl for EcdsaSig 2022-01-07 21:57:42 +01:00
Dr Maxim Orlovsky c92057d98f PSBT serialize/deserialize impl for EcdsaSig type 2022-01-07 21:57:42 +01:00
Dr Maxim Orlovsky 0af1c3f320 Add Display and FromStr for EcdsaSig 2022-01-07 21:57:11 +01:00
Dr Maxim Orlovsky c36a3da6f0 Add EcdsaSig::sighash_all convenience constructor 2022-01-07 21:49:05 +01:00
Andrew Poelstra f332a1967e
Merge rust-bitcoin/rust-bitcoin#750: Use `test_data` for big objects, add big block for benchmarking
247a14f4c3 Use test big block for bench_stream_reader instead of making one (Riccardo Casatta)
b92dfbb63f exclude test_data when publishing the crate (Riccardo Casatta)
f5a9681a2a include a big block in test_data, use it for ser/de benchmark (Riccardo Casatta)
09dada55d6 Move bip158 test vectors to test_data (Riccardo Casatta)
06d1a820c3 Remove testnet block hex from tests, use test_data with include_bytes! (Riccardo Casatta)

Pull request description:

  In the first two commits I moved some data from source files to the newly introduced `test_data` dir, including it with `include_[str|bytes]!` macro.

  The second-to-last commit introduces a big block in test_data which is very handy in ser/de benchmark (I used it for #672) because with smaller blocks you may not notice performance improvements.

  Since I don't want to pollute the package the last commit excludes the `test_data` dir from the published package. I think it's fine to do it because dependent packages don't run dependencies tests.

ACKs for top commit:
  apoelstra:
    ACK 247a14f4c3
  Kixunil:
    tACK 247a14f4c3

Tree-SHA512: a2beb635b0a358737d0b57d3e7205b1ddf87652b9a8c889ce63e2867659a8eaf7e43a5b87a453345d56d953745913f40b58596f449e5fbc87340e0dd2aef0727
2022-01-07 20:22:02 +00:00
Dr Maxim Orlovsky 67b8db05a8 Converting LeafVersion into an enum 2022-01-07 20:28:36 +01:00
Dr Maxim Orlovsky 2405417432 Use TAPROOT_ANNEX_PREFIX in sighash module 2022-01-07 20:27:34 +01:00
sanket1729 91470f56c8 Uncomment sighash test
We can check tweak add priv key with latest secp
2022-01-07 04:45:40 +05:30
sanket1729 2178c7367c Update to secp256k1 0.21.2 2022-01-07 04:45:40 +05:30
Riccardo Casatta 09dada55d6
Move bip158 test vectors to test_data 2022-01-06 13:47:58 +01:00
Riccardo Casatta 06d1a820c3
Remove testnet block hex from tests, use test_data with include_bytes! 2022-01-06 13:47:51 +01:00
Tobin Harding 533120899e Put rustdocs above attributes
Rust idiomatic style is to put the rustdoc _above_ any attributes on
types, functions, etc.

Audit the codebase and move comments/attributes to the correct place.
Add a trailing full stop at times to neaten things up a little extra.
2022-01-06 13:04:47 +11:00
sanket1729 92ee5a7e5f Test BIP341 sighash code 2022-01-01 04:12:46 +05:30
Dr. Maxim Orlovsky 670e808c17
Merge rust-bitcoin/rust-bitcoin#681: Add support for taproot psbt fields BIP 371
7d982fa9a2 Add all tests from BIP 371 (sanket1729)
d22e0149ad Taproot psbt impl BIP 371 (sanket1729)
108fc3d4db Impl encodable traits for TapLeafhash (sanket1729)
c7478d8fd0 Derive serde for taproot stuctures (sanket1729)

Pull request description:

  Built on top of #677 . Will rebase and mark ready for review after #677 is merged.

ACKs for top commit:
  apoelstra:
    ACK 7d982fa9a2
  dr-orlovsky:
    re-tACK 7d982fa9a2 basing on `git range-diff`. The original PR before last re-base was tested commit-by-commit.

Tree-SHA512: feb30e4b38d13110a9c0fabf6466d8f0fb7df09a82f4e01d70b8371b34ab0187004a6c63f9796c6585ee30841e8ee765ae9becae139d2e1e3d839553d64c3d1e
2021-12-30 02:12:03 +02:00
Dr. Maxim Orlovsky 86055d9df5
Merge rust-bitcoin/rust-bitcoin#672: New Witness struct to improve ser/de perfomance
106acdc3ac Add fuzzing for Witness struct (Riccardo Casatta)
2fd0125bfa Introduce Witness struct mainly to improve ser/de performance while keeping most usability. (Riccardo Casatta)

Pull request description:

  At the moment the Witness struct is  `Vec<Vec<u8>>`, the vec inside a vec cause a lot of allocations, specifically:

  - empty witness -> 1 allocation, while an empty vec doesn't allocate, the outer vec is not empty
  - witness with n elements -> n+1 allocations

  The proposed Witness struct contains the serialized format of the witness. This reduces the allocations to:

  - empty witness -> 0 allocations
  - witness with n elements -> 1 allocation for most common cases (you don't know how many bytes is long the entire witness beforehand, thus you need to estimate a good value, not too big to avoid wasting space and not too low to avoid vector reallocation, I used 128 since it covers about 80% of cases on mainnet)

  The inconvenience is having slightly less comfortable access to the witness, but the iterator is efficient (no allocations) and you can always collect the iteration to have a Vec of slices. If you collect the iteration you end up doing allocation anyway, but the rationale is that it is an operation you need to do rarely while ser/de is done much more often.

  I had to add a bigger block to better see the improvement (ae860247e191e2136d7c87382f78c96e0908d700), these are the results of the benches on my machine:

  ```
  RCasatta/master_with_block
  test blockdata::block::benches::bench_block_deserialize                 ... bench:   5,496,821 ns/iter (+/- 298,859)
  test blockdata::block::benches::bench_block_serialize                   ... bench:     437,389 ns/iter (+/- 31,576)
  test blockdata::block::benches::bench_block_serialize_logic             ... bench:     108,759 ns/iter (+/- 5,807)
  test blockdata::transaction::benches::bench_transaction_deserialize     ... bench:         670 ns/iter (+/- 49)
  test blockdata::transaction::benches::bench_transaction_get_size        ... bench:           7 ns/iter (+/- 0)
  test blockdata::transaction::benches::bench_transaction_serialize       ... bench:          51 ns/iter (+/- 5)
  test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench:          13 ns/iter (+/- 0)

  branch witness_with_block (this one)
  test blockdata::block::benches::bench_block_deserialize                 ... bench:   4,302,788 ns/iter (+/- 424,806)
  test blockdata::block::benches::bench_block_serialize                   ... bench:     366,493 ns/iter (+/- 42,216)
  test blockdata::block::benches::bench_block_serialize_logic             ... bench:      84,646 ns/iter (+/- 7,366)
  test blockdata::transaction::benches::bench_transaction_deserialize     ... bench:         648 ns/iter (+/- 77)
  test blockdata::transaction::benches::bench_transaction_get_size        ... bench:           7 ns/iter (+/- 0)
  test blockdata::transaction::benches::bench_transaction_serialize       ... bench:          50 ns/iter (+/- 5)
  test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench:          14 ns/iter (+/- 0)
  ```

  With an increased performance to deserialize a block of about 21% and to serialize a block of about 16% (seems even higher than expected, need to do more tests to confirm, I'll appreciate tests results from reviewers)

ACKs for top commit:
  apoelstra:
    ACK 106acdc3ac
  sanket1729:
    ACK 106acdc3ac
  dr-orlovsky:
    utACK 106acdc3ac

Tree-SHA512: e4f23bdd55075c7ea788bc55846fd9e30f9cb76d5847cb259bddbf72523857715b0d4dbac505be3dfb9d4b1bcae289384ab39885b4887e188f8f1c06caf4049a
2021-12-30 01:55:44 +02:00
sanket1729 7d982fa9a2 Add all tests from BIP 371 2021-12-28 20:40:58 +05:30
sanket1729 d22e0149ad Taproot psbt impl BIP 371 2021-12-28 20:40:58 +05:30
sanket1729 108fc3d4db Impl encodable traits for TapLeafhash 2021-12-28 20:40:58 +05:30
sanket1729 c7478d8fd0 Derive serde for taproot stuctures 2021-12-28 20:40:58 +05:30
Riccardo Casatta 2fd0125bfa
Introduce Witness struct mainly to improve ser/de performance while keeping most usability.
Witness struct is in place of the Vec<Vec<u8>> we have before this commit.

from_vec() and to_vec() methods are provided to switch between this type and Vec<Vec<u8>>

Moreover, implementation of Default, Iterator and others allows to have similar behaviour but
using a single Vec prevent many allocations during deserialization which in turns results in
better performance, even 20% better perfomance on recent block.

last() and second_to_last() allows to access respective element without going through costly Vec
transformation
2021-12-28 09:56:38 +01:00
Alekos Filini 2959e04ebd
Allow specifing a raw `TapLeafHash` in sighash computation
Instead of always requiring the full raw script and leaf version, allow
just specifying a raw leaf hash to the sighash computation functions.

This is very useful when dealing with PSBTs, because the
`PSBT_IN_TAP_BIP32_DERIVATION` field only maps a public key to a leaf
hash, so a signer could just take it and produce a signature with it
rathern than having to jump through hoops to recover the full raw
script.
2021-12-27 16:18:19 +01:00
Riccardo Casatta 9e1f256b54
Merge rust-bitcoin/rust-bitcoin#731: Improve parsing of `Denomination` string
f690b8e362 Be more liberal when parsing Denomination (Tobin Harding)
628168e493 Add missing white space character (Tobin Harding)

Pull request description:

  There is no reason to force users to use a particular format or case for `Denomination` strings. Users may wish to write any of the following and all seem reasonable
  - 100 sats
  - 100 sat
  - 100 SAT

  The same goes for various other `Denomination`s.

  - Patch 1 enables usage of "sats", "sat", "bit", "bits"
  - Patch 2 enables usage of various lower/uper case formatting

  Fixes: #729

ACKs for top commit:
  Kixunil:
    ACK f690b8e362
  apoelstra:
    ACK f690b8e362

Tree-SHA512: a785608e19a7ba6f689dc022cb17a709041ff56abeaa74649d0832a8bd8aac4593c7a79b46a47dd417796c588d669f50fb3c8b8a984be332ca38a1fef2dcd4ce
2021-12-27 10:17:40 +01:00
Riccardo Casatta f9b3fc9ce8
Merge rust-bitcoin/rust-bitcoin#686: Fixed a bunch of clippy lints, added clippy.toml
779d4110c6 Fixed a bunch of clippy lints, added clippy.toml (Martin Habovstiak)

Pull request description:

  This is the initial step towards using and maybe enforcing clippy.
  It does not fix all lints as some are not applicable. They may be
  explicitly ignored later.

  Some discussion about clippy was in #685

ACKs for top commit:
  apoelstra:
    ACK 779d4110c6
  RCasatta:
    ACK 779d4110c6

Tree-SHA512: fb9192c77565a0b1b2118877c6413945d65900e4e95b3741107bf6cddef1fa65ff09fc5b7814de421382292321cca6bd860bf17b73a227d193a0a13758ee25eb
2021-12-24 09:41:03 +01:00
Riccardo Casatta 6fa8a82414
Merge rust-bitcoin/rust-bitcoin#695: BIP341 test vectors
7aacc3782a Add tests from BIP341 (sanket1729)
61629cc733 Make taproot hashes forward display (sanket1729)

Pull request description:

  Add tests for taproot.
  - ~Also fixes one bug in #677, namely, I was returning `LeafVersion::default()` instead of given version~
  - ~ Fixes a bug in #691 about taking secp context as a reference instead of consuming it. This should have not passed my review, but this is easy to miss. ~
  - Makes the display on taproot hashes forward instead of the reverse (because the BIP prints in a forward way, I think we should too and it is more natural. )

ACKs for top commit:
  RCasatta:
    ACK 7aacc3782a
  apoelstra:
    ACK 7aacc3782a

Tree-SHA512: 2e0442131fc036ffa10f88c91c8fc02d9b67ff6c16c592aa6f4e6a220c26a00fc6ca95a288f14aa40667a289fb0446219fd6c76c0196ead766252356592b9941
2021-12-23 15:32:49 +01:00
Tobin Harding f690b8e362 Be more liberal when parsing Denomination
There is no reason to force users to use one particular form when
providing a denomination string. We can be liberal in what we accept
with no loss of clarity.

Allow `Denomination` strings to use a variety of forms, in particular
lower case and uppercase.

Note, we explicitly disallow various forms of `Msat` because it is
ambiguous whether this means milli or mega sats.

Co-developed-by: Martin Habovštiak <martin.habovstiak@gmail.com>
2021-12-22 13:51:11 +11:00
Martin Habovstiak 779d4110c6 Fixed a bunch of clippy lints, added clippy.toml
This is the initial step towards using and maybe enforcing clippy.
It does not fix all lints as some are not applicable. They may be
explicitly ignored later.
2021-12-21 22:50:13 +01:00
Riccardo Casatta fe43e3c9d7
Merge rust-bitcoin/rust-bitcoin#710: Refactor bitcoin_merkle_root functions
b454cf8e15 Return None from merkle_root functions (Tobin Harding)
7a8b017ea3 Use correct spelling of merkle (Tobin Harding)

Pull request description:

  ~Do two minor refactorings to the `bitcoin_merkle_root[_inline] functions.~

  This PR has grown, is no longer a refactoring because the two functions have been changed to return an `Option`.

  First patch is cleanup. Here is the commit message for the second patch
  ```
  The merkle_root of an empty tree is undefined, this is the only error
  case we have for the two `bitcoin_merkle_root*` functions. We can fully
  describe this error case by returning an `Option` if args are found to
  be empty.

  While we are at it, refactor out a recursive helper function to make
  reading the code between the two functions easier.
  ```

ACKs for top commit:
  Kixunil:
    ACK b454cf8e15
  dr-orlovsky:
    ACK b454cf8e15

Tree-SHA512: 961714a8b0eb0dad493a1548317d875d64ca22d2d584c905c502369b5f6e5a9f8be1edd7345136b44964dc0bde7a4c43bfaff4287d1dbf7fd736da79818074e3
2021-12-16 09:48:29 +01:00
Riccardo Casatta e5c6d6559d
Merge rust-bitcoin/rust-bitcoin#742: add MAX_MONEY public constant to Amount
ab12410ae8 add MAX_MONEY public constant to Amount (z8674558)

Pull request description:

  Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/740

ACKs for top commit:
  Kixunil:
    ACK ab12410ae8
  RCasatta:
    ACK ab12410ae8

Tree-SHA512: dfba40d8ae597d97653e13ba2ab1480822d5d75343da487e3d3e57cf6821bcc567d5a883be6fd76a3e1c7d60925fedc3a5a864789cf6370c6ebda0b1d02acdd1
2021-12-16 09:28:07 +01:00
Riccardo Casatta 970f574968
Merge rust-bitcoin/rust-bitcoin#702: Separate signature hash types
8361129518 Add SchnorrSig type (sanket1729)
94cfe79170 Rename existing SigHashType to EcdsaSigHashType (sanket1729)
648b3975a5 Add SchnorrSigHashType::from_u8 (sanket1729)
410e8bf46c Rename sighash::SigHashType::SigHashType to SchnorrSigHashType (sanket1729)
fa112a793a Add EcdsaSig (sanket1729)

Pull request description:

  Fixes #670 . Separates `SchnorrSigHashType` and `LegacySigHashType`. Also adds the following new structs:

  ```rust
  pub struct SchnorrSig {
      /// The underlying schnorr signature
      pub sig: secp256k1::schnorrsig::Signature,
      /// The corresponding hash type
      pub hash_ty: SchnorrSigHashType,
  }

  pub struct EcdsaSig {
      /// The underlying DER serialized Signature
      pub sig: secp256k1::Signature,
      /// The corresponding hash type
      pub hash_ty: LegacySigHashType,
  }
  ```

  This code is currently minimal to aid reviews. We can at a later point implement (Encodeable, psbt::Serialize, FromHex, ToHex) etc in follow-up PRs.

ACKs for top commit:
  Kixunil:
    ACK 8361129518
  RCasatta:
    ACK 8361129518

Tree-SHA512: 800ddcb3677a4f19e9d1c2a7eb7e95b0a677e9135e1e99f9e42956fc6a3fc94f639403076b4925b3adba6fdd95f56a99c2e47d0310675ad51ce5e7453c7355b6
2021-12-15 16:50:55 +01:00
sanket1729 36f3d230b8
Merge rust-bitcoin/rust-bitcoin#643: util/address: make address encoding more modular
506e03fa4d util/address: use hash functions of PublicKey/Script (Marko Bencun)
f826316c25 util/address: avoid .expect/panic (Marko Bencun)
ad83f6ae00 util/address: make address encoding more modular (Marko Bencun)

Pull request description:

  This allow library clients to plug their own encoding parameters in a
  backwards compatible manner.

Top commit has no ACKs.

Tree-SHA512: ae2ececbdfe4984fd62c975f4956686d79f6f5a6e65c34b55daa76fe785b8483ed7f35208d36b8bee545c7edd39ac878277a0fb8ea8c64a1943081e15c818bff
2021-12-15 20:17:45 +05:30
sanket1729 8361129518 Add SchnorrSig type
Export Sigs/Sigerrors
2021-12-15 20:00:52 +05:30
sanket1729 94cfe79170 Rename existing SigHashType to EcdsaSigHashType 2021-12-15 20:00:52 +05:30