@@ -10,7 +10,6 @@ use core::{fmt, ptr, str};
1010
1111#[ cfg( feature = "arbitrary" ) ]
1212use arbitrary:: { Arbitrary , Unstructured } ;
13- use secp256k1_sys:: secp256k1_ec_pubkey_sort;
1413#[ cfg( feature = "serde" ) ]
1514use serde:: ser:: SerializeTuple ;
1615
@@ -1305,38 +1304,44 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
13051304 }
13061305}
13071306
1308- impl < C : Verification > Secp256k1 < C > {
1309- /// Sort public keys using lexicographic (of compressed serialization) order.
1310- ///
1311- /// This is the canonical way to sort public keys for use with Musig2.
1312- ///
1313- /// Example:
1314- ///
1315- /// ```rust
1316- /// # # [cfg(any(test, feature = "rand-std"))] {
1317- /// # use secp256k1::rand::{rng, RngCore};
1318- /// # use secp256k1::{Secp256k1, SecretKey, Keypair, PublicKey, pubkey_sort};
1319- /// # let secp = Secp256k1::new();
1320- /// # let sk1 = SecretKey::new(&mut rng());
1321- /// # let pub_key1 = PublicKey::from_secret_key(&sk1);
1322- /// # let sk2 = SecretKey::new(&mut rng());
1323- /// # let pub_key2 = PublicKey::from_secret_key(&sk2);
1324- /// #
1325- /// # let pubkeys = [pub_key1, pub_key2];
1326- /// # let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
1327- /// # let pubkeys_ref = pubkeys_ref.as_mut_slice();
1328- /// #
1329- /// # secp.sort_pubkeys(pubkeys_ref);
1330- /// # }
1331- /// ```
1332- pub fn sort_pubkeys ( & self , pubkeys : & mut [ & PublicKey ] ) {
1333- let cx = self . ctx ( ) . as_ptr ( ) ;
1334- unsafe {
1335- // SAFETY: `PublicKey` has repr(transparent) so we can convert to `ffi::PublicKey`
1336- let pubkeys_ptr = pubkeys. as_mut_c_ptr ( ) as * mut * const ffi:: PublicKey ;
1337- if secp256k1_ec_pubkey_sort ( cx, pubkeys_ptr, pubkeys. len ( ) ) == 0 {
1338- unreachable ! ( "Invalid public keys for sorting function" )
1339- }
1307+ /// Sort public keys using lexicographic (of compressed serialization) order.
1308+ ///
1309+ /// This is the canonical way to sort public keys for use with Musig2.
1310+ ///
1311+ /// Example:
1312+ ///
1313+ /// ```rust
1314+ /// # # [cfg(any(test, feature = "rand-std"))] {
1315+ /// # use secp256k1::rand::{rng, RngCore};
1316+ /// # use secp256k1::{SecretKey, Keypair, PublicKey, pubkey_sort};
1317+ /// # let sk1 = SecretKey::new(&mut rng());
1318+ /// # let pub_key1 = PublicKey::from_secret_key(&sk1);
1319+ /// # let sk2 = SecretKey::new(&mut rng());
1320+ /// # let pub_key2 = PublicKey::from_secret_key(&sk2);
1321+ /// #
1322+ /// # let pubkeys = [pub_key1, pub_key2];
1323+ /// # let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
1324+ /// # let pubkeys_ref = pubkeys_ref.as_mut_slice();
1325+ /// #
1326+ /// # secp256k1::sort_pubkeys(pubkeys_ref);
1327+ /// # }
1328+ /// ```
1329+ pub fn sort_pubkeys ( pubkeys : & mut [ & PublicKey ] ) {
1330+ // We have no seed here but we want rerandomiziation to happen for `rand` users.
1331+ let seed = [ 0_u8 ; 32 ] ;
1332+ unsafe {
1333+ // SAFETY: `PublicKey` has repr(transparent) so we can convert to `ffi::PublicKey`
1334+ let pubkeys_ptr = pubkeys. as_mut_c_ptr ( ) as * mut * const ffi:: PublicKey ;
1335+
1336+ let ret = crate :: with_global_context (
1337+ |secp : & Secp256k1 < crate :: AllPreallocated > | {
1338+ ffi:: secp256k1_ec_pubkey_sort ( secp. ctx . as_ptr ( ) , pubkeys_ptr, pubkeys. len ( ) )
1339+ } ,
1340+ Some ( & seed) ,
1341+ ) ;
1342+
1343+ if ret == 0 {
1344+ unreachable ! ( "Invalid public keys for sorting function" )
13401345 }
13411346 }
13421347}
0 commit comments