Skip to content

Commit 9c263f8

Browse files
committed
Randomize signing contexts
Randomize signing contexts on creation if `rand-std` feature is enabled.
1 parent 5c2b80e commit 9c263f8

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/context.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ mod alloc_only {
115115
#[cfg(not(feature = "std"))]
116116
use alloc::alloc;
117117

118+
#[cfg(feature = "rand-std")]
119+
use rand;
120+
118121
impl private::Sealed for SignOnly {}
119122
impl private::Sealed for All {}
120123
impl private::Sealed for VerifyOnly {}
@@ -191,16 +194,32 @@ mod alloc_only {
191194
}
192195

193196
impl Secp256k1<All> {
194-
/// Creates a new Secp256k1 context with all capabilities
197+
/// Creates a new Secp256k1 context with all capabilities.
198+
///
199+
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
200+
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
195201
pub fn new() -> Secp256k1<All> {
196-
Secp256k1::gen_new()
202+
let mut ctx = Secp256k1::gen_new();
203+
#[cfg(feature = "rand-std")]
204+
{
205+
ctx.randomize(&mut rand::thread_rng());
206+
}
207+
ctx
197208
}
198209
}
199210

200211
impl Secp256k1<SignOnly> {
201212
/// Creates a new Secp256k1 context that can only be used for signing
213+
///
214+
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
215+
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
202216
pub fn signing_only() -> Secp256k1<SignOnly> {
203-
Secp256k1::gen_new()
217+
let mut ctx = Secp256k1::gen_new();
218+
#[cfg(feature = "rand-std")]
219+
{
220+
ctx.randomize(&mut rand::thread_rng());
221+
}
222+
ctx
204223
}
205224
}
206225

0 commit comments

Comments
 (0)