Move encode_to_hash_engine to hashes from consensus_encoding
What changed, and why it matters
This is a routine internal code reorganization. A helper function that feeds encoded data into a hash calculation was moved from one Rust crate to another, and the dependency direction between those two crates was reversed. The actual logic of the function did not change, and there is no indication this fixes or introduces a security bug.
No security action required. Treat as normal refactoring; verify downstream consumers update their imports if they used the old re-export.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit moves encode_to_hash_engine from bitcoin-consensus-encoding to bitcoin_hashes, reversing the crate dependency so hashes now depends on consensus_encoding instead of the reverse. The function body is essentially identical. Call sites (e.g., primitives/src/block.rs) are updated to use the new path. The stated motivation is preparing consensus_encoding for a 1.0 release by removing its dependency on the pre-1.0 hashes crate.
Changed components
bitcoin-consensus-encoding cratebitcoin_hashes crateprimitives/src/block.rsInspect captured patch +26 / −27
diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock
index 32ad80d9..6a574262 100644
--- a/Cargo-minimal.lock
+++ b/Cargo-minimal.lock
@@ -77,7 +77,6 @@ name = "bitcoin-consensus-encoding"
version = "1.0.0-rc.1"
dependencies = [
"bitcoin-internals",
- "bitcoin_hashes 0.17.0",
]
[[package]]
@@ -174,6 +173,7 @@ dependencies = [
name = "bitcoin_hashes"
version = "0.17.0"
dependencies = [
+ "bitcoin-consensus-encoding",
"bitcoin-internals",
"hex-conservative 0.3.0",
"serde",
diff --git a/Cargo-recent.lock b/Cargo-recent.lock
index 594aedd3..9eab1a1f 100644
--- a/Cargo-recent.lock
+++ b/Cargo-recent.lock
@@ -76,7 +76,6 @@ name = "bitcoin-consensus-encoding"
version = "1.0.0-rc.1"
dependencies = [
"bitcoin-internals",
- "bitcoin_hashes 0.17.0",
]
[[package]]
@@ -173,6 +172,7 @@ dependencies = [
name = "bitcoin_hashes"
version = "0.17.0"
dependencies = [
+ "bitcoin-consensus-encoding",
"bitcoin-internals",
"hex-conservative 0.3.0",
"serde",
diff --git a/consensus_encoding/Cargo.toml b/consensus_encoding/Cargo.toml
index c599fb44..4ace7879 100644
--- a/consensus_encoding/Cargo.toml
+++ b/consensus_encoding/Cargo.toml
@@ -18,7 +18,6 @@ std = ["alloc", "internals/std"]
alloc = ["internals/alloc"]
[dependencies]
-hashes = { package = "bitcoin_hashes", path = "../hashes", version = "0.17.0", default-features = false }
internals = { package = "bitcoin-internals", path = "../internals", version = "0.4.0" }
[package.metadata.docs.rs]
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index 6e44e0ab..fb3518de 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -64,25 +64,6 @@ macro_rules! encoder_newtype{
}
}
-/// Encodes an object into a hash engine.
-///
-/// Consumes and returns the hash engine to make it easier to call
-/// [`hashes::HashEngine::finalize`] directly on the result.
-pub fn encode_to_hash_engine<T, H>(object: &T, mut engine: H) -> H
-where
- T: Encodable + ?Sized,
- H: hashes::HashEngine,
-{
- let mut encoder = object.encoder();
- loop {
- engine.input(encoder.current_chunk());
- if !encoder.advance() {
- break;
- }
- }
- engine
-}
-
/// Encodes an object into a vector.
#[cfg(feature = "alloc")]
pub fn encode_to_vec<T>(object: &T) -> Vec<u8>
diff --git a/consensus_encoding/src/lib.rs b/consensus_encoding/src/lib.rs
index 7c07604a..12ee4a4f 100644
--- a/consensus_encoding/src/lib.rs
+++ b/consensus_encoding/src/lib.rs
@@ -17,9 +17,6 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
-/// Rust implementation of cryptographic hash function algorithms.
-pub extern crate hashes;
-
mod decode;
mod encode;
@@ -45,4 +42,4 @@ pub use self::encode::encoders::{
ArrayEncoder, BytesEncoder, CompactSizeEncoder, Encoder2, Encoder3, Encoder4, Encoder6,
SliceEncoder,
};
-pub use self::encode::{encode_to_hash_engine, Encodable, Encoder};
+pub use self::encode::{Encodable, Encoder};
diff --git a/hashes/Cargo.toml b/hashes/Cargo.toml
index 924ad583..b59733e8 100644
--- a/hashes/Cargo.toml
+++ b/hashes/Cargo.toml
@@ -23,6 +23,7 @@ small-hash = []
[dependencies]
internals = { package = "bitcoin-internals", path = "../internals", version = "0.4.1" }
+encoding = { package = "bitcoin-consensus-encoding", path = "../consensus_encoding", version = "1.0.0-rc.1", default-features = false }
hex = { package = "hex-conservative", version = "0.3.0", default-features = false, optional = true }
serde = { version = "1.0.195", default-features = false, optional = true }
diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs
index 7f91f857..bcb260cf 100644
--- a/hashes/src/lib.rs
+++ b/hashes/src/lib.rs
@@ -115,6 +115,8 @@ pub mod siphash24;
use core::fmt::{self, Write as _};
use core::{convert, hash};
+use encoding::Encoder;
+
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
pub use self::{
@@ -195,6 +197,25 @@ pub trait HashEngine: Clone {
fn finalize(self) -> Self::Hash;
}
+/// Encodes an object into a hash engine.
+///
+/// Consumes and returns the hash engine to make it easier to call
+/// [`HashEngine::finalize`] directly on the result.
+pub fn encode_to_hash_engine<T, H>(object: &T, mut engine: H) -> H
+where
+ T: encoding::Encodable + ?Sized,
+ H: HashEngine,
+{
+ let mut encoder = object.encoder();
+ loop {
+ engine.input(encoder.current_chunk());
+ if !encoder.advance() {
+ break;
+ }
+ }
+ engine
+}
+
/// Trait which applies to hashes of all types.
pub trait Hash:
Copy + Clone + PartialEq + Eq + PartialOrd + Ord + hash::Hash + convert::AsRef<[u8]>
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index 66bc5a70..d306dc8e 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -235,7 +235,7 @@ 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 bare_hash = encoding::encode_to_hash_engine(self, sha256d::Hash::engine()).finalize();
+ let bare_hash = hashes::encode_to_hash_engine(self, sha256d::Hash::engine()).finalize();
BlockHash::from_byte_array(bare_hash.to_byte_array())
}
}
Why this scored 18/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.