@@ -10,7 +10,7 @@ use core::{fmt, ptr, str};
1010
1111#[ cfg( feature = "arbitrary" ) ]
1212use arbitrary:: { Arbitrary , Unstructured } ;
13- use secp256k1_sys :: secp256k1_ec_pubkey_sort ;
13+
1414#[ cfg( feature = "serde" ) ]
1515use 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}
0 commit comments