From 1693d51ce7b7acd96183ad4bc585489087ef3f6d Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Tue, 1 Feb 2022 15:51:08 +1100 Subject: [PATCH] Randomize context on creation Randomize context on creation if `rand-std` feature is enabled. --- src/context.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/context.rs b/src/context.rs index 89fd580..b5edc12 100644 --- a/src/context.rs +++ b/src/context.rs @@ -115,6 +115,9 @@ mod alloc_only { #[cfg(not(feature = "std"))] use alloc::alloc; + #[cfg(feature = "rand-std")] + use rand; + impl private::Sealed for SignOnly {} impl private::Sealed for All {} impl private::Sealed for VerifyOnly {} @@ -174,7 +177,10 @@ mod alloc_only { } impl Secp256k1 { - /// Lets you create a context in a generic manner(sign/verify/all) + /// Lets you create a context in a generic manner (sign/verify/all). + /// + /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`. + #[allow(unused_mut)] // Unused when `rand-std` is not enabled. pub fn gen_new() -> Secp256k1 { #[cfg(target_arch = "wasm32")] ffi::types::sanity_checks_for_wasm(); @@ -182,30 +188,43 @@ mod alloc_only { let size = unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }; let layout = alloc::Layout::from_size_align(size, ALIGN_TO).unwrap(); let ptr = unsafe {alloc::alloc(layout)}; - Secp256k1 { + let mut ctx = Secp256k1 { ctx: unsafe { ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS) }, phantom: PhantomData, size, + }; + + #[cfg(feature = "rand-std")] + { + ctx.randomize(&mut rand::thread_rng()); } + + ctx } } impl Secp256k1 { - /// Creates a new Secp256k1 context with all capabilities + /// Creates a new Secp256k1 context with all capabilities. + /// + /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`. pub fn new() -> Secp256k1 { Secp256k1::gen_new() } } impl Secp256k1 { - /// Creates a new Secp256k1 context that can only be used for signing + /// Creates a new Secp256k1 context that can only be used for signing. + /// + /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`. pub fn signing_only() -> Secp256k1 { Secp256k1::gen_new() } } impl Secp256k1 { - /// Creates a new Secp256k1 context that can only be used for verification + /// Creates a new Secp256k1 context that can only be used for verification. + /// + /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`. pub fn verification_only() -> Secp256k1 { Secp256k1::gen_new() }