Skip to content

Commit 38a7d9e

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 4fe0101 commit 38a7d9e

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
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: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::{fmt, ptr, str};
1010

1111
#[cfg(feature = "arbitrary")]
1212
use arbitrary::{Arbitrary, Unstructured};
13-
use secp256k1_sys::secp256k1_ec_pubkey_sort;
1413
#[cfg(feature = "serde")]
1514
use 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
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ pub use crate::{
202202
Context, PreallocatedContext, SignOnlyPreallocated, Signing, Verification,
203203
VerifyOnlyPreallocated,
204204
},
205-
key::{InvalidParityValue, Keypair, Parity, PublicKey, SecretKey, XOnlyPublicKey},
205+
key::{
206+
sort_pubkeys, InvalidParityValue, Keypair, Parity, PublicKey, SecretKey, XOnlyPublicKey,
207+
},
206208
scalar::Scalar,
207209
};
208210

src/musig.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use core::mem::MaybeUninit;
1212
use std;
1313

1414
use crate::ffi::{self, CPtr};
15+
#[cfg(doc)]
16+
use crate::key;
1517
use crate::{
1618
from_hex, schnorr, Error, Keypair, PublicKey, Scalar, Secp256k1, SecretKey, XOnlyPublicKey,
1719
};
@@ -369,7 +371,7 @@ impl KeyAggCache {
369371
/// ensures the same resulting `agg_pk` for the same multiset of pubkeys.
370372
/// This is useful to do before aggregating pubkeys, such that the order of pubkeys
371373
/// does not affect the combined public key.
372-
/// To do this, call [`Secp256k1::sort_pubkeys`].
374+
/// To do this, call [`key::sort_pubkeys`].
373375
///
374376
/// # Returns
375377
///

0 commit comments

Comments
 (0)