Skip to content

Commit 15868bb

Browse files
committed
Remove context sort_pubkeys
Take the `sort_pubkeys` function off of the context and make it stand alone. Re-export it at the crate root because the `key` module is private.
1 parent f41b482 commit 15868bb

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

examples/musig.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use secp256k1::musig::{
44
new_nonce_pair, AggregatedNonce, KeyAggCache, PartialSignature, PublicNonce, Session,
55
SessionSecretRand,
66
};
7-
use secp256k1::{Keypair, PublicKey, Scalar, Secp256k1, SecretKey};
7+
use secp256k1::{Keypair, PublicKey, Scalar, SecretKey};
88

99
fn main() {
10-
let secp = Secp256k1::new();
1110
let mut rng = rand::rng();
1211

1312
let (seckey1, pubkey1) = secp256k1::generate_keypair(&mut rng);
@@ -19,7 +18,7 @@ fn main() {
1918
let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
2019
let pubkeys_ref = pubkeys_ref.as_mut_slice();
2120

22-
secp.sort_pubkeys(pubkeys_ref);
21+
secp256k1::sort_pubkeys(pubkeys_ref);
2322

2423
let mut musig_key_agg_cache = KeyAggCache::new(pubkeys_ref);
2524

src/key/mod.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::{fmt, ptr, str};
1010

1111
#[cfg(feature = "arbitrary")]
1212
use arbitrary::{Arbitrary, Unstructured};
13-
use secp256k1_sys::secp256k1_ec_pubkey_sort;
13+
1414
#[cfg(feature = "serde")]
1515
use serde::ser::SerializeTuple;
1616

@@ -1298,38 +1298,40 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
12981298
}
12991299
}
13001300

1301-
impl<C: Verification> Secp256k1<C> {
1302-
/// Sort public keys using lexicographic (of compressed serialization) order.
1303-
///
1304-
/// This is the canonical way to sort public keys for use with Musig2.
1305-
///
1306-
/// Example:
1307-
///
1308-
/// ```rust
1309-
/// # # [cfg(any(test, feature = "rand-std"))] {
1310-
/// # use secp256k1::rand::{rng, RngCore};
1311-
/// # use secp256k1::{Secp256k1, SecretKey, Keypair, PublicKey, pubkey_sort};
1312-
/// # let secp = Secp256k1::new();
1313-
/// # let sk1 = SecretKey::new(&mut rng());
1314-
/// # let pub_key1 = PublicKey::from_secret_key(&sk1);
1315-
/// # let sk2 = SecretKey::new(&mut rng());
1316-
/// # let pub_key2 = PublicKey::from_secret_key(&sk2);
1317-
/// #
1318-
/// # let pubkeys = [pub_key1, pub_key2];
1319-
/// # let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
1320-
/// # let pubkeys_ref = pubkeys_ref.as_mut_slice();
1321-
/// #
1322-
/// # secp.sort_pubkeys(pubkeys_ref);
1323-
/// # }
1324-
/// ```
1325-
pub fn sort_pubkeys(&self, pubkeys: &mut [&PublicKey]) {
1326-
let cx = self.ctx().as_ptr();
1327-
unsafe {
1328-
// SAFETY: `PublicKey` has repr(transparent) so we can convert to `ffi::PublicKey`
1329-
let pubkeys_ptr = pubkeys.as_mut_c_ptr() as *mut *const ffi::PublicKey;
1330-
if secp256k1_ec_pubkey_sort(cx, pubkeys_ptr, pubkeys.len()) == 0 {
1331-
unreachable!("Invalid public keys for sorting function")
1332-
}
1301+
/// Sort public keys using lexicographic (of compressed serialization) order.
1302+
///
1303+
/// This is the canonical way to sort public keys for use with Musig2.
1304+
///
1305+
/// Example:
1306+
///
1307+
/// ```rust
1308+
/// # # [cfg(any(test, feature = "rand-std"))] {
1309+
/// # use secp256k1::rand::{rng, RngCore};
1310+
/// # use secp256k1::{SecretKey, Keypair, PublicKey, pubkey_sort};
1311+
/// # let sk1 = SecretKey::new(&mut rng());
1312+
/// # let pub_key1 = PublicKey::from_secret_key(&sk1);
1313+
/// # let sk2 = SecretKey::new(&mut rng());
1314+
/// # let pub_key2 = PublicKey::from_secret_key(&sk2);
1315+
/// #
1316+
/// # let pubkeys = [pub_key1, pub_key2];
1317+
/// # let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
1318+
/// # let pubkeys_ref = pubkeys_ref.as_mut_slice();
1319+
/// #
1320+
/// # secp256k1::sort_pubkeys(pubkeys_ref);
1321+
/// # }
1322+
/// ```
1323+
pub fn sort_pubkeys(pubkeys: &mut [&PublicKey]) {
1324+
unsafe {
1325+
// SAFETY: `PublicKey` has repr(transparent) so we can convert to `ffi::PublicKey`
1326+
let pubkeys_ptr = pubkeys.as_mut_c_ptr() as *mut *const ffi::PublicKey;
1327+
1328+
let ret = crate::with_global_context(
1329+
|secp: &Secp256k1<crate::AllPreallocated>| ffi::secp256k1_ec_pubkey_sort(secp.ctx.as_ptr(), pubkeys_ptr, pubkeys.len()),
1330+
None, // FIXME: What goes here.
1331+
);
1332+
1333+
if ret == 0 {
1334+
unreachable!("Invalid public keys for sorting function")
13331335
}
13341336
}
13351337
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub use crate::context::{
197197
pub use crate::context::{All, SignOnly, VerifyOnly};
198198
use crate::ffi::types::AlignedType;
199199
use crate::ffi::CPtr;
200-
pub use crate::key::{InvalidParityValue, Keypair, Parity, PublicKey, SecretKey, XOnlyPublicKey};
200+
pub use crate::key::{sort_pubkeys, InvalidParityValue, Keypair, Parity, PublicKey, SecretKey, XOnlyPublicKey};
201201
pub use crate::scalar::Scalar;
202202

203203
/// Trait describing something that promises to be a 32-byte uniformly random number.

0 commit comments

Comments
 (0)