From 8da6c1d47dec3f4a5e902ef99e1353a36043ecb2 Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Thu, 14 Mar 2024 10:54:21 +0100 Subject: [PATCH] Use std::sync::OnceLock --- givre/src/ciphersuite/bitcoin.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/givre/src/ciphersuite/bitcoin.rs b/givre/src/ciphersuite/bitcoin.rs index 3cd2cde..8cb7516 100644 --- a/givre/src/ciphersuite/bitcoin.rs +++ b/givre/src/ciphersuite/bitcoin.rs @@ -1,4 +1,5 @@ use generic_ec::{NonZero, Point}; +use sha2::Digest; use super::{Ciphersuite, Secp256k1}; @@ -28,13 +29,7 @@ impl Ciphersuite for Bitcoin { group_public_key: &super::NormalizedPoint>>, msg: &[u8], ) -> generic_ec::Scalar { - use sha2::{Digest, Sha256}; - static HASH: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { - let tag = Sha256::digest("BIP0340/challenge"); - Sha256::new().chain_update(tag).chain_update(tag) - }); - let challenge = HASH - .clone() + let challenge = challenge_hash() .chain_update(group_commitment.to_bytes()) .chain_update(group_public_key.to_bytes()) .chain_update(msg) @@ -88,3 +83,13 @@ impl Ciphersuite for Bitcoin { .expect("the size doesn't match") } } + +fn challenge_hash() -> sha2::Sha256 { + static PRECOMPUTED: std::sync::OnceLock = std::sync::OnceLock::new(); + PRECOMPUTED + .get_or_init(|| { + let tag = sha2::Sha256::digest("BIP0340/challenge"); + sha2::Sha256::new().chain_update(&tag).chain_update(&tag) + }) + .clone() +}