Skip to content

Introduce Arbitrary crate and add PublicKey arbitrary impl #826

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

Merged
merged 1 commit into from
Jul 21, 2025

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
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