Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

on: [push, pull_request]

name: Continuous integration
Expand All @@ -11,7 +12,7 @@ jobs:
rust:
- nightly
steps:
- name: Checkout Crate
- name: Checkout Crate
uses: actions/checkout@v2
- name: Checkout Toolchain
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -60,7 +61,7 @@ jobs:
strategy:
matrix:
rust:
- 1.29.0
- 1.41.1
- beta
- stable
steps:
Expand All @@ -72,14 +73,11 @@ jobs:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- name: Pin cc if rust 1.29
if: matrix.rust == '1.29.0'
run: cargo generate-lockfile && cargo update -p serde_json --precise "1.0.39"
- name: Running cargo
env:
DO_FEATURE_MATRIX: true
DO_SCHEMARS_TESTS: ${{matrix.rust != '1.29.0'}}
DO_ALLOC_TESTS: ${{matrix.rust != '1.29.0'}}
DO_SCHEMARS_TESTS: true
DO_ALLOC_TESTS: true
run: ./contrib/test.sh

Embedded:
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitcoin_hashes"
version = "0.10.0"
version = "0.11.0"
authors = ["Andrew Poelstra <[email protected]>"]
license = "CC0-1.0"
description = "Hash functions used by rust-bitcoin which support rustc 1.29.0"
Expand All @@ -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
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ since these are needed to display hashes anway.

## Minimum Supported Rust Version (MSRV)

This library should always compile with any combination of features on **Rust 1.29**.
However, due to some dependencies breaking their MSRV in patch releases, you may
need to pin these deps explicitly, e.g. with the following commands

```
cargo generate-lockfile
cargo update -p serde_json --precise "1.0.39"
```
This library should always compile with any combination of features on **Rust 1.41.1**.

## Contributions

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
Loading