Skip to content

Commit e310618

Browse files
committed
Merge #826: Introduce Arbitrary crate and add PublicKey arbitrary impl
fa8af6e 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 fa8af6e; successfully ran local tests; nice! Tree-SHA512: cee541d209309e0629e6674523972a67cfc22026975349967fc8eb51b68eda134536956f29b31cfa3e89b04aff47e2544907a52947ff5e9eb53e477361776072
2 parents 5307a78 + fa8af6e commit e310618

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Cargo-minimal.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# It is not intended for manual editing.
33
version = 3
44

5+
[[package]]
6+
name = "arbitrary"
7+
version = "1.4.1"
8+
source = "registry+https://github.com/rust-lang/crates.io-index"
9+
checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223"
10+
511
[[package]]
612
name = "arrayvec"
713
version = "0.7.6"
@@ -224,6 +230,7 @@ checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
224230
name = "secp256k1"
225231
version = "0.31.1"
226232
dependencies = [
233+
"arbitrary",
227234
"bincode",
228235
"bitcoin_hashes",
229236
"getrandom",

Cargo-recent.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# It is not intended for manual editing.
33
version = 3
44

5+
[[package]]
6+
name = "arbitrary"
7+
version = "1.4.1"
8+
source = "registry+https://github.com/rust-lang/crates.io-index"
9+
checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223"
10+
511
[[package]]
612
name = "arrayvec"
713
version = "0.7.6"
@@ -215,6 +221,7 @@ checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
215221
name = "secp256k1"
216222
version = "0.31.1"
217223
dependencies = [
224+
"arbitrary",
218225
"bincode",
219226
"bitcoin_hashes",
220227
"getrandom",

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ global-context = ["std"]
3232
# if you are doing a no-std build, then this feature does nothing
3333
# and is not necessary.)
3434
global-context-less-secure = ["global-context"]
35+
arbitrary = ["dep:arbitrary"]
3536

3637
[dependencies]
3738
secp256k1-sys = { version = "0.11.0", default-features = false, path = "./secp256k1-sys" }
3839

40+
arbitrary = { version = "1.4", optional = true }
3941
hashes = { package = "bitcoin_hashes", version = "0.14", default-features = false, optional = true }
4042
rand = { version = "0.9", default-features = false, optional = true }
4143
serde = { version = "1.0.103", default-features = false, optional = true }

src/key.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use core::ops::{self, BitXor};
77
use core::{fmt, ptr, str};
88

9+
#[cfg(feature = "arbitrary")]
10+
use arbitrary::{Arbitrary, Unstructured};
911
use secp256k1_sys::secp256k1_ec_pubkey_sort;
1012
#[cfg(feature = "serde")]
1113
use serde::ser::SerializeTuple;
@@ -1653,6 +1655,26 @@ impl<C: Verification> Secp256k1<C> {
16531655
}
16541656
}
16551657

1658+
#[cfg(feature = "arbitrary")]
1659+
impl<'a> Arbitrary<'a> for PublicKey {
1660+
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
1661+
let mut bytes = [0u8; 33];
1662+
loop {
1663+
// Unstructured::fill_buffer pads the buffer with zeroes if it runs out of data
1664+
if u.len() < 33 {
1665+
return Err(arbitrary::Error::NotEnoughData);
1666+
}
1667+
1668+
bytes[0] = if u.arbitrary::<bool>()? { 0x02 } else { 0x03 };
1669+
u.fill_buffer(&mut bytes[1..])?;
1670+
1671+
if let Ok(pk) = PublicKey::from_slice(&bytes) {
1672+
return Ok(pk);
1673+
}
1674+
}
1675+
}
1676+
}
1677+
16561678
#[cfg(test)]
16571679
#[allow(unused_imports)]
16581680
mod test {

0 commit comments

Comments
 (0)