hashes: Run the formatter

Run `cargo +nightly fmt`, no other manual changes.
This commit is contained in:
Tobin C. Harding 2023-02-22 10:25:12 +11:00
parent 52c4579057
commit 913575ac91
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
18 changed files with 268 additions and 399 deletions

View File

@ -22,22 +22,30 @@ pub fn fixed_time_eq(a: &[u8], b: &[u8]) -> bool {
for i in 0..count {
let mut rs = unsafe { core::ptr::read_volatile(&r) };
rs |= lhs[i] ^ rhs[i];
unsafe { core::ptr::write_volatile(&mut r, rs); }
unsafe {
core::ptr::write_volatile(&mut r, rs);
}
}
{
let mut t = unsafe { core::ptr::read_volatile(&r) };
t |= t >> 4;
unsafe { core::ptr::write_volatile(&mut r, t); }
unsafe {
core::ptr::write_volatile(&mut r, t);
}
}
{
let mut t = unsafe { core::ptr::read_volatile(&r) };
t |= t >> 2;
unsafe { core::ptr::write_volatile(&mut r, t); }
unsafe {
core::ptr::write_volatile(&mut r, t);
}
}
{
let mut t = unsafe { core::ptr::read_volatile(&r) };
t |= t >> 1;
unsafe { core::ptr::write_volatile(&mut r, t); }
unsafe {
core::ptr::write_volatile(&mut r, t);
}
}
unsafe { (::core::ptr::read_volatile(&r) & 1) == 0 }
}
@ -83,78 +91,62 @@ fn eq_test() {
mod benches {
use test::Bencher;
use crate::{Hash, sha256, sha512};
use crate::cmp::fixed_time_eq;
use crate::{sha256, sha512, Hash};
#[bench]
fn bench_32b_constant_time_cmp_ne(bh: &mut Bencher) {
let hash_a = sha256::Hash::hash(&[0; 1]);
let hash_b = sha256::Hash::hash(&[1; 1]);
bh.iter(|| {
fixed_time_eq(&hash_a[..], &hash_b[..])
})
bh.iter(|| fixed_time_eq(&hash_a[..], &hash_b[..]))
}
#[bench]
fn bench_32b_slice_cmp_ne(bh: &mut Bencher) {
let hash_a = sha256::Hash::hash(&[0; 1]);
let hash_b = sha256::Hash::hash(&[1; 1]);
bh.iter(|| {
&hash_a[..] == &hash_b[..]
})
bh.iter(|| &hash_a[..] == &hash_b[..])
}
#[bench]
fn bench_32b_constant_time_cmp_eq(bh: &mut Bencher) {
let hash_a = sha256::Hash::hash(&[0; 1]);
let hash_b = sha256::Hash::hash(&[0; 1]);
bh.iter(|| {
fixed_time_eq(&hash_a[..], &hash_b[..])
})
bh.iter(|| fixed_time_eq(&hash_a[..], &hash_b[..]))
}
#[bench]
fn bench_32b_slice_cmp_eq(bh: &mut Bencher) {
let hash_a = sha256::Hash::hash(&[0; 1]);
let hash_b = sha256::Hash::hash(&[0; 1]);
bh.iter(|| {
&hash_a[..] == &hash_b[..]
})
bh.iter(|| &hash_a[..] == &hash_b[..])
}
#[bench]
fn bench_64b_constant_time_cmp_ne(bh: &mut Bencher) {
let hash_a = sha512::Hash::hash(&[0; 1]);
let hash_b = sha512::Hash::hash(&[1; 1]);
bh.iter(|| {
fixed_time_eq(&hash_a[..], &hash_b[..])
})
bh.iter(|| fixed_time_eq(&hash_a[..], &hash_b[..]))
}
#[bench]
fn bench_64b_slice_cmp_ne(bh: &mut Bencher) {
let hash_a = sha512::Hash::hash(&[0; 1]);
let hash_b = sha512::Hash::hash(&[1; 1]);
bh.iter(|| {
&hash_a[..] == &hash_b[..]
})
bh.iter(|| &hash_a[..] == &hash_b[..])
}
#[bench]
fn bench_64b_constant_time_cmp_eq(bh: &mut Bencher) {
let hash_a = sha512::Hash::hash(&[0; 1]);
let hash_b = sha512::Hash::hash(&[0; 1]);
bh.iter(|| {
fixed_time_eq(&hash_a[..], &hash_b[..])
})
bh.iter(|| fixed_time_eq(&hash_a[..], &hash_b[..]))
}
#[bench]
fn bench_64b_slice_cmp_eq(bh: &mut Bencher) {
let hash_a = sha512::Hash::hash(&[0; 1]);
let hash_b = sha512::Hash::hash(&[0; 1]);
bh.iter(|| {
&hash_a[..] == &hash_b[..]
})
bh.iter(|| &hash_a[..] == &hash_b[..])
}
}

View File

@ -29,7 +29,8 @@ impl fmt::Display for Error {
use self::Error::*;
match self {
InvalidLength(ref ell, ref ell2) => write!(f, "invalid slice length {} (expected {})", ell2, ell),
InvalidLength(ref ell, ref ell2) =>
write!(f, "invalid slice length {} (expected {})", ell2, ell),
}
}
}

View File

@ -20,11 +20,11 @@
//! HASH160 (SHA256 then RIPEMD160) implementation.
//!
use core::str;
use core::ops::Index;
use core::slice::SliceIndex;
use core::str;
use crate::{Error, ripemd160, sha256};
use crate::{ripemd160, sha256, Error};
crate::internal_macros::hash_type! {
160,
@ -105,7 +105,8 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn ripemd_serde() {
use serde_test::{Configure, Token, assert_tokens};
use serde_test::{assert_tokens, Configure, Token};
use crate::{hash160, Hash};
#[rustfmt::skip]
@ -127,7 +128,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, hash160};
use crate::{hash160, Hash, HashEngine};
#[bench]
pub fn hash160_10(bh: &mut Bencher) {

View File

@ -15,16 +15,15 @@
//! Hex encoding and decoding.
//!
#[cfg(all(feature = "alloc", not(feature = "std")))]
use crate::alloc::vec::Vec;
use core::{fmt, str};
#[cfg(feature = "std")]
use std::io;
#[cfg(all(feature = "core2", not(feature = "std")))]
use core2::io;
use core::{fmt, str};
#[cfg(all(feature = "alloc", not(feature = "std")))]
use crate::alloc::vec::Vec;
/// Hex decoding error.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -42,7 +41,8 @@ impl fmt::Display for Error {
match *self {
Error::InvalidChar(ch) => write!(f, "invalid hex character {}", ch),
Error::OddLengthString(ell) => write!(f, "odd hex string length {}", ell),
Error::InvalidLength(ell, ell2) => write!(f, "bad hex string length {} (expected {})", ell2, ell),
Error::InvalidLength(ell, ell2) =>
write!(f, "bad hex string length {} (expected {})", ell2, ell),
}
}
}
@ -67,9 +67,7 @@ pub trait FromHex: Sized {
I: Iterator<Item = Result<u8, Error>> + ExactSizeIterator + DoubleEndedIterator;
/// Produces an object from a hex string.
fn from_hex(s: &str) -> Result<Self, Error> {
Self::from_byte_iter(HexIterator::new(s)?)
}
fn from_hex(s: &str) -> Result<Self, Error> { Self::from_byte_iter(HexIterator::new(s)?) }
}
/// Iterator over a hex-encoded string slice which decodes hex and yields bytes.
@ -95,12 +93,8 @@ impl<'a> HexIterator<'a> {
}
fn chars_to_hex(hi: u8, lo: u8) -> Result<u8, Error> {
let hih = (hi as char)
.to_digit(16)
.ok_or(Error::InvalidChar(hi))?;
let loh = (lo as char)
.to_digit(16)
.ok_or(Error::InvalidChar(lo))?;
let hih = (hi as char).to_digit(16).ok_or(Error::InvalidChar(hi))?;
let loh = (lo as char).to_digit(16).ok_or(Error::InvalidChar(lo))?;
let ret = (hih << 4) + loh;
Ok(ret as u8)
@ -131,7 +125,7 @@ impl<'a> io::Read for HexIterator<'a> {
Some(Ok(src)) => {
*dst = src;
bytes_read += 1;
},
}
_ => break,
}
}
@ -178,7 +172,7 @@ macro_rules! impl_fromhex_array {
}
}
}
}
};
}
impl_fromhex_array!(2);
@ -204,9 +198,10 @@ impl_fromhex_array!(512);
#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
use super::*;
use internals::hex::exts::DisplayHex;
use super::*;
#[test]
fn hex_roundtrip() {
let expected = "0123456789abcdef";
@ -235,29 +230,11 @@ mod tests {
let badchar2 = "012Y456789abcdeb";
let badchar3 = "«23456789abcdef";
assert_eq!(
Vec::<u8>::from_hex(oddlen),
Err(Error::OddLengthString(17))
);
assert_eq!(
<[u8; 4]>::from_hex(oddlen),
Err(Error::OddLengthString(17))
);
assert_eq!(
<[u8; 8]>::from_hex(oddlen),
Err(Error::OddLengthString(17))
);
assert_eq!(
Vec::<u8>::from_hex(badchar1),
Err(Error::InvalidChar(b'Z'))
);
assert_eq!(
Vec::<u8>::from_hex(badchar2),
Err(Error::InvalidChar(b'Y'))
);
assert_eq!(
Vec::<u8>::from_hex(badchar3),
Err(Error::InvalidChar(194))
);
assert_eq!(Vec::<u8>::from_hex(oddlen), Err(Error::OddLengthString(17)));
assert_eq!(<[u8; 4]>::from_hex(oddlen), Err(Error::OddLengthString(17)));
assert_eq!(<[u8; 8]>::from_hex(oddlen), Err(Error::OddLengthString(17)));
assert_eq!(Vec::<u8>::from_hex(badchar1), Err(Error::InvalidChar(b'Z')));
assert_eq!(Vec::<u8>::from_hex(badchar2), Err(Error::InvalidChar(b'Y')));
assert_eq!(Vec::<u8>::from_hex(badchar3), Err(Error::InvalidChar(194)));
}
}

View File

@ -21,8 +21,9 @@
//!
use core::{borrow, fmt, ops, str};
#[cfg(feature = "serde")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::{Error, Hash, HashEngine};
@ -35,9 +36,7 @@ pub struct Hmac<T: Hash>(T);
impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> {
type Err = <T as str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Hmac(str::FromStr::from_str(s)?))
}
fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Hmac(str::FromStr::from_str(s)?)) }
}
/// Pair of underlying hash midstates which represent the current state of an `HmacEngine`.
@ -56,9 +55,7 @@ pub struct HmacEngine<T: Hash> {
}
impl<T: Hash> Default for HmacEngine<T> {
fn default() -> Self {
HmacEngine::new(&[])
}
fn default() -> Self { HmacEngine::new(&[]) }
}
impl<T: Hash> HmacEngine<T> {
@ -74,10 +71,7 @@ impl<T: Hash> HmacEngine<T> {
let mut ipad = [0x36u8; 128];
let mut opad = [0x5cu8; 128];
let mut ret = HmacEngine {
iengine: <T as Hash>::engine(),
oengine: <T as Hash>::engine(),
};
let mut ret = HmacEngine { iengine: <T as Hash>::engine(), oengine: <T as Hash>::engine() };
if key.len() > T::Engine::BLOCK_SIZE {
let hash = <T as Hash>::hash(key);
@ -103,10 +97,7 @@ impl<T: Hash> HmacEngine<T> {
/// A special constructor giving direct access to the underlying "inner" and "outer" engines.
pub fn from_inner_engines(iengine: T::Engine, oengine: T::Engine) -> HmacEngine<T> {
HmacEngine {
iengine,
oengine,
}
HmacEngine { iengine, oengine }
}
}
@ -114,80 +105,55 @@ impl<T: Hash> HashEngine for HmacEngine<T> {
type MidState = HmacMidState<T>;
fn midstate(&self) -> Self::MidState {
HmacMidState {
inner: self.iengine.midstate(),
outer: self.oengine.midstate(),
}
HmacMidState { inner: self.iengine.midstate(), outer: self.oengine.midstate() }
}
const BLOCK_SIZE: usize = T::Engine::BLOCK_SIZE;
fn n_bytes_hashed(&self) -> usize {
self.iengine.n_bytes_hashed()
}
fn n_bytes_hashed(&self) -> usize { self.iengine.n_bytes_hashed() }
fn input(&mut self, buf: &[u8]) {
self.iengine.input(buf)
}
fn input(&mut self, buf: &[u8]) { self.iengine.input(buf) }
}
impl<T: Hash> fmt::Debug for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&self.0, f) }
}
impl<T: Hash> fmt::Display for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}
impl<T: Hash> fmt::LowerHex for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
}
impl<T: Hash> ops::Index<usize> for Hmac<T> {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
&self.0[index]
}
fn index(&self, index: usize) -> &u8 { &self.0[index] }
}
impl<T: Hash> ops::Index<ops::Range<usize>> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::Range<usize>) -> &[u8] {
&self.0[index]
}
fn index(&self, index: ops::Range<usize>) -> &[u8] { &self.0[index] }
}
impl<T: Hash> ops::Index<ops::RangeFrom<usize>> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] {
&self.0[index]
}
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] { &self.0[index] }
}
impl<T: Hash> ops::Index<ops::RangeTo<usize>> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::RangeTo<usize>) -> &[u8] {
&self.0[index]
}
fn index(&self, index: ops::RangeTo<usize>) -> &[u8] { &self.0[index] }
}
impl<T: Hash> ops::Index<ops::RangeFull> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::RangeFull) -> &[u8] {
&self.0[index]
}
fn index(&self, index: ops::RangeFull) -> &[u8] { &self.0[index] }
}
impl<T: Hash> borrow::Borrow<[u8]> for Hmac<T> {
fn borrow(&self) -> &[u8] {
&self[..]
}
fn borrow(&self) -> &[u8] { &self[..] }
}
impl<T: Hash> Hash for Hmac<T> {
@ -203,21 +169,13 @@ impl<T: Hash> Hash for Hmac<T> {
const LEN: usize = T::LEN;
fn from_slice(sl: &[u8]) -> Result<Hmac<T>, Error> {
T::from_slice(sl).map(Hmac)
}
fn from_slice(sl: &[u8]) -> Result<Hmac<T>, Error> { T::from_slice(sl).map(Hmac) }
fn to_byte_array(self) -> Self::Bytes {
self.0.to_byte_array()
}
fn to_byte_array(self) -> Self::Bytes { self.0.to_byte_array() }
fn as_byte_array(&self) -> &Self::Bytes {
self.0.as_byte_array()
}
fn as_byte_array(&self) -> &Self::Bytes { self.0.as_byte_array() }
fn from_byte_array(bytes: T::Bytes) -> Self {
Hmac(T::from_byte_array(bytes))
}
fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) }
fn all_zeros() -> Self {
let zeros = T::all_zeros();
@ -247,7 +205,7 @@ mod tests {
#[test]
#[cfg(feature = "alloc")]
fn test() {
use crate::{sha256, HashEngine, HmacEngine, Hash, Hmac};
use crate::{sha256, Hash, HashEngine, Hmac, HmacEngine};
#[derive(Clone)]
struct Test {
@ -373,7 +331,8 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn hmac_sha512_serde() {
use serde_test::{Configure, Token, assert_tokens};
use serde_test::{assert_tokens, Configure, Token};
use crate::{sha512, Hash, Hmac};
#[rustfmt::skip]
@ -394,7 +353,7 @@ mod tests {
&hash.readable(),
&[Token::Str(
"8b41e1b78ad11521113c52ff182a1b8e0a195754aa527fcd00a411620b46f20f\
fffb8088ccf85497121ad4499e0845b876f6dd6640088a2f0b2d8a600bdf4c0c"
fffb8088ccf85497121ad4499e0845b876f6dd6640088a2f0b2d8a600bdf4c0c",
)],
);
}
@ -404,7 +363,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hmac, Hash, HashEngine, sha256};
use crate::{sha256, Hash, HashEngine, Hmac};
#[bench]
pub fn hmac_sha256_10(bh: &mut Bencher) {
@ -435,5 +394,4 @@ mod benches {
});
bh.bytes = bytes.len() as u64;
}
}

View File

@ -23,7 +23,7 @@ use std::io;
#[cfg(not(feature = "std"))]
use core2::io;
use crate::{HashEngine, sha1, sha256, sha512, ripemd160, siphash24, hmac};
use crate::{hmac, ripemd160, sha1, sha256, sha512, siphash24, HashEngine};
impl io::Write for sha1::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
@ -82,8 +82,7 @@ impl<T: crate::Hash> io::Write for hmac::HmacEngine<T> {
#[cfg(test)]
mod tests {
use super::io::Write;
use crate::{Hash, sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24, hmac};
use crate::{hash160, hmac, ripemd160, sha1, sha256, sha256d, sha512, siphash24, Hash};
macro_rules! write_test {
($mod:ident, $exp_empty:expr, $exp_256:expr, $exp_64k:expr,) => {
@ -91,26 +90,17 @@ mod tests {
fn $mod() {
let mut engine = $mod::Hash::engine();
engine.write_all(&[]).unwrap();
assert_eq!(
format!("{}", $mod::Hash::from_engine(engine)),
$exp_empty
);
assert_eq!(format!("{}", $mod::Hash::from_engine(engine)), $exp_empty);
let mut engine = $mod::Hash::engine();
engine.write_all(&[1; 256]).unwrap();
assert_eq!(
format!("{}", $mod::Hash::from_engine(engine)),
$exp_256
);
assert_eq!(format!("{}", $mod::Hash::from_engine(engine)), $exp_256);
let mut engine = $mod::Hash::engine();
engine.write_all(&[99; 64000]).unwrap();
assert_eq!(
format!("{}", $mod::Hash::from_engine(engine)),
$exp_64k
);
}
assert_eq!(format!("{}", $mod::Hash::from_engine(engine)), $exp_64k);
}
};
}
write_test!(
@ -158,12 +148,7 @@ mod tests {
"a9608c952c8dbcc20c53803d2ca5ad31d64d9313",
);
write_test!(
siphash24,
"d70077739d4b921e",
"3a3ccefde9b5b1e3",
"ce456e4e4ecbc5bf",
);
write_test!(siphash24, "d70077739d4b921e", "3a3ccefde9b5b1e3", "ce456e4e4ecbc5bf",);
#[test]
fn hmac() {

View File

@ -186,21 +186,16 @@ macro_rules! hash_type {
#[cfg_attr(feature = "schemars", derive(crate::schemars::JsonSchema))]
#[repr(transparent)]
pub struct Hash(
#[cfg_attr(feature = "schemars", schemars(schema_with = $schemars))]
[u8; $bits / 8]
#[cfg_attr(feature = "schemars", schemars(schema_with = $schemars))] [u8; $bits / 8],
);
impl Hash {
fn internal_new(arr: [u8; $bits / 8]) -> Self {
Hash(arr)
}
fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) }
fn internal_engine() -> HashEngine {
Default::default()
}
fn internal_engine() -> HashEngine { Default::default() }
}
crate::internal_macros::hash_trait_impls!($bits, $reverse);
}
};
}
pub(crate) use hash_type;

View File

@ -79,28 +79,30 @@
// Coding conventions
#![warn(missing_docs)]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(bench, feature(test))]
// In general, rust is absolutely horrid at supporting users doing things like,
// for example, compiling Rust code for real environments. Disable useless lints
// that don't do anything but annoy us and cant actually ever be resolved.
#![allow(bare_trait_objects)]
#![allow(ellipsis_inclusive_range_patterns)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
// Instead of littering the codebase for non-fuzzing code just globally allow.
#![cfg_attr(fuzzing, allow(dead_code, unused_imports))]
#[cfg(bench)] extern crate test;
#[cfg(any(test, feature = "std"))] extern crate core;
#[cfg(feature = "core2")] extern crate core2;
#[cfg(all(feature = "alloc", not(feature = "std")))] extern crate alloc;
#[cfg(feature = "serde")] pub extern crate serde;
#[cfg(all(test,feature = "serde"))] extern crate serde_test;
#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;
#[cfg(any(test, feature = "std"))]
extern crate core;
#[cfg(feature = "core2")]
extern crate core2;
#[cfg(feature = "serde")]
pub extern crate serde;
#[cfg(all(test, feature = "serde"))]
extern crate serde_test;
#[cfg(bench)]
extern crate test;
#[doc(hidden)]
pub mod _export {
@ -114,27 +116,30 @@ pub mod _export {
extern crate actual_schemars as schemars;
mod internal_macros;
#[macro_use] mod util;
#[macro_use] pub mod serde_macros;
#[cfg(any(feature = "std", feature = "core2"))] mod impls;
#[macro_use]
mod util;
#[macro_use]
pub mod serde_macros;
pub mod cmp;
pub mod error;
pub mod hex;
pub mod hash160;
pub mod hex;
pub mod hmac;
#[cfg(any(feature = "std", feature = "core2"))]
mod impls;
pub mod ripemd160;
pub mod sha1;
pub mod sha256;
pub mod sha256d;
pub mod sha256t;
pub mod siphash24;
pub mod sha512;
pub mod sha512_256;
pub mod cmp;
pub mod siphash24;
use core::{borrow, fmt, hash, ops};
pub use hmac::{Hmac, HmacEngine};
pub use error::Error;
pub use hmac::{Hmac, HmacEngine};
/// A hashing engine which bytes can be serialized into.
pub trait HashEngine: Clone + Default {
@ -156,14 +161,23 @@ pub trait HashEngine: Clone + Default {
}
/// Trait which applies to hashes of all types.
pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord +
hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex +
ops::Index<ops::RangeFull, Output = [u8]> +
ops::Index<ops::RangeFrom<usize>, Output = [u8]> +
ops::Index<ops::RangeTo<usize>, Output = [u8]> +
ops::Index<ops::Range<usize>, Output = [u8]> +
ops::Index<usize, Output = u8> +
borrow::Borrow<[u8]>
pub trait Hash:
Copy
+ Clone
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ hash::Hash
+ fmt::Debug
+ fmt::Display
+ fmt::LowerHex
+ ops::Index<ops::RangeFull, Output = [u8]>
+ ops::Index<ops::RangeFrom<usize>, Output = [u8]>
+ ops::Index<ops::RangeTo<usize>, Output = [u8]>
+ ops::Index<ops::Range<usize>, Output = [u8]>
+ ops::Index<usize, Output = u8>
+ borrow::Borrow<[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
@ -174,9 +188,7 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord +
type Bytes: hex::FromHex + Copy;
/// Constructs a new engine.
fn engine() -> Self::Engine {
Self::Engine::default()
}
fn engine() -> Self::Engine { Self::Engine::default() }
/// Produces a hash from the current state of a given engine.
fn from_engine(e: Self::Engine) -> Self;
@ -218,7 +230,7 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord +
#[cfg(test)]
mod tests {
use crate::{Hash, sha256d};
use crate::{sha256d, Hash};
hash_newtype! {
/// A test newtype

View File

@ -20,10 +20,10 @@
//! RIPEMD160 implementation.
//!
use core::{cmp, str};
use core::convert::TryInto;
use core::ops::Index;
use core::slice::SliceIndex;
use core::{cmp, str};
use crate::{Error, HashEngine as _};
@ -102,9 +102,7 @@ impl crate::HashEngine for HashEngine {
const BLOCK_SIZE: usize = 64;
fn n_bytes_hashed(&self) -> usize {
self.length
}
fn n_bytes_hashed(&self) -> usize { self.length }
engine_input_impl!();
}
@ -407,7 +405,7 @@ mod tests {
#[test]
#[cfg(feature = "alloc")]
fn test() {
use crate::{Hash, HashEngine, ripemd160};
use crate::{ripemd160, Hash, HashEngine};
#[derive(Clone)]
struct Test {
@ -488,7 +486,8 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn ripemd_serde() {
use serde_test::{Configure, Token, assert_tokens};
use serde_test::{assert_tokens, Configure, Token};
use crate::{ripemd160, Hash};
#[rustfmt::skip]
@ -510,7 +509,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, ripemd160};
use crate::{ripemd160, Hash, HashEngine};
#[bench]
pub fn ripemd160_10(bh: &mut Bencher) {
@ -541,5 +540,4 @@ mod benches {
});
bh.bytes = bytes.len() as u64;
}
}

View File

@ -19,13 +19,13 @@
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub mod serde_details {
use crate::Error;
use core::marker::PhantomData;
use core::{fmt, ops, str};
use core::str::FromStr;
use core::{fmt, ops, str};
use crate::Error;
struct HexVisitor<ValueT>(PhantomData<ValueT>);
use serde::{de, Serializer, Deserializer};
use serde::{de, Deserializer, Serializer};
impl<'de, ValueT> de::Visitor<'de> for HexVisitor<ValueT>
where
@ -45,10 +45,7 @@ pub mod serde_details {
if let Ok(hex) = str::from_utf8(v) {
Self::Value::from_str(hex).map_err(E::custom)
} else {
return Err(E::invalid_value(
de::Unexpected::Bytes(v),
&self,
));
return Err(E::invalid_value(de::Unexpected::Bytes(v), &self));
}
}

View File

@ -15,10 +15,10 @@
//! SHA1 implementation.
//!
use core::{cmp, str};
use core::convert::TryInto;
use core::ops::Index;
use core::slice::SliceIndex;
use core::{cmp, str};
use crate::{Error, HashEngine as _};
@ -89,9 +89,7 @@ impl crate::HashEngine for HashEngine {
const BLOCK_SIZE: usize = 64;
fn n_bytes_hashed(&self) -> usize {
self.length
}
fn n_bytes_hashed(&self) -> usize { self.length }
engine_input_impl!();
}
@ -121,10 +119,11 @@ impl HashEngine {
20...39 => (b ^ c ^ d, 0x6ed9eba1),
40...59 => ((b & c) | (b & d) | (c & d), 0x8f1bbcdc),
60...79 => (b ^ c ^ d, 0xca62c1d6),
_ => unreachable!()
_ => unreachable!(),
};
let new_a = a.rotate_left(5).wrapping_add(f).wrapping_add(e).wrapping_add(k).wrapping_add(wi);
let new_a =
a.rotate_left(5).wrapping_add(f).wrapping_add(e).wrapping_add(k).wrapping_add(wi);
e = d;
d = c;
c = b.rotate_left(30);
@ -213,7 +212,8 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn sha1_serde() {
use serde_test::{Configure, Token, assert_tokens};
use serde_test::{assert_tokens, Configure, Token};
use crate::{sha1, Hash};
#[rustfmt::skip]
@ -235,7 +235,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, sha1};
use crate::{sha1, Hash, HashEngine};
#[bench]
pub fn sha1_10(bh: &mut Bencher) {
@ -266,5 +266,4 @@ mod benches {
});
bh.bytes = bytes.len() as u64;
}
}

View File

@ -15,12 +15,12 @@
//! SHA256 implementation.
//!
use core::{cmp, str};
use core::convert::TryInto;
use core::ops::Index;
use core::slice::SliceIndex;
use core::{cmp, str};
use crate::{Error, HashEngine as _, hex, sha256d};
use crate::{hex, sha256d, Error, HashEngine as _};
crate::internal_macros::hash_type! {
256,
@ -73,7 +73,10 @@ pub struct HashEngine {
impl Default for HashEngine {
fn default() -> Self {
HashEngine {
h: [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19],
h: [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
],
length: 0,
buffer: [0; BLOCK_SIZE],
}
@ -101,9 +104,7 @@ impl crate::HashEngine for HashEngine {
const BLOCK_SIZE: usize = 64;
fn n_bytes_hashed(&self) -> usize {
self.length
}
fn n_bytes_hashed(&self) -> usize { self.length }
engine_input_impl!();
}
@ -127,16 +128,12 @@ impl<I: SliceIndex<[u8]>> Index<I> for Midstate {
type Output = I::Output;
#[inline]
fn index(&self, index: I) -> &Self::Output {
&self.0[index]
}
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
}
impl str::FromStr for Midstate {
type Err = hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
hex::FromHex::from_hex(s)
}
fn from_str(s: &str) -> Result<Self, Self::Err> { hex::FromHex::from_hex(s) }
}
impl Midstate {
@ -149,9 +146,7 @@ impl Midstate {
const DISPLAY_BACKWARD: bool = true;
/// Construct a new [`Midstate`] from the inner value.
pub fn from_byte_array(inner: [u8; 32]) -> Self {
Midstate(inner)
}
pub fn from_byte_array(inner: [u8; 32]) -> Self { Midstate(inner) }
/// Copies a byte slice into the [`Midstate`] object.
pub fn from_slice(sl: &[u8]) -> Result<Midstate, Error> {
@ -165,9 +160,7 @@ impl Midstate {
}
/// Unwraps the [`Midstate`] and returns the underlying byte array.
pub fn to_byte_array(self) -> [u8; 32] {
self.0
}
pub fn to_byte_array(self) -> [u8; 32] { self.0 }
}
impl hex::FromHex for Midstate {
@ -182,7 +175,8 @@ impl hex::FromHex for Midstate {
macro_rules! Ch( ($x:expr, $y:expr, $z:expr) => ($z ^ ($x & ($y ^ $z))) );
macro_rules! Maj( ($x:expr, $y:expr, $z:expr) => (($x & $y) | ($z & ($x | $y))) );
macro_rules! Sigma0( ($x:expr) => ($x.rotate_left(30) ^ $x.rotate_left(19) ^ $x.rotate_left(10)) ); macro_rules! Sigma1( ($x:expr) => ( $x.rotate_left(26) ^ $x.rotate_left(21) ^ $x.rotate_left(7)) );
macro_rules! Sigma0( ($x:expr) => ($x.rotate_left(30) ^ $x.rotate_left(19) ^ $x.rotate_left(10)) );
macro_rules! Sigma1( ($x:expr) => ( $x.rotate_left(26) ^ $x.rotate_left(21) ^ $x.rotate_left(7)) );
macro_rules! sigma0( ($x:expr) => ($x.rotate_left(25) ^ $x.rotate_left(14) ^ ($x >> 3)) );
macro_rules! sigma1( ($x:expr) => ($x.rotate_left(15) ^ $x.rotate_left(13) ^ ($x >> 10)) );
@ -215,11 +209,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,
}
HashEngine { buffer: [0; BLOCK_SIZE], h: ret, length }
}
// Algorithm copied from libsecp256k1
@ -321,7 +311,7 @@ impl HashEngine {
#[cfg(test)]
mod tests {
use crate::{Hash, HashEngine, sha256};
use crate::{sha256, Hash, HashEngine};
#[test]
#[cfg(feature = "alloc")]
@ -469,7 +459,7 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn sha256_serde() {
use serde_test::{Configure, Token, assert_tokens};
use serde_test::{assert_tokens, Configure, Token};
#[rustfmt::skip]
static HASH_BYTES: [u8; 32] = [
@ -481,14 +471,17 @@ mod tests {
let hash = sha256::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
assert_tokens(&hash.readable(), &[Token::Str("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")]);
assert_tokens(
&hash.readable(),
&[Token::Str("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")],
);
}
#[cfg(target_arch = "wasm32")]
mod wasm_tests {
extern crate wasm_bindgen_test;
use super::*;
use self::wasm_bindgen_test::*;
use super::*;
#[wasm_bindgen_test]
fn sha256_tests() {
test();
@ -502,7 +495,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, sha256};
use crate::{sha256, Hash, HashEngine};
#[bench]
pub fn sha256_10(bh: &mut Bencher) {
@ -533,5 +526,4 @@ mod benches {
});
bh.bytes = bytes.len() as u64;
}
}

View File

@ -15,11 +15,11 @@
//! SHA256d implementation (double SHA256).
//!
use core::str;
use core::ops::Index;
use core::slice::SliceIndex;
use core::str;
use crate::{Error, sha256};
use crate::{sha256, Error};
crate::internal_macros::hash_type! {
256,
@ -97,7 +97,8 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn sha256_serde() {
use serde_test::{Configure, Token, assert_tokens};
use serde_test::{assert_tokens, Configure, Token};
use crate::{sha256d, Hash};
#[rustfmt::skip]
@ -110,7 +111,10 @@ mod tests {
let hash = sha256d::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
assert_tokens(&hash.readable(), &[Token::Str("6cfb35868c4465b7c289d7d5641563aa973db6a929655282a7bf95c8257f53ef")]);
assert_tokens(
&hash.readable(),
&[Token::Str("6cfb35868c4465b7c289d7d5641563aa973db6a929655282a7bf95c8257f53ef")],
);
}
}
@ -118,7 +122,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, sha256d};
use crate::{sha256d, Hash, HashEngine};
#[bench]
pub fn sha256d_10(bh: &mut Bencher) {

View File

@ -15,12 +15,12 @@
//! SHA256t implementation (tagged SHA256).
//!
use core::{cmp, str};
use core::marker::PhantomData;
use core::ops::Index;
use core::slice::SliceIndex;
use core::{cmp, str};
use crate::{Error, sha256};
use crate::{sha256, Error};
type HashEngine = sha256::HashEngine;
@ -34,38 +34,30 @@ pub trait Tag {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[repr(transparent)]
pub struct Hash<T: Tag>(
#[cfg_attr(feature = "schemars", schemars(schema_with = "crate::util::json_hex_string::len_32"))]
#[cfg_attr(
feature = "schemars",
schemars(schema_with = "crate::util::json_hex_string::len_32")
)]
[u8; 32],
#[cfg_attr(feature = "schemars", schemars(skip))]
PhantomData<T>
#[cfg_attr(feature = "schemars", schemars(skip))] PhantomData<T>,
);
impl<T: Tag> Hash<T> {
fn internal_new(arr: [u8; 32]) -> Self {
Hash(arr, Default::default())
}
fn internal_new(arr: [u8; 32]) -> Self { Hash(arr, Default::default()) }
fn internal_engine() -> HashEngine {
T::engine()
}
fn internal_engine() -> HashEngine { T::engine() }
}
impl<T: Tag> Copy for Hash<T> {}
impl<T: Tag> Clone for Hash<T> {
fn clone(&self) -> Self {
Hash(self.0, self.1)
}
fn clone(&self) -> Self { Hash(self.0, self.1) }
}
impl<T: Tag> PartialEq for Hash<T> {
fn eq(&self, other: &Hash<T>) -> bool {
self.0 == other.0
}
fn eq(&self, other: &Hash<T>) -> bool { self.0 == other.0 }
}
impl<T: Tag> Eq for Hash<T> {}
impl<T: Tag> Default for Hash<T> {
fn default() -> Self {
Hash([0; 32], PhantomData)
}
fn default() -> Self { Hash([0; 32], PhantomData) }
}
impl<T: Tag> PartialOrd for Hash<T> {
fn partial_cmp(&self, other: &Hash<T>) -> Option<cmp::Ordering> {
@ -73,14 +65,10 @@ impl<T: Tag> PartialOrd for Hash<T> {
}
}
impl<T: Tag> Ord for Hash<T> {
fn cmp(&self, other: &Hash<T>) -> cmp::Ordering {
cmp::Ord::cmp(&self.0, &other.0)
}
fn cmp(&self, other: &Hash<T>) -> cmp::Ordering { cmp::Ord::cmp(&self.0, &other.0) }
}
impl<T: Tag> core::hash::Hash for Hash<T> {
fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
self.0.hash(h)
}
fn hash<H: core::hash::Hasher>(&self, h: &mut H) { self.0.hash(h) }
}
crate::internal_macros::hash_trait_impls!(256, true, T: Tag);
@ -98,7 +86,15 @@ fn from_engine<T: Tag>(e: sha256::HashEngine) -> Hash<T> {
#[macro_export]
macro_rules! sha256t_hash_newtype {
($newtype:ident, $tag:ident, $midstate:ident, $midstate_len:expr, $docs:meta, $direction:tt) => {
sha256t_hash_newtype!($newtype, $tag, $midstate, $midstate_len, $docs, $direction, stringify!($newtype));
sha256t_hash_newtype!(
$newtype,
$tag,
$midstate,
$midstate_len,
$docs,
$direction,
stringify!($newtype)
);
};
($newtype:ident, $tag:ident, $midstate:ident, $midstate_len:expr, $docs:meta, $direction:tt, $sname:expr) => {
@ -125,13 +121,13 @@ macro_rules! sha256t_hash_newtype {
#[cfg(test)]
mod tests {
use crate::{sha256, sha256t};
#[cfg(feature = "alloc")]
use crate::Hash;
use crate::{sha256, sha256t};
const TEST_MIDSTATE: [u8; 32] = [
156, 224, 228, 230, 124, 17, 108, 57, 56, 179, 202, 242, 195, 15, 80, 137, 211, 243,
147, 108, 71, 99, 110, 96, 125, 179, 62, 234, 221, 198, 240, 201,
156, 224, 228, 230, 124, 17, 108, 57, 56, 179, 202, 242, 195, 15, 80, 137, 211, 243, 147,
108, 71, 99, 110, 96, 125, 179, 62, 234, 221, 198, 240, 201,
];
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]

View File

@ -20,10 +20,10 @@
//! SHA512 implementation.
//!
use core::{cmp, hash, str};
use core::convert::TryInto;
use core::ops::Index;
use core::slice::SliceIndex;
use core::{cmp, hash, str};
use crate::{Error, HashEngine as _};
@ -74,9 +74,7 @@ impl crate::HashEngine for HashEngine {
const BLOCK_SIZE: usize = 128;
fn n_bytes_hashed(&self) -> usize {
self.length
}
fn n_bytes_hashed(&self) -> usize { self.length }
engine_input_impl!();
}
@ -85,18 +83,17 @@ impl crate::HashEngine for HashEngine {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[repr(transparent)]
pub struct Hash(
#[cfg_attr(feature = "schemars", schemars(schema_with = "crate::util::json_hex_string::len_64"))]
[u8; 64]
#[cfg_attr(
feature = "schemars",
schemars(schema_with = "crate::util::json_hex_string::len_64")
)]
[u8; 64],
);
impl Hash {
fn internal_new(arr: [u8; 64]) -> Self {
Hash(arr)
}
fn internal_new(arr: [u8; 64]) -> Self { Hash(arr) }
fn internal_engine() -> HashEngine {
Default::default()
}
fn internal_engine() -> HashEngine { Default::default() }
}
impl Copy for Hash {}
@ -110,35 +107,25 @@ impl Clone for Hash {
}
impl PartialEq for Hash {
fn eq(&self, other: &Hash) -> bool {
self.0[..] == other.0[..]
}
fn eq(&self, other: &Hash) -> bool { self.0[..] == other.0[..] }
}
impl Eq for Hash {}
impl Default for Hash {
fn default() -> Hash {
Hash([0; 64])
}
fn default() -> Hash { Hash([0; 64]) }
}
impl PartialOrd for Hash {
fn partial_cmp(&self, other: &Hash) -> Option<cmp::Ordering> {
self.0.partial_cmp(&other.0)
}
fn partial_cmp(&self, other: &Hash) -> Option<cmp::Ordering> { self.0.partial_cmp(&other.0) }
}
impl Ord for Hash {
fn cmp(&self, other: &Hash) -> cmp::Ordering {
self.0.cmp(&other.0)
}
fn cmp(&self, other: &Hash) -> cmp::Ordering { self.0.cmp(&other.0) }
}
impl hash::Hash for Hash {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.0.hash(state) }
}
#[cfg(not(fuzzing))]
@ -388,7 +375,8 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn sha512_serde() {
use serde_test::{Configure, Token, assert_tokens};
use serde_test::{assert_tokens, Configure, Token};
use crate::{sha512, Hash};
#[rustfmt::skip]
@ -409,7 +397,7 @@ mod tests {
&hash.readable(),
&[Token::Str(
"8b41e1b78ad11521113c52ff182a1b8e0a195754aa527fcd00a411620b46f20f\
fffb8088ccf85497121ad4499e0845b876f6dd6640088a2f0b2d8a600bdf4c0c"
fffb8088ccf85497121ad4499e0845b876f6dd6640088a2f0b2d8a600bdf4c0c",
)],
);
}
@ -419,7 +407,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, sha512};
use crate::{sha512, Hash, HashEngine};
#[bench]
pub fn sha512_10(bh: &mut Bencher) {
@ -450,5 +438,4 @@ mod benches {
});
bh.bytes = bytes.len() as u64;
}
}

View File

@ -24,11 +24,12 @@
//! produces an entirely different hash compared to sha512. More information at
//! <https://eprint.iacr.org/2010/548.pdf>.
use core::str;
use core::ops::Index;
use core::slice::SliceIndex;
use core::str;
use crate::{sha512, sha512::BLOCK_SIZE, Error};
use crate::sha512::BLOCK_SIZE;
use crate::{sha512, Error};
/// Engine to compute SHA512/256 hash function.
///
@ -56,19 +57,13 @@ impl Default for HashEngine {
impl crate::HashEngine for HashEngine {
type MidState = [u8; 64];
fn midstate(&self) -> [u8; 64] {
self.0.midstate()
}
fn midstate(&self) -> [u8; 64] { self.0.midstate() }
const BLOCK_SIZE: usize = sha512::BLOCK_SIZE;
fn n_bytes_hashed(&self) -> usize {
self.0.length
}
fn n_bytes_hashed(&self) -> usize { self.0.length }
fn input(&mut self, inp: &[u8]) {
self.0.input(inp);
}
fn input(&mut self, inp: &[u8]) { self.0.input(inp); }
}
crate::internal_macros::hash_type! {
@ -176,7 +171,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, sha512_256};
use crate::{sha512_256, Hash, HashEngine};
#[bench]
pub fn sha512_256_10(bh: &mut Bencher) {
@ -207,5 +202,4 @@ mod benches {
});
bh.bytes = bytes.len() as u64;
}
}

View File

@ -20,9 +20,9 @@
//! SipHash 2-4 implementation.
//!
use core::{cmp, mem, ptr, str};
use core::ops::Index;
use core::slice::SliceIndex;
use core::{cmp, mem, ptr, str};
use crate::{Error, Hash as _, HashEngine as _};
@ -34,9 +34,7 @@ crate::internal_macros::hash_type! {
}
#[cfg(not(fuzzing))]
fn from_engine(e: HashEngine) -> Hash {
Hash::from_u64(Hash::from_engine_to_u64(e))
}
fn from_engine(e: HashEngine) -> Hash { Hash::from_u64(Hash::from_engine_to_u64(e)) }
#[cfg(fuzzing)]
fn from_engine(e: HashEngine) -> Hash {
@ -44,7 +42,6 @@ fn from_engine(e: HashEngine) -> Hash {
Hash::from_u64(state.v0 ^ state.v1 ^ state.v2 ^ state.v3)
}
macro_rules! compress {
($state:expr) => {{
compress!($state.v0, $state.v1, $state.v2, $state.v3)
@ -128,14 +125,10 @@ impl HashEngine {
}
/// Creates a new SipHash24 engine.
pub fn new() -> HashEngine {
HashEngine::with_keys(0, 0)
}
pub fn new() -> HashEngine { HashEngine::with_keys(0, 0) }
/// Retrieves the keys of this engine.
pub fn keys(&self) -> (u64, u64) {
(self.k0, self.k1)
}
pub fn keys(&self) -> (u64, u64) { (self.k0, self.k1) }
#[inline]
fn c_rounds(state: &mut State) {
@ -153,17 +146,13 @@ impl HashEngine {
}
impl Default for HashEngine {
fn default() -> Self {
HashEngine::new()
}
fn default() -> Self { HashEngine::new() }
}
impl crate::HashEngine for HashEngine {
type MidState = State;
fn midstate(&self) -> State {
self.state.clone()
}
fn midstate(&self) -> State { self.state.clone() }
const BLOCK_SIZE: usize = 8;
@ -207,10 +196,7 @@ impl crate::HashEngine for HashEngine {
self.ntail = left;
}
fn n_bytes_hashed(&self) -> usize {
self.length
}
fn n_bytes_hashed(&self) -> usize { self.length }
}
impl Hash {
@ -246,14 +232,10 @@ impl Hash {
}
/// Returns the (little endian) 64-bit integer representation of the hash value.
pub fn as_u64(&self) -> u64 {
u64::from_le_bytes(self.0)
}
pub fn as_u64(&self) -> u64 { u64::from_le_bytes(self.0) }
/// Creates a hash from its (little endian) 64-bit integer representation.
pub fn from_u64(hash: u64) -> Hash {
Hash(hash.to_le_bytes())
}
pub fn from_u64(hash: u64) -> Hash { Hash(hash.to_le_bytes()) }
}
/// Load an u64 using up to 7 bytes of a byte slice.
@ -376,7 +358,7 @@ mod tests {
mod benches {
use test::Bencher;
use crate::{Hash, HashEngine, siphash24};
use crate::{siphash24, Hash, HashEngine};
#[bench]
pub fn siphash24_1ki(bh: &mut Bencher) {

View File

@ -107,8 +107,6 @@ macro_rules! engine_input_impl(
)
);
/// Creates a new newtype around a [`Hash`] type.
///
/// The syntax is similar to the usual tuple struct syntax:
@ -403,8 +401,9 @@ macro_rules! hash_newtype_known_attrs {
#[cfg(feature = "schemars")]
#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
pub mod json_hex_string {
use schemars::gen::SchemaGenerator;
use schemars::schema::{Schema, SchemaObject};
use schemars::{gen::SchemaGenerator, JsonSchema};
use schemars::JsonSchema;
macro_rules! define_custom_hex {
($name:ident, $len:expr) => {
pub fn $name(gen: &mut SchemaGenerator) -> Schema {
@ -426,7 +425,7 @@ pub mod json_hex_string {
#[cfg(test)]
mod test {
use crate::{Hash, sha256};
use crate::{sha256, Hash};
#[test]
fn hash_as_ref_array() {