Skip to content

Add arbitrary impl for types used in PSBTs #828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shinghim
Copy link
Contributor

No description provided.

@apoelstra
Copy link
Member

concept NACK these ones -- we aren't allowed to put arbitrary data into these structures. All the C library guarantees us is that the size is OK to assume. In particular, this will produce invalid x-only public keys which will cause aborts if used with the rest of the library.

@shinghim
Copy link
Contributor Author

Oh i see. Would it be acceptable to create random SecretKey from the doc example:

/// let secp = Secp256k1::new();
/// let secret_key = SecretKey::new(&mut rand::rng());

and use that to generate the arbitrary types in secp256k1 and leave the ones in ffi alone?

@apoelstra
Copy link
Member

@shinghim yeah, secret keys in particular I think you can go ahead and produce using Arbitrary. The upstream C library just uses unsigned char* for these.

For the other FFI types, I think you can do it, but you need to use a similar strategy to the main crate, where you generate byte arrays then use the (unsafe) decoding functions to produce the actual objects.

@shinghim shinghim force-pushed the arbitrary branch 3 times, most recently from 0718503 to 40f073b Compare July 25, 2025 22:42
@shinghim shinghim marked this pull request as ready for review July 25, 2025 22:43
@shinghim
Copy link
Contributor Author

Updated to derive XOnlyPublicKey from an arbitrary PublicKey and added an impl for SecretKey. I removed the impl for Signature since I didn't think there would be any use for an arbitrary one. I think it might more make sense to generate an arbitrary SecretKey and use that to create an "arbitrary" Signature

@apoelstra
Copy link
Member

In 40f073b:

Sorry to keep iterating on this, but could you:

  • Derive XOnlyPublicKey directly by attempting to parse a [u8; 32]
  • Derive PublicKey by deriving a Parity and XOnlyPublicKey and using those to construct a PublicKey.

Currently you have the opposite -- to get a XOnlyPublicKey you construct a PublicKey, which consumes a byte to determine parity, which you then just forget.

@shinghim
Copy link
Contributor Author

shinghim commented Jul 29, 2025

No worries, I appreciate the feedback and the review. Updated to create the PublicKey with an XOnlyPublicKey and Parity

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