From d094350230757afb3f0bf93cf49ea2a051153090 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 6 May 2024 13:42:08 +1000 Subject: [PATCH] hashes: Modify trait bounds Currently we require indexing trait bounds as well as `Borrow` on the `Hash` trait. We also already implement `AsRef`. It was observed that `Borrow<[u8]>` does not best describe what we want from the `Hash` trait implementor but rather `AsRef<[u8]>` does. Remove all the inexing trait bounds. Remove the `borrow::Borrow<[u8]>` trait bound. Add a `convert::AsRef<[u8]>` trait bound. This leaves the `Borrow<[u8]>` implementation for hashes created with `hash_newtype`, I'm not sure if this should be removed or not. --- bitcoin/src/bip32.rs | 8 +++---- bitcoin/src/psbt/map/input.rs | 2 +- hashes/src/hmac.rs | 39 +++++++---------------------------- hashes/src/lib.rs | 9 ++------ 4 files changed, 14 insertions(+), 44 deletions(-) diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 1014ffdc7..a913e44d4 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -45,7 +45,7 @@ impl_bytes_newtype!(ChainCode, 32); impl ChainCode { fn from_hmac(hmac: Hmac) -> Self { - hmac[32..].try_into().expect("half of hmac is guaranteed to be 32 bytes") + hmac.as_ref()[32..].try_into().expect("half of hmac is guaranteed to be 32 bytes") } } @@ -566,7 +566,7 @@ impl Xpriv { depth: 0, parent_fingerprint: Default::default(), child_number: ChildNumber::from_normal_idx(0)?, - private_key: secp256k1::SecretKey::from_slice(&hmac_result[..32])?, + private_key: secp256k1::SecretKey::from_slice(&hmac_result.as_ref()[..32])?, chain_code: ChainCode::from_hmac(hmac_result), }) } @@ -621,7 +621,7 @@ impl Xpriv { hmac_engine.input(&u32::from(i).to_be_bytes()); let hmac_result: Hmac = Hmac::from_engine(hmac_engine); - let sk = secp256k1::SecretKey::from_slice(&hmac_result[..32]) + let sk = secp256k1::SecretKey::from_slice(&hmac_result.as_ref()[..32]) .expect("statistically impossible to hit"); let tweaked = sk.add_tweak(&self.private_key.into()).expect("statistically impossible to hit"); @@ -742,7 +742,7 @@ impl Xpub { let hmac_result: Hmac = Hmac::from_engine(hmac_engine); - let private_key = secp256k1::SecretKey::from_slice(&hmac_result[..32])?; + let private_key = secp256k1::SecretKey::from_slice(&hmac_result.as_ref()[..32])?; let chain_code = ChainCode::from_hmac(hmac_result); Ok((private_key, chain_code)) } diff --git a/bitcoin/src/psbt/map/input.rs b/bitcoin/src/psbt/map/input.rs index 6c722f2fc..152c6f5b4 100644 --- a/bitcoin/src/psbt/map/input.rs +++ b/bitcoin/src/psbt/map/input.rs @@ -514,7 +514,7 @@ where if ::hash(&val) != key_val { return Err(psbt::Error::InvalidPreimageHashPair { preimage: val.into_boxed_slice(), - hash: Box::from(key_val.borrow()), + hash: Box::from(key_val.as_ref()), hash_type, }); } diff --git a/hashes/src/hmac.rs b/hashes/src/hmac.rs index 32b967a5f..53be1959e 100644 --- a/hashes/src/hmac.rs +++ b/hashes/src/hmac.rs @@ -8,7 +8,7 @@ //! Hash-based Message Authentication Code (HMAC). //! -use core::{borrow, fmt, ops, str}; +use core::{convert, fmt, str}; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -72,10 +72,10 @@ impl HmacEngine { if key.len() > T::Engine::BLOCK_SIZE { let hash = ::hash(key); - for (b_i, b_h) in ipad.iter_mut().zip(&hash[..]) { + for (b_i, b_h) in ipad.iter_mut().zip(hash.as_ref()) { *b_i ^= *b_h; } - for (b_o, b_h) in opad.iter_mut().zip(&hash[..]) { + for (b_o, b_h) in opad.iter_mut().zip(hash.as_ref()) { *b_o ^= *b_h; } } else { @@ -124,33 +124,8 @@ impl fmt::LowerHex for Hmac { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) } } -impl ops::Index for Hmac { - type Output = u8; - fn index(&self, index: usize) -> &u8 { &self.0[index] } -} - -impl ops::Index> for Hmac { - type Output = [u8]; - fn index(&self, index: ops::Range) -> &[u8] { &self.0[index] } -} - -impl ops::Index> for Hmac { - type Output = [u8]; - fn index(&self, index: ops::RangeFrom) -> &[u8] { &self.0[index] } -} - -impl ops::Index> for Hmac { - type Output = [u8]; - fn index(&self, index: ops::RangeTo) -> &[u8] { &self.0[index] } -} - -impl ops::Index for Hmac { - type Output = [u8]; - fn index(&self, index: ops::RangeFull) -> &[u8] { &self.0[index] } -} - -impl borrow::Borrow<[u8]> for Hmac { - fn borrow(&self) -> &[u8] { &self[..] } +impl convert::AsRef<[u8]> for Hmac { + fn as_ref(&self) -> &[u8] { self.0.as_ref() } } impl Hash for Hmac { @@ -159,7 +134,7 @@ impl Hash for Hmac { fn from_engine(mut e: HmacEngine) -> Hmac { let ihash = T::from_engine(e.iengine); - e.oengine.input(&ihash[..]); + e.oengine.input(ihash.as_ref()); let ohash = T::from_engine(e.oengine); Hmac(ohash) } @@ -318,7 +293,7 @@ mod tests { let mut engine = HmacEngine::::new(&test.key); engine.input(&test.input); let hash = Hmac::::from_engine(engine); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(hash.as_byte_array(), test.output.as_slice()); } } diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 3bfdbf623..1aa7a6b92 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -123,7 +123,7 @@ pub mod sha512; pub mod sha512_256; pub mod siphash24; -use core::{borrow, fmt, hash, ops}; +use core::{convert, fmt, hash}; pub use hmac::{Hmac, HmacEngine}; @@ -158,12 +158,7 @@ pub trait Hash: + fmt::Debug + fmt::Display + fmt::LowerHex - + ops::Index - + ops::Index, Output = [u8]> - + ops::Index, Output = [u8]> - + ops::Index, Output = [u8]> - + ops::Index - + borrow::Borrow<[u8]> + + convert::AsRef<[u8]> { /// A hashing engine which bytes can be serialized into. It is expected /// to implement the `io::Write` trait, and to never return errors under