cs_mwc_util/
secp_static.rs

1// Copyright 2019 The Grin Developers
2// Copyright 2024 The MWC Developers
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Globally accessible static instance of secp256k1, to avoid
17//! initialization overhead
18
19use crate::secp;
20use crate::Mutex;
21use rand::thread_rng;
22use std::sync::Arc;
23
24lazy_static! {
25	/// Static reference to secp instance
26	pub static ref SECP256K1:Arc<Mutex<secp::Secp256k1>>
27		= Arc::new(Mutex::new(secp::Secp256k1::with_caps(secp::ContextFlag::Commit)));
28}
29
30/// Returns the static instance, but calls randomize on it as well
31/// (Recommended to avoid side channel attacks
32pub fn static_secp_instance() -> Arc<Mutex<secp::Secp256k1>> {
33	let mut secp_inst = SECP256K1.lock();
34	secp_inst.randomize(&mut thread_rng());
35	SECP256K1.clone()
36}
37
38/// Convenient way to generate a commitment to zero.
39pub fn commit_to_zero_value() -> secp::pedersen::Commitment {
40	secp::pedersen::Commitment::from_vec(vec![0])
41}