Skip to content

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

@apoelstra
Copy link
Member

Can you run cargo +nightly --all --check on this? I see one line that's not formatted.

     fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
         match bool::arbitrary(u)? {
             true => Ok(Parity::Even),
-            false => Ok(Parity::Odd)
+            false => Ok(Parity::Odd),
         }
     }
 }

@apoelstra
Copy link
Member

Unsure why CI is not complaining about this..

@shinghim
Copy link
Contributor Author

Updated after running cargo fmt --all (I think you meant fmt instead of check). The missing comma you mentioned was the only thing it added

Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 02ac1f2

@tcharding
Copy link
Member

Is there any chance these loops spin forever making debugging fuzzing harder?

@shinghim
Copy link
Contributor Author

Is there any chance these loops spin forever making debugging fuzzing harder?

I don't think so since we're checking the amount of bytes left each time

@tcharding
Copy link
Member

Ah sweet, I misread that check as checking the fill buffer length. Thanks man.

@apoelstra
Copy link
Member

Heh, interesting. I considered nitting the check with "eh, who cares if it 0-fills" but actually, since all-zeroes is not a valid x coordinate, the zero-filling would've caused an infinite loop! Good save.

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 02ac1f2; successfully ran local tests; nice!!

@apoelstra apoelstra merged commit d329d79 into rust-bitcoin:master Jul 30, 2025
13 checks passed
chain-forgexcr45 added a commit to chain-forgexcr45/rust-secp256k1 that referenced this pull request Sep 28, 2025
…sed in PSBTs

02ac1f216bea85bbef43b419fd0c2f8453e5f7d2 Add `Arbitrary` impl for SecretKey, Parity, and XOnlyPublicKey (Shing Him Ng)

Pull request description:

  


ACKs for top commit:
  tcharding:
    ACK 02ac1f216bea85bbef43b419fd0c2f8453e5f7d2
  apoelstra:
    ACK 02ac1f216bea85bbef43b419fd0c2f8453e5f7d2; successfully ran local tests; nice!!


Tree-SHA512: bc6248e54770e27dbc72a5e1e72e7918fe6916cdae2208f2841833d56b214e95025a9a4c8ddb991dd99910a58e28d04c283ecf7bc74f60eb1e265b82e4778381
william2332-limf added a commit to william2332-limf/rust-secp256k1 that referenced this pull request Oct 2, 2025
…sed in PSBTs

02ac1f216bea85bbef43b419fd0c2f8453e5f7d2 Add `Arbitrary` impl for SecretKey, Parity, and XOnlyPublicKey (Shing Him Ng)

Pull request description:

  


ACKs for top commit:
  tcharding:
    ACK 02ac1f216bea85bbef43b419fd0c2f8453e5f7d2
  apoelstra:
    ACK 02ac1f216bea85bbef43b419fd0c2f8453e5f7d2; successfully ran local tests; nice!!


Tree-SHA512: bc6248e54770e27dbc72a5e1e72e7918fe6916cdae2208f2841833d56b214e95025a9a4c8ddb991dd99910a58e28d04c283ecf7bc74f60eb1e265b82e4778381
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.

3 participants