Replace uses of encode_to_engine with encode_to_hash
What changed, and why it matters
This is a small internal code cleanup in the rust-bitcoin library. It replaces a slightly longer two-step hashing pattern with a shorter helper function that does the same thing. The actual cryptographic results and behavior are unchanged.
No security action needed. Treat as a normal refactoring/code-quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors two call sites to use hashes::encode_to_hash::<_, EngineType>(value) instead of manually creating a hash engine, encoding into it, and then finalizing. In bitcoin/src/crypto/sighash.rs, the Taproot SIGHASH_SINGLE output hash is computed the same way but with less boilerplate. In primitives/src/block.rs, Header::block_hash() is similarly simplified. A #[cfg(feature = "alloc")] guard is added to the HashEngine as _ import because the refactored code no longer uses the trait outside the alloc feature path.
Changed components
bitcoin/src/crypto/sighash.rsprimitives/src/block.rsInspect captured patch +6 / −7
diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs
index c2db1a38..ee063131 100644
--- a/bitcoin/src/crypto/sighash.rs
+++ b/bitcoin/src/crypto/sighash.rs
@@ -450,7 +450,6 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
// If hash_type & 3 equals SIGHASH_SINGLE:
// sha_single_output (32): the SHA256 of the corresponding output in CTxOut format.
if sighash == TapSighashType::Single {
- let mut enc = sha256::Hash::engine();
let txout = self
.tx
.borrow()
@@ -461,8 +460,7 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
outputs_length: self.tx.borrow().outputs.len(),
}))
.map_err(SigningDataError::Sighash)?;
- hashes::encode_to_engine(txout, &mut enc);
- let hash = sha256::Hash::from_engine(enc);
+ let hash = hashes::encode_to_hash::<_, sha256::HashEngine>(txout);
writer.write_all(&hash.to_byte_array())?;
}
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index 4910bc37..c917da06 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -16,7 +16,9 @@ use arbitrary::{Arbitrary, Unstructured};
use encoding::{ArrayDecoder, Decoder6};
#[cfg(feature = "alloc")]
use encoding::{CompactSizeEncoder, Decoder2, Encoder2, SliceEncoder, VecDecoder};
-use hashes::{sha256d, HashEngine as _};
+use hashes::sha256d;
+#[cfg(feature = "alloc")]
+use hashes::HashEngine as _;
#[cfg(feature = "hex")]
use crate::hex_codec::HexPrimitive;
@@ -465,9 +467,8 @@ impl Header {
/// Returns the block hash.
// This is the same as `Encodable` but done manually because `Encodable` isn't in `primitives`.
pub fn block_hash(&self) -> BlockHash {
- let mut engine = sha256d::Hash::engine();
- hashes::encode_to_engine(self, &mut engine);
- BlockHash::from_byte_array(engine.finalize().to_byte_array())
+ let hash = hashes::encode_to_hash::<_, sha256d::HashEngine>(self);
+ BlockHash::from_byte_array(hash.to_byte_array())
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.