Skip to content

Commit f2e4b29

Browse files
committed
Do not test secret->public derivation or pk validity in fuzzing cfg
In the next commit the secret->public key derivation in fuzzing cfg is changed to be simpler, as well as the validity rules of public keys relaxed. This adds a new test to ensure random keys can be added, not just the hard-coded keys test that exists today.
1 parent 5ff59f7 commit f2e4b29

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/key.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,13 @@ mod test {
690690

691691
let s = Secp256k1::signing_only();
692692
let sk = SecretKey::from_slice(&SK_BYTES).expect("sk");
693+
694+
// In fuzzing mode secret->public key derivation is different, so
695+
// hard-code the epected result.
696+
#[cfg(not(fuzzing))]
693697
let pk = PublicKey::from_secret_key(&s, &sk);
698+
#[cfg(fuzzing)]
699+
let pk = PublicKey::from_slice(&[0x02, 0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d, 0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54, 0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66]).expect("pk");
694700

695701
assert_eq!(
696702
sk.to_string(),
@@ -733,6 +739,9 @@ mod test {
733739
}
734740

735741
#[test]
742+
// In fuzzing mode the Y coordinate is expected to match the X, so this
743+
// test uses invalid public keys.
744+
#[cfg(not(fuzzing))]
736745
fn test_pubkey_serialize() {
737746
struct DumbRng(u32);
738747
impl RngCore for DumbRng {
@@ -841,7 +850,7 @@ mod test {
841850
assert_eq!(set.len(), COUNT);
842851
}
843852

844-
#[test]
853+
#[cfg_attr(not(fuzzing), test)]
845854
fn pubkey_combine() {
846855
let compressed1 = PublicKey::from_slice(
847856
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
@@ -861,7 +870,7 @@ mod test {
861870
assert_eq!(sum1.unwrap(), exp_sum);
862871
}
863872

864-
#[test]
873+
#[cfg_attr(not(fuzzing), test)]
865874
fn pubkey_combine_keys() {
866875
let compressed1 = PublicKey::from_slice(
867876
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
@@ -884,6 +893,24 @@ mod test {
884893
assert_eq!(sum1.unwrap(), exp_sum);
885894
}
886895

896+
#[test]
897+
fn create_pubkey_combine() {
898+
let s = Secp256k1::new();
899+
900+
let (mut sk1, pk1) = s.generate_keypair(&mut thread_rng());
901+
let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
902+
903+
let sum1 = pk1.combine(&pk2);
904+
assert!(sum1.is_ok());
905+
let sum2 = pk2.combine(&pk1);
906+
assert!(sum2.is_ok());
907+
assert_eq!(sum1, sum2);
908+
909+
assert!(sk1.add_assign(&sk2.as_ref()[..]).is_ok());
910+
let sksum = PublicKey::from_secret_key(&s, &sk1);
911+
assert_eq!(Ok(sksum), sum1);
912+
}
913+
887914
#[test]
888915
fn pubkey_equal() {
889916
let pk1 = PublicKey::from_slice(

src/schnorrsig.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,9 @@ mod tests {
706706
PublicKey::from_slice(&[0xff; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]),
707707
Err(InvalidPublicKey)
708708
);
709+
// In fuzzing mode restrictions on public key validity are much more
710+
// relaxed, thus the invalid check below is expected to fail.
711+
#[cfg(not(fuzzing))]
709712
assert_eq!(
710713
PublicKey::from_slice(&[0x55; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]),
711714
Err(InvalidPublicKey)
@@ -724,7 +727,13 @@ mod tests {
724727

725728
let s = Secp256k1::signing_only();
726729
let sk = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("sk");
730+
731+
// In fuzzing mode secret->public key derivation is different, so
732+
// hard-code the epected result.
733+
#[cfg(not(fuzzing))]
727734
let pk = PublicKey::from_keypair(&s, &sk);
735+
#[cfg(fuzzing)]
736+
let pk = PublicKey::from_slice(&[0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d, 0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54, 0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66]).expect("pk");
728737

729738
assert_eq!(
730739
pk.to_string(),
@@ -762,6 +771,9 @@ mod tests {
762771
}
763772

764773
#[test]
774+
// In fuzzing mode secret->public key derivation is different, so
775+
// this test will never correctly derive the static pubkey.
776+
#[cfg(not(fuzzing))]
765777
fn test_pubkey_serialize() {
766778
struct DumbRng(u32);
767779
impl RngCore for DumbRng {

0 commit comments

Comments
 (0)