@@ -115,6 +115,9 @@ mod alloc_only {
115
115
#[ cfg( not( feature = "std" ) ) ]
116
116
use alloc:: alloc;
117
117
118
+ #[ cfg( feature = "rand-std" ) ]
119
+ use rand;
120
+
118
121
impl private:: Sealed for SignOnly { }
119
122
impl private:: Sealed for All { }
120
123
impl private:: Sealed for VerifyOnly { }
@@ -174,38 +177,54 @@ mod alloc_only {
174
177
}
175
178
176
179
impl < C : Context > Secp256k1 < C > {
177
- /// Lets you create a context in a generic manner(sign/verify/all)
180
+ /// Lets you create a context in a generic manner (sign/verify/all).
181
+ ///
182
+ /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
183
+ #[ allow( unused_mut) ] // Unused when `rand-std` is not enabled.
178
184
pub fn gen_new ( ) -> Secp256k1 < C > {
179
185
#[ cfg( target_arch = "wasm32" ) ]
180
186
ffi:: types:: sanity_checks_for_wasm ( ) ;
181
187
182
188
let size = unsafe { ffi:: secp256k1_context_preallocated_size ( C :: FLAGS ) } ;
183
189
let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
184
190
let ptr = unsafe { alloc:: alloc ( layout) } ;
185
- Secp256k1 {
191
+ let mut ctx = Secp256k1 {
186
192
ctx : unsafe { ffi:: secp256k1_context_preallocated_create ( ptr as * mut c_void , C :: FLAGS ) } ,
187
193
phantom : PhantomData ,
188
194
size,
195
+ } ;
196
+
197
+ #[ cfg( feature = "rand-std" ) ]
198
+ {
199
+ ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
189
200
}
201
+
202
+ ctx
190
203
}
191
204
}
192
205
193
206
impl Secp256k1 < All > {
194
- /// Creates a new Secp256k1 context with all capabilities
207
+ /// Creates a new Secp256k1 context with all capabilities.
208
+ ///
209
+ /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
195
210
pub fn new ( ) -> Secp256k1 < All > {
196
211
Secp256k1 :: gen_new ( )
197
212
}
198
213
}
199
214
200
215
impl Secp256k1 < SignOnly > {
201
- /// Creates a new Secp256k1 context that can only be used for signing
216
+ /// Creates a new Secp256k1 context that can only be used for signing.
217
+ ///
218
+ /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
202
219
pub fn signing_only ( ) -> Secp256k1 < SignOnly > {
203
220
Secp256k1 :: gen_new ( )
204
221
}
205
222
}
206
223
207
224
impl Secp256k1 < VerifyOnly > {
208
- /// Creates a new Secp256k1 context that can only be used for verification
225
+ /// Creates a new Secp256k1 context that can only be used for verification.
226
+ ///
227
+ /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
209
228
pub fn verification_only ( ) -> Secp256k1 < VerifyOnly > {
210
229
Secp256k1 :: gen_new ( )
211
230
}
0 commit comments