rust-bitcoin-unsafe-fast/bitcoin/src/blockdata
Andrew Poelstra 2664f978f3
Merge rust-bitcoin/rust-bitcoin#2213: Fix InputWeightPrediction::P2WPKH_MAX constant DER sig length
f41ebc2149 Add test for input weight predictions (conduition)
4514a80a23 Fix the InputWeightPrediction constants for DER signatures (conduition)
b5ce219c62 add weight method to InputWeightPrediction (conduition)

Pull request description:

  The `P2WPKH_MAX` constant assumed DER signatures in the witness have a max length of 73. In practice, their maximum length is 72, because [BIP62](https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki) forbids nodes from relaying transactions which contain non-canonical ECDSA signatures (i.e. TX sigs must have an $s$ value of less than $\frac{n}{2}$).

  This means $s$ is never encoded with a leading zero byte, and the signature as a whole never exceeds 72 bytes in total encoded length. The `ground_p2wpkh` function was already correct; only the constant needed to be corrected.

  Technically 73 bytes *is* the upper limit for signatures, as nothing forbids miners from including such non-standard transaction signatures in blocks, but for the purposes of fee estimation and input weight prediction, 72 is the number which 99.9% of implementations should use as their ceiling. We already use it as the ceiling for the `ground_p2wpkh` function - `ground_p2wpkh(0)` returns a prediction which uses a witness signature of length 72.

  Reference:
  - https://bitcoin.stackexchange.com/questions/77191/what-is-the-maximum-size-of-a-der-encoded-ecdsa-signature
  - https://bitcoin.stackexchange.com/questions/106435/are-high-s-ecdsa-signatures-forbidden-in-segwit-witnesses
  - https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki

  To enable testing, I added a `weight()` method to `InputWeightPrediction` and made it public but i'm not sure whether it has a use-case. Let me know if I should make it private instead.

ACKs for top commit:
  tcharding:
    ACK f41ebc2149
  apoelstra:
    ACK f41ebc2149

Tree-SHA512: 10e837bad9881c0efebb0598eaefd4ab039f2a6ececead75a68e253d84f5e85cb30496a6069eee8dfe9714773f3aa23cfe373f5d88d1c5609e1b1be1ff142e37
2023-11-22 20:36:11 +00:00
..
locktime Remove code deprecated since v0.31.0 2023-11-04 12:14:54 +11:00
script Improve Script documentation and fix typos 2023-11-15 20:57:22 +01:00
block.rs Merge rust-bitcoin/rust-bitcoin#2066: Add a `bitcoin-io` crate 2023-11-19 14:26:19 +00:00
constants.rs Remove code deprecated since v0.31.0 2023-11-04 12:14:54 +11:00
fee_rate.rs add second test case 2023-11-10 17:19:33 +00:00
mod.rs Improve public re-exports 2023-10-31 15:16:47 +11:00
opcodes.rs Rename All to Opcode 2023-08-07 17:08:11 +10:00
transaction.rs Add test for input weight predictions 2023-11-22 00:50:59 +00:00
weight.rs Add from_vb_const function 2023-11-21 06:23:32 +01:00
witness.rs Remove code deprecated since v0.31.0 2023-11-04 12:14:54 +11:00