Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,17 +1658,50 @@ impl<C: Verification> Secp256k1<C> {
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for PublicKey {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; 33];
Ok(PublicKey::from_x_only_public_key(u.arbitrary()?, u.arbitrary()?))
}
}

#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for Parity {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
match bool::arbitrary(u)? {
true => Ok(Parity::Even),
false => Ok(Parity::Odd),
}
}
}

#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for SecretKey {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; constants::SECRET_KEY_SIZE];
loop {
// Unstructured::fill_buffer pads the buffer with zeroes if it runs out of data
if u.len() < 33 {
if u.len() < constants::SECRET_KEY_SIZE {
return Err(arbitrary::Error::NotEnoughData);
}
u.fill_buffer(&mut bytes[..])?;

bytes[0] = if u.arbitrary::<bool>()? { 0x02 } else { 0x03 };
u.fill_buffer(&mut bytes[1..])?;
if let Ok(sk) = SecretKey::from_byte_array(bytes) {
return Ok(sk);
}
}
}
}

#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for XOnlyPublicKey {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; 32];
loop {
// Unstructured::fill_buffer pads the buffer with zeroes if it runs out of data
if u.len() < 32 {
return Err(arbitrary::Error::NotEnoughData);
}

if let Ok(pk) = PublicKey::from_slice(&bytes) {
u.fill_buffer(&mut bytes[..])?;
if let Ok(pk) = XOnlyPublicKey::from_byte_array(bytes) {
return Ok(pk);
}
}
Expand Down
Loading