From 7cc074bfd3ba868da46c891d75b52d03a6175e54 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 28 Nov 2019 15:14:34 +0100 Subject: [PATCH] do detection only once --- sha2/Cargo.toml | 7 +++++++ sha2/src/sha256.rs | 12 ++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sha2/Cargo.toml b/sha2/Cargo.toml index 025a495dc..54cf0d434 100644 --- a/sha2/Cargo.toml +++ b/sha2/Cargo.toml @@ -16,6 +16,13 @@ fake-simd = "0.1" opaque-debug = "0.2" sha2-asm = { version="0.5", optional=true } +[dependencies.lazy_static] +version = "1.4.0" +default-features = false +# no_std feature is an anti-pattern. Why, lazy_static, why? +# See https://github.com/rust-lang-nursery/lazy-static.rs/issues/150 +features = ["spin_no_std"] + [dev-dependencies] digest = { version = "0.8", features = ["dev"] } hex-literal = "0.1" diff --git a/sha2/src/sha256.rs b/sha2/src/sha256.rs index 780c48479..3ea785eb8 100644 --- a/sha2/src/sha256.rs +++ b/sha2/src/sha256.rs @@ -10,25 +10,25 @@ use platform::Implementation; type BlockSize = U64; type Block = GenericArray; +lazy_static::lazy_static! { + static ref IMPL: Implementation = Implementation::detect(); +} + /// A structure that represents that state of a digest computation for the /// SHA-2 512 family of digest functions #[derive(Clone)] struct Engine256State { h: [u32; 8], - implementation: Implementation, } impl Engine256State { fn new(h: &[u32; STATE_LEN]) -> Engine256State { - Engine256State { - h: *h, - implementation: Implementation::detect(), - } + Engine256State { h: *h } } pub fn process_block(&mut self, block: &Block) { let block = unsafe { &*(block.as_ptr() as *const [u8; 64]) }; - self.implementation.compress256(&mut self.h, block); + IMPL.compress256(&mut self.h, block); } }