Randomize context on initialization

Signed-off-by: Sebastian Geisler <sebastian@blockstream.io>
This commit is contained in:
Sebastian Geisler 2020-08-03 12:07:26 +02:00
parent a959de4174
commit 2046a40905
2 changed files with 4 additions and 2 deletions

View File

@ -29,7 +29,7 @@ rand-std = ["rand/std"]
recovery = ["secp256k1-sys/recovery"] recovery = ["secp256k1-sys/recovery"]
endomorphism = ["secp256k1-sys/endomorphism"] endomorphism = ["secp256k1-sys/endomorphism"]
lowmemory = ["secp256k1-sys/lowmemory"] lowmemory = ["secp256k1-sys/lowmemory"]
global-context = [] global-context = ["std", "rand"]
# Use this feature to not compile the bundled libsecp256k1 C symbols, # Use this feature to not compile the bundled libsecp256k1 C symbols,
# but use external ones. Use this only if you know what you are doing! # but use external ones. Use this only if you know what you are doing!

View File

@ -31,7 +31,9 @@ pub mod global {
static ONCE: Once = Once::new(); static ONCE: Once = Once::new();
static mut CONTEXT: Option<Secp256k1<All>> = None; static mut CONTEXT: Option<Secp256k1<All>> = None;
ONCE.call_once(|| unsafe { ONCE.call_once(|| unsafe {
CONTEXT = Some(Secp256k1::new()); let mut ctx = Secp256k1::new();
ctx.randomize(&mut rand::thread_rng());
CONTEXT = Some(ctx);
}); });
unsafe { CONTEXT.as_ref().unwrap() } unsafe { CONTEXT.as_ref().unwrap() }
} }