Merge rust-bitcoin/rust-bitcoin#3982: hashes: Move from_engine functions

b98c489066 hashes: Move from_engine functions (Tobin C. Harding)

Pull request description:

  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.

ACKs for top commit:
  Kixunil:
    ACK b98c489066
  apoelstra:
    ACK b98c489066e8916a383099e5037e5a24832548ba; successfully ran local tests

Tree-SHA512: 8dfbf2b422d078d687708fa94a478ca597fae141f5c1f0a318a36152ca33f4760bb0545ab67523c558a8c3b8d258356975c5e357600d0ac980d473250a2af20e
This commit is contained in:
merge-script 2025-02-02 03:19:45 +00:00
commit 4700fe7fb2
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 18 additions and 18 deletions

View File

@ -15,6 +15,15 @@ crate::internal_macros::general_hash_type! {
"Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))" "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. /// Engine to compute HASH160 hash function.
#[derive(Clone)] #[derive(Clone)]
pub struct HashEngine(sha256::HashEngine); 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 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)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]

View File

@ -10,6 +10,15 @@ crate::internal_macros::general_hash_type! {
"Output of the SHA256d hash function." "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. /// Engine to compute SHA256d hash function.
#[derive(Clone)] #[derive(Clone)]
pub struct HashEngine(sha256::HashEngine); 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 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)] #[cfg(test)]
mod tests { mod tests {
#[allow(unused_imports)] // whether this is used depends on features #[allow(unused_imports)] // whether this is used depends on features