Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lazy_static dep #10

Merged
merged 1 commit into from
Jul 26, 2024
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
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]);
}
}
}
Loading