Skip to content

Commit

Permalink
Remove lazy_static dep (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul authored Jul 26, 2024
1 parent 184472e commit 8bb97ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ repository = "https://github.com/sigp/ethereum_hashing"
documentation = "https://docs.rs/ethereum_hashing"
keywords = ["ethereum"]
categories = ["cryptography::cryptocurrencies"]
rust-version = "1.80.0"

[dependencies]
lazy_static = { version = "1.1", optional = true }
ring = "0.17"

[target.'cfg(target_arch = "x86_64")'.dependencies]
Expand All @@ -26,4 +26,4 @@ wasm-bindgen-test = "0.3.33"

[features]
default = ["zero_hash_cache"]
zero_hash_cache = ["lazy_static"]
zero_hash_cache = []
22 changes: 10 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use self::DynamicContext as Context;
use sha2_impl::Sha2CrateImpl;

#[cfg(feature = "zero_hash_cache")]
use lazy_static::lazy_static;
use std::sync::LazyLock;

/// Length of a SHA256 hash in bytes.
pub const HASH_LEN: usize = 32;
Expand Down Expand Up @@ -193,18 +193,16 @@ impl Sha256Context for DynamicContext {
pub const ZERO_HASHES_MAX_INDEX: usize = 48;

#[cfg(feature = "zero_hash_cache")]
lazy_static! {
/// Cached zero hashes where `ZERO_HASHES[i]` is the hash of a Merkle tree with 2^i zero leaves.
pub static ref ZERO_HASHES: Vec<Vec<u8>> = {
let mut hashes = vec![vec![0; 32]; ZERO_HASHES_MAX_INDEX + 1];
/// Cached zero hashes where `ZERO_HASHES[i]` is the hash of a Merkle tree with 2^i zero leaves.
pub static ZERO_HASHES: LazyLock<Vec<[u8; HASH_LEN]>> = LazyLock::new(|| {
let mut hashes = vec![[0; HASH_LEN]; ZERO_HASHES_MAX_INDEX + 1];

for i in 0..ZERO_HASHES_MAX_INDEX {
hashes[i + 1] = hash32_concat(&hashes[i], &hashes[i])[..].to_vec();
}
for i in 0..ZERO_HASHES_MAX_INDEX {
hashes[i + 1] = hash32_concat(&hashes[i], &hashes[i]);
}

hashes
};
}
hashes
});

#[cfg(test)]
mod tests {
Expand All @@ -231,7 +229,7 @@ mod tests {

#[test]
fn zero_hash_zero() {
assert_eq!(ZERO_HASHES[0], vec![0; 32]);
assert_eq!(ZERO_HASHES[0], [0; 32]);
}
}
}

0 comments on commit 8bb97ff

Please sign in to comment.