Skip to content

Commit e0e575d

Browse files
committed
Run cargo fmt
Run the command `cargo +nightly fmt` to fix formatting issues. The formatter got confused in one place, adding an incorrect indentation, this was manually fixed.
1 parent 41449e4 commit e0e575d

15 files changed

+731
-747
lines changed

examples/sign_verify.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ extern crate bitcoin_hashes;
22
extern crate secp256k1;
33

44
use bitcoin_hashes::{sha256, Hash};
5-
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, ecdsa, Signing, Verification};
6-
7-
fn verify<C: Verification>(secp: &Secp256k1<C>, msg: &[u8], sig: [u8; 64], pubkey: [u8; 33]) -> Result<bool, Error> {
5+
use secp256k1::{ecdsa, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
6+
7+
fn verify<C: Verification>(
8+
secp: &Secp256k1<C>,
9+
msg: &[u8],
10+
sig: [u8; 64],
11+
pubkey: [u8; 33],
12+
) -> Result<bool, Error> {
813
let msg = sha256::Hash::hash(msg);
914
let msg = Message::from_slice(&msg)?;
1015
let sig = ecdsa::Signature::from_compact(&sig)?;
@@ -13,7 +18,11 @@ fn verify<C: Verification>(secp: &Secp256k1<C>, msg: &[u8], sig: [u8; 64], pubke
1318
Ok(secp.verify_ecdsa(&msg, &sig, &pubkey).is_ok())
1419
}
1520

16-
fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<ecdsa::Signature, Error> {
21+
fn sign<C: Signing>(
22+
secp: &Secp256k1<C>,
23+
msg: &[u8],
24+
seckey: [u8; 32],
25+
) -> Result<ecdsa::Signature, Error> {
1726
let msg = sha256::Hash::hash(msg);
1827
let msg = Message::from_slice(&msg)?;
1928
let seckey = SecretKey::from_slice(&seckey)?;
@@ -23,8 +32,14 @@ fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result
2332
fn main() {
2433
let secp = Secp256k1::new();
2534

26-
let seckey = [59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28];
27-
let pubkey = [2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54];
35+
let seckey = [
36+
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253,
37+
102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
38+
];
39+
let pubkey = [
40+
2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141,
41+
134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
42+
];
2843
let msg = b"This is some message";
2944

3045
let signature = sign(&secp, msg, seckey).unwrap();

examples/sign_verify_recovery.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
21
extern crate bitcoin_hashes;
32
extern crate secp256k1;
43

54
use bitcoin_hashes::{sha256, Hash};
6-
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification, ecdsa};
7-
8-
fn recover<C: Verification>(secp: &Secp256k1<C>,msg: &[u8],sig: [u8; 64],recovery_id: u8) -> Result<PublicKey, Error> {
5+
use secp256k1::{ecdsa, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
6+
7+
fn recover<C: Verification>(
8+
secp: &Secp256k1<C>,
9+
msg: &[u8],
10+
sig: [u8; 64],
11+
recovery_id: u8,
12+
) -> Result<PublicKey, Error> {
913
let msg = sha256::Hash::hash(msg);
1014
let msg = Message::from_slice(&msg)?;
1115
let id = ecdsa::RecoveryId::from_i32(recovery_id as i32)?;
@@ -14,7 +18,11 @@ fn recover<C: Verification>(secp: &Secp256k1<C>,msg: &[u8],sig: [u8; 64],recover
1418
secp.recover_ecdsa(&msg, &sig)
1519
}
1620

17-
fn sign_recovery<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<ecdsa::RecoverableSignature, Error> {
21+
fn sign_recovery<C: Signing>(
22+
secp: &Secp256k1<C>,
23+
msg: &[u8],
24+
seckey: [u8; 32],
25+
) -> Result<ecdsa::RecoverableSignature, Error> {
1826
let msg = sha256::Hash::hash(msg);
1927
let msg = Message::from_slice(&msg)?;
2028
let seckey = SecretKey::from_slice(&seckey)?;
@@ -25,22 +33,19 @@ fn main() {
2533
let secp = Secp256k1::new();
2634

2735
let seckey = [
28-
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107,
29-
94, 203, 174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
36+
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253,
37+
102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
3038
];
3139
let pubkey = PublicKey::from_slice(&[
32-
2,
33-
29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231,
34-
245, 41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
35-
]).unwrap();
40+
2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141,
41+
134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
42+
])
43+
.unwrap();
3644
let msg = b"This is some message";
3745

3846
let signature = sign_recovery(&secp, msg, seckey).unwrap();
3947

4048
let (recovery_id, serialize_sig) = signature.serialize_compact();
4149

42-
assert_eq!(
43-
recover(&secp, msg, serialize_sig, recovery_id.to_i32() as u8),
44-
Ok(pubkey)
45-
);
50+
assert_eq!(recover(&secp, msg, serialize_sig, recovery_id.to_i32() as u8), Ok(pubkey));
4651
}

src/context.rs

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use core::marker::PhantomData;
22
use 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")))]
106
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};
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"))))]
108110
mod 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

324327
impl<'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

354359
impl<'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

386393
impl<'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

Comments
 (0)