8000 Remove usage of ThirtyTwoByteHash by tcharding · Pull Request #1998 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove usage of ThirtyTwoByteHash #1998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bitcoin/examples/sighash.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bitcoin::hashes::Hash;
use bitcoin::{consensus, ecdsa, sighash, Amount, PublicKey, Script, ScriptBuf, Transaction};
use hex_lit::hex;

Expand Down Expand Up @@ -44,7 +45,9 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, value: u64) {
.p2wpkh_signature_hash(inp_idx, &spk, Amount::from_sat(value), sig.hash_ty)
.expect("failed to compute sighash");
println!("Segwit p2wpkh sighash: {:x}", sighash);
let msg = secp256k1::Message::from(sighash);
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg =
secp256k1::Message::from_slice(sighash.as_byte_array()).expect("sighash is 32 bytes long");
println!("Message is {:x}", msg);
let secp = secp256k1::Secp256k1::verification_only();
secp.verify_ecdsa(&msg, &sig.sig, &pk.inner).unwrap();
Expand Down
6 changes: 5 additions & 1 deletion bitcoin/examples/taproot-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ use std::str::FromStr;

use bitcoin::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, Fingerprint};
use bitcoin::consensus::encode;
use bitcoin::hashes::Hash;
use bitcoin::key::{TapTweak, XOnlyPublicKey};
use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP};
use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType};
Expand Down Expand Up @@ -737,7 +738,10 @@ fn sign_psbt_taproot(
Some(_) => keypair, // no tweak for script spend
};

let sig = secp.sign_schnorr(&hash.into(), &keypair);
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg =
secp256k1::Message::from_slice(hash.as_byte_array()).expect("tap sighash is 32 bytes long");
let sig = secp.sign_schnorr(&msg, &keypair);

let final_signature = taproot::Signature { sig, hash_ty };

Expand Down
4 changes: 3 additions & 1 deletion bitcoin/src/crypto/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,9 @@ mod tests {
.taproot_signature_hash(tx_ind, &Prevouts::All(&utxos), None, None, hash_ty)
.unwrap();

let msg = secp256k1::Message::from(sighash);
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long");
let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);

assert_eq!(expected.internal_pubkey, internal_key);
Expand Down
31 changes: 26 additions & 5 deletions bitcoin/src/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::{cmp, fmt};
#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};

use hashes::Hash;
use internals::write_err;
use secp256k1::{Message, Secp256k1, Signing};

Expand Down Expand Up @@ -325,31 +326,51 @@ impl Psbt {
match self.output_type(input_index)? {
Bare => {
let sighash = cache.legacy_signature_hash(input_index, spk, hash_ty.to_u32())?;
Ok((Message::from(sighash), hash_ty))
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_slice(sighash.as_byte_array()).expect("sighash is 32 bytes long"),
hash_ty,
))
}
Sh => {
let script_code =
input.redeem_script.as_ref().ok_or(SignError::MissingRedeemScript)?;
let sighash =
cache.legacy_signature_hash(input_index, script_code, hash_ty.to_u32())?;
Ok((Message::from(sighash), hash_ty))
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_slice(sighash.as_byte_array()).expect("sighash is 32 bytes long"),
hash_ty,
))
}
Wpkh => {
let sighash = cache.p2wpkh_signature_hash(input_index, spk, utxo.value, hash_ty)?;
Ok((Message::from(sighash), hash_ty))
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_slice(sighash.as_byte_array()).expect("sighash is 32 bytes long"),
hash_ty,
))
}
ShWpkh => {
let redeem_script = input.redeem_script.as_ref().expect("checked above");
let sighash =
cache.p2wpkh_signature_hash(input_index, redeem_script, utxo.value, hash_ty)?;
Ok((Message::from(sighash), hash_ty))
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_slice(sighash.as_byte_array()).expect("sighash is 32 bytes long"),
hash_ty,
))
}
Wsh | ShWsh => {
let witness_script =
input.witness_script.as_ref().ok_or(SignError::MissingWitnessScript)?;
let sighash =
cache.p2wsh_signature_hash(input_index, witness_script, utxo.value, hash_ty)?;
Ok((Message::from(sighash), hash_ty))
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_slice(sighash.as_byte_array()).expect("sighash is 32 bytes long"),
hash_ty,
))
}
Tr => {
// This PSBT signing API is WIP, taproot to come shortly.
Expand Down
11 changes: 8 additions & 3 deletions bitcoin/src/sign_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const BITCOIN_SIGNED_MSG_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
mod message_signing {
use core::fmt;

use hashes::sha256d;
use hashes::{sha256d, Hash};
use internals::write_err;
use secp256k1;
use secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
Expand Down Expand Up @@ -132,7 +132,10 @@ mod message_signing {
secp_ctx: &secp256k1::Secp256k1<C>,
msg_hash: sha256d::Hash,
) -> Result<PublicKey, MessageSignatureError> {
let msg = secp256k1::Message::from(msg_hash);
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_slice(msg_hash.as_byte_array())
.expect("sh256d hash is 32 bytes long");

let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?;
Ok(PublicKey { inner: pubkey, compressed: self.compressed })
}
Expand Down Expand Up @@ -226,7 +229,9 @@ mod tests {
let secp = secp256k1::Secp256k1::new();
let message = "rust-bitcoin MessageSignature test";
let msg_hash = super::signed_msg_hash(message);
let msg = secp256k1::Message::from(msg_hash);
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_slice(msg_hash.as_byte_array())
.expect("sh256d hash is 32 bytes long");

let privkey = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng());
let secp_sig = secp.sign_ecdsa_recoverable(&msg, &privkey);
Expand Down
0