@@ -29,7 +29,7 @@ pub mod global {
29
29
/// A global, static context to avoid repeatedly creating contexts where one can't be passed
30
30
///
31
31
/// If the global-context feature is enabled (and not just the global-context-less-secure),
32
- /// this will have been randomized.
32
+ /// this will have been randomized for additional defense-in-depth side channel protection .
33
33
pub static SECP256K1 : & GlobalContext = & GlobalContext { __private : ( ) } ;
34
34
35
35
impl Deref for GlobalContext {
@@ -39,7 +39,7 @@ pub mod global {
39
39
static ONCE : Once = Once :: new ( ) ;
40
40
static mut CONTEXT : Option < Secp256k1 < All > > = None ;
41
41
ONCE . call_once ( || unsafe {
42
- let mut ctx = Secp256k1 :: new ( ) ;
42
+ let mut ctx = Secp256k1 :: new_no_randomize ( ) ;
43
43
#[ cfg( feature = "global-context" ) ]
44
44
{
45
45
ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
@@ -166,8 +166,8 @@ mod alloc_only {
166
166
}
167
167
168
168
impl < C : Context > Secp256k1 < C > {
169
- /// Lets you create a context in a generic manner(sign/verify/all)
170
- pub fn gen_new ( ) -> Secp256k1 < C > {
169
+ /// Helper function only intended to be called by other gen_new_* functions.
170
+ fn _gen_new ( ) -> Secp256k1 < C > {
171
171
#[ cfg( target_arch = "wasm32" ) ]
172
172
ffi:: types:: sanity_checks_for_wasm ( ) ;
173
173
@@ -180,32 +180,119 @@ mod alloc_only {
180
180
size,
181
181
}
182
182
}
183
+
184
+ /// Lets you create a context in a generic manner(sign/verify/all).
185
+ ///
186
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
187
+ /// protection.
188
+ #[ cfg( feature = "rand-std" ) ]
189
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
190
+ pub fn gen_new_randomize ( ) -> Secp256k1 < C > {
191
+ use rand:: thread_rng;
192
+ let mut secp = Secp256k1 :: _gen_new ( ) ;
193
+ secp. randomize ( & mut thread_rng ( ) ) ;
194
+ secp
195
+ }
196
+
197
+ /// Lets you create a context in a generic manner(sign/verify/all).
198
+ ///
199
+ /// No randomization is done, this context is perfectly safe but for additional
200
+ /// defense-in-depth side channel protection consider using [`gen_new_seeded_randomize`].
201
+ pub fn gen_new_no_randomize ( ) -> Secp256k1 < C > {
202
+ Secp256k1 :: _gen_new ( )
203
+ }
204
+
205
+ /// Lets you create a context in a generic manner(sign/verify/all).
206
+ ///
207
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
208
+ /// protection.
209
+ pub fn gen_new_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < C > {
210
+ let mut secp = Secp256k1 :: _gen_new ( ) ;
211
+ secp. seeded_randomize ( seed) ;
212
+ secp
213
+ }
183
214
}
184
215
185
216
impl Secp256k1 < All > {
186
- /// Creates a new Secp256k1 context with all capabilities
187
- pub fn new ( ) -> Secp256k1 < All > {
188
- Secp256k1 :: gen_new ( )
217
+ /// Creates a new Secp256k1 context with all capabilities.
218
+ ///
219
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
220
+ /// protection.
221
+ #[ cfg( feature = "rand-std" ) ]
222
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
223
+ pub fn new_randomize ( ) -> Secp256k1 < All > {
224
+ Secp256k1 :: gen_new_randomize ( )
225
+ }
226
+ /// Creates a new Secp256k1 context with all capabilities.
227
+ ///
228
+ /// No randomization is done, this context is perfectly safe but for additional
229
+ /// defense-in-depth side channel protection consider using [`new_seeded_randomize`].
230
+ pub fn new_no_randomize ( ) -> Secp256k1 < All > {
231
+ Secp256k1 :: gen_new_no_randomize ( )
232
+ }
233
+
234
+ /// Creates a new Secp256k1 context with all capabilities.
235
+ ///
236
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
237
+ /// protection.
238
+ pub fn new_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < All > {
239
+ Secp256k1 :: gen_new_seeded_randomize ( seed)
189
240
}
190
241
}
191
242
192
243
impl Secp256k1 < SignOnly > {
193
- /// Creates a new Secp256k1 context that can only be used for signing
194
- pub fn signing_only ( ) -> Secp256k1 < SignOnly > {
195
- Secp256k1 :: gen_new ( )
244
+ /// Creates a new Secp256k1 context that can only be used for signing.
245
+ ///
246
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
247
+ /// protection.
248
+ #[ cfg( feature = "rand-std" ) ]
249
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
250
+ pub fn signing_only_randomize ( ) -> Secp256k1 < SignOnly > {
251
+ Secp256k1 :: gen_new_randomize ( )
252
+ }
253
+
254
+ /// Creates a new Secp256k1 context that can only be used for signing.
255
+ ///
256
+ /// No randomization is done, this context is perfectly safe but for additional
257
+ /// defense-in-depth side channel protection consider using [`signing_only_seeded_randomize`].
258
+ pub fn signing_only_no_randomize ( ) -> Secp256k1 < SignOnly > {
259
+ Secp256k1 :: gen_new_no_randomize ( )
260
+ }
261
+
262
+ /// Creates a new Secp256k1 context that can only be used for signing.
263
+ ///
264
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
265
+ /// protection.
266
+ pub fn signing_only_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < SignOnly > {
267
+ Secp256k1 :: gen_new_seeded_randomize ( seed)
196
268
}
197
269
}
198
270
199
271
impl Secp256k1 < VerifyOnly > {
200
- /// Creates a new Secp256k1 context that can only be used for verification
201
- pub fn verification_only ( ) -> Secp256k1 < VerifyOnly > {
202
- Secp256k1 :: gen_new ( )
272
+ /// Creates a new Secp256k1 context that can only be used for verifying.
273
+ ///
274
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
275
+ /// protection.
276
+ #[ cfg( feature = "rand-std" ) ]
277
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
278
+ pub fn verification_only_randomize ( ) -> Secp256k1 < VerifyOnly > {
279
+ Secp256k1 :: gen_new_randomize ( )
280
+ }
281
+
282
+ /// Creates a new Secp256k1 context that can only be used for verifying.
283
+ ///
284
+ /// No randomization is done, this context is perfectly safe but for additional
285
+ /// defense-in-depth side channel protection consider using [`verification_only_seeded_randomize`].
286
+ pub fn verification_only_no_randomize ( ) -> Secp256k1 < VerifyOnly > {
287
+ Secp256k1 :: gen_new_no_randomize ( )
203
288
}
204
- }
205
289
206
- impl Default for Secp256k1 < All > {
207
- fn default ( ) -> Self {
208
- Self :: new ( )
290
+ /// Creates a new Secp256k1 context that can only be used for verifying.
291
+ ///
292
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
293
+ /// protection.
294
+ pub fn verification_only_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < VerifyOnly > {
295
+ Secp256k1 :: gen_new_seeded_randomize ( seed)
209
296
}
210
297
}
211
298
0 commit comments