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

Commit aff0e6f

Browse files
committed
Merge #148: Add all_zeros method to Hash trait
51be9bd Add all_zeros method to Hash trait (Tobin Harding) Pull request description: Currently we use `Default::default()` to get an all zeros hash. This is confusing because an all zeroes hash does not have a real meaning since there is no known input to create it. The all zeros hash is, however used by the Bitcoin network to signify certain things (e.g. the previous blockhash in the genesis block). Remove the `Default` implementation from the `Hash` trait and add a `all_zeros` method instead. Remove the `Default` derive from any hash type that has it. To see the patch to `rust-bitcoin` that this PR imposes, check out [this branch](https://github.com/tcharding/rust-bitcoin/tree/bitcoin_hashes-all-zeros). Resolves #139 ACKs for top commit: apoelstra: ACK 51be9bd Tree-SHA512: e5f56362fec5ef8f32e87f0949b2a28fd9a5a85bbbf1e0295d535b6e79582e16cb6c165becf6ed00051ab8ac42be40ba23427d553213e320b38366684ef9c1af
2 parents 9887c4e + 51be9bd commit aff0e6f

File tree

11 files changed

+59
-9
lines changed

11 files changed

+59
-9
lines changed

src/hash160.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use core::slice::SliceIndex;
2727
use crate::{Error, hex, ripemd160, sha256};
2828

2929
/// Output of the Bitcoin HASH160 hash function.
30-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
30+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3131
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3232
#[repr(transparent)]
3333
pub struct Hash(
@@ -97,6 +97,10 @@ impl crate::Hash for Hash {
9797
fn from_inner(inner: Self::Inner) -> Self {
9898
Hash(inner)
9999
}
100+
101+
fn all_zeros() -> Self {
102+
Hash([0x00; 20])
103+
}
100104
}
101105

102106
#[cfg(test)]

src/hmac.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use serde::{Serialize, Serializer, Deserialize, Deserializer};
2727
use crate::{Error, Hash, HashEngine};
2828

2929
/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
30-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
30+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3131
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3232
#[cfg_attr(feature = "schemars", schemars(transparent))]
3333
#[repr(transparent)]
@@ -218,6 +218,11 @@ impl<T: Hash> Hash for Hmac<T> {
218218
fn from_inner(inner: T::Inner) -> Self {
219219
Hmac(T::from_inner(inner))
220220
}
221+
222+
fn all_zeros() -> Self {
223+
let zeros = T::all_zeros();
224+
Hmac(zeros)
225+
}
221226
}
222227

223228
#[cfg(feature = "serde")]

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub trait HashEngine: Clone + Default {
9898
}
9999

100100
/// Trait which applies to hashes of all types.
101-
pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
101+
pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord +
102102
hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex +
103103
ops::Index<ops::RangeFull, Output = [u8]> +
104104
ops::Index<ops::RangeFrom<usize>, Output = [u8]> +
@@ -149,6 +149,13 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
149149

150150
/// Constructs a hash from the underlying byte array.
151151
fn from_inner(inner: Self::Inner) -> Self;
152+
153+
/// Returns an all zero hash.
154+
///
155+
/// An all zeros hash is a made up construct because there is not a known input that can create
156+
/// it, however it is used in various places in Bitcoin e.g., the Bitcoin genesis block's
157+
/// previous blockhash and the coinbase transaction's outpoint txid.
158+
fn all_zeros() -> Self;
152159
}
153160

154161
#[cfg(test)]

src/ripemd160.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl crate::HashEngine for HashEngine {
7575
}
7676

7777
/// Output of the RIPEMD160 hash function.
78-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
78+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7979
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
8080
#[repr(transparent)]
8181
pub struct Hash(
@@ -159,6 +159,10 @@ impl crate::Hash for Hash {
159159
fn from_inner(inner: Self::Inner) -> Self {
160160
Hash(inner)
161161
}
162+
163+
fn all_zeros() -> Self {
164+
Hash([0x00; 20])
165+
}
162166
}
163167

164168
macro_rules! round(

src/sha1.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl crate::HashEngine for HashEngine {
7070
}
7171

7272
/// Output of the SHA1 hash function.
73-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
73+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7474
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7575
#[repr(transparent)]
7676
pub struct Hash(
@@ -146,6 +146,10 @@ impl crate::Hash for Hash {
146146
fn from_inner(inner: Self::Inner) -> Self {
147147
Hash(inner)
148148
}
149+
150+
fn all_zeros() -> Self {
151+
Hash([0x00; 20])
152+
}
149153
}
150154

151155
impl HashEngine {

src/sha256.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl crate::HashEngine for HashEngine {
7070
}
7171

7272
/// Output of the SHA256 hash function.
73-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
73+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7474
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7575
#[repr(transparent)]
7676
pub struct Hash(
@@ -158,6 +158,10 @@ impl crate::Hash for Hash {
158158
fn from_inner(inner: Self::Inner) -> Self {
159159
Hash(inner)
160160
}
161+
162+
fn all_zeros() -> Self {
163+
Hash([0x00; 32])
164+
}
161165
}
162166

163167
/// Output of the SHA256 hash function.

src/sha256d.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::slice::SliceIndex;
2222
use crate::{Error, hex, sha256};
2323

2424
/// Output of the SHA256d hash function.
25-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
25+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2626
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2727
#[repr(transparent)]
2828
pub struct Hash(
@@ -94,6 +94,10 @@ impl crate::Hash for Hash {
9494
fn from_inner(inner: Self::Inner) -> Self {
9595
Hash(inner)
9696
}
97+
98+
fn all_zeros() -> Self {
99+
Hash([0x00; 32])
100+
}
97101
}
98102

99103
#[cfg(test)]

src/sha256t.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ impl<T: Tag> crate::Hash for Hash<T> {
132132
fn from_inner(inner: Self::Inner) -> Self {
133133
Hash(inner, PhantomData)
134134
}
135+
136+
fn all_zeros() -> Self {
137+
Hash([0x00; 32], PhantomData)
138+
}
135139
}
136140

137141
/// Macro used to define a newtype tagged hash.

src/sha512.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ impl crate::Hash for Hash {
204204
fn from_inner(inner: Self::Inner) -> Self {
205205
Hash(inner)
206206
}
207+
208+
fn all_zeros() -> Self {
209+
Hash([0x00; 64])
210+
}
207211
}
208212

209213
macro_rules! Ch( ($x:expr, $y:expr, $z:expr) => ($z ^ ($x & ($y ^ $z))) );

src/siphash24.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl crate::HashEngine for HashEngine {
195195
}
196196

197197
/// Output of the SipHash24 hash function.
198-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
198+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
199199
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
200200
#[repr(transparent)]
201201
pub struct Hash(
@@ -306,6 +306,10 @@ impl crate::Hash for Hash {
306306
fn from_inner(inner: Self::Inner) -> Self {
307307
Hash(inner)
308308
}
309+
310+
fn all_zeros() -> Self {
311+
Hash([0x00; 8])
312+
}
309313
}
310314

311315
/// Load an u64 using up to 7 bytes of a byte slice.

0 commit comments

Comments
 (0)