Skip to content

Implement byte array conversions for XOnlyPublicKey#892

Closed
Abeeujah wants to merge 1 commit into
rust-bitcoin:masterfrom
Abeeujah:xonlypubkey-rtt
Closed

Implement byte array conversions for XOnlyPublicKey#892
Abeeujah wants to merge 1 commit into
rust-bitcoin:masterfrom
Abeeujah:xonlypubkey-rtt

Conversation

@Abeeujah

Copy link
Copy Markdown
Contributor

Description

This PR adds byte array conversion support for XOnlyPublicKey. Since Schnorr public keys (x-only) must represent valid points on the Secp256k1 curve, the implementation includes both fallible and infallible (asserting) constructors to support different use cases and testing requirements.

Changes

  • Added XOnlyPublicKey::to_byte_array: Alias to serialize() method.
  • Added XOnlyPublicKey::try_from_byte_array: A safe, fallible constructor that returns Error::InvalidPublicKey if parsing fails.
  • Added XOnlyPublicKey::from_byte_array: An infallible constructor that uses debug_assert_eq! on the FFI return value, allowing it to conform to the bytes_rtt_test! macro.
  • Enabled RTT Test: Activated rtt_c for XOnlyPublicKey.

Child #885
References #859

@apoelstra

Copy link
Copy Markdown
Member

Needs rebase.

Adds `to_byte_array`, `from_byte_array`, and `parse_from_byte_array` to
XOnlyPublicKey. The implementation wraps `secp256k1_xonly_pubkey_parse`
to ensure the provided x-coordinate represents a valid point on the curve.

The `from_byte_array` method uses a `debug_assert` to satisfy the
`bytes_rtt_test` macro requirements, while `parse_from_byte_array` in
conjunction with `TryFrom<[u8; 32]>` provides a fallible interface
for production use.
@apoelstra

Copy link
Copy Markdown
Member

This is confused slop.

@apoelstra apoelstra closed this Feb 23, 2026
@Abeeujah

Copy link
Copy Markdown
Contributor Author

https://github.com/rust-bitcoin/rust-secp256k1/pull/892/changes#diff-404372cb382c38bf706c977e753c656ed95e29afb8354c9992341bd8f6d8e900L894-L895

Creating an XOnlyPublicKey from a byte array is a fallible operation, the bytes_rtt_test! expects from_byte_array to be infallible

https://github.com/rust-bitcoin/rust-secp256k1/pull/892/changes#diff-2bf91016034b880e946d008664616f7872b290606b8a76db8f20d9358ec3e2efL45-L53

Which for this case, could produce keys not on the curve, and the error would not be propagated to the caller. that's why I did the from_byte_array and implemented the TryFrom trait.

If the caller is sure/certain he has valid bytes array, he could use from_byte_array directly, otherwise, he'd use the parse_byte_array/try_from

Real time the bip340_test_vectors test in
https://github.com/rust-bitcoin/rust-secp256k1/pull/892/changes#diff-ca866735681590c6fc69b50b0a9100c0ea0c485b164e1dc508a14bf60496941bL545
expected the XOnlyPublicKey from bytes to be infallible, else the assertion here would fail
https://github.com/rust-bitcoin/rust-secp256k1/pull/892/changes#diff-ca866735681590c6fc69b50b0a9100c0ea0c485b164e1dc508a14bf60496941bR736-L741

These were the factors put into consideration while implementing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants