Skip to content

Commit

Permalink
Use std::sync::OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
survived committed Mar 14, 2024
1 parent 7e05894 commit 8da6c1d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions givre/src/ciphersuite/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use generic_ec::{NonZero, Point};
use sha2::Digest;

use super::{Ciphersuite, Secp256k1};

Expand Down Expand Up @@ -28,13 +29,7 @@ impl Ciphersuite for Bitcoin {
group_public_key: &super::NormalizedPoint<Self, NonZero<Point<Self::Curve>>>,
msg: &[u8],
) -> generic_ec::Scalar<Self::Curve> {
use sha2::{Digest, Sha256};
static HASH: once_cell::sync::Lazy<Sha256> = 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)
Expand Down Expand Up @@ -88,3 +83,13 @@ impl Ciphersuite for Bitcoin {
.expect("the size doesn't match")
}
}

fn challenge_hash() -> sha2::Sha256 {
static PRECOMPUTED: std::sync::OnceLock<sha2::Sha256> = 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()
}

0 comments on commit 8da6c1d

Please sign in to comment.