diff --git a/hashes/src/hash160/mod.rs b/hashes/src/hash160/mod.rs index 406bdc5ab..fbb11e1d0 100644 --- a/hashes/src/hash160/mod.rs +++ b/hashes/src/hash160/mod.rs @@ -15,17 +15,16 @@ crate::internal_macros::general_hash_type! { "Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))" } - impl Hash { -/// Finalize a hash engine to produce a hash. -pub fn from_engine(e: HashEngine) -> Self { - let sha2 = sha256::Hash::from_engine(e.0); - let rmd = ripemd160::Hash::hash(sha2.as_byte_array()); + /// Finalize a hash engine to produce a hash. + pub fn from_engine(e: HashEngine) -> Self { + 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) -} + let mut ret = [0; 20]; + ret.copy_from_slice(rmd.as_byte_array()); + Hash(ret) + } } /// Engine to compute HASH160 hash function. diff --git a/hashes/src/ripemd160/mod.rs b/hashes/src/ripemd160/mod.rs index 7b0e5377f..36f2b2416 100644 --- a/hashes/src/ripemd160/mod.rs +++ b/hashes/src/ripemd160/mod.rs @@ -20,36 +20,35 @@ crate::internal_macros::general_hash_type! { "Output of the RIPEMD160 hash function." } - impl Hash { -/// Finalize a hash engine to produce a hash. -#[cfg(not(hashes_fuzz))] -pub fn from_engine(mut e: HashEngine) -> Self { - // pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining - let n_bytes_hashed = e.bytes_hashed; + /// Finalize a hash engine to produce a hash. + #[cfg(not(hashes_fuzz))] + pub fn from_engine(mut e: HashEngine) -> Self { + // pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining + let n_bytes_hashed = e.bytes_hashed; - let zeroes = [0; BLOCK_SIZE - 8]; - e.input(&[0x80]); - if crate::incomplete_block_len(&e) > zeroes.len() { - e.input(&zeroes); + let zeroes = [0; BLOCK_SIZE - 8]; + e.input(&[0x80]); + if crate::incomplete_block_len(&e) > zeroes.len() { + e.input(&zeroes); + } + let pad_length = zeroes.len() - incomplete_block_len(&e); + e.input(&zeroes[..pad_length]); + debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); + + e.input(&(8 * n_bytes_hashed).to_le_bytes()); + debug_assert_eq!(incomplete_block_len(&e), 0); + + Hash(e.midstate()) } - let pad_length = zeroes.len() - incomplete_block_len(&e); - e.input(&zeroes[..pad_length]); - debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); - e.input(&(8 * n_bytes_hashed).to_le_bytes()); - debug_assert_eq!(incomplete_block_len(&e), 0); - - Hash(e.midstate()) -} - -/// Finalize a hash engine to produce a hash. -#[cfg(hashes_fuzz)] -pub fn from_engine(e: HashEngine) -> Self { - let mut res = e.midstate(); - res[0] ^= (e.bytes_hashed & 0xff) as u8; - Hash(res) -} + /// Finalize a hash engine to produce a hash. + #[cfg(hashes_fuzz)] + pub fn from_engine(e: HashEngine) -> Self { + let mut res = e.midstate(); + res[0] ^= (e.bytes_hashed & 0xff) as u8; + Hash(res) + } } const BLOCK_SIZE: usize = 64; diff --git a/hashes/src/sha1/mod.rs b/hashes/src/sha1/mod.rs index ae49c364f..ca5e92acb 100644 --- a/hashes/src/sha1/mod.rs +++ b/hashes/src/sha1/mod.rs @@ -21,25 +21,25 @@ crate::internal_macros::general_hash_type! { } impl Hash { -/// Finalize a hash engine to produce a hash. -pub fn from_engine(mut e: HashEngine) -> Self { - // pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining - let n_bytes_hashed = e.bytes_hashed; + /// Finalize a hash engine to produce a hash. + pub fn from_engine(mut e: HashEngine) -> Self { + // pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining + let n_bytes_hashed = e.bytes_hashed; - let zeroes = [0; BLOCK_SIZE - 8]; - e.input(&[0x80]); - if incomplete_block_len(&e) > zeroes.len() { - e.input(&zeroes); + let zeroes = [0; BLOCK_SIZE - 8]; + e.input(&[0x80]); + if incomplete_block_len(&e) > zeroes.len() { + e.input(&zeroes); + } + let pad_length = zeroes.len() - incomplete_block_len(&e); + e.input(&zeroes[..pad_length]); + debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); + + e.input(&(8 * n_bytes_hashed).to_be_bytes()); + debug_assert_eq!(incomplete_block_len(&e), 0); + + Hash(e.midstate()) } - let pad_length = zeroes.len() - incomplete_block_len(&e); - e.input(&zeroes[..pad_length]); - debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); - - e.input(&(8 * n_bytes_hashed).to_be_bytes()); - debug_assert_eq!(incomplete_block_len(&e), 0); - - Hash(e.midstate()) -} } const BLOCK_SIZE: usize = 64; diff --git a/hashes/src/sha256/mod.rs b/hashes/src/sha256/mod.rs index 1def47bf5..39c3257be 100644 --- a/hashes/src/sha256/mod.rs +++ b/hashes/src/sha256/mod.rs @@ -110,38 +110,38 @@ impl crate::HashEngine for HashEngine { } impl Hash { -/// Finalize a hash engine to obtain a hash. -#[cfg(not(hashes_fuzz))] -pub fn from_engine(mut e: HashEngine) -> Self { - // pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining - let n_bytes_hashed = e.bytes_hashed; + /// Finalize a hash engine to obtain a hash. + #[cfg(not(hashes_fuzz))] + pub fn from_engine(mut e: HashEngine) -> Self { + // pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining + let n_bytes_hashed = e.bytes_hashed; - let zeroes = [0; BLOCK_SIZE - 8]; - e.input(&[0x80]); - if incomplete_block_len(&e) > zeroes.len() { - e.input(&zeroes); + let zeroes = [0; BLOCK_SIZE - 8]; + e.input(&[0x80]); + if incomplete_block_len(&e) > zeroes.len() { + e.input(&zeroes); + } + let pad_length = zeroes.len() - incomplete_block_len(&e); + e.input(&zeroes[..pad_length]); + debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); + + e.input(&(8 * n_bytes_hashed).to_be_bytes()); + debug_assert_eq!(incomplete_block_len(&e), 0); + + Hash(e.midstate_unchecked().bytes) } - let pad_length = zeroes.len() - incomplete_block_len(&e); - e.input(&zeroes[..pad_length]); - debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); - e.input(&(8 * n_bytes_hashed).to_be_bytes()); - debug_assert_eq!(incomplete_block_len(&e), 0); - - Hash(e.midstate_unchecked().bytes) -} - -/// Finalize a hash engine to obtain a hash. -#[cfg(hashes_fuzz)] -pub fn from_engine(e: HashEngine) -> Self { - let mut hash = e.midstate_unchecked().bytes; - if hash == [0; 32] { - // Assume sha256 is secure and never generate 0-hashes (which represent invalid - // secp256k1 secret keys, causing downstream application breakage). - hash[0] = 1; + /// Finalize a hash engine to obtain a hash. + #[cfg(hashes_fuzz)] + pub fn from_engine(e: HashEngine) -> Self { + let mut hash = e.midstate_unchecked().bytes; + if hash == [0; 32] { + // Assume sha256 is secure and never generate 0-hashes (which represent invalid + // secp256k1 secret keys, causing downstream application breakage). + hash[0] = 1; + } + Hash(hash) } - Hash(hash) -} /// Iterate the sha256 algorithm to turn a sha256 hash into a sha256d hash #[must_use] diff --git a/hashes/src/sha256d/mod.rs b/hashes/src/sha256d/mod.rs index 8ec236e2c..add1935e1 100644 --- a/hashes/src/sha256d/mod.rs +++ b/hashes/src/sha256d/mod.rs @@ -11,15 +11,15 @@ crate::internal_macros::general_hash_type! { } impl Hash { -/// Finalize a hash engine to produce a hash. -pub fn from_engine(e: HashEngine) -> Self { - let sha2 = sha256::Hash::from_engine(e.0); - let sha2d = sha256::Hash::hash(sha2.as_byte_array()); + /// Finalize a hash engine to produce a hash. + pub fn from_engine(e: HashEngine) -> Self { + 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) -} + let mut ret = [0; 32]; + ret.copy_from_slice(sha2d.as_byte_array()); + Hash(ret) + } } /// Engine to compute SHA256d hash function. diff --git a/hashes/src/sha384/mod.rs b/hashes/src/sha384/mod.rs index e127e1b93..06d55e62e 100644 --- a/hashes/src/sha384/mod.rs +++ b/hashes/src/sha384/mod.rs @@ -11,12 +11,12 @@ crate::internal_macros::general_hash_type! { } impl Hash { -/// Finalize a hash engine to produce a hash. -pub fn from_engine(e: HashEngine) -> Self { - let mut ret = [0; 48]; - ret.copy_from_slice(&sha512::Hash::from_engine(e.0).as_byte_array()[..48]); - Hash(ret) -} + /// Finalize a hash engine to produce a hash. + pub fn from_engine(e: HashEngine) -> Self { + let mut ret = [0; 48]; + ret.copy_from_slice(&sha512::Hash::from_engine(e.0).as_byte_array()[..48]); + Hash(ret) + } } /// Engine to compute SHA384 hash function. diff --git a/hashes/src/sha512/mod.rs b/hashes/src/sha512/mod.rs index 91814488e..0f73f328e 100644 --- a/hashes/src/sha512/mod.rs +++ b/hashes/src/sha512/mod.rs @@ -21,35 +21,35 @@ crate::internal_macros::general_hash_type! { } impl Hash { -/// Finalize a hash engine to produce a hash. -#[cfg(not(hashes_fuzz))] -pub fn from_engine(mut e: HashEngine) -> Self { - // pad buffer with a single 1-bit then all 0s, until there are exactly 16 bytes remaining - let n_bytes_hashed = e.bytes_hashed; + /// Finalize a hash engine to produce a hash. + #[cfg(not(hashes_fuzz))] + pub fn from_engine(mut e: HashEngine) -> Self { + // pad buffer with a single 1-bit then all 0s, until there are exactly 16 bytes remaining + let n_bytes_hashed = e.bytes_hashed; - let zeroes = [0; BLOCK_SIZE - 16]; - e.input(&[0x80]); - if incomplete_block_len(&e) > zeroes.len() { - e.input(&zeroes); + let zeroes = [0; BLOCK_SIZE - 16]; + e.input(&[0x80]); + if incomplete_block_len(&e) > zeroes.len() { + e.input(&zeroes); + } + let pad_length = zeroes.len() - incomplete_block_len(&e); + e.input(&zeroes[..pad_length]); + debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); + + e.input(&[0; 8]); + e.input(&(8 * n_bytes_hashed).to_be_bytes()); + debug_assert_eq!(incomplete_block_len(&e), 0); + + Hash(e.midstate()) } - let pad_length = zeroes.len() - incomplete_block_len(&e); - e.input(&zeroes[..pad_length]); - debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); - e.input(&[0; 8]); - e.input(&(8 * n_bytes_hashed).to_be_bytes()); - debug_assert_eq!(incomplete_block_len(&e), 0); - - Hash(e.midstate()) -} - -/// Finalize a hash engine to produce a hash. -#[cfg(hashes_fuzz)] -pub fn from_engine(e: HashEngine) -> Self { - let mut hash = e.midstate(); - hash[0] ^= 0xff; // Make this distinct from SHA-256 - Hash(hash) -} + /// Finalize a hash engine to produce a hash. + #[cfg(hashes_fuzz)] + pub fn from_engine(e: HashEngine) -> Self { + let mut hash = e.midstate(); + hash[0] ^= 0xff; // Make this distinct from SHA-256 + Hash(hash) + } } pub(crate) const BLOCK_SIZE: usize = 128; diff --git a/hashes/src/sha512_256/mod.rs b/hashes/src/sha512_256/mod.rs index 7cd817820..291eac9f7 100644 --- a/hashes/src/sha512_256/mod.rs +++ b/hashes/src/sha512_256/mod.rs @@ -16,12 +16,12 @@ crate::internal_macros::general_hash_type! { } impl Hash { -/// Finalize a hash engine to produce a hash. -pub fn from_engine(e: HashEngine) -> Self { - let mut ret = [0; 32]; - ret.copy_from_slice(&sha512::Hash::from_engine(e.0).as_byte_array()[..32]); - Hash(ret) -} + /// Finalize a hash engine to produce a hash. + pub fn from_engine(e: HashEngine) -> Self { + let mut ret = [0; 32]; + ret.copy_from_slice(&sha512::Hash::from_engine(e.0).as_byte_array()[..32]); + Hash(ret) + } } /// Engine to compute SHA512/256 hash function.