Introduce roundtrip tests for all key types
What changed, and why it matters
This commit only adds new automated tests that check whether Bitcoin-style cryptographic keys can be converted to and from the underlying secp256k1 library's key types. It does not change any production code, fix a bug, or alter behavior. There is no security issue here.
No action needed; this is a test-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds four unit tests in bitcoin/src/crypto/key.rs under #[cfg(all(feature = “rand”, feature = “std”))]. They roundtrip Keypair, PublicKey, XOnlyPublicKey, and PrivateKey through secp256k1 types to verify conversion correctness. No implementation code is modified.
Changed components
Inspect captured patch +45 / −0
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index 7d1726e2..d8adab40 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -2244,4 +2244,49 @@ mod tests {
let decoded = encoded.parse::<Keypair>().unwrap();
assert_eq!(decoded, keypair);
}
+
+ #[test]
+ #[cfg(all(feature = "rand", feature = "std"))]
+ fn keypair_secp_roundtrip() {
+ let bitcoin_key = Keypair::generate();
+ let secp_key =
+ secp256k1::Keypair::from_seckey_byte_array(bitcoin_key.to_secret_bytes()).unwrap();
+ assert_eq!(Keypair::from_secp(secp_key), bitcoin_key);
+ }
+
+ #[test]
+ #[cfg(all(feature = "rand", feature = "std"))]
+ fn public_key_secp_roundtrip() {
+ let bitcoin_key = Keypair::generate().to_public_key();
+ let secp_key =
+ secp256k1::PublicKey::from_byte_array_compressed(bitcoin_key.serialize_compressed())
+ .unwrap();
+ assert_eq!(PublicKey::from_secp(secp_key), bitcoin_key);
+ // Also assert that generating a secp from compressed or uncompressed yields the same value
+ assert_eq!(
+ secp256k1::PublicKey::from_byte_array_uncompressed(
+ bitcoin_key.serialize_uncompressed()
+ )
+ .unwrap(),
+ secp_key,
+ );
+ }
+
+ #[test]
+ #[cfg(all(feature = "rand", feature = "std"))]
+ fn xonly_secp_roundtrip() {
+ let bitcoin_key = Keypair::generate().to_x_only_public_key();
+ let secp_key =
+ secp256k1::XOnlyPublicKey::from_byte_array(bitcoin_key.serialize().0).unwrap();
+ assert_eq!(bitcoin_key, XOnlyPublicKey::from_secp(secp_key, bitcoin_key.parity()),);
+ }
+
+ #[test]
+ #[cfg(all(feature = "rand", feature = "std"))]
+ fn private_key_secp_roundtrip() {
+ let bitcoin_key = PrivateKey::generate();
+ let secp_key =
+ secp256k1::SecretKey::from_secret_bytes(bitcoin_key.to_secret_bytes()).unwrap();
+ assert_eq!(PrivateKey::from_secp(secp_key), bitcoin_key);
+ }
}
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.