Merge rust-bitcoin/rust-secp256k1#648: Rename KeyPair to Keypair

33747bb16f Rename KeyPair to Keypair (Tobin C. Harding)

Pull request description:

  We use "keypair" in identifiers (local vars and function names) but `KeyPair` - one of them is wrong.

  Elect to follow upstream and define keypair as a single word i.e., use `Keypair` for type name and `keypair` in identifiers.

  This patch can be reproduced mechanically by doing two search-and-replace operations on all files excluding the CHANGELOG

  - Replace "KeyPair" with "Keypair"
  - Replace "key_pair" with "keypair"

  Fix #647

ACKs for top commit:
  apoelstra:
    ACK 33747bb16f

Tree-SHA512: 2cb67cde3a39c4681e0628b17b12724fe17d00bc71e985b4eb0b3ad58d574f4a372aa782fc9cb7221cd2032d8312c579195ad2230a3eeb4c550865fa36e42c0b
This commit is contained in:
Andrew Poelstra 2023-08-20 14:01:30 +00:00
commit d9aadc6eaa
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
6 changed files with 140 additions and 140 deletions

View File

@ -49,7 +49,7 @@ bench marks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench --features=recove
### A note on `non_secure_erase`
This crate's secret types (`SecretKey`, `KeyPair`, `SharedSecret`, `Scalar`, and `DisplaySecret`)
This crate's secret types (`SecretKey`, `Keypair`, `SharedSecret`, `Scalar`, and `DisplaySecret`)
have a method called `non_secure_erase` that *attempts* to overwrite the contained secret. This
method is provided to assist other libraries in building secure secret erasure. However, this
library makes no guarantees about the security of using `non_secure_erase`. In particular,

View File

@ -395,11 +395,11 @@ impl core::hash::Hash for XOnlyPublicKey {
#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(secp256k1_fuzz, derive(PartialEq, Eq, PartialOrd, Ord, Hash))]
pub struct KeyPair([c_uchar; 96]);
impl_array_newtype!(KeyPair, c_uchar, 96);
impl_raw_debug!(KeyPair);
pub struct Keypair([c_uchar; 96]);
impl_array_newtype!(Keypair, c_uchar, 96);
impl_raw_debug!(Keypair);
impl KeyPair {
impl Keypair {
/// Creates an "uninitialized" FFI keypair which is zeroed out
///
/// # Safety
@ -421,7 +421,7 @@ impl KeyPair {
/// that you obtained from the FFI interface of the same version of this
/// library.
pub unsafe fn from_array_unchecked(data: [c_uchar; 96]) -> Self {
KeyPair(data)
Keypair(data)
}
/// Returns the underlying FFI opaque representation of the x-only public key
@ -480,15 +480,15 @@ pub fn non_secure_erase_impl<T>(dst: &mut T, src: T) {
}
#[cfg(not(secp256k1_fuzz))]
impl PartialOrd for KeyPair {
fn partial_cmp(&self, other: &KeyPair) -> Option<core::cmp::Ordering> {
impl PartialOrd for Keypair {
fn partial_cmp(&self, other: &Keypair) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[cfg(not(secp256k1_fuzz))]
impl Ord for KeyPair {
fn cmp(&self, other: &KeyPair) -> core::cmp::Ordering {
impl Ord for Keypair {
fn cmp(&self, other: &Keypair) -> core::cmp::Ordering {
let this = self.public_key();
let that = other.public_key();
this.cmp(&that)
@ -496,17 +496,17 @@ impl Ord for KeyPair {
}
#[cfg(not(secp256k1_fuzz))]
impl PartialEq for KeyPair {
impl PartialEq for Keypair {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == core::cmp::Ordering::Equal
}
}
#[cfg(not(secp256k1_fuzz))]
impl Eq for KeyPair {}
impl Eq for Keypair {}
#[cfg(not(secp256k1_fuzz))]
impl core::hash::Hash for KeyPair {
impl core::hash::Hash for Keypair {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
// To hash the key pair we just hash the serialized public key. Since any change to the
// secret key would also be a change to the public key this is a valid one way function from
@ -592,13 +592,13 @@ extern "C" {
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_keypair_sec")]
pub fn secp256k1_keypair_sec(cx: *const Context,
output_seckey: *mut c_uchar,
keypair: *const KeyPair)
keypair: *const Keypair)
-> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_keypair_pub")]
pub fn secp256k1_keypair_pub(cx: *const Context,
output_pubkey: *mut PublicKey,
keypair: *const KeyPair)
keypair: *const Keypair)
-> c_int;
}
@ -702,7 +702,7 @@ extern "C" {
cx: *const Context,
sig: *mut c_uchar,
msg32: *const c_uchar,
keypair: *const KeyPair,
keypair: *const Keypair,
aux_rand32: *const c_uchar
) -> c_int;
@ -713,7 +713,7 @@ extern "C" {
sig: *mut c_uchar,
msg: *const c_uchar,
msg_len: size_t,
keypair: *const KeyPair,
keypair: *const Keypair,
extra_params: *const SchnorrSigExtraParams,
) -> c_int;
@ -730,7 +730,7 @@ extern "C" {
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_keypair_create")]
pub fn secp256k1_keypair_create(
cx: *const Context,
keypair: *mut KeyPair,
keypair: *mut Keypair,
seckey: *const c_uchar,
) -> c_int;
@ -776,13 +776,13 @@ extern "C" {
cx: *const Context,
pubkey: *mut XOnlyPublicKey,
pk_parity: *mut c_int,
keypair: *const KeyPair
keypair: *const Keypair
) -> c_int;
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_keypair_xonly_tweak_add")]
pub fn secp256k1_keypair_xonly_tweak_add(
cx: *const Context,
keypair: *mut KeyPair,
keypair: *mut Keypair,
tweak32: *const c_uchar,
) -> c_int;
@ -1316,12 +1316,12 @@ mod fuzz_dummy {
cx: *const Context,
sig64: *mut c_uchar,
msg32: *const c_uchar,
keypair: *const KeyPair,
keypair: *const Keypair,
_aux_rand32: *const c_uchar
) -> c_int {
check_context_flags(cx, SECP256K1_START_SIGN);
// Check context is built for signing
let mut new_kp = KeyPair::new();
let mut new_kp = Keypair::new();
if secp256k1_keypair_create(cx, &mut new_kp, (*keypair).0.as_ptr()) != 1 {
return 0;
}
@ -1341,7 +1341,7 @@ mod fuzz_dummy {
sig: *mut c_uchar,
msg: *const c_uchar,
_msg_len: size_t,
keypair: *const KeyPair,
keypair: *const Keypair,
_extra_params: *const SchnorrSigExtraParams,
) -> c_int {
secp256k1_schnorrsig_sign(cx, sig, msg, keypair, ptr::null())
@ -1350,7 +1350,7 @@ mod fuzz_dummy {
// Extra keys
pub unsafe fn secp256k1_keypair_create(
cx: *const Context,
keypair: *mut KeyPair,
keypair: *mut Keypair,
seckey: *const c_uchar,
) -> c_int {
check_context_flags(cx, SECP256K1_START_SIGN);
@ -1419,7 +1419,7 @@ mod fuzz_dummy {
cx: *const Context,
pubkey: *mut XOnlyPublicKey,
pk_parity: *mut c_int,
keypair: *const KeyPair
keypair: *const Keypair
) -> c_int {
check_context_flags(cx, 0);
if !pk_parity.is_null() {
@ -1431,7 +1431,7 @@ mod fuzz_dummy {
pub unsafe fn secp256k1_keypair_xonly_tweak_add(
cx: *const Context,
keypair: *mut KeyPair,
keypair: *mut Keypair,
tweak32: *const c_uchar,
) -> c_int {
check_context_flags(cx, SECP256K1_START_VERIFY);

View File

@ -226,21 +226,21 @@ impl SecretKey {
}
}
/// Creates a new secret key using data from BIP-340 [`KeyPair`].
/// Creates a new secret key using data from BIP-340 [`Keypair`].
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{rand, Secp256k1, SecretKey, KeyPair};
/// use secp256k1::{rand, Secp256k1, SecretKey, Keypair};
///
/// let secp = Secp256k1::new();
/// let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
/// let secret_key = SecretKey::from_keypair(&key_pair);
/// let keypair = Keypair::new(&secp, &mut rand::thread_rng());
/// let secret_key = SecretKey::from_keypair(&keypair);
/// # }
/// ```
#[inline]
pub fn from_keypair(keypair: &KeyPair) -> Self {
pub fn from_keypair(keypair: &Keypair) -> Self {
let mut sk = [0u8; constants::SECRET_KEY_SIZE];
unsafe {
let ret = ffi::secp256k1_keypair_sec(
@ -342,12 +342,12 @@ impl SecretKey {
#[cfg(feature = "global-context")]
pub fn sign_ecdsa(&self, msg: Message) -> ecdsa::Signature { SECP256K1.sign_ecdsa(&msg, self) }
/// Returns the [`KeyPair`] for this [`SecretKey`].
/// Returns the [`Keypair`] for this [`SecretKey`].
///
/// This is equivalent to using [`KeyPair::from_secret_key`].
/// This is equivalent to using [`Keypair::from_secret_key`].
#[inline]
pub fn keypair<C: Signing>(&self, secp: &Secp256k1<C>) -> KeyPair {
KeyPair::from_secret_key(secp, self)
pub fn keypair<C: Signing>(&self, secp: &Secp256k1<C>) -> Keypair {
Keypair::from_secret_key(secp, self)
}
/// Returns the [`PublicKey`] for this [`SecretKey`].
@ -478,21 +478,21 @@ impl PublicKey {
}
}
/// Creates a new compressed public key using data from BIP-340 [`KeyPair`].
/// Creates a new compressed public key using data from BIP-340 [`Keypair`].
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{rand, Secp256k1, PublicKey, KeyPair};
/// use secp256k1::{rand, Secp256k1, PublicKey, Keypair};
///
/// let secp = Secp256k1::new();
/// let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
/// let public_key = PublicKey::from_keypair(&key_pair);
/// let keypair = Keypair::new(&secp, &mut rand::thread_rng());
/// let public_key = PublicKey::from_keypair(&keypair);
/// # }
/// ```
#[inline]
pub fn from_keypair(keypair: &KeyPair) -> Self {
pub fn from_keypair(keypair: &Keypair) -> Self {
unsafe {
let mut pk = ffi::PublicKey::new();
let ret = ffi::secp256k1_keypair_pub(
@ -778,25 +778,25 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{rand, KeyPair, Secp256k1};
/// use secp256k1::{rand, Keypair, Secp256k1};
///
/// let secp = Secp256k1::new();
/// let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
/// let key_pair = KeyPair::from_secret_key(&secp, &secret_key);
/// let keypair = Keypair::from_secret_key(&secp, &secret_key);
/// # }
/// ```
/// [`bincode`]: https://docs.rs/bincode
/// [`cbor`]: https://docs.rs/cbor
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct KeyPair(ffi::KeyPair);
impl_display_secret!(KeyPair);
impl_fast_comparisons!(KeyPair);
pub struct Keypair(ffi::Keypair);
impl_display_secret!(Keypair);
impl_fast_comparisons!(Keypair);
impl KeyPair {
impl Keypair {
/// Obtains a raw const pointer suitable for use with FFI functions.
#[inline]
#[deprecated(since = "0.25.0", note = "Use Self::as_c_ptr if you need to access the FFI layer")]
pub fn as_ptr(&self) -> *const ffi::KeyPair { self.as_c_ptr() }
pub fn as_ptr(&self) -> *const ffi::Keypair { self.as_c_ptr() }
/// Obtains a raw mutable pointer suitable for use with FFI functions.
#[inline]
@ -804,22 +804,22 @@ impl KeyPair {
since = "0.25.0",
note = "Use Self::as_mut_c_ptr if you need to access the FFI layer"
)]
pub fn as_mut_ptr(&mut self) -> *mut ffi::KeyPair { self.as_mut_c_ptr() }
pub fn as_mut_ptr(&mut self) -> *mut ffi::Keypair { self.as_mut_c_ptr() }
/// Creates a [`KeyPair`] directly from a Secp256k1 secret key.
/// Creates a [`Keypair`] directly from a Secp256k1 secret key.
#[inline]
pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>, sk: &SecretKey) -> KeyPair {
pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>, sk: &SecretKey) -> Keypair {
unsafe {
let mut kp = ffi::KeyPair::new();
let mut kp = ffi::Keypair::new();
if ffi::secp256k1_keypair_create(secp.ctx.as_ptr(), &mut kp, sk.as_c_ptr()) == 1 {
KeyPair(kp)
Keypair(kp)
} else {
panic!("the provided secret key is invalid: it is corrupted or was not produced by Secp256k1 library")
}
}
}
/// Creates a [`KeyPair`] directly from a secret key slice.
/// Creates a [`Keypair`] directly from a secret key slice.
///
/// # Errors
///
@ -829,45 +829,45 @@ impl KeyPair {
pub fn from_seckey_slice<C: Signing>(
secp: &Secp256k1<C>,
data: &[u8],
) -> Result<KeyPair, Error> {
) -> Result<Keypair, Error> {
if data.is_empty() || data.len() != constants::SECRET_KEY_SIZE {
return Err(Error::InvalidSecretKey);
}
unsafe {
let mut kp = ffi::KeyPair::new();
let mut kp = ffi::Keypair::new();
if ffi::secp256k1_keypair_create(secp.ctx.as_ptr(), &mut kp, data.as_c_ptr()) == 1 {
Ok(KeyPair(kp))
Ok(Keypair(kp))
} else {
Err(Error::InvalidSecretKey)
}
}
}
/// Creates a [`KeyPair`] directly from a secret key string.
/// Creates a [`Keypair`] directly from a secret key string.
///
/// # Errors
///
/// [`Error::InvalidSecretKey`] if corresponding public key for the provided secret key is not even.
#[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];
match from_hex(s, &mut res) {
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(Error::InvalidPublicKey),
}
}
/// Creates a [`KeyPair`] directly from a secret key string and the global [`SECP256K1`] context.
/// Creates a [`Keypair`] directly from a secret key string and the global [`SECP256K1`] context.
///
/// # Errors
///
/// [`Error::InvalidSecretKey`] if corresponding public key for the provided secret key is not even.
#[inline]
#[cfg(feature = "global-context")]
pub fn from_seckey_str_global(s: &str) -> Result<KeyPair, Error> {
KeyPair::from_seckey_str(SECP256K1, s)
pub fn from_seckey_str_global(s: &str) -> Result<Keypair, Error> {
Keypair::from_seckey_str(SECP256K1, s)
}
/// Generates a new random secret key.
@ -875,32 +875,32 @@ impl KeyPair {
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{rand, Secp256k1, SecretKey, KeyPair};
/// use secp256k1::{rand, Secp256k1, SecretKey, Keypair};
///
/// let secp = Secp256k1::new();
/// let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
/// let keypair = Keypair::new(&secp, &mut rand::thread_rng());
/// # }
/// ```
#[inline]
#[cfg(feature = "rand")]
pub fn new<R: rand::Rng + ?Sized, C: Signing>(secp: &Secp256k1<C>, rng: &mut R) -> KeyPair {
pub fn new<R: rand::Rng + ?Sized, C: Signing>(secp: &Secp256k1<C>, rng: &mut R) -> Keypair {
let mut data = crate::random_32_bytes(rng);
unsafe {
let mut keypair = ffi::KeyPair::new();
let mut keypair = ffi::Keypair::new();
while ffi::secp256k1_keypair_create(secp.ctx.as_ptr(), &mut keypair, data.as_c_ptr())
== 0
{
data = crate::random_32_bytes(rng);
}
KeyPair(keypair)
Keypair(keypair)
}
}
/// Generates a new random secret key using the global [`SECP256K1`] context.
#[inline]
#[cfg(all(feature = "global-context", feature = "rand"))]
pub fn new_global<R: ::rand::Rng + ?Sized>(rng: &mut R) -> KeyPair {
KeyPair::new(SECP256K1, rng)
pub fn new_global<R: ::rand::Rng + ?Sized>(rng: &mut R) -> Keypair {
Keypair::new(SECP256K1, rng)
}
/// Returns the secret bytes for this key pair.
@ -922,13 +922,13 @@ impl KeyPair {
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{Secp256k1, KeyPair, Scalar};
/// use secp256k1::{Secp256k1, Keypair, Scalar};
///
/// let secp = Secp256k1::new();
/// let tweak = Scalar::random();
///
/// let mut key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
/// let tweaked = key_pair.add_xonly_tweak(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
/// let mut keypair = Keypair::new(&secp, &mut rand::thread_rng());
/// let tweaked = keypair.add_xonly_tweak(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
/// # }
/// ```
// TODO: Add checked implementation
@ -937,7 +937,7 @@ impl KeyPair {
mut self,
secp: &Secp256k1<C>,
tweak: &Scalar,
) -> Result<KeyPair, Error> {
) -> Result<Keypair, Error> {
unsafe {
let err = ffi::secp256k1_keypair_xonly_tweak_add(
secp.ctx.as_ptr(),
@ -952,19 +952,19 @@ impl KeyPair {
}
}
/// Returns the [`SecretKey`] for this [`KeyPair`].
/// Returns the [`SecretKey`] for this [`Keypair`].
///
/// This is equivalent to using [`SecretKey::from_keypair`].
#[inline]
pub fn secret_key(&self) -> SecretKey { SecretKey::from_keypair(self) }
/// Returns the [`PublicKey`] for this [`KeyPair`].
/// Returns the [`PublicKey`] for this [`Keypair`].
///
/// This is equivalent to using [`PublicKey::from_keypair`].
#[inline]
pub fn public_key(&self) -> PublicKey { PublicKey::from_keypair(self) }
/// Returns the [`XOnlyPublicKey`] (and it's [`Parity`]) for this [`KeyPair`].
/// Returns the [`XOnlyPublicKey`] (and it's [`Parity`]) for this [`Keypair`].
///
/// This is equivalent to using [`XOnlyPublicKey::from_keypair`].
#[inline]
@ -989,27 +989,27 @@ impl KeyPair {
pub fn non_secure_erase(&mut self) { self.0.non_secure_erase(); }
}
impl From<KeyPair> for SecretKey {
impl From<Keypair> for SecretKey {
#[inline]
fn from(pair: KeyPair) -> Self { SecretKey::from_keypair(&pair) }
fn from(pair: Keypair) -> Self { SecretKey::from_keypair(&pair) }
}
impl<'a> From<&'a KeyPair> for SecretKey {
impl<'a> From<&'a Keypair> for SecretKey {
#[inline]
fn from(pair: &'a KeyPair) -> Self { SecretKey::from_keypair(pair) }
fn from(pair: &'a Keypair) -> Self { SecretKey::from_keypair(pair) }
}
impl From<KeyPair> for PublicKey {
impl From<Keypair> for PublicKey {
#[inline]
fn from(pair: KeyPair) -> Self { PublicKey::from_keypair(&pair) }
fn from(pair: Keypair) -> Self { PublicKey::from_keypair(&pair) }
}
impl<'a> From<&'a KeyPair> for PublicKey {
impl<'a> From<&'a Keypair> for PublicKey {
#[inline]
fn from(pair: &'a KeyPair) -> Self { PublicKey::from_keypair(pair) }
fn from(pair: &'a Keypair) -> Self { PublicKey::from_keypair(pair) }
}
impl str::FromStr for KeyPair {
impl str::FromStr for Keypair {
type Err = Error;
#[allow(unused_variables, unreachable_code)] // When built with no default features.
@ -1024,12 +1024,12 @@ impl str::FromStr for KeyPair {
let ctx: Secp256k1<crate::SignOnlyPreallocated> = panic!("The previous implementation was panicking too, please enable the global-context feature of rust-secp256k1");
#[allow(clippy::needless_borrow)]
KeyPair::from_seckey_str(&ctx, s)
Keypair::from_seckey_str(&ctx, s)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for KeyPair {
impl serde::Serialize for Keypair {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() {
let mut buf = [0u8; constants::SECRET_KEY_SIZE * 2];
@ -1049,15 +1049,15 @@ impl serde::Serialize for KeyPair {
#[cfg(feature = "serde")]
#[allow(unused_variables)] // For `data` under some feature combinations (the unconditional panic below).
#[allow(unreachable_code)] // For `KeyPair::from_seckey_slice` after unconditional panic.
impl<'de> serde::Deserialize<'de> for KeyPair {
#[allow(unreachable_code)] // For `Keypair::from_seckey_slice` after unconditional panic.
impl<'de> serde::Deserialize<'de> for Keypair {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
if d.is_human_readable() {
d.deserialize_str(super::serde_util::FromStrVisitor::new(
"a hex string representing 32 byte KeyPair",
"a hex string representing 32 byte Keypair",
))
} else {
let visitor = super::serde_util::Tuple32Visitor::new("raw 32 bytes KeyPair", |data| {
let visitor = super::serde_util::Tuple32Visitor::new("raw 32 bytes Keypair", |data| {
#[cfg(feature = "global-context")]
let ctx = SECP256K1;
@ -1068,15 +1068,15 @@ impl<'de> serde::Deserialize<'de> for KeyPair {
let ctx: Secp256k1<crate::SignOnlyPreallocated> = panic!("cannot deserialize key pair without a context (please enable either the global-context or alloc feature)");
#[allow(clippy::needless_borrow)]
KeyPair::from_seckey_slice(&ctx, data)
Keypair::from_seckey_slice(&ctx, data)
});
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
}
}
}
impl CPtr for KeyPair {
type Target = ffi::KeyPair;
impl CPtr for Keypair {
type Target = ffi::Keypair;
fn as_c_ptr(&self) -> *const Self::Target { &self.0 }
fn as_mut_c_ptr(&mut self) -> *mut Self::Target { &mut self.0 }
@ -1096,11 +1096,11 @@ impl CPtr for KeyPair {
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{rand, Secp256k1, KeyPair, XOnlyPublicKey};
/// use secp256k1::{rand, Secp256k1, Keypair, XOnlyPublicKey};
///
/// let secp = Secp256k1::new();
/// let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
/// let xonly = XOnlyPublicKey::from_keypair(&key_pair);
/// let keypair = Keypair::new(&secp, &mut rand::thread_rng());
/// let xonly = XOnlyPublicKey::from_keypair(&keypair);
/// # }
/// ```
/// [`bincode`]: https://docs.rs/bincode
@ -1151,7 +1151,7 @@ impl XOnlyPublicKey {
/// Returns the [`XOnlyPublicKey`] (and it's [`Parity`]) for `keypair`.
#[inline]
pub fn from_keypair(keypair: &KeyPair) -> (XOnlyPublicKey, Parity) {
pub fn from_keypair(keypair: &Keypair) -> (XOnlyPublicKey, Parity) {
let mut pk_parity = 0;
unsafe {
let mut xonly_pk = ffi::XOnlyPublicKey::new();
@ -1228,13 +1228,13 @@ impl XOnlyPublicKey {
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{Secp256k1, KeyPair, Scalar, XOnlyPublicKey};
/// use secp256k1::{Secp256k1, Keypair, Scalar, XOnlyPublicKey};
///
/// let secp = Secp256k1::new();
/// let tweak = Scalar::random();
///
/// let mut key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
/// let (xonly, _parity) = key_pair.x_only_public_key();
/// let mut keypair = Keypair::new(&secp, &mut rand::thread_rng());
/// let (xonly, _parity) = keypair.x_only_public_key();
/// let tweaked = xonly.add_tweak(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
/// # }
/// ```
@ -1288,13 +1288,13 @@ impl XOnlyPublicKey {
///
/// ```
/// # #[cfg(feature = "rand-std")] {
/// use secp256k1::{Secp256k1, KeyPair, Scalar};
/// use secp256k1::{Secp256k1, Keypair, Scalar};
///
/// let secp = Secp256k1::new();
/// let tweak = Scalar::random();
///
/// let mut key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
/// let (mut public_key, _) = key_pair.x_only_public_key();
/// let mut keypair = Keypair::new(&secp, &mut rand::thread_rng());
/// let (mut public_key, _) = keypair.x_only_public_key();
/// let original = public_key;
/// let (tweaked, parity) = public_key.add_tweak(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
/// assert!(original.tweak_add_check(&secp, &tweaked, parity, tweak));
@ -1550,7 +1550,7 @@ mod test {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
use super::{KeyPair, Parity, PublicKey, Secp256k1, SecretKey, XOnlyPublicKey, *};
use super::{Keypair, Parity, PublicKey, Secp256k1, SecretKey, XOnlyPublicKey, *};
use crate::Error::{InvalidPublicKey, InvalidSecretKey};
use crate::{constants, from_hex, to_hex, Scalar};
@ -1607,7 +1607,7 @@ mod test {
#[cfg(all(feature = "std", not(secp256k1_fuzz)))]
fn erased_keypair_is_valid() {
let s = Secp256k1::new();
let kp = KeyPair::from_seckey_slice(&s, &[1u8; constants::SECRET_KEY_SIZE])
let kp = Keypair::from_seckey_slice(&s, &[1u8; constants::SECRET_KEY_SIZE])
.expect("valid secret key");
let mut kp2 = kp;
kp2.non_secure_erase();
@ -2159,7 +2159,7 @@ mod test {
for _ in 0..10 {
let tweak = Scalar::random();
let kp = KeyPair::new(&s, &mut rand::thread_rng());
let kp = Keypair::new(&s, &mut rand::thread_rng());
let (xonly, _) = XOnlyPublicKey::from_keypair(&kp);
let tweaked_kp = kp.add_xonly_tweak(&s, &tweak).expect("keypair tweak add failed");
@ -2199,7 +2199,7 @@ mod test {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_test::{assert_tokens, Configure, Token};
use crate::key::KeyPair;
use crate::key::Keypair;
use crate::SECP256K1;
#[rustfmt::skip]
@ -2211,7 +2211,7 @@ mod test {
];
static SK_STR: &str = "01010101010101010001020304050607ffff0000ffff00006363636363636363";
let sk = KeyPair::from_seckey_slice(SECP256K1, &SK_BYTES).unwrap();
let sk = Keypair::from_seckey_slice(SECP256K1, &SK_BYTES).unwrap();
#[rustfmt::skip]
assert_tokens(&sk.compact(), &[
Token::Tuple{ len: 32 },
@ -2228,7 +2228,7 @@ mod test {
}
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
fn keys() -> (SecretKey, PublicKey, KeyPair, XOnlyPublicKey) {
fn keys() -> (SecretKey, PublicKey, Keypair, XOnlyPublicKey) {
let secp = Secp256k1::new();
#[rustfmt::skip]
@ -2253,7 +2253,7 @@ mod test {
let sk = SecretKey::from_slice(&SK_BYTES).expect("failed to parse sk bytes");
let pk = PublicKey::from_slice(&pk_bytes).expect("failed to create pk from iterator");
let kp = KeyPair::from_secret_key(&secp, &sk);
let kp = Keypair::from_secret_key(&secp, &sk);
let xonly = XOnlyPublicKey::from_slice(&PK_BYTES).expect("failed to get xonly from slice");
(sk, pk, kp, xonly)
@ -2311,7 +2311,7 @@ mod test {
assert_eq!(got, want)
}
// SecretKey -> KeyPair -> SecretKey
// SecretKey -> Keypair -> SecretKey
#[test]
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
fn roundtrip_secret_key_via_keypair() {
@ -2324,7 +2324,7 @@ mod test {
assert_eq!(back, sk)
}
// KeyPair -> SecretKey -> KeyPair
// Keypair -> SecretKey -> Keypair
#[test]
#[cfg(all(not(secp256k1_fuzz), feature = "alloc"))]
fn roundtrip_keypair_via_secret_key() {
@ -2391,7 +2391,7 @@ mod test {
static PK_STR: &str = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166";
let kp = KeyPair::from_seckey_slice(crate::SECP256K1, &SK_BYTES).unwrap();
let kp = Keypair::from_seckey_slice(crate::SECP256K1, &SK_BYTES).unwrap();
let (pk, _parity) = XOnlyPublicKey::from_keypair(&kp);
#[rustfmt::skip]
@ -2413,10 +2413,10 @@ mod test {
#[cfg(feature = "rand-std")]
fn test_keypair_from_str() {
let ctx = crate::Secp256k1::new();
let keypair = KeyPair::new(&ctx, &mut rand::thread_rng());
let keypair = Keypair::new(&ctx, &mut rand::thread_rng());
let mut buf = [0_u8; constants::SECRET_KEY_SIZE * 2]; // Holds hex digits.
let s = to_hex(&keypair.secret_key().secret_bytes(), &mut buf).unwrap();
let parsed_key = KeyPair::from_str(s).unwrap();
let parsed_key = Keypair::from_str(s).unwrap();
assert_eq!(parsed_key, keypair);
}
@ -2425,7 +2425,7 @@ mod test {
fn test_keypair_deserialize_serde() {
let ctx = crate::Secp256k1::new();
let sec_key_str = "4242424242424242424242424242424242424242424242424242424242424242";
let keypair = KeyPair::from_seckey_str(&ctx, sec_key_str).unwrap();
let keypair = Keypair::from_seckey_str(&ctx, sec_key_str).unwrap();
serde_test::assert_tokens(&keypair.readable(), &[Token::String(sec_key_str)]);
@ -2442,7 +2442,7 @@ mod test {
#[cfg(not(any(feature = "alloc", feature = "global-context")))]
fn test_parse_keypair_no_alloc_panic() {
let key_hex = "4242424242424242424242424242424242424242424242424242424242424242";
let _: KeyPair = key_hex.parse().expect("We shouldn't even get this far");
let _: Keypair = key_hex.parse().expect("We shouldn't even get this far");
}
}

View File

@ -9,7 +9,7 @@ use core::{fmt, ptr, str};
use rand::{CryptoRng, Rng};
use crate::ffi::{self, CPtr};
use crate::key::{KeyPair, XOnlyPublicKey};
use crate::key::{Keypair, XOnlyPublicKey};
#[cfg(feature = "global-context")]
use crate::SECP256K1;
use crate::{
@ -104,7 +104,7 @@ impl<C: Signing> Secp256k1<C> {
fn sign_schnorr_helper(
&self,
msg: &Message,
keypair: &KeyPair,
keypair: &Keypair,
nonce_data: *const ffi::types::c_uchar,
) -> Signature {
unsafe {
@ -127,12 +127,12 @@ impl<C: Signing> Secp256k1<C> {
/// Creates a schnorr signature internally using the [`rand::rngs::ThreadRng`] random number
/// generator to generate the auxiliary random data.
#[cfg(feature = "rand-std")]
pub fn sign_schnorr(&self, msg: &Message, keypair: &KeyPair) -> Signature {
pub fn sign_schnorr(&self, msg: &Message, keypair: &Keypair) -> Signature {
self.sign_schnorr_with_rng(msg, keypair, &mut rand::thread_rng())
}
/// Creates a schnorr signature without using any auxiliary random data.
pub fn sign_schnorr_no_aux_rand(&self, msg: &Message, keypair: &KeyPair) -> Signature {
pub fn sign_schnorr_no_aux_rand(&self, msg: &Message, keypair: &Keypair) -> Signature {
self.sign_schnorr_helper(msg, keypair, ptr::null())
}
@ -140,7 +140,7 @@ impl<C: Signing> Secp256k1<C> {
pub fn sign_schnorr_with_aux_rand(
&self,
msg: &Message,
keypair: &KeyPair,
keypair: &Keypair,
aux_rand: &[u8; 32],
) -> Signature {
self.sign_schnorr_helper(msg, keypair, aux_rand.as_c_ptr() as *const ffi::types::c_uchar)
@ -152,7 +152,7 @@ impl<C: Signing> Secp256k1<C> {
pub fn sign_schnorr_with_rng<R: Rng + CryptoRng>(
&self,
msg: &Message,
keypair: &KeyPair,
keypair: &Keypair,
rng: &mut R,
) -> Signature {
let mut aux = [0u8; 32];
@ -198,7 +198,7 @@ mod tests {
use wasm_bindgen_test::wasm_bindgen_test as test;
use super::*;
use crate::schnorr::{KeyPair, Signature, XOnlyPublicKey};
use crate::schnorr::{Keypair, Signature, XOnlyPublicKey};
use crate::Error::InvalidPublicKey;
use crate::{constants, from_hex, Message, Secp256k1, SecretKey};
@ -238,12 +238,12 @@ mod tests {
#[cfg(feature = "rand-std")]
fn sign_helper(
sign: fn(&Secp256k1<crate::All>, &Message, &KeyPair, &mut ThreadRng) -> Signature,
sign: fn(&Secp256k1<crate::All>, &Message, &Keypair, &mut ThreadRng) -> Signature,
) {
let secp = Secp256k1::new();
let mut rng = rand::thread_rng();
let kp = KeyPair::new(&secp, &mut rng);
let kp = Keypair::new(&secp, &mut rng);
let (pk, _parity) = kp.x_only_public_key();
for _ in 0..100 {
@ -264,7 +264,7 @@ mod tests {
let hex_msg = hex_32!("E48441762FB75010B2AA31A512B62B4148AA3FB08EB0765D76B252559064A614");
let msg = Message::from_digest_slice(&hex_msg).unwrap();
let sk = KeyPair::from_seckey_str(
let sk = Keypair::from_seckey_str(
&secp,
"688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF",
)
@ -324,7 +324,7 @@ mod tests {
#[cfg(feature = "rand-std")]
fn test_pubkey_serialize_roundtrip() {
let secp = Secp256k1::new();
let kp = KeyPair::new(&secp, &mut rand::thread_rng());
let kp = Keypair::new(&secp, &mut rand::thread_rng());
let (pk, _parity) = kp.x_only_public_key();
let ser = pk.serialize();
@ -337,7 +337,7 @@ mod tests {
fn test_xonly_key_extraction() {
let secp = Secp256k1::new();
let sk_str = "688C77BC2D5AAFF5491CF309D4753B732135470D05B7B2CD21ADD0744FE97BEF";
let keypair = KeyPair::from_seckey_str(&secp, sk_str).unwrap();
let keypair = Keypair::from_seckey_str(&secp, sk_str).unwrap();
let sk = SecretKey::from_keypair(&keypair);
assert_eq!(SecretKey::from_str(sk_str).unwrap(), sk);
let pk = crate::key::PublicKey::from_keypair(&keypair);
@ -385,7 +385,7 @@ mod tests {
0x63, 0x63, 0x63, 0x63,
];
let kp = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("sk");
let kp = Keypair::from_seckey_slice(&secp, &SK_BYTES).expect("sk");
// In fuzzing mode secret->public key derivation is different, so
// hard-code the expected result.
@ -445,7 +445,7 @@ mod tests {
fn test_pubkey_serialize() {
use rand::rngs::mock::StepRng;
let secp = Secp256k1::new();
let kp = KeyPair::new(&secp, &mut StepRng::new(1, 1));
let kp = Keypair::new(&secp, &mut StepRng::new(1, 1));
let (pk, _parity) = kp.x_only_public_key();
assert_eq!(
&pk.serialize()[..],
@ -465,7 +465,7 @@ mod tests {
let s = Secp256k1::new();
let msg = Message::from_digest_slice(&[1; 32]).unwrap();
let keypair = KeyPair::from_seckey_slice(&s, &[2; 32]).unwrap();
let keypair = Keypair::from_seckey_slice(&s, &[2; 32]).unwrap();
let aux = [3u8; 32];
let sig = s.sign_schnorr_with_aux_rand(&msg, &keypair, &aux);
static SIG_BYTES: [u8; constants::SCHNORR_SIGNATURE_SIZE] = [

View File

@ -6,7 +6,7 @@ use core::fmt;
use crate::constants::SECRET_KEY_SIZE;
use crate::ecdh::SharedSecret;
use crate::key::{KeyPair, SecretKey};
use crate::key::{Keypair, SecretKey};
use crate::to_hex;
macro_rules! impl_display_secret {
// Default hasher exists only in standard library and not alloc
@ -127,7 +127,7 @@ impl SecretKey {
pub fn display_secret(&self) -> DisplaySecret { DisplaySecret { secret: self.secret_bytes() } }
}
impl KeyPair {
impl Keypair {
/// Formats the explicit byte value of the secret key kept inside the type as a
/// little-endian hexadecimal string using the provided formatter.
///
@ -139,11 +139,11 @@ impl KeyPair {
/// ```
/// # #[cfg(feature = "std")] {
/// # use std::str::FromStr;
/// use secp256k1::{KeyPair, Secp256k1, SecretKey};
/// use secp256k1::{Keypair, Secp256k1, SecretKey};
///
/// let secp = Secp256k1::new();
/// let key = SecretKey::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap();
/// let key = KeyPair::from_secret_key(&secp, &key);
/// let key = Keypair::from_secret_key(&secp, &key);
/// // Here we explicitly display the secret value:
/// assert_eq!(
/// "0000000000000000000000000000000000000000000000000000000000000001",

View File

@ -5,7 +5,7 @@ extern crate secp256k1;
extern crate serde_cbor;
#[cfg(feature = "global-context")]
use secp256k1::{KeyPair, Secp256k1};
use secp256k1::{Keypair, Secp256k1};
use secp256k1::{PublicKey, SecretKey, XOnlyPublicKey};
// Arbitrary key data.
@ -59,9 +59,9 @@ fn bincode_public_key() {
#[test]
#[cfg(feature = "global-context")]
fn bincode_key_pair() {
fn bincode_keypair() {
let secp = Secp256k1::new();
let kp = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("failed to create keypair");
let kp = Keypair::from_seckey_slice(&secp, &SK_BYTES).expect("failed to create keypair");
let ser = bincode::serialize(&kp).unwrap();
assert_eq!(ser, SK_BYTES);