Skip to content

Commit

Permalink
Merge rust-bitcoin#3982: hashes: Move from_engine functions
Browse files Browse the repository at this point in the history
b98c489 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 b98c489
  apoelstra:
    ACK b98c489; successfully ran local tests

Tree-SHA512: 8dfbf2b422d078d687708fa94a478ca597fae141f5c1f0a318a36152ca33f4760bb0545ab67523c558a8c3b8d258356975c5e357600d0ac980d473250a2af20e
  • Loading branch information
apoelstra committed Feb 2, 2025
2 parents 5ac27b9 + b98c489 commit 4700fe7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions hashes/src/hash160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]
Expand Down
18 changes: 9 additions & 9 deletions hashes/src/sha256d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 4700fe7

Please sign in to comment.