Merge rust-bitcoin/rust-bitcoin#1118: Use infallible conversions: `Hash` -> `secp256k1::Message`

aeede12604 Infallible conversions: `Hash` -> `Message` (Arturo Marquez)

Pull request description:

  Replaces all instances of `secp256k1::Message::from_slice(_).expect(_)` with `secp256k1::Message::from(_)`. This also implements `ThirtyTwoByteHash` for `TapSighashHash`.

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

ACKs for top commit:
  Kixunil:
    ACK aeede12604
  tcharding:
    ACK aeede12604
  apoelstra:
    ACK aeede12604

Tree-SHA512: cd392f0e93e2560680c579a889a46f7e4484380058b2d8d03b6ecec351d880efa9beea5e3be128158e9e26243b7dfcef1f48a448028d9155958a5af62bcc9ec2
This commit is contained in:
Andrew Poelstra 2022-07-21 22:36:57 +00:00
commit f6e2d959cc
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 10 additions and 5 deletions

View File

@ -35,7 +35,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies] [dependencies]
bech32 = { version = "0.8.1", default-features = false } bech32 = { version = "0.8.1", default-features = false }
bitcoin_hashes = { version = "0.11.0", default-features = false } bitcoin_hashes = { version = "0.11.0", default-features = false }
secp256k1 = { version = "0.24.0", default-features = false } secp256k1 = { version = "0.24.0", default-features = false, features = ["bitcoin_hashes"] }
core2 = { version = "0.3.0", optional = true, default-features = false } core2 = { version = "0.3.0", optional = true, default-features = false }
base64 = { version = "0.13.0", optional = true } base64 = { version = "0.13.0", optional = true }

View File

@ -141,8 +141,7 @@ mod message_signing {
secp_ctx: &secp256k1::Secp256k1<C>, secp_ctx: &secp256k1::Secp256k1<C>,
msg_hash: sha256d::Hash msg_hash: sha256d::Hash
) -> Result<PublicKey, MessageSignatureError> { ) -> Result<PublicKey, MessageSignatureError> {
let msg = secp256k1::Message::from_slice(&msg_hash[..]) let msg = secp256k1::Message::from(msg_hash);
.expect("cannot fail");
let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?; let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?;
Ok(PublicKey { Ok(PublicKey {
inner: pubkey, inner: pubkey,
@ -319,7 +318,7 @@ mod tests {
let secp = secp256k1::Secp256k1::new(); let secp = secp256k1::Secp256k1::new();
let message = "rust-bitcoin MessageSignature test"; let message = "rust-bitcoin MessageSignature test";
let msg_hash = super::signed_msg_hash(&message); let msg_hash = super::signed_msg_hash(&message);
let msg = secp256k1::Message::from_slice(&msg_hash).expect("message"); let msg = secp256k1::Message::from(msg_hash);
let privkey = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()); let privkey = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng());

View File

@ -1148,7 +1148,7 @@ mod tests {
hash_ty hash_ty
).unwrap(); ).unwrap();
let msg = secp256k1::Message::from_slice(&sighash).unwrap(); let msg = secp256k1::Message::from(sighash);
let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]); let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);
assert_eq!(expected_internal_pk, internal_key); assert_eq!(expected_internal_pk, internal_key);

View File

@ -62,6 +62,12 @@ sha256t_hash_newtype!(TapSighashHash, TapSighashTag, MIDSTATE_TAPSIGHASH, 64,
doc="Taproot-tagged hash for the taproot signature hash", false doc="Taproot-tagged hash for the taproot signature hash", false
); );
impl secp256k1::ThirtyTwoByteHash for TapSighashHash {
fn into_32(self) -> [u8; 32] {
self.into_inner()
}
}
impl TapTweakHash { impl TapTweakHash {
/// Creates a new BIP341 [`TapTweakHash`] from key and tweak. Produces `H_taptweak(P||R)` where /// Creates a new BIP341 [`TapTweakHash`] from key and tweak. Produces `H_taptweak(P||R)` where
/// `P` is the internal key and `R` is the merkle root. /// `P` is the internal key and `R` is the merkle root.