1
1
use core:: marker:: PhantomData ;
2
2
use core:: mem:: ManuallyDrop ;
3
3
4
- use crate :: { Error , Secp256k1 } ;
5
- use crate :: ffi:: { self , CPtr , types:: AlignedType } ;
6
- use crate :: ffi:: types:: { c_uint, c_void} ;
7
-
8
4
#[ cfg( feature = "alloc" ) ]
9
5
#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) ) ) ]
10
6
pub use self :: alloc_only:: * ;
7
+ use crate :: ffi:: types:: { c_uint, c_void, AlignedType } ;
8
+ use crate :: ffi:: { self , CPtr } ;
9
+ use crate :: { Error , Secp256k1 } ;
11
10
12
11
#[ cfg( all( feature = "global-context" , feature = "std" ) ) ]
13
12
#[ cfg_attr( docsrs, doc( cfg( all( feature = "global-context" , feature = "std" ) ) ) ) ]
@@ -41,13 +40,17 @@ pub mod global {
41
40
impl Deref for GlobalContext {
42
41
type Target = Secp256k1 < All > ;
43
42
44
- #[ allow( unused_mut) ] // Unused when `rand-std` is not enabled.
43
+ #[ allow( unused_mut) ] // Unused when `rand-std` is not enabled.
45
44
fn deref ( & self ) -> & Self :: Target {
46
45
static ONCE : Once = Once :: new ( ) ;
47
46
static mut CONTEXT : Option < Secp256k1 < All > > = None ;
48
47
ONCE . call_once ( || unsafe {
49
48
let mut ctx = Secp256k1 :: new ( ) ;
50
- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "rand-std" , not( feature = "global-context-less-secure" ) ) ) ]
49
+ #[ cfg( all(
50
+ not( target_arch = "wasm32" ) ,
51
+ feature = "rand-std" ,
52
+ not( feature = "global-context-less-secure" )
53
+ ) ) ]
51
54
{
52
55
ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
53
56
}
@@ -58,10 +61,9 @@ pub mod global {
58
61
}
59
62
}
60
63
61
-
62
64
/// A trait for all kinds of contexts that lets you define the exact flags and a function to
63
65
/// deallocate memory. It isn't possible to implement this for types outside this crate.
64
- pub unsafe trait Context : private:: Sealed {
66
+ pub unsafe trait Context : private:: Sealed {
65
67
/// Flags for the ffi.
66
68
const FLAGS : c_uint ;
67
69
/// A constant description of the context.
@@ -106,13 +108,13 @@ mod private {
106
108
#[ cfg( feature = "alloc" ) ]
107
109
#[ cfg_attr( docsrs, doc( cfg( any( feature = "alloc" ) ) ) ) ]
108
110
mod alloc_only {
109
- use crate :: alloc:: alloc;
110
-
111
111
use core:: marker:: PhantomData ;
112
112
113
113
use super :: private;
114
- use crate :: ffi:: { self , types:: { c_uint, c_void} } ;
115
- use crate :: { Secp256k1 , Signing , Verification , Context , AlignedType } ;
114
+ use crate :: alloc:: alloc;
115
+ use crate :: ffi:: types:: { c_uint, c_void} ;
116
+ use crate :: ffi:: { self } ;
117
+ use crate :: { AlignedType , Context , Secp256k1 , Signing , Verification } ;
116
118
117
119
impl private:: Sealed for SignOnly { }
118
120
impl private:: Sealed for All { }
@@ -195,16 +197,22 @@ mod alloc_only {
195
197
196
198
let size = unsafe { ffi:: secp256k1_context_preallocated_size ( C :: FLAGS ) } ;
197
199
let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
198
- let ptr = unsafe { alloc:: alloc ( layout) } ;
200
+ let ptr = unsafe { alloc:: alloc ( layout) } ;
199
201
200
202
#[ allow( unused_mut) ] // ctx is not mutated under some feature combinations.
201
203
let mut ctx = Secp256k1 {
202
- ctx : unsafe { ffi:: secp256k1_context_preallocated_create ( ptr as * mut c_void , C :: FLAGS ) } ,
204
+ ctx : unsafe {
205
+ ffi:: secp256k1_context_preallocated_create ( ptr as * mut c_void , C :: FLAGS )
206
+ } ,
203
207
phantom : PhantomData ,
204
208
size,
205
209
} ;
206
210
207
- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "rand-std" , not( feature = "global-context-less-secure" ) ) ) ]
211
+ #[ cfg( all(
212
+ not( target_arch = "wasm32" ) ,
213
+ feature = "rand-std" ,
214
+ not( feature = "global-context-less-secure" )
215
+ ) ) ]
208
216
{
209
217
ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
210
218
}
@@ -220,9 +228,7 @@ mod alloc_only {
220
228
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
221
229
/// If `rand-std` feature is not enabled please consider randomizing the context (see docs
222
230
/// for `Secp256k1::gen_new()`).
223
- pub fn new ( ) -> Secp256k1 < All > {
224
- Secp256k1 :: gen_new ( )
225
- }
231
+ pub fn new ( ) -> Secp256k1 < All > { Secp256k1 :: gen_new ( ) }
226
232
}
227
233
228
234
impl Secp256k1 < SignOnly > {
@@ -231,9 +237,7 @@ mod alloc_only {
231
237
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
232
238
/// If `rand-std` feature is not enabled please consider randomizing the context (see docs
233
239
/// for `Secp256k1::gen_new()`).
234
- pub fn signing_only ( ) -> Secp256k1 < SignOnly > {
235
- Secp256k1 :: gen_new ( )
236
- }
240
+ pub fn signing_only ( ) -> Secp256k1 < SignOnly > { Secp256k1 :: gen_new ( ) }
237
241
}
238
242
239
243
impl Secp256k1 < VerifyOnly > {
@@ -242,24 +246,22 @@ mod alloc_only {
242
246
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
243
247
/// If `rand-std` feature is not enabled please consider randomizing the context (see docs
244
248
/// for `Secp256k1::gen_new()`).
245
- pub fn verification_only ( ) -> Secp256k1 < VerifyOnly > {
246
- Secp256k1 :: gen_new ( )
247
- }
249
+ pub fn verification_only ( ) -> Secp256k1 < VerifyOnly > { Secp256k1 :: gen_new ( ) }
248
250
}
249
251
250
252
impl Default for Secp256k1 < All > {
251
- fn default ( ) -> Self {
252
- Self :: new ( )
253
- }
253
+ fn default ( ) -> Self { Self :: new ( ) }
254
254
}
255
255
256
256
impl < C : Context > Clone for Secp256k1 < C > {
257
257
fn clone ( & self ) -> Secp256k1 < C > {
258
- let size = unsafe { ffi:: secp256k1_context_preallocated_clone_size ( self . ctx as _ ) } ;
258
+ let size = unsafe { ffi:: secp256k1_context_preallocated_clone_size ( self . ctx as _ ) } ;
259
259
let layout = alloc:: Layout :: from_size_align ( size, ALIGN_TO ) . unwrap ( ) ;
260
- let ptr = unsafe { alloc:: alloc ( layout) } ;
260
+ let ptr = unsafe { alloc:: alloc ( layout) } ;
261
261
Secp256k1 {
262
- ctx : unsafe { ffi:: secp256k1_context_preallocated_clone ( self . ctx , ptr as * mut c_void ) } ,
262
+ ctx : unsafe {
263
+ ffi:: secp256k1_context_preallocated_clone ( self . ctx , ptr as * mut c_void )
264
+ } ,
263
265
phantom : PhantomData ,
264
266
size,
265
267
}
@@ -313,7 +315,8 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
313
315
ctx : unsafe {
314
316
ffi:: secp256k1_context_preallocated_create (
315
317
buf. as_mut_c_ptr ( ) as * mut c_void ,
316
- C :: FLAGS )
318
+ C :: FLAGS ,
319
+ )
317
320
} ,
318
321
phantom : PhantomData ,
319
322
size : 0 , // We don't care about the size because it's the caller responsibility to deallocate.
@@ -323,13 +326,13 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
323
326
324
327
impl < ' buf > Secp256k1 < AllPreallocated < ' buf > > {
325
328
/// Creates a new Secp256k1 context with all capabilities
326
- pub fn preallocated_new ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < AllPreallocated < ' buf > > , Error > {
329
+ pub fn preallocated_new (
330
+ buf : & ' buf mut [ AlignedType ] ,
331
+ ) -> Result < Secp256k1 < AllPreallocated < ' buf > > , Error > {
327
332
Secp256k1 :: preallocated_gen_new ( buf)
328
333
}
329
334
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context.
330
- pub fn preallocate_size ( ) -> usize {
331
- Self :: preallocate_size_gen ( )
332
- }
335
+ pub fn preallocate_size ( ) -> usize { Self :: preallocate_size_gen ( ) }
333
336
334
337
/// Create a context from a raw context.
335
338
///
@@ -342,7 +345,9 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
342
345
/// * The user must handle the freeing of the context(using the correct functions) by himself.
343
346
/// * Violating these may lead to Undefined Behavior.
344
347
///
345
- pub unsafe fn from_raw_all ( raw_ctx : * mut ffi:: Context ) -> ManuallyDrop < Secp256k1 < AllPreallocated < ' buf > > > {
348
+ pub unsafe fn from_raw_all (
349
+ raw_ctx : * mut ffi:: Context ,
350
+ ) -> ManuallyDrop < Secp256k1 < AllPreallocated < ' buf > > > {
346
351
ManuallyDrop :: new ( Secp256k1 {
347
352
ctx : raw_ctx,
348
353
phantom : PhantomData ,
@@ -353,15 +358,15 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
353
358
354
359
impl < ' buf > Secp256k1 < SignOnlyPreallocated < ' buf > > {
355
360
/// Creates a new Secp256k1 context that can only be used for signing.
356
- pub fn preallocated_signing_only ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < SignOnlyPreallocated < ' buf > > , Error > {
361
+ pub fn preallocated_signing_only (
362
+ buf : & ' buf mut [ AlignedType ] ,
363
+ ) -> Result < Secp256k1 < SignOnlyPreallocated < ' buf > > , Error > {
357
364
Secp256k1 :: preallocated_gen_new ( buf)
358
365
}
359
366
360
367
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context.
361
368
#[ inline]
362
- pub fn preallocate_signing_size ( ) -> usize {
363
- Self :: preallocate_size_gen ( )
364
- }
369
+ pub fn preallocate_signing_size ( ) -> usize { Self :: preallocate_size_gen ( ) }
365
370
366
371
/// Create a context from a raw context.
367
372
///
@@ -374,7 +379,9 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
374
379
/// * The user must handle the freeing of the context(using the correct functions) by himself.
375
380
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
376
381
///
377
- pub unsafe fn from_raw_signing_only ( raw_ctx : * mut ffi:: Context ) -> ManuallyDrop < Secp256k1 < SignOnlyPreallocated < ' buf > > > {
382
+ pub unsafe fn from_raw_signing_only (
383
+ raw_ctx : * mut ffi:: Context ,
384
+ ) -> ManuallyDrop < Secp256k1 < SignOnlyPreallocated < ' buf > > > {
378
385
ManuallyDrop :: new ( Secp256k1 {
379
386
ctx : raw_ctx,
380
387
phantom : PhantomData ,
@@ -385,15 +392,15 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
385
392
386
393
impl < ' buf > Secp256k1 < VerifyOnlyPreallocated < ' buf > > {
387
394
/// Creates a new Secp256k1 context that can only be used for verification
388
- pub fn preallocated_verification_only ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < VerifyOnlyPreallocated < ' buf > > , Error > {
395
+ pub fn preallocated_verification_only (
396
+ buf : & ' buf mut [ AlignedType ] ,
397
+ ) -> Result < Secp256k1 < VerifyOnlyPreallocated < ' buf > > , Error > {
389
398
Secp256k1 :: preallocated_gen_new ( buf)
390
399
}
391
400
392
401
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context.
393
402
#[ inline]
394
- pub fn preallocate_verification_size ( ) -> usize {
395
- Self :: preallocate_size_gen ( )
396
- }
403
+ pub fn preallocate_verification_size ( ) -> usize { Self :: preallocate_size_gen ( ) }
397
404
398
405
/// Create a context from a raw context.
399
406
///
@@ -406,7 +413,9 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
406
413
/// * The user must handle the freeing of the context(using the correct functions) by himself.
407
414
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
408
415
///
409
- pub unsafe fn from_raw_verification_only ( raw_ctx : * mut ffi:: Context ) -> ManuallyDrop < Secp256k1 < VerifyOnlyPreallocated < ' buf > > > {
416
+ pub unsafe fn from_raw_verification_only (
417
+ raw_ctx : * mut ffi:: Context ,
418
+ ) -> ManuallyDrop < Secp256k1 < VerifyOnlyPreallocated < ' buf > > > {
410
419
ManuallyDrop :: new ( Secp256k1 {
411
420
ctx : raw_ctx,
412
421
phantom : PhantomData ,
0 commit comments