Skip to content

Commit 4fe99c9

Browse files
committed
key: move pubkey_sort to method on Secp256k1; rename
This feels more natural as `secp.musig_sort_pubkeys` rather than `musig::pubkey_sort.
1 parent 3396d9a commit 4fe99c9

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

examples/musig.rs

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

99
fn main() {
1010
let secp = Secp256k1::new();
@@ -19,7 +19,7 @@ fn main() {
1919
let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
2020
let pubkeys_ref = pubkeys_ref.as_mut_slice();
2121

22-
pubkey_sort(&secp, pubkeys_ref);
22+
secp.musig_sort_pubkeys(pubkeys_ref);
2323

2424
let mut musig_key_agg_cache = KeyAggCache::new(&secp, pubkeys_ref);
2525

src/key.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,35 +1617,38 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
16171617
}
16181618
}
16191619

1620-
/// Sort public keys using lexicographic (of compressed serialization) order.
1621-
/// Example:
1622-
///
1623-
/// ```rust
1624-
/// # # [cfg(any(test, feature = "rand-std"))] {
1625-
/// # use secp256k1::rand::{rng, RngCore};
1626-
/// # use secp256k1::{Secp256k1, SecretKey, Keypair, PublicKey, pubkey_sort};
1627-
/// # let secp = Secp256k1::new();
1628-
/// # let sk1 = SecretKey::new(&mut rng());
1629-
/// # let pub_key1 = PublicKey::from_secret_key(&secp, &sk1);
1630-
/// # let sk2 = SecretKey::new(&mut rng());
1631-
/// # let pub_key2 = PublicKey::from_secret_key(&secp, &sk2);
1632-
/// #
1633-
/// # let pubkeys = [pub_key1, pub_key2];
1634-
/// # let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
1635-
/// # let pubkeys_ref = pubkeys_ref.as_mut_slice();
1636-
/// #
1637-
/// # pubkey_sort(&secp, pubkeys_ref);
1638-
/// # }
1639-
/// ```
1640-
pub fn pubkey_sort<C: Verification>(secp: &Secp256k1<C>, pubkeys: &mut [&PublicKey]) {
1641-
let cx = secp.ctx().as_ptr();
1642-
unsafe {
1643-
let mut pubkeys_ref = core::slice::from_raw_parts(
1644-
pubkeys.as_c_ptr() as *mut *const ffi::PublicKey,
1645-
pubkeys.len(),
1646-
);
1647-
if secp256k1_ec_pubkey_sort(cx, pubkeys_ref.as_mut_c_ptr(), pubkeys_ref.len()) == 0 {
1648-
unreachable!("Invalid public keys for sorting function")
1620+
impl<C: Verification> Secp256k1<C> {
1621+
/// Sort public keys using lexicographic (of compressed serialization) order.
1622+
///
1623+
/// Example:
1624+
///
1625+
/// ```rust
1626+
/// # # [cfg(any(test, feature = "rand-std"))] {
1627+
/// # use secp256k1::rand::{rng, RngCore};
1628+
/// # use secp256k1::{Secp256k1, SecretKey, Keypair, PublicKey, pubkey_sort};
1629+
/// # let secp = Secp256k1::new();
1630+
/// # let sk1 = SecretKey::new(&mut rng());
1631+
/// # let pub_key1 = PublicKey::from_secret_key(&secp, &sk1);
1632+
/// # let sk2 = SecretKey::new(&mut rng());
1633+
/// # let pub_key2 = PublicKey::from_secret_key(&secp, &sk2);
1634+
/// #
1635+
/// # let pubkeys = [pub_key1, pub_key2];
1636+
/// # let mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
1637+
/// # let pubkeys_ref = pubkeys_ref.as_mut_slice();
1638+
/// #
1639+
/// # secp.musig_sort_pubkeys(pubkeys_ref);
1640+
/// # }
1641+
/// ```
1642+
pub fn musig_sort_pubkeys(&self, pubkeys: &mut [&PublicKey]) {
1643+
let cx = self.ctx().as_ptr();
1644+
unsafe {
1645+
let mut pubkeys_ref = core::slice::from_raw_parts(
1646+
pubkeys.as_c_ptr() as *mut *const ffi::PublicKey,
1647+
pubkeys.len(),
1648+
);
1649+
if secp256k1_ec_pubkey_sort(cx, pubkeys_ref.as_mut_c_ptr(), pubkeys_ref.len()) == 0 {
1650+
unreachable!("Invalid public keys for sorting function")
1651+
}
16491652
}
16501653
}
16511654
}

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ pub use crate::context::{
192192
};
193193
use crate::ffi::types::AlignedType;
194194
use crate::ffi::CPtr;
195-
pub use crate::key::{
196-
pubkey_sort, InvalidParityValue, Keypair, Parity, PublicKey, SecretKey, XOnlyPublicKey,
197-
};
195+
pub use crate::key::{InvalidParityValue, Keypair, Parity, PublicKey, SecretKey, XOnlyPublicKey};
198196
pub use crate::scalar::Scalar;
199197

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

0 commit comments

Comments
 (0)