Merge rust-bitcoin/rust-bitcoin#2166: Move `sha512_256` code

0bb537d45b Move sha512_256 code (Tobin C. Harding)

Pull request description:

  Make the `sha512_256` module use similar code layout to the other hash modules by putting the call to `hash_type` at the top followed by the `from_engine` function.

  Code move only, no other changes.

ACKs for top commit:
  Kixunil:
    ACK 0bb537d45b
  apoelstra:
    ACK 0bb537d45b

Tree-SHA512: 1b2471782b6aa39c6feb4ac3e7a899d13a12f621e1260365737f56511d7c4dd54bbe47695829a11cc4756ca4a5bf720f5d909632ff2598a6139e7bbc99f64e25
This commit is contained in:
Andrew Poelstra 2023-11-03 19:46:40 +00:00
commit 1cf5bd2e93
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 13 additions and 13 deletions

View File

@ -13,6 +13,19 @@ use core::str;
use crate::{sha512, FromSliceError};
crate::internal_macros::hash_type! {
256,
false,
"Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at <https://eprint.iacr.org/2010/548.pdf>. ",
"crate::util::json_hex_string::len_32"
}
fn from_engine(e: HashEngine) -> Hash {
let mut ret = [0; 32];
ret.copy_from_slice(&sha512::from_engine(e.0)[..32]);
Hash(ret)
}
/// Engine to compute SHA512/256 hash function.
///
/// SHA512/256 is a hash function that uses the sha512 alogrithm but it truncates
@ -41,19 +54,6 @@ impl crate::HashEngine for HashEngine {
fn input(&mut self, inp: &[u8]) { self.0.input(inp); }
}
crate::internal_macros::hash_type! {
256,
false,
"Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at <https://eprint.iacr.org/2010/548.pdf>. ",
"crate::util::json_hex_string::len_32"
}
fn from_engine(e: HashEngine) -> Hash {
let mut ret = [0; 32];
ret.copy_from_slice(&sha512::from_engine(e.0)[..32]);
Hash(ret)
}
#[cfg(test)]
mod tests {
#[test]