Skip to content

Commit 7f8b0cd

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

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/context.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,32 @@ mod alloc_only {
191191
}
192192

193193
impl Secp256k1<All> {
194-
/// Creates a new Secp256k1 context with all capabilities
194+
/// Creates a new Secp256k1 context with all capabilities.
195+
///
196+
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
197+
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
195198
pub fn new() -> Secp256k1<All> {
196-
Secp256k1::gen_new()
199+
let mut ctx = Secp256k1::gen_new();
200+
#[cfg(feature = "rand-std")]
201+
{
202+
ctx.randomize(&mut rand::thread_rng());
203+
}
204+
ctx
197205
}
198206
}
199207

200208
impl Secp256k1<SignOnly> {
201209
/// Creates a new Secp256k1 context that can only be used for signing
210+
///
211+
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
212+
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
202213
pub fn signing_only() -> Secp256k1<SignOnly> {
203-
Secp256k1::gen_new()
214+
let mut ctx = Secp256k1::gen_new();
215+
#[cfg(feature = "rand-std")]
216+
{
217+
ctx.randomize(&mut rand::thread_rng());
218+
}
219+
ctx
204220
}
205221
}
206222

0 commit comments

Comments
 (0)