Skip to content

secp256k1-sys: remove CPtr impl for &[T] and associated weirdness #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions secp256k1-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ extern "C" {
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_ec_pubkey_sort")]
pub fn secp256k1_ec_pubkey_sort(
ctx: *const Context,
pubkeys: *const *const PublicKey,
pubkeys: *mut *const PublicKey,
n_pubkeys: size_t,
) -> c_int;
}
Expand Down Expand Up @@ -1388,25 +1388,6 @@ impl<T> CPtr for [T] {
}
}

impl<T> CPtr for &[T] {
type Target = T;
fn as_c_ptr(&self) -> *const Self::Target {
if self.is_empty() {
ptr::null()
} else {
self.as_ptr()
}
}

fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
if self.is_empty() {
ptr::null_mut()
} else {
self.as_ptr() as *mut Self::Target
}
}
}

impl CPtr for [u8; 32] {
type Target = u8;
fn as_c_ptr(&self) -> *const Self::Target { self.as_ptr() }
Expand Down
9 changes: 4 additions & 5 deletions src/ellswift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ impl ElligatorSwift {
/// let alice_es = ElligatorSwift::from_seckey(&secp, alice_sk, None);
/// let bob_es = ElligatorSwift::from_seckey(&secp, bob_sk, None);
///
/// let alice_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, alice_sk, Party::Initiator, None);
/// let bob_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, bob_sk, Party::Responder, None);
/// let alice_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, alice_sk, Party::Initiator);
/// let bob_shared_secret = ElligatorSwift::shared_secret(alice_es, bob_es, bob_sk, Party::Responder);
///
/// assert_eq!(alice_shared_secret, bob_shared_secret);
/// # }
Expand All @@ -178,7 +178,6 @@ impl ElligatorSwift {
ellswift_b: ElligatorSwift,
secret_key: SecretKey,
party: impl Into<Party>,
data: Option<&[u8]>,
) -> ElligatorSwiftSharedSecret {
let mut shared_secret = [0u8; 32];
let p: Party = party.into();
Expand All @@ -191,7 +190,7 @@ impl ElligatorSwift {
secret_key.as_c_ptr(),
p.to_ffi_int(),
ffi::secp256k1_ellswift_xdh_hash_function_bip324,
data.as_c_ptr() as *mut c_void,
ptr::null_mut(),
);
debug_assert_eq!(ret, 1);
}
Expand Down Expand Up @@ -631,7 +630,7 @@ mod tests {
let sec_key = SecretKey::from_byte_array(my_secret).unwrap();
let initiator = if initiator == 0 { Party::Responder } else { Party::Initiator };

let shared = ElligatorSwift::shared_secret(el_a, el_b, sec_key, initiator, None);
let shared = ElligatorSwift::shared_secret(el_a, el_b, sec_key, initiator);

assert_eq!(shared.0, shared_secret);
}
Expand Down
8 changes: 3 additions & 5 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,11 +1644,9 @@ impl<C: Verification> Secp256k1<C> {
pub fn sort_pubkeys(&self, pubkeys: &mut [&PublicKey]) {
let cx = self.ctx().as_ptr();
unsafe {
let mut pubkeys_ref = core::slice::from_raw_parts(
pubkeys.as_c_ptr() as *mut *const ffi::PublicKey,
pubkeys.len(),
);
if secp256k1_ec_pubkey_sort(cx, pubkeys_ref.as_mut_c_ptr(), pubkeys_ref.len()) == 0 {
// SAFETY: `PublicKey` has repr(transparent) so we can convert to `ffi::PublicKey`
let pubkeys_ptr = pubkeys.as_mut_c_ptr() as *mut *const ffi::PublicKey;
if secp256k1_ec_pubkey_sort(cx, pubkeys_ptr, pubkeys.len()) == 0 {
unreachable!("Invalid public keys for sorting function")
}
}
Expand Down
Loading