From 37b54dd54c4a84b35178ac7d60913ef6a1ccaa59 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 12 Jul 2024 13:47:00 +1000 Subject: [PATCH] Move from_midstate function Move the `sha256::HashEngine::from_midstate` function to be in the same impl block as `midstate`. Refactor only, no logic change. --- hashes/src/sha256.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index e6bb03f12..56b455c34 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -74,6 +74,22 @@ impl HashEngine { } } + /// Create a new [`HashEngine`] from a [`Midstate`]. + /// + /// # Panics + /// + /// If `length` is not a multiple of the block size. + pub fn from_midstate(midstate: Midstate, length: usize) -> HashEngine { + assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); + + let mut ret = [0; 8]; + for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate[..].chunks_exact(4)) { + *ret_val = u32::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice")); + } + + HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length } + } + /// Outputs the midstate of the hash engine. This function should not be /// used directly unless you really know what you're doing. #[cfg(not(hashes_fuzz))] @@ -433,22 +449,6 @@ impl Midstate { } impl HashEngine { - /// Create a new [`HashEngine`] from a [`Midstate`]. - /// - /// # Panics - /// - /// If `length` is not a multiple of the block size. - pub fn from_midstate(midstate: Midstate, length: usize) -> HashEngine { - assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); - - let mut ret = [0; 8]; - for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate[..].chunks_exact(4)) { - *ret_val = u32::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice")); - } - - HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length } - } - fn process_block(&mut self) { #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))] {