Skip to content

Commit 432f293

Browse files
committed
Merge #507: Minimise FFI in the public API
68c7385 Minimise FFI in the public API (Tobin C. Harding) Pull request description: Normal users should never need to directly interact with the FFI layer. Audit and reduce the use of `ffi` types in the public API of various types. Leave only the implementation of `CPtr`, and document this clearly as not required by normal users. Done for: - PublicKey - XOnlyPublicKey - KeyPair - ecdsa::Signature - ecdsa::RecoverableSignature ACKs for top commit: apoelstra: ACK 68c7385 Tree-SHA512: 8242527837872f9aba2aab19b02c2280ca1eb1dfd33c8ca619726d981811d72de3e5a57cbde2fbe621eb8e50e43f488804cd51d27949459da1c0ceb03fca35e3
2 parents 1911845 + 68c7385 commit 432f293

File tree

6 files changed

+59
-29
lines changed

6 files changed

+59
-29
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,12 @@ unsafe fn strlen(mut str_ptr: *const c_char) -> usize {
691691
}
692692

693693

694-
/// A trait for producing pointers that will always be valid in C. (assuming NULL pointer is a valid no-op)
695-
/// Rust doesn't promise what pointers does it give to ZST (<https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts>)
696-
/// In case the type is empty this trait will give a NULL pointer, which should be handled in C.
694+
/// A trait for producing pointers that will always be valid in C (assuming NULL pointer is a valid
695+
/// no-op).
697696
///
697+
/// Rust does not guarantee pointers to Zero Sized Types
698+
/// (<https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts>). In case the type
699+
/// is empty this trait will return a NULL pointer, which should be handled in C.
698700
pub trait CPtr {
699701
type Target;
700702
fn as_c_ptr(&self) -> *const Self::Target;

src/ecdh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub fn shared_secret_point(point: &PublicKey, scalar: &SecretKey) -> [u8; 64] {
150150
ffi::secp256k1_ecdh(
151151
ffi::secp256k1_context_no_precomp,
152152
xy.as_mut_ptr(),
153-
point.as_ptr(),
153+
point.as_c_ptr(),
154154
scalar.as_ptr(),
155155
Some(c_callback),
156156
ptr::null_mut(),

src/ecdsa/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,16 @@ impl Signature {
144144

145145
/// Obtains a raw pointer suitable for use with FFI functions
146146
#[inline]
147+
#[deprecated(since = "0.25.0", note = "Use Self::as_c_ptr if you need to access the FFI layer")]
147148
pub fn as_ptr(&self) -> *const ffi::Signature {
148-
&self.0
149+
self.as_c_ptr()
149150
}
150151

151152
/// Obtains a raw mutable pointer suitable for use with FFI functions
152153
#[inline]
154+
#[deprecated(since = "0.25.0", note = "Use Self::as_mut_c_ptr if you need to access the FFI layer")]
153155
pub fn as_mut_ptr(&mut self) -> *mut ffi::Signature {
154-
&mut self.0
156+
self.as_mut_c_ptr()
155157
}
156158

157159
#[inline]
@@ -199,11 +201,11 @@ impl CPtr for Signature {
199201
type Target = ffi::Signature;
200202

201203
fn as_c_ptr(&self) -> *const Self::Target {
202-
self.as_ptr()
204+
&self.0
203205
}
204206

205207
fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
206-
self.as_mut_ptr()
208+
&mut self.0
207209
}
208210
}
209211

@@ -307,7 +309,7 @@ impl<C: Signing> Secp256k1<C> {
307309

308310
counter += 1;
309311
extra_entropy[..4].copy_from_slice(&counter.to_le_bytes());
310-
entropy_p = extra_entropy.as_ptr().cast::<ffi::types::c_void>();
312+
entropy_p = extra_entropy.as_c_ptr().cast::<ffi::types::c_void>();
311313

312314
// When fuzzing, these checks will usually spinloop forever, so just short-circuit them.
313315
#[cfg(fuzzing)]

src/ecdsa/recovery.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ impl RecoverableSignature {
7676

7777
/// Obtains a raw pointer suitable for use with FFI functions.
7878
#[inline]
79+
#[deprecated(since = "0.25.0", note = "Use Self::as_c_ptr if you need to access the FFI layer")]
7980
pub fn as_ptr(&self) -> *const ffi::RecoverableSignature {
80-
&self.0
81+
self.as_c_ptr()
8182
}
8283

8384
/// Obtains a raw mutable pointer suitable for use with FFI functions.
8485
#[inline]
86+
#[deprecated(since = "0.25.0", note = "Use Self::as_mut_c_ptr if you need to access the FFI layer")]
8587
pub fn as_mut_ptr(&mut self) -> *mut ffi::RecoverableSignature {
86-
&mut self.0
88+
self.as_mut_c_ptr()
8789
}
8890

8991
#[inline]
@@ -133,11 +135,11 @@ impl RecoverableSignature {
133135
impl CPtr for RecoverableSignature {
134136
type Target = ffi::RecoverableSignature;
135137
fn as_c_ptr(&self) -> *const Self::Target {
136-
self.as_ptr()
138+
&self.0
137139
}
138140

139141
fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
140-
self.as_mut_ptr()
142+
&mut self.0
141143
}
142144
}
143145

src/key.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl SecretKey {
221221
let ret = ffi::secp256k1_keypair_sec(
222222
ffi::secp256k1_context_no_precomp,
223223
sk.as_mut_c_ptr(),
224-
keypair.as_ptr()
224+
keypair.as_c_ptr()
225225
);
226226
debug_assert_eq!(ret, 1);
227227
}
@@ -395,14 +395,16 @@ impl<'de> serde::Deserialize<'de> for SecretKey {
395395
impl PublicKey {
396396
/// Obtains a raw const pointer suitable for use with FFI functions.
397397
#[inline]
398+
#[deprecated(since = "0.25.0", note = "Use Self::as_c_ptr if you need to access the FFI layer")]
398399
pub fn as_ptr(&self) -> *const ffi::PublicKey {
399-
&self.0
400+
self.as_c_ptr()
400401
}
401402

402403
/// Obtains a raw mutable pointer suitable for use with FFI functions.
403404
#[inline]
405+
#[deprecated(since = "0.25.0", note = "Use Self::as_mut_c_ptr if you need to access the FFI layer")]
404406
pub fn as_mut_ptr(&mut self) -> *mut ffi::PublicKey {
405-
&mut self.0
407+
self.as_mut_c_ptr()
406408
}
407409

408410
/// Creates a new public key from a [`SecretKey`].
@@ -479,7 +481,7 @@ impl PublicKey {
479481
let ret = ffi::secp256k1_keypair_pub(
480482
ffi::secp256k1_context_no_precomp,
481483
&mut pk,
482-
keypair.as_ptr()
484+
keypair.as_c_ptr()
483485
);
484486
debug_assert_eq!(ret, 1);
485487
PublicKey(pk)
@@ -666,7 +668,7 @@ impl PublicKey {
666668
ffi::secp256k1_context_no_precomp,
667669
&mut xonly_pk,
668670
&mut pk_parity,
669-
self.as_ptr(),
671+
self.as_c_ptr(),
670672
);
671673
debug_assert_eq!(ret, 1);
672674
let parity = Parity::from_i32(pk_parity).expect("should not panic, pk_parity is 0 or 1");
@@ -676,19 +678,26 @@ impl PublicKey {
676678
}
677679
}
678680

681+
/// This trait enables interaction with the FFI layer and even though it is part of the public API
682+
/// normal users should never need to directly interact with FFI types.
679683
impl CPtr for PublicKey {
680684
type Target = ffi::PublicKey;
685+
686+
/// Obtains a const pointer suitable for use with FFI functions.
681687
fn as_c_ptr(&self) -> *const Self::Target {
682-
self.as_ptr()
688+
&self.0
683689
}
684690

691+
/// Obtains a mutable pointer suitable for use with FFI functions.
685692
fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
686-
self.as_mut_ptr()
693+
&mut self.0
687694
}
688695
}
689696

690697

691-
/// Creates a new public key from a FFI public key
698+
/// Creates a new public key from a FFI public key.
699+
///
700+
/// Note, normal users should never need to interact directly with FFI types.
692701
impl From<ffi::PublicKey> for PublicKey {
693702
#[inline]
694703
fn from(pk: ffi::PublicKey) -> PublicKey {
@@ -796,14 +805,16 @@ impl_display_secret!(KeyPair);
796805
impl KeyPair {
797806
/// Obtains a raw const pointer suitable for use with FFI functions.
798807
#[inline]
808+
#[deprecated(since = "0.25.0", note = "Use Self::as_c_ptr if you need to access the FFI layer")]
799809
pub fn as_ptr(&self) -> *const ffi::KeyPair {
800-
&self.0
810+
self.as_c_ptr()
801811
}
802812

803813
/// Obtains a raw mutable pointer suitable for use with FFI functions.
804814
#[inline]
815+
#[deprecated(since = "0.25.0", note = "Use Self::as_mut_c_ptr if you need to access the FFI layer")]
805816
pub fn as_mut_ptr(&mut self) -> *mut ffi::KeyPair {
806-
&mut self.0
817+
self.as_mut_c_ptr()
807818
}
808819

809820
/// Creates a [`KeyPair`] directly from a Secp256k1 secret key.
@@ -1090,6 +1101,17 @@ impl<'de> serde::Deserialize<'de> for KeyPair {
10901101
}
10911102
}
10921103

1104+
impl CPtr for KeyPair {
1105+
type Target = ffi::KeyPair;
1106+
fn as_c_ptr(&self) -> *const Self::Target {
1107+
&self.0
1108+
}
1109+
1110+
fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
1111+
&mut self.0
1112+
}
1113+
}
1114+
10931115
/// An x-only public key, used for verification of Schnorr signatures and serialized according to BIP-340.
10941116
///
10951117
/// # Serde support
@@ -1148,14 +1170,16 @@ impl str::FromStr for XOnlyPublicKey {
11481170
impl XOnlyPublicKey {
11491171
/// Obtains a raw const pointer suitable for use with FFI functions.
11501172
#[inline]
1173+
#[deprecated(since = "0.25.0", note = "Use Self::as_c_ptr if you need to access the FFI layer")]
11511174
pub fn as_ptr(&self) -> *const ffi::XOnlyPublicKey {
1152-
&self.0
1175+
self.as_c_ptr()
11531176
}
11541177

11551178
/// Obtains a raw mutable pointer suitable for use with FFI functions.
11561179
#[inline]
1180+
#[deprecated(since = "0.25.0", note = "Use Self::as_mut_c_ptr if you need to access the FFI layer")]
11571181
pub fn as_mut_ptr(&mut self) -> *mut ffi::XOnlyPublicKey {
1158-
&mut self.0
1182+
self.as_mut_c_ptr()
11591183
}
11601184

11611185
/// Returns the [`XOnlyPublicKey`] (and it's [`Parity`]) for `keypair`.
@@ -1168,7 +1192,7 @@ impl XOnlyPublicKey {
11681192
ffi::secp256k1_context_no_precomp,
11691193
&mut xonly_pk,
11701194
&mut pk_parity,
1171-
keypair.as_ptr(),
1195+
keypair.as_c_ptr(),
11721196
);
11731197
debug_assert_eq!(ret, 1);
11741198
let parity = Parity::from_i32(pk_parity).expect("should not panic, pk_parity is 0 or 1");
@@ -1496,11 +1520,11 @@ impl<'de> serde::Deserialize<'de> for Parity {
14961520
impl CPtr for XOnlyPublicKey {
14971521
type Target = ffi::XOnlyPublicKey;
14981522
fn as_c_ptr(&self) -> *const Self::Target {
1499-
self.as_ptr()
1523+
&self.0
15001524
}
15011525

15021526
fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
1503-
self.as_mut_ptr()
1527+
&mut self.0
15041528
}
15051529
}
15061530

src/schnorr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<C: Signing> Secp256k1<C> {
112112
self.ctx,
113113
sig.as_mut_c_ptr(),
114114
msg.as_c_ptr(),
115-
keypair.as_ptr(),
115+
keypair.as_c_ptr(),
116116
nonce_data,
117117
)
118118
);

0 commit comments

Comments
 (0)