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

Commit 51be9bd

Browse files
committed
Add all_zeros method to Hash trait
Currently we use `Default::default()` to get an all zeros hash. This is misleading because an all zeroes hash does not have a real meaning since there is no known input to create it. The all zeros has is 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.
1 parent b8d135b commit 51be9bd

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
@@ -30,7 +30,7 @@ use Hash as HashTrait;
3030
use Error;
3131

3232
/// Output of the Bitcoin HASH160 hash function.
33-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
33+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3434
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3535
#[repr(transparent)]
3636
pub struct Hash(
@@ -100,6 +100,10 @@ impl HashTrait for Hash {
100100
fn from_inner(inner: Self::Inner) -> Self {
101101
Hash(inner)
102102
}
103+
104+
fn all_zeros() -> Self {
105+
Hash([0x00; 20])
106+
}
103107
}
104108

105109
#[cfg(test)]

src/hmac.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use Hash as HashTrait;
2929
use Error;
3030

3131
/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
32-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
32+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3333
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3434
#[cfg_attr(feature = "schemars", schemars(transparent))]
3535
#[repr(transparent)]
@@ -220,6 +220,11 @@ impl<T: HashTrait> HashTrait for Hmac<T> {
220220
fn from_inner(inner: T::Inner) -> Self {
221221
Hmac(T::from_inner(inner))
222222
}
223+
224+
fn all_zeros() -> Self {
225+
let zeros = T::all_zeros();
226+
Hmac(zeros)
227+
}
223228
}
224229

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

src/lib.rs

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

9797
/// Trait which applies to hashes of all types.
98-
pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
98+
pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord +
9999
hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex +
100100
ops::Index<ops::RangeFull, Output = [u8]> +
101101
ops::Index<ops::RangeFrom<usize>, Output = [u8]> +
@@ -146,6 +146,13 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
146146

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

151158
#[cfg(test)]

src/ripemd160.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl EngineTrait for HashEngine {
7878
}
7979

8080
/// Output of the RIPEMD160 hash function.
81-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
81+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8282
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
8383
#[repr(transparent)]
8484
pub struct Hash(
@@ -162,6 +162,10 @@ impl HashTrait for Hash {
162162
fn from_inner(inner: Self::Inner) -> Self {
163163
Hash(inner)
164164
}
165+
166+
fn all_zeros() -> Self {
167+
Hash([0x00; 20])
168+
}
165169
}
166170

167171
macro_rules! round(

src/sha1.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl EngineTrait for HashEngine {
7373
}
7474

7575
/// Output of the SHA1 hash function.
76-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
76+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7777
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7878
#[repr(transparent)]
7979
pub struct Hash(
@@ -149,6 +149,10 @@ impl HashTrait for Hash {
149149
fn from_inner(inner: Self::Inner) -> Self {
150150
Hash(inner)
151151
}
152+
153+
fn all_zeros() -> Self {
154+
Hash([0x00; 20])
155+
}
152156
}
153157

154158
impl HashEngine {

src/sha256.rs

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

7676
/// Output of the SHA256 hash function.
77-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
77+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7878
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7979
#[repr(transparent)]
8080
pub struct Hash(
@@ -162,6 +162,10 @@ impl HashTrait for Hash {
162162
fn from_inner(inner: Self::Inner) -> Self {
163163
Hash(inner)
164164
}
165+
166+
fn all_zeros() -> Self {
167+
Hash([0x00; 32])
168+
}
165169
}
166170

167171
/// Output of the SHA256 hash function.

src/sha256d.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use Hash as HashTrait;
2424
use Error;
2525

2626
/// Output of the SHA256d hash function.
27-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
27+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2828
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2929
#[repr(transparent)]
3030
pub struct Hash(
@@ -96,6 +96,10 @@ impl HashTrait for Hash {
9696
fn from_inner(inner: Self::Inner) -> Self {
9797
Hash(inner)
9898
}
99+
100+
fn all_zeros() -> Self {
101+
Hash([0x00; 32])
102+
}
99103
}
100104

101105
#[cfg(test)]

src/sha256t.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ impl<T: Tag> HashTrait for Hash<T> {
134134
fn from_inner(inner: Self::Inner) -> Self {
135135
Hash(inner, PhantomData)
136136
}
137+
138+
fn all_zeros() -> Self {
139+
Hash([0x00; 32], PhantomData)
140+
}
137141
}
138142

139143
/// 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
@@ -207,6 +207,10 @@ impl HashTrait for Hash {
207207
fn from_inner(inner: Self::Inner) -> Self {
208208
Hash(inner)
209209
}
210+
211+
fn all_zeros() -> Self {
212+
Hash([0x00; 64])
213+
}
210214
}
211215

212216
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
@@ -198,7 +198,7 @@ impl EngineTrait for HashEngine {
198198
}
199199

200200
/// Output of the SipHash24 hash function.
201-
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
201+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
202202
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
203203
#[repr(transparent)]
204204
pub struct Hash(
@@ -309,6 +309,10 @@ impl HashTrait for Hash {
309309
fn from_inner(inner: Self::Inner) -> Self {
310310
Hash(inner)
311311
}
312+
313+
fn all_zeros() -> Self {
314+
Hash([0x00; 8])
315+
}
312316
}
313317

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

0 commit comments

Comments
 (0)