Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Update to use edition 2018
Browse files Browse the repository at this point in the history
Add `edition = "2018"` to the minifest file. In order to get the
codebase to build cleanly do:

- Remove usage of `use Hash as HashTrait`, instead use
  `impl crate::Hash for Hash` and `use Hash as _`.
- Same for HashEngine (remove EngineTrait).
- Add `crate::` to import statements and group same level (only did this
  for crate imports, the rest can wait for rustfmt :)
- Make test imports uniform, elect to _not_ use `super::*` because it
  seems cleaner, we are always importing the module we are testing and
  the same set of traits in each `test` module. Can change if requested.
  • Loading branch information
tcharding committed May 1, 2022
1 parent e762962 commit b270023
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 166 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/rust-bitcoin/bitcoin_hashes/"
documentation = "https://docs.rs/bitcoin_hashes/"
keywords = [ "crypto", "bitcoin", "hash", "digest" ]
readme = "README.md"
edition = "2018"

[lib]
name = "bitcoin_hashes"
Expand Down
6 changes: 2 additions & 4 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ fn eq_test() {
mod benches {
use test::Bencher;

use sha256;
use sha512;
use Hash;
use cmp::fixed_time_eq;
use crate::{Hash, sha256, sha512};
use crate::cmp::fixed_time_eq;

#[bench]
fn bench_32b_constant_time_cmp_ne(bh: &mut Bencher) {
Expand Down
21 changes: 8 additions & 13 deletions src/hash160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use core::str;
use core::ops::Index;
use core::slice::SliceIndex;

use sha256;
use ripemd160;
use Hash as HashTrait;
use Error;
use crate::{Error, hex, ripemd160, sha256};

/// Output of the Bitcoin HASH160 hash function.
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
Expand All @@ -54,13 +51,13 @@ impl<I: SliceIndex<[u8]>> Index<I> for Hash {
}

impl str::FromStr for Hash {
type Err = ::hex::Error;
type Err = hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
hex::FromHex::from_hex(s)
}
}

impl HashTrait for Hash {
impl crate::Hash for Hash {
type Engine = sha256::HashEngine;
type Inner = [u8; 20];

Expand Down Expand Up @@ -107,8 +104,8 @@ mod tests {
#[test]
#[cfg(any(feature = "std", feature = "alloc"))]
fn test() {
use {hash160, Hash, HashEngine};
use hex::{FromHex, ToHex};
use crate::{hash160, Hash, HashEngine};
use crate::hex::{FromHex, ToHex};

#[derive(Clone)]
#[cfg(any(feature = "std", feature = "alloc"))]
Expand Down Expand Up @@ -162,7 +159,7 @@ mod tests {
#[test]
fn ripemd_serde() {
use serde_test::{Configure, Token, assert_tokens};
use {hash160, Hash};
use crate::{hash160, Hash};

static HASH_BYTES: [u8; 20] = [
0x13, 0x20, 0x72, 0xdf,
Expand All @@ -182,9 +179,7 @@ mod tests {
mod benches {
use test::Bencher;

use hash160;
use Hash;
use HashEngine;
use crate::{Hash, HashEngine, hash160};

#[bench]
pub fn hash160_10(bh: &mut Bencher) {
Expand Down
6 changes: 3 additions & 3 deletions src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
//!

#[cfg(any(feature = "std", feature = "alloc"))]
use alloc::{string::String, vec::Vec};
use crate::alloc::{string::String, vec::Vec};
#[cfg(feature = "alloc")]
use alloc::format;
use crate::alloc::format;

#[cfg(feature = "std")]
use std::io;
#[cfg(all(not(feature = "std"), feature = "core2"))]
use core2::io;

use core::{fmt, str};
use Hash;
use crate::Hash;

/// Hex decoding error.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
63 changes: 30 additions & 33 deletions src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,44 @@ use core::{borrow, fmt, ops, str};
#[cfg(feature = "serde")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};

use HashEngine as EngineTrait;
use Hash as HashTrait;
use Error;
use crate::{Error, Hash, HashEngine};

/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "schemars", schemars(transparent))]
#[repr(transparent)]
pub struct Hmac<T: HashTrait>(T);
pub struct Hmac<T: Hash>(T);

impl<T: HashTrait + str::FromStr> str::FromStr for Hmac<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)?))
}
}

/// Pair of underlying hash midstates which represent the current state of an `HmacEngine`.
pub struct HmacMidState<T: HashTrait> {
pub struct HmacMidState<T: Hash> {
/// Midstate of the inner hash engine
pub inner: <T::Engine as EngineTrait>::MidState,
pub inner: <T::Engine as HashEngine>::MidState,
/// Midstate of the outer hash engine
pub outer: <T::Engine as EngineTrait>::MidState,
pub outer: <T::Engine as HashEngine>::MidState,
}

/// Pair of underyling hash engines, used for the inner and outer hash of HMAC.
#[derive(Clone)]
pub struct HmacEngine<T: HashTrait> {
pub struct HmacEngine<T: Hash> {
iengine: T::Engine,
oengine: T::Engine,
}

impl<T: HashTrait> Default for HmacEngine<T> {
impl<T: Hash> Default for HmacEngine<T> {
fn default() -> Self {
HmacEngine::new(&[])
}
}

impl<T: HashTrait> HmacEngine<T> {
impl<T: Hash> HmacEngine<T> {
/// Constructs a new keyed HMAC from `key`.
///
/// We only support underlying hashes whose block sizes are ≤ 128 bytes.
Expand All @@ -77,12 +75,12 @@ impl<T: HashTrait> HmacEngine<T> {
let mut ipad = [0x36u8; 128];
let mut opad = [0x5cu8; 128];
let mut ret = HmacEngine {
iengine: <T as HashTrait>::engine(),
oengine: <T as HashTrait>::engine(),
iengine: <T as Hash>::engine(),
oengine: <T as Hash>::engine(),
};

if key.len() > T::Engine::BLOCK_SIZE {
let hash = <T as HashTrait>::hash(key);
let hash = <T as Hash>::hash(key);
for (b_i, b_h) in ipad.iter_mut().zip(&hash[..]) {
*b_i ^= *b_h;
}
Expand All @@ -98,8 +96,8 @@ impl<T: HashTrait> HmacEngine<T> {
}
};

EngineTrait::input(&mut ret.iengine, &ipad[..T::Engine::BLOCK_SIZE]);
EngineTrait::input(&mut ret.oengine, &opad[..T::Engine::BLOCK_SIZE]);
HashEngine::input(&mut ret.iengine, &ipad[..T::Engine::BLOCK_SIZE]);
HashEngine::input(&mut ret.oengine, &opad[..T::Engine::BLOCK_SIZE]);
ret
}

Expand All @@ -112,7 +110,7 @@ impl<T: HashTrait> HmacEngine<T> {
}
}

impl<T: HashTrait> EngineTrait for HmacEngine<T> {
impl<T: Hash> HashEngine for HmacEngine<T> {
type MidState = HmacMidState<T>;

fn midstate(&self) -> Self::MidState {
Expand All @@ -133,66 +131,66 @@ impl<T: HashTrait> EngineTrait for HmacEngine<T> {
}
}

impl<T: HashTrait> fmt::Debug for Hmac<T> {
impl<T: Hash> fmt::Debug for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}

impl<T: HashTrait> fmt::Display for Hmac<T> {
impl<T: Hash> fmt::Display for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

impl<T: HashTrait> fmt::LowerHex for Hmac<T> {
impl<T: Hash> fmt::LowerHex for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
}

impl<T: HashTrait> ops::Index<usize> for Hmac<T> {
impl<T: Hash> ops::Index<usize> for Hmac<T> {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
&self.0[index]
}
}

impl<T: HashTrait> ops::Index<ops::Range<usize>> for Hmac<T> {
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]
}
}

impl<T: HashTrait> ops::Index<ops::RangeFrom<usize>> for Hmac<T> {
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]
}
}

impl<T: HashTrait> ops::Index<ops::RangeTo<usize>> for Hmac<T> {
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]
}
}

impl<T: HashTrait> ops::Index<ops::RangeFull> for Hmac<T> {
impl<T: Hash> ops::Index<ops::RangeFull> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::RangeFull) -> &[u8] {
&self.0[index]
}
}

impl<T: HashTrait> borrow::Borrow<[u8]> for Hmac<T> {
impl<T: Hash> borrow::Borrow<[u8]> for Hmac<T> {
fn borrow(&self) -> &[u8] {
&self[..]
}
}

impl<T: HashTrait> HashTrait for Hmac<T> {
impl<T: Hash> Hash for Hmac<T> {
type Engine = HmacEngine<T>;
type Inner = T::Inner;

Expand Down Expand Up @@ -224,15 +222,15 @@ impl<T: HashTrait> HashTrait for Hmac<T> {

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<T: HashTrait + Serialize> Serialize for Hmac<T> {
impl<T: Hash + Serialize> Serialize for Hmac<T> {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
Serialize::serialize(&self.0, s)
}
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de, T: HashTrait + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
impl<'de, T: Hash + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Hmac<T>, D::Error> {
let inner = Deserialize::deserialize(d)?;
Ok(Hmac(inner))
Expand All @@ -244,7 +242,7 @@ mod tests {
#[test]
#[cfg(any(feature = "std", feature = "alloc"))]
fn test() {
use {sha256, HashEngine, HmacEngine, Hash, Hmac};
use crate::{sha256, HashEngine, HmacEngine, Hash, Hmac};

#[derive(Clone)]
struct Test {
Expand Down Expand Up @@ -370,7 +368,7 @@ mod tests {
#[test]
fn hmac_sha512_serde() {
use serde_test::{Configure, Token, assert_tokens};
use {sha512, Hash, Hmac};
use crate::{sha512, Hash, Hmac};

static HASH_BYTES: [u8; 64] = [
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21,
Expand Down Expand Up @@ -399,8 +397,7 @@ mod tests {
mod benches {
use test::Bencher;

use sha256;
use {Hmac, Hash, HashEngine};
use crate::{Hmac, Hash, HashEngine, sha256};

#[bench]
pub fn hmac_sha256_10(bh: &mut Bencher) {
Expand Down
9 changes: 3 additions & 6 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ use std::{error, io};
#[cfg(not(feature = "std"))]
use core2::{error, io};

use {hex, sha1, sha256, sha512, ripemd160, siphash24, hmac};
use HashEngine;
use Error;
use crate::{Error, HashEngine, hex, sha1, sha256, sha512, ripemd160, siphash24, hmac};

impl error::Error for Error {
#[cfg(feature = "std")]
Expand Down Expand Up @@ -86,7 +84,7 @@ impl io::Write for siphash24::HashEngine {
}
}

impl<T: ::Hash> io::Write for hmac::HmacEngine<T> {
impl<T: crate::Hash> io::Write for hmac::HmacEngine<T> {
fn flush(&mut self) -> io::Result<()> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Expand All @@ -99,8 +97,7 @@ impl<T: ::Hash> io::Write for hmac::HmacEngine<T> {
mod tests {
use super::io::Write;

use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24, hmac};
use Hash;
use crate::{Hash, sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24, hmac};

macro_rules! write_test {
($mod:ident, $exp_empty:expr, $exp_256:expr, $exp_64k:expr,) => {
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,18 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +

#[cfg(test)]
mod tests {
use Hash;
hash_newtype!(TestNewtype, ::sha256d::Hash, 32, doc="A test newtype");
hash_newtype!(TestNewtype2, ::sha256d::Hash, 32, doc="A test newtype");
use crate::{Hash, sha256d};

hash_newtype!(TestNewtype, sha256d::Hash, 32, doc="A test newtype");
hash_newtype!(TestNewtype2, sha256d::Hash, 32, doc="A test newtype");

#[test]
fn convert_newtypes() {
let h1 = TestNewtype::hash(&[]);
let h2: TestNewtype2 = h1.as_hash().into();
assert_eq!(&h1[..], &h2[..]);

let h = ::sha256d::Hash::hash(&[]);
let h = sha256d::Hash::hash(&[]);
let h2: TestNewtype = h.to_string().parse().unwrap();
assert_eq!(h2.as_hash(), h);
}
Expand Down

0 comments on commit b270023

Please sign in to comment.