Skip to content

Commit faeff90

Browse files
committed
musig: a couple small improvements of byte array APIs
I decided not to rename the `serialize` functions to `to_byte_array`. Maybe we should do that, but we use the name `serialize` *all over* this library so we should do it in a separate PR that changes everything. Along the way we'll have to decide what to call the methods that produce e.g. a SerializedSignature; this is conceptually a byte array but it's actually not one.
1 parent 4fe99c9 commit faeff90

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/musig.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,23 @@ impl SessionSecretRand {
5757
///
5858
/// If the **rand** feature is enabled, [`SessionSecretRand::from_rng`] can be used to generate a
5959
/// random session id.
60-
pub fn assume_unique_per_nonce_gen(inner: [u8; 32]) -> Self { SessionSecretRand(inner) }
60+
///
61+
/// # Panics
62+
///
63+
/// Panics if passed the all-zeros string. This is disallowed by the upstream
64+
/// library. The input to this function should either be the whitened output of
65+
/// a random number generator, or if that is not available, the output of a
66+
/// stable monotonic counter.
67+
pub fn assume_unique_per_nonce_gen(inner: [u8; 32]) -> Self {
68+
assert_ne!(inner, [0; 32], "session secrets may not be all zero");
69+
SessionSecretRand(inner)
70+
}
6171

6272
/// Obtains the inner bytes of the [`SessionSecretRand`].
63-
pub fn to_bytes(&self) -> [u8; 32] { self.0 }
73+
pub fn to_byte_array(&self) -> [u8; 32] { self.0 }
6474

6575
/// Obtains a reference to the inner bytes of the [`SessionSecretRand`].
66-
pub fn as_bytes(&self) -> &[u8; 32] { &self.0 }
76+
pub fn as_byte_array(&self) -> &[u8; 32] { &self.0 }
6777

6878
/// Obtains a mutable raw pointer to the beginning of the underlying storage.
6979
///
@@ -213,13 +223,9 @@ impl CPtr for PartialSignature {
213223
}
214224

215225
impl PartialSignature {
216-
/// Serialize a PartialSignature.
217-
///
218-
/// # Returns
219-
///
220-
/// 32-byte array
221-
pub fn serialize(&self) -> [u8; 32] {
222-
let mut data = MaybeUninit::<[u8; 32]>::uninit();
226+
/// Serialize a PartialSignature as a byte array.
227+
pub fn serialize(&self) -> [u8; ffi::MUSIG_PART_SIG_SERIALIZED_LEN] {
228+
let mut data = MaybeUninit::<[u8; ffi::MUSIG_PART_SIG_SERIALIZED_LEN]>::uninit();
223229
unsafe {
224230
if ffi::secp256k1_musig_partial_sig_serialize(
225231
ffi::secp256k1_context_no_precomp,

0 commit comments

Comments
 (0)