Merge rust-bitcoin/rust-bitcoin#2185: add input weight predictions for p2pkh outputs

c745c97e5f add input weight predictions for p2pkh outputs (conduition)

Pull request description:

  Adds input weight prediction constant and `ground_p2pkh_*` methods, mirroring those for `P2WPKH`. This seemed to be missing.

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

Tree-SHA512: 69b4484686ecf761b766d2a7d7408c784981a9ba8c4aa8abd9d8655bd9421eb882e346ece232530edf9edff602d2fe1570992a5eb7b420b928d8b13397d2f60f
This commit is contained in:
Andrew Poelstra 2023-11-15 23:34:12 +00:00
commit 43cd352cf9
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 41 additions and 0 deletions

View File

@ -1323,6 +1323,26 @@ impl InputWeightPrediction {
/// [signature grinding]: https://bitcoin.stackexchange.com/questions/111660/what-is-signature-grinding
pub const P2WPKH_MAX: Self = InputWeightPrediction::from_slice(0, &[73, 33]);
/// Input weight prediction corresponding to spending of a P2PKH output with the largest possible
/// DER-encoded signature, and a compressed public key.
///
/// If the input in your transaction uses P2PKH with a compressed key, you can use this instead of
/// [`InputWeightPrediction::new`].
///
/// This is useful when you **do not** use [signature grinding] and want to ensure you are not
/// under-paying. See [`ground_p2pkh_compressed`](Self::ground_p2pkh_compressed) if you do use
/// signature grinding.
///
/// [signature grinding]: https://bitcoin.stackexchange.com/questions/111660/what-is-signature-grinding
pub const P2PKH_COMPRESSED_MAX: Self = InputWeightPrediction::from_slice(107, &[]);
/// Input weight prediction corresponding to spending of a P2PKH output with the largest possible
/// DER-encoded signature, and an uncompressed public key.
///
/// If the input in your transaction uses P2PKH with an uncompressed key, you can use this instead of
/// [`InputWeightPrediction::new`].
pub const P2PKH_UNCOMPRESSED_MAX: Self = InputWeightPrediction::from_slice(139, &[]);
/// Input weight prediction corresponding to spending of taproot output using the key and
/// default sighash.
///
@ -1357,6 +1377,27 @@ impl InputWeightPrediction {
InputWeightPrediction::from_slice(0, &[der_signature_size, 33])
}
/// Input weight prediction corresponding to spending of a P2PKH output using [signature
/// grinding], and a compressed public key.
///
/// If the input in your transaction uses compressed P2PKH and you use signature grinding you
/// can use this instead of [`InputWeightPrediction::new`]. See
/// [`P2PKH_COMPRESSED_MAX`](Self::P2PKH_COMPRESSED_MAX) if you don't use signature grinding.
///
/// Note: `bytes_to_grind` is usually `1` because of exponential cost of higher values.
///
/// # Panics
///
/// The funcion panics in const context and debug builds if `bytes_to_grind` is higher than 62.
///
/// [signature grinding]: https://bitcoin.stackexchange.com/questions/111660/what-is-signature-grinding
pub const fn ground_p2pkh_compressed(bytes_to_grind: usize) -> Self {
// Written to trigger const/debug panic for unreasonably high values.
let der_signature_size = 10 + (62 - bytes_to_grind);
InputWeightPrediction::from_slice(2 + 33 + der_signature_size, &[])
}
/// Computes the prediction for a single input.
pub fn new<T>(input_script_len: usize, witness_element_lengths: T) -> Self
where