Skip to content

Commit 931b560

Browse files
committed
Fixing documentation for BIP 340-related functions
1 parent 24a9c9c commit 931b560

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/schnorrsig.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rand::thread_rng;
77
#[cfg(any(test, feature = "rand"))]
88
use rand::{CryptoRng, Rng};
99

10-
use super::Error::{InvalidPublicKey, InvalidSecretKey, InvalidSignature};
1110
use super::{from_hex, Error};
1211
use core::{fmt, ptr, str};
1312
use ffi::{self, CPtr};
@@ -107,7 +106,7 @@ impl str::FromStr for PublicKey {
107106
Ok(constants::SCHNORRSIG_PUBLIC_KEY_SIZE) => {
108107
PublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE])
109108
}
110-
_ => Err(InvalidPublicKey),
109+
_ => Err(Error::InvalidPublicKey),
111110
}
112111
}
113112
}
@@ -122,7 +121,7 @@ impl Signature {
122121
ret[..].copy_from_slice(data);
123122
Ok(Signature(ret))
124123
}
125-
_ => Err(InvalidSignature),
124+
_ => Err(Error::InvalidSignature),
126125
}
127126
}
128127
}
@@ -142,9 +141,11 @@ impl KeyPair {
142141

143142
/// Creates a Schnorr KeyPair directly from generic Secp256k1 secret key
144143
///
145-
/// Panics if internal representation of the provided [`SecretKey`] does not
146-
/// holds correct secret key value obtained from Secp256k1 library
147-
/// previously
144+
/// # Panic
145+
///
146+
/// Panics if internal representation of the provided [`SecretKey`] does not hold correct secret
147+
/// key value obtained from Secp256k1 library previously, specifically when secret key value is
148+
/// out-of-range (0 or in excess of the group order).
148149
#[inline]
149150
pub fn from_secret_key<C: Signing>(
150151
secp: &Secp256k1<C>,
@@ -160,35 +161,44 @@ impl KeyPair {
160161
}
161162
}
162163

163-
/// Creates a Schnorr KeyPair directly from a secret key slice
164+
/// Creates a Schnorr KeyPair directly from a secret key slice.
165+
///
166+
/// # Errors
167+
///
168+
/// [`Error::InvalidSecretKey`] if the provided data has an incorrect length, exceeds Secp256k1
169+
/// field `p` value or the corresponding public key is not even.
164170
#[inline]
165171
pub fn from_seckey_slice<C: Signing>(
166172
secp: &Secp256k1<C>,
167173
data: &[u8],
168174
) -> Result<KeyPair, Error> {
169175
if data.is_empty() || data.len() != constants::SECRET_KEY_SIZE {
170-
return Err(InvalidSecretKey);
176+
return Err(Error::InvalidSecretKey);
171177
}
172178

173179
unsafe {
174180
let mut kp = ffi::KeyPair::new();
175181
if ffi::secp256k1_keypair_create(secp.ctx, &mut kp, data.as_c_ptr()) == 1 {
176182
Ok(KeyPair(kp))
177183
} else {
178-
Err(InvalidSecretKey)
184+
Err(Error::InvalidSecretKey)
179185
}
180186
}
181187
}
182188

183189
/// Creates a Schnorr KeyPair directly from a secret key string
190+
///
191+
/// # Errors
192+
///
193+
/// [`Error::InvalidSecretKey`] if corresponding public key for the provided secret key is not even.
184194
#[inline]
185195
pub fn from_seckey_str<C: Signing>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error> {
186196
let mut res = [0; constants::SECRET_KEY_SIZE];
187197
match from_hex(s, &mut res) {
188198
Ok(constants::SECRET_KEY_SIZE) => {
189199
KeyPair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE])
190200
}
191-
_ => Err(InvalidPublicKey),
201+
_ => Err(Error::InvalidPublicKey),
192202
}
193203
}
194204

@@ -217,10 +227,15 @@ impl KeyPair {
217227
*SecretKey::from_keypair(self).as_ref()
218228
}
219229

220-
/// Tweak a keypair by adding the given tweak to the secret key and updating the
221-
/// public key accordingly.
222-
/// Will return an error if the resulting key would be invalid or if
223-
/// the tweak was not a 32-byte length slice.
230+
/// Tweak a keypair by adding the given tweak to the secret key and updating the public key
231+
/// accordingly.
232+
///
233+
/// Will return an error if the resulting key would be invalid or if the tweak was not a 32-byte
234+
/// length slice.
235+
///
236+
/// NB: Will not error if the tweaked public key has an odd value and can't be used for
237+
/// BIP 340-342 purposes.
238+
// TODO: Add checked implementation
224239
#[inline]
225240
pub fn tweak_add_assign<C: Verification>(
226241
&mut self,
@@ -260,7 +275,7 @@ impl PublicKey {
260275
&mut self.0
261276
}
262277

263-
/// Creates a new Schnorr public key from a Schnorr key pair
278+
/// Creates a new Schnorr public key from a Schnorr key pair.
264279
#[inline]
265280
pub fn from_keypair<C: Signing>(secp: &Secp256k1<C>, keypair: &KeyPair) -> PublicKey {
266281
let mut pk_parity = 0;
@@ -278,10 +293,15 @@ impl PublicKey {
278293
}
279294

280295
/// Creates a Schnorr public key directly from a slice
296+
///
297+
/// # Errors
298+
///
299+
/// Returns [`Error::InvalidPublicKey`] if the length of the data slice is not 32 bytes or the
300+
/// slice does not represent a valid Secp256k1 point x coordinate
281301
#[inline]
282302
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
283303
if data.is_empty() || data.len() != constants::SCHNORRSIG_PUBLIC_KEY_SIZE {
284-
return Err(InvalidPublicKey);
304+
return Err(Error::InvalidPublicKey);
285305
}
286306

287307
unsafe {
@@ -294,15 +314,13 @@ impl PublicKey {
294314
{
295315
Ok(PublicKey(pk))
296316
} else {
297-
Err(InvalidPublicKey)
317+
Err(Error::InvalidPublicKey)
298318
}
299319
}
300320
}
301321

302322
#[inline]
303-
/// Serialize the key as a byte-encoded pair of values. In compressed form
304-
/// the y-coordinate is represented by only a single bit, as x determines
305-
/// it up to one bit.
323+
/// Serialize the key as a byte-encoded x coordinate value (32 bytes).
306324
pub fn serialize(&self) -> [u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE] {
307325
let mut ret = [0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
308326

0 commit comments

Comments
 (0)