Merge rust-bitcoin/rust-secp256k1#307: Fixing documentation for BIP 340-related functions

931b560dc7 Fixing documentation for BIP 340-related functions (Dr Maxim Orlovsky)

Pull request description:

ACKs for top commit:
  thomaseizinger:
    ACK 931b560dc7
  apoelstra:
    ACK 931b560dc7

Tree-SHA512: d78fbda138e636c04c6f356e2a41fcdd0270eea710a9af6c965ff90acfd6740bd650a8909638c558badd52d307cbdcd914fa25846236b064ca8c38faccec8be8
This commit is contained in:
Andrew Poelstra 2021-11-02 18:39:05 +00:00
commit 476ced62fe
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 38 additions and 20 deletions

View File

@ -7,7 +7,6 @@ use rand::thread_rng;
#[cfg(any(test, feature = "rand"))] #[cfg(any(test, feature = "rand"))]
use rand::{CryptoRng, Rng}; use rand::{CryptoRng, Rng};
use super::Error::{InvalidPublicKey, InvalidSecretKey, InvalidSignature};
use super::{from_hex, Error}; use super::{from_hex, Error};
use core::{fmt, ptr, str}; use core::{fmt, ptr, str};
use ffi::{self, CPtr}; use ffi::{self, CPtr};
@ -108,7 +107,7 @@ impl str::FromStr for PublicKey {
Ok(constants::SCHNORRSIG_PUBLIC_KEY_SIZE) => { Ok(constants::SCHNORRSIG_PUBLIC_KEY_SIZE) => {
PublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE]) PublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE])
} }
_ => Err(InvalidPublicKey), _ => Err(Error::InvalidPublicKey),
} }
} }
} }
@ -123,7 +122,7 @@ impl Signature {
ret[..].copy_from_slice(data); ret[..].copy_from_slice(data);
Ok(Signature(ret)) Ok(Signature(ret))
} }
_ => Err(InvalidSignature), _ => Err(Error::InvalidSignature),
} }
} }
} }
@ -143,9 +142,11 @@ impl KeyPair {
/// Creates a Schnorr KeyPair directly from generic Secp256k1 secret key /// Creates a Schnorr KeyPair directly from generic Secp256k1 secret key
/// ///
/// Panics if internal representation of the provided [`SecretKey`] does not /// # Panic
/// holds correct secret key value obtained from Secp256k1 library ///
/// previously /// Panics if internal representation of the provided [`SecretKey`] does not hold correct secret
/// key value obtained from Secp256k1 library previously, specifically when secret key value is
/// out-of-range (0 or in excess of the group order).
#[inline] #[inline]
pub fn from_secret_key<C: Signing>( pub fn from_secret_key<C: Signing>(
secp: &Secp256k1<C>, secp: &Secp256k1<C>,
@ -161,14 +162,19 @@ impl KeyPair {
} }
} }
/// Creates a Schnorr KeyPair directly from a secret key slice /// Creates a Schnorr KeyPair directly from a secret key slice.
///
/// # Errors
///
/// [`Error::InvalidSecretKey`] if the provided data has an incorrect length, exceeds Secp256k1
/// field `p` value or the corresponding public key is not even.
#[inline] #[inline]
pub fn from_seckey_slice<C: Signing>( pub fn from_seckey_slice<C: Signing>(
secp: &Secp256k1<C>, secp: &Secp256k1<C>,
data: &[u8], data: &[u8],
) -> Result<KeyPair, Error> { ) -> Result<KeyPair, Error> {
if data.is_empty() || data.len() != constants::SECRET_KEY_SIZE { if data.is_empty() || data.len() != constants::SECRET_KEY_SIZE {
return Err(InvalidSecretKey); return Err(Error::InvalidSecretKey);
} }
unsafe { unsafe {
@ -176,12 +182,16 @@ impl KeyPair {
if ffi::secp256k1_keypair_create(secp.ctx, &mut kp, data.as_c_ptr()) == 1 { if ffi::secp256k1_keypair_create(secp.ctx, &mut kp, data.as_c_ptr()) == 1 {
Ok(KeyPair(kp)) Ok(KeyPair(kp))
} else { } else {
Err(InvalidSecretKey) Err(Error::InvalidSecretKey)
} }
} }
} }
/// Creates a Schnorr KeyPair directly from a secret key string /// Creates a Schnorr KeyPair directly from a secret key string
///
/// # Errors
///
/// [`Error::InvalidSecretKey`] if corresponding public key for the provided secret key is not even.
#[inline] #[inline]
pub fn from_seckey_str<C: Signing>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error> { pub fn from_seckey_str<C: Signing>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error> {
let mut res = [0u8; constants::SECRET_KEY_SIZE]; let mut res = [0u8; constants::SECRET_KEY_SIZE];
@ -189,7 +199,7 @@ impl KeyPair {
Ok(constants::SECRET_KEY_SIZE) => { Ok(constants::SECRET_KEY_SIZE) => {
KeyPair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE]) KeyPair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE])
} }
_ => Err(InvalidPublicKey), _ => Err(Error::InvalidPublicKey),
} }
} }
@ -218,10 +228,15 @@ impl KeyPair {
*SecretKey::from_keypair(self).as_ref() *SecretKey::from_keypair(self).as_ref()
} }
/// Tweak a keypair by adding the given tweak to the secret key and updating the /// Tweak a keypair by adding the given tweak to the secret key and updating the public key
/// public key accordingly. /// accordingly.
/// Will return an error if the resulting key would be invalid or if ///
/// the tweak was not a 32-byte length slice. /// Will return an error if the resulting key would be invalid or if the tweak was not a 32-byte
/// length slice.
///
/// NB: Will not error if the tweaked public key has an odd value and can't be used for
/// BIP 340-342 purposes.
// TODO: Add checked implementation
#[inline] #[inline]
pub fn tweak_add_assign<C: Verification>( pub fn tweak_add_assign<C: Verification>(
&mut self, &mut self,
@ -261,7 +276,7 @@ impl PublicKey {
&mut self.0 &mut self.0
} }
/// Creates a new Schnorr public key from a Schnorr key pair /// Creates a new Schnorr public key from a Schnorr key pair.
#[inline] #[inline]
pub fn from_keypair<C: Signing>(secp: &Secp256k1<C>, keypair: &KeyPair) -> PublicKey { pub fn from_keypair<C: Signing>(secp: &Secp256k1<C>, keypair: &KeyPair) -> PublicKey {
let mut pk_parity = 0; let mut pk_parity = 0;
@ -279,10 +294,15 @@ impl PublicKey {
} }
/// Creates a Schnorr public key directly from a slice /// Creates a Schnorr public key directly from a slice
///
/// # Errors
///
/// Returns [`Error::InvalidPublicKey`] if the length of the data slice is not 32 bytes or the
/// slice does not represent a valid Secp256k1 point x coordinate
#[inline] #[inline]
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> { pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
if data.is_empty() || data.len() != constants::SCHNORRSIG_PUBLIC_KEY_SIZE { if data.is_empty() || data.len() != constants::SCHNORRSIG_PUBLIC_KEY_SIZE {
return Err(InvalidPublicKey); return Err(Error::InvalidPublicKey);
} }
unsafe { unsafe {
@ -295,15 +315,13 @@ impl PublicKey {
{ {
Ok(PublicKey(pk)) Ok(PublicKey(pk))
} else { } else {
Err(InvalidPublicKey) Err(Error::InvalidPublicKey)
} }
} }
} }
#[inline] #[inline]
/// Serialize the key as a byte-encoded pair of values. In compressed form /// Serialize the key as a byte-encoded x coordinate value (32 bytes).
/// the y-coordinate is represented by only a single bit, as x determines
/// it up to one bit.
pub fn serialize(&self) -> [u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE] { pub fn serialize(&self) -> [u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE] {
let mut ret = [0u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]; let mut ret = [0u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];