From b98c489066e8916a383099e5037e5a24832548ba Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jan 2025 10:42:10 +1100 Subject: [PATCH] hashes: Move from_engine functions In order to use the `general_hash_type` macro the must exist a standalone `from_engine` function. Currently this function is in different places in different modules. In an effort to make the `hashes` code easier to reason about put the functions right below the macro. Code move only, no other changes. --- hashes/src/hash160.rs | 18 +++++++++--------- hashes/src/sha256d.rs | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/hashes/src/hash160.rs b/hashes/src/hash160.rs index 2e90ca391..b984960c9 100644 --- a/hashes/src/hash160.rs +++ b/hashes/src/hash160.rs @@ -15,6 +15,15 @@ crate::internal_macros::general_hash_type! { "Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))" } +fn from_engine(e: HashEngine) -> Hash { + let sha2 = sha256::Hash::from_engine(e.0); + let rmd = ripemd160::Hash::hash(sha2.as_byte_array()); + + let mut ret = [0; 20]; + ret.copy_from_slice(rmd.as_byte_array()); + Hash(ret) +} + /// Engine to compute HASH160 hash function. #[derive(Clone)] pub struct HashEngine(sha256::HashEngine); @@ -34,15 +43,6 @@ impl crate::HashEngine for HashEngine { fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() } } -fn from_engine(e: HashEngine) -> Hash { - let sha2 = sha256::Hash::from_engine(e.0); - let rmd = ripemd160::Hash::hash(sha2.as_byte_array()); - - let mut ret = [0; 20]; - ret.copy_from_slice(rmd.as_byte_array()); - Hash(ret) -} - #[cfg(test)] mod tests { #[test] diff --git a/hashes/src/sha256d.rs b/hashes/src/sha256d.rs index 332d6bc11..165bee52e 100644 --- a/hashes/src/sha256d.rs +++ b/hashes/src/sha256d.rs @@ -10,6 +10,15 @@ crate::internal_macros::general_hash_type! { "Output of the SHA256d hash function." } +fn from_engine(e: HashEngine) -> Hash { + let sha2 = sha256::Hash::from_engine(e.0); + let sha2d = sha256::Hash::hash(sha2.as_byte_array()); + + let mut ret = [0; 32]; + ret.copy_from_slice(sha2d.as_byte_array()); + Hash(ret) +} + /// Engine to compute SHA256d hash function. #[derive(Clone)] pub struct HashEngine(sha256::HashEngine); @@ -29,15 +38,6 @@ impl crate::HashEngine for HashEngine { fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() } } -fn from_engine(e: HashEngine) -> Hash { - let sha2 = sha256::Hash::from_engine(e.0); - let sha2d = sha256::Hash::hash(sha2.as_byte_array()); - - let mut ret = [0; 32]; - ret.copy_from_slice(sha2d.as_byte_array()); - Hash(ret) -} - #[cfg(test)] mod tests { #[allow(unused_imports)] // whether this is used depends on features