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 Jan 15, 2022
1 parent c2c8f9e commit d0dab4c
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 173 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
23 changes: 7 additions & 16 deletions src/hash160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

use core::str;

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 @@ -35,8 +32,6 @@ pub struct Hash(
[u8; 20]
);



hex_fmt_impl!(Debug, Hash);
hex_fmt_impl!(Display, Hash);
hex_fmt_impl!(LowerHex, Hash);
Expand All @@ -45,13 +40,13 @@ serde_impl!(Hash, 20);
borrow_slice_impl!(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 @@ -95,10 +90,8 @@ impl HashTrait for Hash {

#[cfg(test)]
mod tests {
use hash160;
use hex::{FromHex, ToHex};
use Hash;
use HashEngine;
use crate::{Hash, HashEngine, hash160};
use crate::hex::{FromHex, ToHex};

#[derive(Clone)]
struct Test {
Expand Down Expand Up @@ -173,9 +166,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
64 changes: 30 additions & 34 deletions src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ 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)?))
Expand All @@ -43,27 +41,27 @@ impl<T: HashTrait + str::FromStr> str::FromStr for Hmac<T> {

/// 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> {
/// Construct a new keyed HMAC with the given key. We only support underlying hashes
/// whose block sizes are ≤ 128 bytes; larger hashes will result in panics.
pub fn new(key: &[u8]) -> HmacEngine<T> {
Expand All @@ -72,12 +70,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 @@ -93,8 +91,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 @@ -108,7 +106,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 @@ -129,66 +127,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 @@ -219,14 +217,14 @@ impl<T: HashTrait> HashTrait for Hmac<T> {
}

#[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")]
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 @@ -235,9 +233,8 @@ impl<'de, T: HashTrait + Deserialize<'de>> Deserialize<'de> for Hmac<T> {

#[cfg(test)]
mod tests {
use sha256;
#[cfg(feature="serde")] use sha512;
use {Hash, HashEngine, Hmac, HmacEngine};
use crate::{Hash, HashEngine, Hmac, HmacEngine, sha256};
#[cfg(feature="serde")] use crate::sha512;

#[derive(Clone)]
struct Test {
Expand Down Expand Up @@ -393,8 +390,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 @@ -22,9 +22,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 @@ -85,7 +83,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 @@ -98,8 +96,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 @@ -150,17 +150,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
Loading

0 comments on commit d0dab4c

Please sign in to comment.