From d5142a4f35736f55abc9a45a6b3b512a8c8514d1 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Fri, 26 Jul 2024 12:12:12 +1000 Subject: [PATCH] Remove lazy_static dep --- Cargo.toml | 4 ++-- src/lib.rs | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6ca1bae..18070db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] @@ -26,4 +26,4 @@ wasm-bindgen-test = "0.3.33" [features] default = ["zero_hash_cache"] -zero_hash_cache = ["lazy_static"] +zero_hash_cache = [] diff --git a/src/lib.rs b/src/lib.rs index 5a99d6f..06e7e04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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> = { - 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> = 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 { @@ -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]); } } }