Skip to content

Conversation

shinghim
Copy link
Contributor

I was looking into writing some Arbitrary impls in the bitcoin crate, and one of the types contained a PublicKey. Any interest in adding the arbitrary feature here?

@shinghim shinghim force-pushed the arbitrary branch 3 times, most recently from e025ffe to e68d976 Compare July 20, 2025 22:17
@shinghim shinghim marked this pull request as ready for review July 20, 2025 22:20
src/key.rs Outdated
#[cfg(feature = "global-context")]
impl<'a> Arbitrary<'a> for PublicKey {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(PublicKey::from_secret_key(&SECP256K1, &SecretKey::arbitrary(u)?))
Copy link
Member

Choose a reason for hiding this comment

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

In e68d976:

We can be much faster (at least 100x) than this by choosing 32 random bytes, putting 2 or 3 in front of it, and then attempting to deserialize the resulting 33-byte array as a public key. If we fail, we should retry. On average this will take 2 tries.

If the intention is to use this logic in fuzzers and such then we should definitely do this.

Copy link
Member

Choose a reason for hiding this comment

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

But concept ACK bringing arbitrary into this crate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can be much faster (at least 100x) than this by choosing 32 random bytes, putting 2 or 3 in front of it, and then attempting to deserialize the resulting 33-byte array as a public key. If we fail, we should retry. On average this will take 2 tries.

Nice! Updated with that logic

@apoelstra
Copy link
Member

Nice. fa8af6e looks great.

I'm a liiiitle tempted to try to save on entropy by using a PRNG for retries. But I think that's the wrong approach. Aside from being slow or biased (or both :)) it's not very kind to fuzzers to try to "stretch" their entropy with a PRNG. Since then the fuzzer wastes effort trying to probe the insides of the PRNG instead of the actual code being tested.

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 fa8af6e; successfully ran local tests; nice!

@apoelstra apoelstra merged commit e310618 into rust-bitcoin:master Jul 21, 2025
13 checks passed
chain-forgexcr45 added a commit to chain-forgexcr45/rust-secp256k1 that referenced this pull request Sep 28, 2025
…add PublicKey arbitrary impl

fa8af6e1273503b6ebb71969a5260dc705ebe305 Introduce Arbitrary crate and add PublicKey impl (Shing Him Ng)

Pull request description:

  I was looking into writing some `Arbitrary` impls in the `bitcoin` crate, and one of the types contained a `PublicKey`. Any interest in adding the arbitrary feature here?


ACKs for top commit:
  apoelstra:
    ACK fa8af6e1273503b6ebb71969a5260dc705ebe305; successfully ran local tests; nice!


Tree-SHA512: cee541d209309e0629e6674523972a67cfc22026975349967fc8eb51b68eda134536956f29b31cfa3e89b04aff47e2544907a52947ff5e9eb53e477361776072
william2332-limf added a commit to william2332-limf/rust-secp256k1 that referenced this pull request Oct 2, 2025
…add PublicKey arbitrary impl

fa8af6e1273503b6ebb71969a5260dc705ebe305 Introduce Arbitrary crate and add PublicKey impl (Shing Him Ng)

Pull request description:

  I was looking into writing some `Arbitrary` impls in the `bitcoin` crate, and one of the types contained a `PublicKey`. Any interest in adding the arbitrary feature here?


ACKs for top commit:
  apoelstra:
    ACK fa8af6e1273503b6ebb71969a5260dc705ebe305; successfully ran local tests; nice!


Tree-SHA512: cee541d209309e0629e6674523972a67cfc22026975349967fc8eb51b68eda134536956f29b31cfa3e89b04aff47e2544907a52947ff5e9eb53e477361776072
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