@@ -29,17 +29,18 @@ 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 {
36
36
type Target = Secp256k1 < All > ;
37
37
38
+ #[ allow( unused_mut) ] // mut is unused when "global-context" is not enabled.
38
39
fn deref ( & self ) -> & Self :: Target {
39
40
static ONCE : Once = Once :: new ( ) ;
40
41
static mut CONTEXT : Option < Secp256k1 < All > > = None ;
41
42
ONCE . call_once ( || unsafe {
42
- let mut ctx = Secp256k1 :: new ( ) ;
43
+ let mut ctx = Secp256k1 :: new_no_randomize ( ) ;
43
44
#[ cfg( feature = "global-context" ) ]
44
45
{
45
46
ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
@@ -166,8 +167,8 @@ mod alloc_only {
166
167
}
167
168
168
169
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 > {
170
+ /// Helper function only intended to be called by other gen_new_* functions.
171
+ fn _gen_new ( ) -> Secp256k1 < C > {
171
172
#[ cfg( target_arch = "wasm32" ) ]
172
173
ffi:: types:: sanity_checks_for_wasm ( ) ;
173
174
@@ -180,32 +181,118 @@ mod alloc_only {
180
181
size,
181
182
}
182
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" ) ]
190
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
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
+ }
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" ) ]
222
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
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" ) ]
249
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
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" ) ]
277
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand" ) ) ) ]
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