11use core:: marker:: PhantomData ;
22use core:: mem:: ManuallyDrop ;
33
4- use crate :: { Error , Secp256k1 } ;
5- use crate :: ffi:: { self , CPtr , types:: AlignedType } ;
6- use crate :: ffi:: types:: { c_uint, c_void} ;
7-
84#[ cfg( feature = "alloc" ) ]
95#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) ) ) ]
106pub 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 } ;
1110
1211#[ cfg( all( feature = "global-context" , feature = "std" ) ) ]
1312#[ cfg_attr( docsrs, doc( cfg( all( feature = "global-context" , feature = "std" ) ) ) ) ]
@@ -41,13 +40,17 @@ pub mod global {
4140 impl Deref for GlobalContext {
4241 type Target = Secp256k1 < All > ;
4342
44- #[ allow( unused_mut) ] // Unused when `rand-std` is not enabled.
43+ #[ allow( unused_mut) ] // Unused when `rand-std` is not enabled.
4544 fn deref ( & self ) -> & Self :: Target {
4645 static ONCE : Once = Once :: new ( ) ;
4746 static mut CONTEXT : Option < Secp256k1 < All > > = None ;
4847 ONCE . call_once ( || unsafe {
4948 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+ ) ) ]
5154 {
5255 ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
5356 }
@@ -58,10 +61,9 @@ pub mod global {
5861 }
5962}
6063
61-
6264/// A trait for all kinds of contexts that lets you define the exact flags and a function to
6365/// 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 {
6567 /// Flags for the ffi.
6668 const FLAGS : c_uint ;
6769 /// A constant description of the context.
@@ -106,13 +108,13 @@ mod private {
106108#[ cfg( feature = "alloc" ) ]
107109#[ cfg_attr( docsrs, doc( cfg( any( feature = "alloc" ) ) ) ) ]
108110mod alloc_only {
109- use crate :: alloc:: alloc;
110-
111111 use core:: marker:: PhantomData ;
112112
113113 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 } ;
116118
117119 impl private:: Sealed for SignOnly { }
118120 impl private:: Sealed for All { }
@@ -195,16 +197,22 @@ mod alloc_only {
195197
196198 let size = unsafe { ffi:: secp256k1_context_preallocated_size ( C :: FLAGS ) } ;
197199 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) } ;
199201
200202 #[ allow( unused_mut) ] // ctx is not mutated under some feature combinations.
201203 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+ } ,
203207 phantom : PhantomData ,
204208 size,
205209 } ;
206210
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+ ) ) ]
208216 {
209217 ctx. randomize ( & mut rand:: thread_rng ( ) ) ;
210218 }
@@ -220,9 +228,7 @@ mod alloc_only {
220228 /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
221229 /// If `rand-std` feature is not enabled please consider randomizing the context (see docs
222230 /// for `Secp256k1::gen_new()`).
223- pub fn new ( ) -> Secp256k1 < All > {
224- Secp256k1 :: gen_new ( )
225- }
231+ pub fn new ( ) -> Secp256k1 < All > { Secp256k1 :: gen_new ( ) }
226232 }
227233
228234 impl Secp256k1 < SignOnly > {
@@ -231,9 +237,7 @@ mod alloc_only {
231237 /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
232238 /// If `rand-std` feature is not enabled please consider randomizing the context (see docs
233239 /// 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 ( ) }
237241 }
238242
239243 impl Secp256k1 < VerifyOnly > {
@@ -242,24 +246,22 @@ mod alloc_only {
242246 /// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
243247 /// If `rand-std` feature is not enabled please consider randomizing the context (see docs
244248 /// 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 ( ) }
248250 }
249251
250252 impl Default for Secp256k1 < All > {
251- fn default ( ) -> Self {
252- Self :: new ( )
253- }
253+ fn default ( ) -> Self { Self :: new ( ) }
254254 }
255255
256256 impl < C : Context > Clone for Secp256k1 < C > {
257257 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 _ ) } ;
259259 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) } ;
261261 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+ } ,
263265 phantom : PhantomData ,
264266 size,
265267 }
@@ -313,7 +315,8 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
313315 ctx : unsafe {
314316 ffi:: secp256k1_context_preallocated_create (
315317 buf. as_mut_c_ptr ( ) as * mut c_void ,
316- C :: FLAGS )
318+ C :: FLAGS ,
319+ )
317320 } ,
318321 phantom : PhantomData ,
319322 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> {
323326
324327impl < ' buf > Secp256k1 < AllPreallocated < ' buf > > {
325328 /// 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 > {
327332 Secp256k1 :: preallocated_gen_new ( buf)
328333 }
329334 /// 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 ( ) }
333336
334337 /// Create a context from a raw context.
335338 ///
@@ -342,7 +345,9 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
342345 /// * The user must handle the freeing of the context(using the correct functions) by himself.
343346 /// * Violating these may lead to Undefined Behavior.
344347 ///
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 > > > {
346351 ManuallyDrop :: new ( Secp256k1 {
347352 ctx : raw_ctx,
348353 phantom : PhantomData ,
@@ -353,15 +358,15 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
353358
354359impl < ' buf > Secp256k1 < SignOnlyPreallocated < ' buf > > {
355360 /// 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 > {
357364 Secp256k1 :: preallocated_gen_new ( buf)
358365 }
359366
360367 /// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context.
361368 #[ 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 ( ) }
365370
366371 /// Create a context from a raw context.
367372 ///
@@ -374,7 +379,9 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
374379 /// * The user must handle the freeing of the context(using the correct functions) by himself.
375380 /// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
376381 ///
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 > > > {
378385 ManuallyDrop :: new ( Secp256k1 {
379386 ctx : raw_ctx,
380387 phantom : PhantomData ,
@@ -385,15 +392,15 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
385392
386393impl < ' buf > Secp256k1 < VerifyOnlyPreallocated < ' buf > > {
387394 /// 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 > {
389398 Secp256k1 :: preallocated_gen_new ( buf)
390399 }
391400
392401 /// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context.
393402 #[ 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 ( ) }
397404
398405 /// Create a context from a raw context.
399406 ///
@@ -406,7 +413,9 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
406413 /// * The user must handle the freeing of the context(using the correct functions) by himself.
407414 /// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
408415 ///
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 > > > {
410419 ManuallyDrop :: new ( Secp256k1 {
411420 ctx : raw_ctx,
412421 phantom : PhantomData ,
0 commit comments