From edafb88f8c6b9f8f21b253621cd5ecbf1e2bffd1 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Tue, 4 Jan 2022 08:23:05 +1100 Subject: [PATCH] Move key unit tests to key module There are currently two unit tests in the `schnorr` module that are testing keys from the `key` module. This is possible because the tests are only testing the public interface, none the less they are better placed in the `key` module. --- src/key.rs | 34 ++++++++++++++++++++++++++++++++++ src/schnorr.rs | 33 --------------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/key.rs b/src/key.rs index c6974e3..57a4280 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1476,4 +1476,38 @@ mod test { assert_tokens(&pk.readable(), &[Token::String(PK_STR)]); } + + #[test] + fn test_tweak_add_assign_then_tweak_add_check() { + let s = Secp256k1::new(); + + for _ in 0..10 { + let mut tweak = [0u8; 32]; + thread_rng().fill_bytes(&mut tweak); + let (mut kp, mut pk) = s.generate_schnorrsig_keypair(&mut thread_rng()); + let orig_pk = pk; + kp.tweak_add_assign(&s, &tweak).expect("Tweak error"); + let parity = pk.tweak_add_assign(&s, &tweak).expect("Tweak error"); + assert_eq!(XOnlyPublicKey::from_keypair(&kp), pk); + assert!(orig_pk.tweak_add_check(&s, &pk, parity, tweak)); + } + } + + #[test] + fn test_from_key_pubkey() { + let kpk1 = PublicKey::from_str( + "02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443", + ) + .unwrap(); + let kpk2 = PublicKey::from_str( + "0384526253c27c7aef56c7b71a5cd25bebb66dddda437826defc5b2568bde81f07", + ) + .unwrap(); + + let pk1 = XOnlyPublicKey::from(kpk1); + let pk2 = XOnlyPublicKey::from(kpk2); + + assert_eq!(pk1.serialize()[..], kpk1.serialize()[1..]); + assert_eq!(pk2.serialize()[..], kpk2.serialize()[1..]); + } } diff --git a/src/schnorr.rs b/src/schnorr.rs index 4516f2d..fc1a628 100644 --- a/src/schnorr.rs +++ b/src/schnorr.rs @@ -565,37 +565,4 @@ mod tests { assert_tokens(&pk.readable(), &[Token::Str(PK_STR)]); assert_tokens(&pk.readable(), &[Token::String(PK_STR)]); } - #[test] - fn test_addition() { - let s = Secp256k1::new(); - - for _ in 0..10 { - let mut tweak = [0u8; 32]; - thread_rng().fill_bytes(&mut tweak); - let (mut kp, mut pk) = s.generate_schnorrsig_keypair(&mut thread_rng()); - let orig_pk = pk; - kp.tweak_add_assign(&s, &tweak).expect("Tweak error"); - let parity = pk.tweak_add_assign(&s, &tweak).expect("Tweak error"); - assert_eq!(XOnlyPublicKey::from_keypair(&kp), pk); - assert!(orig_pk.tweak_add_check(&s, &pk, parity, tweak)); - } - } - - #[test] - fn test_from_key_pubkey() { - let kpk1 = ::key::PublicKey::from_str( - "02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443", - ) - .unwrap(); - let kpk2 = ::key::PublicKey::from_str( - "0384526253c27c7aef56c7b71a5cd25bebb66dddda437826defc5b2568bde81f07", - ) - .unwrap(); - - let pk1 = XOnlyPublicKey::from(kpk1); - let pk2 = XOnlyPublicKey::from(kpk2); - - assert_eq!(pk1.serialize()[..], kpk1.serialize()[1..]); - assert_eq!(pk2.serialize()[..], kpk2.serialize()[1..]); - } }