@@ -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,119 @@ 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
+ use rand:: thread_rng;
193
+ let mut secp = Secp256k1 :: _gen_new ( ) ;
194
+ secp. randomize ( & mut thread_rng ( ) ) ;
195
+ secp
196
+ }
197
+
198
+ /// Lets you create a context in a generic manner(sign/verify/all).
199
+ ///
200
+ /// No randomization is done, this context is perfectly safe but for additional
201
+ /// defense-in-depth side channel protection consider using [`gen_new_seeded_randomize`].
202
+ pub fn gen_new_no_randomize ( ) -> Secp256k1 < C > {
203
+ Secp256k1 :: _gen_new ( )
204
+ }
205
+
206
+ /// Lets you create a context in a generic manner(sign/verify/all).
207
+ ///
208
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
209
+ /// protection.
210
+ pub fn gen_new_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < C > {
211
+ let mut secp = Secp256k1 :: _gen_new ( ) ;
212
+ secp. seeded_randomize ( seed) ;
213
+ secp
214
+ }
184
215
}
185
216
186
217
impl Secp256k1 < All > {
187
- /// Creates a new Secp256k1 context with all capabilities
188
- pub fn new ( ) -> Secp256k1 < All > {
189
- Secp256k1 :: gen_new ( )
218
+ /// Creates a new Secp256k1 context with all capabilities.
219
+ ///
220
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
221
+ /// protection.
222
+ #[ cfg( feature = "rand-std" ) ]
223
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
224
+ pub fn new_randomize ( ) -> Secp256k1 < All > {
225
+ Secp256k1 :: gen_new_randomize ( )
226
+ }
227
+ /// Creates a new Secp256k1 context with all capabilities.
228
+ ///
229
+ /// No randomization is done, this context is perfectly safe but for additional
230
+ /// defense-in-depth side channel protection consider using [`new_seeded_randomize`].
231
+ pub fn new_no_randomize ( ) -> Secp256k1 < All > {
232
+ Secp256k1 :: gen_new_no_randomize ( )
233
+ }
234
+
235
+ /// Creates a new Secp256k1 context with all capabilities.
236
+ ///
237
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
238
+ /// protection.
239
+ pub fn new_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < All > {
240
+ Secp256k1 :: gen_new_seeded_randomize ( seed)
190
241
}
191
242
}
192
243
193
244
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 ( )
245
+ /// Creates a new Secp256k1 context that can only be used for signing.
246
+ ///
247
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
248
+ /// protection.
249
+ #[ cfg( feature = "rand-std" ) ]
250
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
251
+ pub fn signing_only_randomize ( ) -> Secp256k1 < SignOnly > {
252
+ Secp256k1 :: gen_new_randomize ( )
253
+ }
254
+
255
+ /// Creates a new Secp256k1 context that can only be used for signing.
256
+ ///
257
+ /// No randomization is done, this context is perfectly safe but for additional
258
+ /// defense-in-depth side channel protection consider using [`signing_only_seeded_randomize`].
259
+ pub fn signing_only_no_randomize ( ) -> Secp256k1 < SignOnly > {
260
+ Secp256k1 :: gen_new_no_randomize ( )
261
+ }
262
+
263
+ /// Creates a new Secp256k1 context that can only be used for signing.
264
+ ///
265
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
266
+ /// protection.
267
+ pub fn signing_only_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < SignOnly > {
268
+ Secp256k1 :: gen_new_seeded_randomize ( seed)
197
269
}
198
270
}
199
271
200
272
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 ( )
273
+ /// Creates a new Secp256k1 context that can only be used for verifying.
274
+ ///
275
+ /// Context is randomized using `thread_rng` for additional defense-in-depth side channel
276
+ /// protection.
277
+ #[ cfg( feature = "rand-std" ) ]
278
+ #[ cfg_attr( docsrs, doc( cfg( feature = "rand-std" ) ) ) ]
279
+ pub fn verification_only_randomize ( ) -> Secp256k1 < VerifyOnly > {
280
+ Secp256k1 :: gen_new_randomize ( )
281
+ }
282
+
283
+ /// Creates a new Secp256k1 context that can only be used for verifying.
284
+ ///
285
+ /// No randomization is done, this context is perfectly safe but for additional
286
+ /// defense-in-depth side channel protection consider using [`verification_only_seeded_randomize`].
287
+ pub fn verification_only_no_randomize ( ) -> Secp256k1 < VerifyOnly > {
288
+ Secp256k1 :: gen_new_no_randomize ( )
204
289
}
205
- }
206
290
207
- impl Default for Secp256k1 < All > {
208
- fn default ( ) -> Self {
209
- Self :: new ( )
291
+ /// Creates a new Secp256k1 context that can only be used for verifying.
292
+ ///
293
+ /// Context is randomized using `seed` for additional defense-in-depth side channel
294
+ /// protection.
295
+ pub fn verification_only_seeded_randomize ( seed : & [ u8 ; 32 ] ) -> Secp256k1 < VerifyOnly > {
296
+ Secp256k1 :: gen_new_seeded_randomize ( seed)
210
297
}
211
298
}
212
299
0 commit comments