@@ -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 {
@@ -40,7 +40,7 @@ pub mod global {
40
40
static ONCE : Once = Once :: new ( ) ;
41
41
static mut CONTEXT : Option < Secp256k1 < All > > = None ;
42
42
ONCE . call_once ( || unsafe {
43
- let mut ctx = Secp256k1 :: new ( ) ;
43
+ let mut ctx = Secp256k1 :: new_no_randomize ( ) ;
44
44
#[ cfg( feature = "global-context" ) ]
45
45
{
46
46
ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
@@ -167,8 +167,8 @@ mod alloc_only {
167
167
}
168
168
169
169
impl < C : Context > Secp256k1 < C > {
170
- /// Lets you create a context in a generic manner(sign/verify/all)
171
- pub fn gen_new ( ) -> Secp256k1 < C > {
170
+ /// Helper function only intended to be called by other gen_new_* functions.
171
+ fn _gen_new ( ) -> Secp256k1 < C > {
172
172
#[ cfg( target_arch = "wasm32" ) ]
173
173
ffi:: types:: sanity_checks_for_wasm ( ) ;
174
174
@@ -181,32 +181,118 @@ mod alloc_only {
181
181
size,
182
182
}
183
183
}
184
+
185
+ /// Lets you create a context in a generic manner(sign/verify/all).
186
+ ///
187
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
188
+ /// protection.
189
+ #[ cfg( feature = "rand-std" ) ]
190
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
191
+ pub fn gen_new_randomize ( ) -> Secp256k1 < C > {
192
+ let mut secp = Secp256k1 :: _gen_new ( ) ;
193
+ secp. randomize ( & mut rand:: 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
+ }
184
214
}
185
215
186
216
impl Secp256k1 < All > {
187
- /// Creates a new Secp256k1 context with all capabilities
188
- pub fn new ( ) -> Secp256k1 < All > {
189
- 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)
190
240
}
191
241
}
192
242
193
243
impl Secp256k1 < SignOnly > {
194
- /// Creates a new Secp256k1 context that can only be used for signing
195
- pub fn signing_only ( ) -> Secp256k1 < SignOnly > {
196
- 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)
197
268
}
198
269
}
199
270
200
271
impl Secp256k1 < VerifyOnly > {
201
- /// Creates a new Secp256k1 context that can only be used for verification
202
- pub fn verification_only ( ) -> Secp256k1 < VerifyOnly > {
203
- 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 ( )
204
288
}
205
- }
206
289
207
- impl Default for Secp256k1 < All > {
208
- fn default ( ) -> Self {
209
- 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)
210
296
}
211
297
}
212
298
0 commit comments