key: Add From conversions for references for secret key types
What changed, and why it matters
This commit adds new Rust conversion helpers (From trait implementations) that let callers pass secret key types by reference instead of by value. It is a routine ergonomic/API improvement and does not change security behavior. The existing logic for deriving public keys from secret keys is unchanged; only the way callers provide the input is made more convenient.
No security action required. Treat as a normal API ergonomics change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch in bitcoin/src/crypto/key.rs adds three new From impls: From<&PrivateKey> for Keypair, From<&TweakedKeypair> for &Keypair, and From<&TweakedKeypair> for TweakedPublicKey. It also updates existing From impls to delegate to reference-taking helpers (e.g., From
Changed components
bitcoin/src/crypto/key.rsKeypairPrivateKeyTweakedKeypairTweakedPublicKeyInspect captured patch +17 / −3
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index 74656985..86b18c25 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -222,7 +222,7 @@ mod encapsulate {
/// // There are various conversion methods available to get a tweaked pubkey from a tweaked keypair.
/// let (_pk, _parity) = keypair.public_parts();
/// let _pk = TweakedPublicKey::from_keypair(&keypair);
- /// let _pk = TweakedPublicKey::from(keypair.clone());
+ /// let _pk = TweakedPublicKey::from(&keypair);
/// # }
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
@@ -490,7 +490,11 @@ impl From<Keypair> for secp256k1::PublicKey {
}
impl From<PrivateKey> for Keypair {
- fn from(pk: PrivateKey) -> Self { Self::from_private_key(&pk) }
+ fn from(pk: PrivateKey) -> Self { Self::from(&pk) }
+}
+
+impl From<&PrivateKey> for Keypair {
+ fn from(pk: &PrivateKey) -> Self { Self::from_private_key(pk) }
}
#[deprecated(since = "TBD", note = "use `LegacyPublicKey` instead")]
@@ -1524,9 +1528,19 @@ impl From<TweakedKeypair> for Keypair {
fn from(pair: TweakedKeypair) -> Self { pair.into_keypair() }
}
+impl<'a> From<&'a TweakedKeypair> for &'a Keypair {
+ #[inline]
+ fn from(pair: &'a TweakedKeypair) -> Self { pair.as_keypair() }
+}
+
impl From<TweakedKeypair> for TweakedPublicKey {
#[inline]
- fn from(pair: TweakedKeypair) -> Self { Self::from_keypair(&pair) }
+ fn from(pair: TweakedKeypair) -> Self { Self::from(&pair) }
+}
+
+impl From<&TweakedKeypair> for TweakedPublicKey {
+ #[inline]
+ fn from(pair: &TweakedKeypair) -> Self { Self::from_keypair(pair) }
}
/// Error returned while generating key from slice.
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.