Skip to content
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

Re-implementing public key ordering using underlying FFI functions #309

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions secp256k1-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ extern "C" {
pk: *mut PublicKey) -> c_int;


#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_4_1_ec_pubkey_cmp")]
pub fn secp256k1_ec_pubkey_cmp(cx: *const Context,
pubkey1: *const PublicKey,
pubkey2: *const PublicKey)
-> c_int;

#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_4_1_ec_pubkey_tweak_add")]
pub fn secp256k1_ec_pubkey_tweak_add(cx: *const Context,
pk: *mut PublicKey,
Expand Down Expand Up @@ -499,6 +505,13 @@ extern "C" {
pubkey: *const PublicKey,
) -> c_int;

#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_4_1_xonly_pubkey_cmp")]
pub fn secp256k1_xonly_pubkey_cmp(
cx: *const Context,
pubkey1: *const XOnlyPublicKey,
pubkey2: *const XOnlyPublicKey
) -> c_int;

#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_4_1_xonly_pubkey_tweak_add")]
pub fn secp256k1_xonly_pubkey_tweak_add(
cx: *const Context,
Expand Down
38 changes: 35 additions & 3 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,25 @@ impl<'de> ::serde::Deserialize<'de> for PublicKey {
}
}

#[cfg(not(fuzzing))]
impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &PublicKey) -> Option<::core::cmp::Ordering> {
self.serialize().partial_cmp(&other.serialize())
Some(self.cmp(other))
}
}

#[cfg(not(fuzzing))]
impl Ord for PublicKey {
fn cmp(&self, other: &PublicKey) -> ::core::cmp::Ordering {
self.serialize().cmp(&other.serialize())
let ret = unsafe {
ffi::secp256k1_ec_pubkey_cmp(ffi::secp256k1_context_no_precomp, self.as_c_ptr(), other.as_c_ptr())
};
match ret {
0 => ::core::cmp::Ordering::Equal,
v if v < 0 => ::core::cmp::Ordering::Less,
v if v > 0 => ::core::cmp::Ordering::Greater,
_ => unreachable!()
}
}
}

Expand Down Expand Up @@ -911,6 +921,7 @@ mod test {
assert_eq!(Ok(sksum), sum1);
}

#[cfg(not(fuzzing))]
#[test]
fn pubkey_equal() {
let pk1 = PublicKey::from_slice(
Expand All @@ -921,7 +932,7 @@ mod test {
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
).unwrap();

assert!(pk1 == pk2);
assert_eq!(pk1, pk2);
assert!(pk1 <= pk2);
assert!(pk2 <= pk1);
assert!(!(pk2 < pk1));
Expand Down Expand Up @@ -985,3 +996,24 @@ mod test {

}
}

#[cfg(all(test, feature = "unstable"))]
mod benches {
use test::Bencher;
use std::collections::BTreeSet;
use PublicKey;
use constants::GENERATOR_X;

#[bench]
fn bench_pk_ordering(b: &mut Bencher) {
let mut map = BTreeSet::new();
let mut g_slice = [02u8; 33];
g_slice[1..].copy_from_slice(&GENERATOR_X);
let g = PublicKey::from_slice(&g_slice).unwrap();
let mut pk = g;
b.iter(|| {
map.insert(pk);
pk = pk.combine(&pk).unwrap();
})
}
}
29 changes: 27 additions & 2 deletions src/schnorrsig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ impl str::FromStr for Signature {
}

/// Opaque data structure that holds a keypair consisting of a secret and a public key.
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
///
/// NB: Since the type keeps secret key value as explicit bytes it should be used with the same
/// caution as [`SecretKey`]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct KeyPair(ffi::KeyPair);

/// A Schnorr public key, used for verification of Schnorr signatures
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct PublicKey(ffi::XOnlyPublicKey);

impl fmt::LowerHex for PublicKey {
Expand Down Expand Up @@ -112,6 +115,28 @@ impl str::FromStr for PublicKey {
}
}

#[cfg(not(fuzzing))]
impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &PublicKey) -> Option<::core::cmp::Ordering> {
Some(self.cmp(other))
}
}

#[cfg(not(fuzzing))]
impl Ord for PublicKey {
fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
let ret = unsafe {
ffi::secp256k1_xonly_pubkey_cmp(ffi::secp256k1_context_no_precomp, self.as_c_ptr(), other.as_c_ptr())
};
match ret {
0 => ::core::cmp::Ordering::Equal,
v if v < 0 => ::core::cmp::Ordering::Less,
v if v > 0 => ::core::cmp::Ordering::Greater,
_ => unreachable!()
}
}
}

impl Signature {
/// Creates a Signature directly from a slice
#[inline]
Expand Down