diff --git a/embedded/src/main.rs b/embedded/src/main.rs index 447626c..6529376 100644 --- a/embedded/src/main.rs +++ b/embedded/src/main.rs @@ -45,7 +45,7 @@ fn main() -> ! { loop {} } -fn check_result(engine: sha256::HashEngine) { +fn check_result(engine: sha256::Engine) { let hash = TestType::from_engine(engine); let hash_check = diff --git a/extended_tests/schemars/src/main.rs b/extended_tests/schemars/src/main.rs index 6a1d18d..181fec8 100644 --- a/extended_tests/schemars/src/main.rs +++ b/extended_tests/schemars/src/main.rs @@ -88,10 +88,10 @@ mod tests { pub struct TestHashTag; impl sha256t::Tag for TestHashTag { - fn engine() -> sha256::HashEngine { + fn engine() -> sha256::Engine { // The TapRoot TapLeaf midstate. let midstate = sha256::Midstate::from_byte_array(TEST_MIDSTATE); - sha256::HashEngine::from_midstate(midstate, 64) + sha256::Engine::from_midstate(midstate, 64) } } diff --git a/src/hmac.rs b/src/hmac.rs index f1bb652..c96720f 100644 --- a/src/hmac.rs +++ b/src/hmac.rs @@ -12,7 +12,7 @@ use core::{fmt, ops, str}; use hex::DisplayHex; -use crate::FromSliceError; +use crate::{FromSliceError, HashEngine}; /// A hash computed from a RFC 2104 HMAC. Parameterized by the size of the underlying hash function. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -22,7 +22,7 @@ impl Hash { /// Returns a hash engine that is ready to be used for the data. pub fn engine() -> E where - E: crate::HashEngine, + E: HashEngine, { E::new() } @@ -32,7 +32,7 @@ impl Hash { /// This is equivalent to calling `Hash::from_byte_array(engine.finalize())`. pub fn from_engine(engine: E) -> Self where - E: crate::HashEngine, + E: HashEngine, { let digest = engine.finalize(); Self::from_byte_array(digest) @@ -183,7 +183,7 @@ impl<'de, const N: usize> crate::serde::Deserialize<'de> for Hash { } /// Pair of underlying hash midstates which represent the current state of an `HashEngine`. -pub struct MidState { +pub struct MidState { /// Midstate of the inner hash engine pub inner: E::Midstate, /// Midstate of the outer hash engine @@ -192,16 +192,16 @@ pub struct MidState { /// Pair of underlying hash engines, used for the inner and outer hash of HMAC. #[derive(Clone)] -pub struct HashEngine { +pub struct Engine { iengine: E, oengine: E, } -impl Default for HashEngine { - fn default() -> Self { HashEngine::new(&[]) } +impl Default for Engine { + fn default() -> Self { Engine::new(&[]) } } -impl HashEngine { +impl Engine { /// Constructs a new keyed HMAC from `key`. /// /// We only support hash engines whose internal block sizes are ≤ 128 bytes. @@ -209,15 +209,15 @@ impl HashEngine { /// # Panics /// /// Larger block sizes will result in a panic. - pub fn new(key: &[u8]) -> HashEngine { + pub fn new(key: &[u8]) -> Engine { debug_assert!(E::BLOCK_SIZE <= 128); let mut ipad = [0x36u8; 128]; let mut opad = [0x5cu8; 128]; - let mut ret = HashEngine { iengine: E::default(), oengine: E::default() }; + let mut ret = Engine { iengine: E::default(), oengine: E::default() }; if key.len() > E::BLOCK_SIZE { - let hash = ::hash(key); + let hash = ::hash(key); for (b_i, b_h) in ipad.iter_mut().zip(hash.as_ref()) { *b_i ^= *b_h; } @@ -239,7 +239,7 @@ impl HashEngine { } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = E::Digest; type Midstate = Midstate; const BLOCK_SIZE: usize = E::BLOCK_SIZE; @@ -264,7 +264,7 @@ impl crate::HashEngine for HashEngine { #[inline] fn from_midstate(midstate: Midstate, length: usize) -> Self { - HashEngine { + Engine { iengine: E::from_midstate(midstate.inner, length), oengine: E::from_midstate(midstate.outer, length), } @@ -274,7 +274,7 @@ impl crate::HashEngine for HashEngine { /// Pair of underlying hash engine midstates which represent the current state of an `HashEngine`. // TODO: Use derives? //#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)] -pub struct Midstate { +pub struct Midstate { /// Midstate of the inner hash engine. pub inner: E::Midstate, /// Midstate of the outer hash engine. @@ -402,7 +402,7 @@ mod tests { ]; for test in tests { - let mut engine = HashEngine::::new(&test.key); + let mut engine = Engine::::new(&test.key); engine.input(&test.input); let hash = engine.finalize(); assert_eq!(&hash[..], &test.output[..]); @@ -413,9 +413,10 @@ mod tests { #[cfg(feature = "serde")] #[test] fn hmac_sha512_serde() { - use crate::hmac; use serde_test::{assert_tokens, Configure, Token}; + use crate::hmac; + #[rustfmt::skip] static HASH_BYTES: [u8; 64] = [ 0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21, @@ -445,7 +446,7 @@ mod tests { use super::*; use crate::sha256; - let engine: HashEngine = Default::default(); + let engine: Engine = Default::default(); let hmac = Hash::from_engine(engine); let hint = hmac.0.iter().size_hint().1; @@ -457,11 +458,11 @@ mod tests { mod benches { use test::Bencher; - use crate::{sha256, HashEngine, HashEngine}; + use crate::{hmac, sha256, HashEngine, HashEngine}; #[bench] pub fn hmac_sha256_10(bh: &mut Bencher) { - let mut engine: HashEngine = Default::default(); + let mut engine: hmac::Engine = Default::default(); let bytes = [1u8; 10]; bh.iter(|| { engine.input(&bytes); @@ -471,7 +472,7 @@ mod benches { #[bench] pub fn hmac_sha256_1k(bh: &mut Bencher) { - let mut engine: HashEngine = Default::default(); + let mut engine: hmac::Engine = Default::default(); let bytes = [1u8; 1024]; bh.iter(|| { engine.input(&bytes); @@ -481,7 +482,7 @@ mod benches { #[bench] pub fn hmac_sha256_64k(bh: &mut Bencher) { - let mut engine: HashEngine = Default::default(); + let mut engine: hmac::Engine = Default::default(); let bytes = [1u8; 65536]; bh.iter(|| { engine.input(&bytes); diff --git a/src/impls.rs b/src/impls.rs index a9ebd9e..b1cd22c 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -7,11 +7,11 @@ use bitcoin_io::impl_write; -use crate::{hmac, ripemd160, sha1, sha256, sha256t, sha512, siphash24, HashEngine as _}; +use crate::{hmac, ripemd160, sha1, sha256, sha256t, sha512, siphash24, HashEngine}; impl_write!( - sha1::HashEngine, - |us: &mut sha1::HashEngine, buf| { + sha1::Engine, + |us: &mut sha1::Engine, buf| { us.input(buf); Ok(buf.len()) }, @@ -19,8 +19,8 @@ impl_write!( ); impl_write!( - sha256::HashEngine, - |us: &mut sha256::HashEngine, buf| { + sha256::Engine, + |us: &mut sha256::Engine, buf| { us.input(buf); Ok(buf.len()) }, @@ -28,8 +28,8 @@ impl_write!( ); impl_write!( - sha512::HashEngine, - |us: &mut sha512::HashEngine, buf| { + sha512::Engine, + |us: &mut sha512::Engine, buf| { us.input(buf); Ok(buf.len()) }, @@ -37,8 +37,8 @@ impl_write!( ); impl_write!( - ripemd160::HashEngine, - |us: &mut ripemd160::HashEngine, buf| { + ripemd160::Engine, + |us: &mut ripemd160::Engine, buf| { us.input(buf); Ok(buf.len()) }, @@ -46,18 +46,17 @@ impl_write!( ); impl_write!( - siphash24::HashEngine, - |us: &mut siphash24::HashEngine, buf| { + siphash24::Engine, + |us: &mut siphash24::Engine, buf| { us.input(buf); Ok(buf.len()) }, |_us| { Ok(()) } ); -impl bitcoin_io::Write for hmac::HashEngine { +impl bitcoin_io::Write for hmac::Engine { #[inline] fn write(&mut self, buf: &[u8]) -> Result { - use crate::HashEngine as _; self.input(buf); Ok(buf.len()) } @@ -67,7 +66,7 @@ impl bitcoin_io::Write for hmac::HashEngine { } #[cfg(feature = "std")] -impl std::io::Write for hmac::HashEngine { +impl std::io::Write for hmac::Engine { #[inline] fn write(&mut self, buf: &[u8]) -> std::io::Result { self.input(buf); @@ -79,8 +78,8 @@ impl std::io::Write for hmac::HashEngine { } impl_write!( - sha256t::HashEngine, - |us: &mut sha256t::HashEngine, buf| { + sha256t::Engine, + |us: &mut sha256t::Engine, buf| { us.input(buf); Ok(buf.len()) }, @@ -98,17 +97,17 @@ mod tests { ($mod:ident, $exp_empty:expr, $exp_256:expr, $exp_64k:expr,) => { #[test] fn $mod() { - let mut engine = $mod::HashEngine::new(); + let mut engine = $mod::Engine::new(); engine.write_all(&[]).unwrap(); let hash = $mod::Hash::from_engine(engine); assert_eq!(format!("{}", hash), $exp_empty); - let mut engine = $mod::HashEngine::new(); + let mut engine = $mod::Engine::new(); engine.write_all(&[1; 256]).unwrap(); let hash = $mod::Hash::from_engine(engine); assert_eq!(format!("{}", hash), $exp_256); - let mut engine = $mod::HashEngine::new(); + let mut engine = $mod::Engine::new(); engine.write_all(&[99; 64000]).unwrap(); let hash = $mod::Hash::from_engine(engine); assert_eq!(format!("{}", hash), $exp_64k); @@ -151,21 +150,21 @@ mod tests { #[test] fn hmac() { - let mut engine = hmac::HashEngine::::new(&[0xde, 0xad, 0xbe, 0xef]); + let mut engine = hmac::Engine::::new(&[0xde, 0xad, 0xbe, 0xef]); engine.write_all(&[]).unwrap(); assert_eq!( format!("{}", hmac::Hash::from_engine(engine)), "bf5515149cf797955c4d3194cca42472883281951697c8375d9d9b107f384225" ); - let mut engine = hmac::HashEngine::::new(&[0xde, 0xad, 0xbe, 0xef]); + let mut engine = hmac::Engine::::new(&[0xde, 0xad, 0xbe, 0xef]); engine.write_all(&[1; 256]).unwrap(); assert_eq!( format!("{}", hmac::Hash::from_engine(engine)), "59c9aca10c81c73cb4c196d94db741b6bf2050e0153d5a45f2526bff34675ac5" ); - let mut engine = hmac::HashEngine::::new(&[0xde, 0xad, 0xbe, 0xef]); + let mut engine = hmac::Engine::::new(&[0xde, 0xad, 0xbe, 0xef]); engine.write_all(&[99; 64000]).unwrap(); assert_eq!( format!("{}", hmac::Hash::from_engine(engine)), diff --git a/src/internal_macros.rs b/src/internal_macros.rs index 7a654b4..04a1407 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -162,12 +162,12 @@ macro_rules! hash_type { } /// Returns a hash engine that is ready to be used for data. - pub fn engine() -> HashEngine { HashEngine::new() } + pub fn engine() -> Engine { Engine::new() } /// Creates a `Hash` from an `engine`. /// /// This is equivalent to calling `Hash::from_byte_array(engine.finalize())`. - pub fn from_engine(engine: HashEngine) -> Self { + pub fn from_engine(engine: Engine) -> Self { let digest = engine.finalize(); Self(digest) } diff --git a/src/lib.rs b/src/lib.rs index 668a5b6..4caa767 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ //! #[cfg(std)] //! # fn main() -> std::io::Result<()> { //! let mut reader: &[u8] = b"hello"; // in real code, this could be a `File` or `TcpStream` -//! let mut engine = sha256::HashEngine::default(); +//! let mut engine = sha256::Engine::default(); //! std::io::copy(&mut reader, &mut engine)?; //! let hash = sha256::Hash::from_engine(engine); //! # Ok(()) @@ -58,7 +58,7 @@ //! let mut part1: &[u8] = b"hello"; //! let mut part2: &[u8] = b" "; //! let mut part3: &[u8] = b"world"; -//! let mut engine = sha256::HashEngine::default(); +//! let mut engine = sha256::Engine::default(); //! engine.write_all(part1)?; //! engine.write_all(part2)?; //! engine.write_all(part3)?; diff --git a/src/ripemd160.rs b/src/ripemd160.rs index 541a64e..e064084 100644 --- a/src/ripemd160.rs +++ b/src/ripemd160.rs @@ -4,7 +4,7 @@ use core::str; -use crate::HashEngine as _; +use crate::HashEngine; crate::internal_macros::hash_type! { 160, @@ -15,15 +15,15 @@ const BLOCK_SIZE: usize = 64; /// Engine to compute RIPEMD160 hash function. #[derive(Clone)] -pub struct HashEngine { +pub struct Engine { buffer: [u8; BLOCK_SIZE], h: [u32; 5], length: usize, } -impl Default for HashEngine { +impl Default for Engine { fn default() -> Self { - HashEngine { + Engine { h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0], length: 0, buffer: [0; BLOCK_SIZE], @@ -31,7 +31,7 @@ impl Default for HashEngine { } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 20]; type Midstate = [u8; 20]; const BLOCK_SIZE: usize = BLOCK_SIZE; @@ -87,7 +87,7 @@ impl crate::HashEngine for HashEngine { } #[inline] - fn from_midstate(midstate: Self::Midstate, length: usize) -> HashEngine { + fn from_midstate(midstate: Self::Midstate, length: usize) -> Engine { assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); let mut ret = [0; 5]; @@ -95,7 +95,7 @@ impl crate::HashEngine for HashEngine { *ret_val = u32::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice")); } - HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length } + Engine { buffer: [0; BLOCK_SIZE], h: ret, length } } } @@ -224,7 +224,7 @@ macro_rules! process_block( }); ); -impl HashEngine { +impl Engine { fn process_block(&mut self) { debug_assert_eq!(self.buffer.len(), BLOCK_SIZE); @@ -504,7 +504,7 @@ mod tests { ); // Hash through engine, checking that we can input byte by byte - let mut engine = ripemd160::HashEngine::new(); + let mut engine = ripemd160::Engine::new(); for ch in test.input.as_bytes() { engine.input(&[*ch]); } diff --git a/src/sha1.rs b/src/sha1.rs index 1f3d760..bf2ec8a 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -4,7 +4,7 @@ use core::str; -use crate::HashEngine as _; +use crate::HashEngine; crate::internal_macros::hash_type! { 160, @@ -15,15 +15,15 @@ const BLOCK_SIZE: usize = 64; /// Engine to compute SHA-1 hash function. #[derive(Clone)] -pub struct HashEngine { +pub struct Engine { buffer: [u8; BLOCK_SIZE], h: [u32; 5], length: usize, } -impl Default for HashEngine { +impl Default for Engine { fn default() -> Self { - HashEngine { + Engine { h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0], length: 0, buffer: [0; BLOCK_SIZE], @@ -31,7 +31,7 @@ impl Default for HashEngine { } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 20]; type Midstate = [u8; 20]; const BLOCK_SIZE: usize = BLOCK_SIZE; @@ -79,7 +79,7 @@ impl crate::HashEngine for HashEngine { } #[inline] - fn from_midstate(midstate: Self::Midstate, length: usize) -> HashEngine { + fn from_midstate(midstate: Self::Midstate, length: usize) -> Engine { assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); let mut ret = [0; 5]; @@ -87,11 +87,11 @@ impl crate::HashEngine for HashEngine { *ret_val = u32::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice")); } - HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length } + Engine { buffer: [0; BLOCK_SIZE], h: ret, length } } } -impl HashEngine { +impl Engine { // Basic unoptimized algorithm from Wikipedia fn process_block(&mut self) { debug_assert_eq!(self.buffer.len(), BLOCK_SIZE); @@ -197,7 +197,7 @@ mod tests { assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte - let mut engine = sha1::HashEngine::new(); + let mut engine = sha1::Engine::new(); for ch in test.input.as_bytes() { engine.input(&[*ch]); } diff --git a/src/sha256.rs b/src/sha256.rs index 692bf01..b089486 100644 --- a/src/sha256.rs +++ b/src/sha256.rs @@ -10,7 +10,7 @@ use core::ops::Index; use core::slice::SliceIndex; use core::str; -use crate::{FromSliceError, HashEngine as _}; +use crate::{FromSliceError, HashEngine}; crate::internal_macros::hash_type! { 256, @@ -22,15 +22,15 @@ pub const BLOCK_SIZE: usize = 64; /// Engine to compute SHA-256 hash function. #[derive(Clone)] -pub struct HashEngine { +pub struct Engine { buffer: [u8; BLOCK_SIZE], h: [u32; 8], length: usize, } -impl Default for HashEngine { +impl Default for Engine { fn default() -> Self { - HashEngine { + Engine { h: [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, @@ -41,7 +41,7 @@ impl Default for HashEngine { } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 32]; type Midstate = Midstate; const BLOCK_SIZE: usize = BLOCK_SIZE; @@ -101,7 +101,7 @@ impl crate::HashEngine for HashEngine { } #[inline] - fn from_midstate(midstate: Midstate, length: usize) -> HashEngine { + fn from_midstate(midstate: Midstate, length: usize) -> Engine { assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); let mut ret = [0; 8]; @@ -109,7 +109,7 @@ impl crate::HashEngine for HashEngine { *ret_val = u32::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice")); } - HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length } + Engine { buffer: [0; BLOCK_SIZE], h: ret, length } } } @@ -425,13 +425,13 @@ impl Midstate { } } -impl HashEngine { - /// Create a new [`HashEngine`] from a [`Midstate`]. +impl Engine { + /// Create a new [`Engine`] from a [`Midstate`]. /// /// # Panics /// /// If `length` is not a multiple of the block size. - pub fn from_midstate(midstate: Midstate, length: usize) -> HashEngine { + pub fn from_midstate(midstate: Midstate, length: usize) -> Engine { assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); let mut ret = [0; 8]; @@ -439,7 +439,7 @@ impl HashEngine { *ret_val = u32::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice")); } - HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length } + Engine { buffer: [0; BLOCK_SIZE], h: ret, length } } fn process_block(&mut self) { @@ -872,7 +872,7 @@ mod tests { assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte - let mut engine = sha256::HashEngine::new(); + let mut engine = sha256::Engine::new(); for ch in test.input.as_bytes() { engine.input(&[*ch]); } @@ -894,7 +894,7 @@ mod tests { #[rustfmt::skip] fn midstate() { // Test vector obtained by doing an asset issuance on Elements - let mut engine = sha256::HashEngine::new(); + let mut engine = sha256::Engine::new(); // sha256dhash of outpoint // 73828cbc65fd68ab78dc86992b76ae50ae2bf8ceedbe8de0483172f0886219f7:0 engine.input(&[ @@ -920,7 +920,7 @@ mod tests { #[test] fn engine_with_state() { let mut engine = sha256::Hash::engine(); - let midstate_engine = sha256::HashEngine::from_midstate(engine.midstate(), 0); + let midstate_engine = sha256::Engine::from_midstate(engine.midstate(), 0); // Fresh engine and engine initialized with fresh state should have same state assert_eq!(engine.h, midstate_engine.h); @@ -936,7 +936,7 @@ mod tests { for data in data_vec { let mut engine = engine.clone(); let mut midstate_engine = - sha256::HashEngine::from_midstate(engine.midstate(), engine.length); + sha256::Engine::from_midstate(engine.midstate(), engine.length); assert_eq!(engine.h, midstate_engine.h); assert_eq!(engine.length, midstate_engine.length); engine.input(&data); @@ -965,7 +965,7 @@ mod tests { 0x8f, 0xf1, 0xf7, 0xa9, 0xd5, 0x69, 0x09, 0x59, ]; let midstate_engine = - sha256::HashEngine::from_midstate(sha256::Midstate::from_byte_array(MIDSTATE), 64); + sha256::Engine::from_midstate(sha256::Midstate::from_byte_array(MIDSTATE), 64); let hash = sha256::Hash::from_engine(midstate_engine); assert_eq!(hash, sha256::Hash(HASH_EXPECTED)); } diff --git a/src/sha256t.rs b/src/sha256t.rs index 4482cc0..798c111 100644 --- a/src/sha256t.rs +++ b/src/sha256t.rs @@ -5,17 +5,17 @@ use core::cmp; use core::marker::PhantomData; -use crate::{sha256, HashEngine as _}; +use crate::{sha256, HashEngine}; /// Engine to compute tagged SHA-256 hash function. #[derive(Clone)] -pub struct HashEngine(sha256::HashEngine, PhantomData); +pub struct Engine(sha256::Engine, PhantomData); -impl Default for HashEngine { +impl Default for Engine { fn default() -> Self { ::engine() } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 32]; type Midstate = sha256::Midstate; const BLOCK_SIZE: usize = sha256::BLOCK_SIZE; @@ -36,8 +36,8 @@ impl crate::HashEngine for HashEngine { fn midstate(&self) -> Self::Midstate { self.0.midstate() } #[inline] - fn from_midstate(midstate: sha256::Midstate, length: usize) -> HashEngine { - let inner = sha256::HashEngine::from_midstate(midstate, length); + fn from_midstate(midstate: sha256::Midstate, length: usize) -> Engine { + let inner = sha256::Engine::from_midstate(midstate, length); Self(inner, PhantomData) } } @@ -45,7 +45,7 @@ impl crate::HashEngine for HashEngine { /// Trait representing a tag that can be used as a context for SHA256t hashes. pub trait Tag: Clone { /// Returns a hash engine that is pre-tagged and is ready to be used for the data. - fn engine() -> HashEngine; + fn engine() -> Engine; } /// Output of the SHA256t hash function. @@ -69,12 +69,12 @@ impl Hash { } /// Returns a hash engine that is ready to be used for data. - pub fn engine() -> HashEngine { ::engine() } + pub fn engine() -> Engine { ::engine() } /// Creates a `Hash` from an `engine`. /// /// This is equivalent to calling `Hash::from_byte_array(engine.finalize())`. - pub fn from_engine(engine: HashEngine) -> Self { + pub fn from_engine(engine: Engine) -> Self { let digest = engine.finalize(); Self(digest, PhantomData) } @@ -186,10 +186,10 @@ mod tests { pub struct TestTag; impl Tag for TestTag { - fn engine() -> HashEngine { + fn engine() -> Engine { let midstate = sha256::Midstate::from_byte_array(TEST_MIDSTATE); - let inner = sha256::HashEngine::from_midstate(midstate, 64); - HashEngine(inner, PhantomData) + let inner = sha256::Engine::from_midstate(midstate, 64); + Engine(inner, PhantomData) } } diff --git a/src/sha384.rs b/src/sha384.rs index 896b00b..72d9bea 100644 --- a/src/sha384.rs +++ b/src/sha384.rs @@ -4,7 +4,7 @@ use core::str; -use crate::{sha512, HashEngine as _}; +use crate::{sha512, HashEngine}; crate::internal_macros::hash_type! { 384, @@ -13,16 +13,16 @@ crate::internal_macros::hash_type! { /// Engine to compute SHA-384 hash function. #[derive(Clone)] -pub struct HashEngine(sha512::HashEngine); +pub struct Engine(sha512::Engine); -impl Default for HashEngine { +impl Default for Engine { #[rustfmt::skip] fn default() -> Self { - HashEngine(sha512::HashEngine::sha384()) + Engine(sha512::Engine::sha384()) } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 48]; type Midstate = [u8; 64]; // SHA-512 midstate. const BLOCK_SIZE: usize = sha512::BLOCK_SIZE; @@ -44,8 +44,8 @@ impl crate::HashEngine for HashEngine { fn midstate(&self) -> [u8; 64] { self.0.midstate() } #[inline] - fn from_midstate(midstate: [u8; 64], length: usize) -> HashEngine { - HashEngine(sha512::HashEngine::from_midstate(midstate, length)) + fn from_midstate(midstate: [u8; 64], length: usize) -> Engine { + Engine(sha512::Engine::from_midstate(midstate, length)) } } diff --git a/src/sha512.rs b/src/sha512.rs index d75e67e..7c73967 100644 --- a/src/sha512.rs +++ b/src/sha512.rs @@ -4,7 +4,7 @@ use core::str; -use crate::HashEngine as _; +use crate::HashEngine; crate::internal_macros::hash_type! { 512, @@ -15,16 +15,16 @@ pub(crate) const BLOCK_SIZE: usize = 128; /// Engine to compute SHA-512 hash function. #[derive(Clone)] -pub struct HashEngine { +pub struct Engine { h: [u64; 8], length: usize, buffer: [u8; BLOCK_SIZE], } -impl Default for HashEngine { +impl Default for Engine { #[rustfmt::skip] fn default() -> Self { - HashEngine { + Engine { h: [ 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, @@ -35,11 +35,11 @@ impl Default for HashEngine { } } -impl HashEngine { - /// Constructs a hash engine suitable for use inside the default `sha512_256::HashEngine`. +impl Engine { + /// Constructs a hash engine suitable for use inside the default `sha512_256::Engine`. #[rustfmt::skip] pub(crate) fn sha512_256() -> Self { - HashEngine { + Engine { h: [ 0x22312194fc2bf72c, 0x9f555fa3c84c64c2, 0x2393b86b6f53b151, 0x963877195940eabd, 0x96283ee2a88effe3, 0xbe5e1e2553863992, 0x2b0199fc2c85b8aa, 0x0eb72ddc81c52ca2, @@ -49,10 +49,10 @@ impl HashEngine { } } - /// Constructs a hash engine suitable for use inside the default `sha384::HashEngine`. + /// Constructs a hash engine suitable for use inside the default `sha384::Engine`. #[rustfmt::skip] pub(crate) fn sha384() -> Self { - HashEngine { + Engine { h: [ 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939, 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4, @@ -63,7 +63,7 @@ impl HashEngine { } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 64]; type Midstate = [u8; 64]; const BLOCK_SIZE: usize = BLOCK_SIZE; @@ -120,7 +120,7 @@ impl crate::HashEngine for HashEngine { } #[inline] - fn from_midstate(midstate: Self::Midstate, length: usize) -> HashEngine { + fn from_midstate(midstate: Self::Midstate, length: usize) -> Engine { assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); let mut ret = [0; 8]; @@ -128,7 +128,7 @@ impl crate::HashEngine for HashEngine { *ret_val = u64::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice")); } - HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length } + Engine { buffer: [0; BLOCK_SIZE], h: ret, length } } } @@ -199,7 +199,7 @@ mod fast_hash { ); } -impl HashEngine { +impl Engine { // Algorithm copied from libsecp256k1 pub(crate) fn process_block(&mut self) { debug_assert_eq!(self.buffer.len(), BLOCK_SIZE); @@ -319,7 +319,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test() { - use crate::{sha512, HashEngine}; + use super::*; + use crate::sha512; #[derive(Clone)] struct Test { diff --git a/src/sha512_256.rs b/src/sha512_256.rs index a4e7dd8..5a4380c 100644 --- a/src/sha512_256.rs +++ b/src/sha512_256.rs @@ -9,7 +9,7 @@ use core::str; -use crate::{sha512, HashEngine as _}; +use crate::{sha512, HashEngine}; crate::internal_macros::hash_type! { 256, @@ -23,16 +23,16 @@ crate::internal_macros::hash_type! { /// produces an entirely different hash compared to sha512. More information at /// . #[derive(Clone)] -pub struct HashEngine(sha512::HashEngine); +pub struct Engine(sha512::Engine); -impl Default for HashEngine { +impl Default for Engine { #[rustfmt::skip] fn default() -> Self { - HashEngine(sha512::HashEngine::sha512_256()) + Engine(sha512::Engine::sha512_256()) } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 32]; type Midstate = [u8; 64]; // SHA-512 midstate. const BLOCK_SIZE: usize = sha512::BLOCK_SIZE; @@ -55,8 +55,8 @@ impl crate::HashEngine for HashEngine { fn midstate(&self) -> [u8; 64] { self.0.midstate() } #[inline] - fn from_midstate(midstate: [u8; 64], length: usize) -> HashEngine { - HashEngine(sha512::HashEngine::from_midstate(midstate, length)) + fn from_midstate(midstate: [u8; 64], length: usize) -> Engine { + Engine(sha512::Engine::from_midstate(midstate, length)) } } diff --git a/src/siphash24.rs b/src/siphash24.rs index fccc2bb..2f93fe8 100644 --- a/src/siphash24.rs +++ b/src/siphash24.rs @@ -4,7 +4,7 @@ use core::{cmp, mem, ptr, str}; -use crate::HashEngine as _; +use crate::HashEngine; crate::internal_macros::hash_type! { 64, @@ -51,7 +51,7 @@ macro_rules! load_int_le { }}; } -/// Internal state of the [`HashEngine`]. +/// Internal state of the [`Engine`]. #[derive(Debug, Clone)] pub struct State { // v0, v2 and v1, v3 show up in pairs in the algorithm, @@ -66,7 +66,7 @@ pub struct State { /// Engine to compute the SipHash24 hash function. #[derive(Debug, Clone)] -pub struct HashEngine { +pub struct Engine { k0: u64, k1: u64, length: usize, // how many bytes we've processed @@ -75,11 +75,11 @@ pub struct HashEngine { ntail: usize, // how many bytes in tail are valid } -impl HashEngine { +impl Engine { /// Creates a new SipHash24 engine with keys. #[inline] - pub const fn with_keys(k0: u64, k1: u64) -> HashEngine { - HashEngine { + pub const fn with_keys(k0: u64, k1: u64) -> Engine { + Engine { k0, k1, length: 0, @@ -96,7 +96,7 @@ impl HashEngine { /// Creates a new SipHash24 engine. #[inline] - pub const fn new() -> HashEngine { HashEngine::with_keys(0, 0) } + pub const fn new() -> Engine { Engine::with_keys(0, 0) } /// Retrieves the keys of this engine. pub fn keys(&self) -> (u64, u64) { (self.k0, self.k1) } @@ -116,11 +116,11 @@ impl HashEngine { } } -impl Default for HashEngine { - fn default() -> Self { HashEngine::new() } +impl Default for Engine { + fn default() -> Self { Engine::new() } } -impl crate::HashEngine for HashEngine { +impl HashEngine for Engine { type Digest = [u8; 8]; type Midstate = State; const BLOCK_SIZE: usize = 32; // Unused in siphash. @@ -145,7 +145,7 @@ impl crate::HashEngine for HashEngine { return; } else { self.state.v3 ^= self.tail; - HashEngine::c_rounds(&mut self.state); + Engine::c_rounds(&mut self.state); self.state.v0 ^= self.tail; self.ntail = 0; } @@ -160,7 +160,7 @@ impl crate::HashEngine for HashEngine { let mi = unsafe { load_int_le!(data, i, u64) }; self.state.v3 ^= mi; - HashEngine::c_rounds(&mut self.state); + Engine::c_rounds(&mut self.state); self.state.v0 ^= mi; i += 8; @@ -178,11 +178,11 @@ impl crate::HashEngine for HashEngine { let b: u64 = ((self.length as u64 & 0xff) << 56) | self.tail; state.v3 ^= b; - HashEngine::c_rounds(&mut state); + Engine::c_rounds(&mut state); state.v0 ^= b; state.v2 ^= 0xff; - HashEngine::d_rounds(&mut state); + Engine::d_rounds(&mut state); let hash = state.v0 ^ state.v1 ^ state.v2 ^ state.v3; hash.to_le_bytes() @@ -199,39 +199,39 @@ impl crate::HashEngine for HashEngine { fn midstate(&self) -> State { self.state.clone() } #[inline] - fn from_midstate(midstate: Self::Midstate, _length: usize) -> HashEngine { - HashEngine { state: midstate, ..Default::default() } + fn from_midstate(midstate: Self::Midstate, _length: usize) -> Engine { + Engine { state: midstate, ..Default::default() } } } impl Hash { /// Hashes the given data with an engine with the provided keys. pub fn hash_with_keys(k0: u64, k1: u64, data: &[u8]) -> Hash { - let mut engine = HashEngine::with_keys(k0, k1); + let mut engine = Engine::with_keys(k0, k1); engine.input(data); Hash(engine.finalize()) } /// Hashes the given data directly to u64 with an engine with the provided keys. pub fn hash_to_u64_with_keys(k0: u64, k1: u64, data: &[u8]) -> u64 { - let mut engine = HashEngine::with_keys(k0, k1); + let mut engine = Engine::with_keys(k0, k1); engine.input(data); Hash::from_engine_to_u64(engine) } /// Produces a hash as `u64` from the current state of a given engine. #[inline] - pub fn from_engine_to_u64(e: HashEngine) -> u64 { + pub fn from_engine_to_u64(e: Engine) -> u64 { let mut state = e.state; let b: u64 = ((e.length as u64 & 0xff) << 56) | e.tail; state.v3 ^= b; - HashEngine::c_rounds(&mut state); + Engine::c_rounds(&mut state); state.v0 ^= b; state.v2 ^= 0xff; - HashEngine::d_rounds(&mut state); + Engine::d_rounds(&mut state); state.v0 ^ state.v1 ^ state.v2 ^ state.v3 } @@ -344,7 +344,7 @@ mod tests { let k0 = 0x_07_06_05_04_03_02_01_00; let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08; let mut vin = [0u8; 64]; - let mut state_inc = HashEngine::with_keys(k0, k1); + let mut state_inc = Engine::with_keys(k0, k1); for i in 0..64 { vin[i] = i as u8;