Merge rust-bitcoin/rust-bitcoin#721: Improvements to script methods related to Taproot

d0a87bea72 Add slice 'serialize' method for TweakedPublicKey (Dr. Maxim Orlovsky)
37352d1df5 Add Display and LowerHex to TweakedPublicKey (Dr. Maxim Orlovsky)

Pull request description:

  Extraction of a portion from #696 which can be done without changes in `rust-secp256k1`

ACKs for top commit:
  Kixunil:
    ACK d0a87bea72
  sanket1729:
    ACK d0a87bea72

Tree-SHA512: d439ea1a4c4235bea9867e5d87514f928ad481f7a32403922654c33e101cfaba444eec8b61899f2aaaf1dcf5236bb618b9e14674736d3798effd56ca7097dc78
This commit is contained in:
sanket1729 2021-12-15 14:10:16 +05:30
commit d09ef6f356
No known key found for this signature in database
GPG Key ID: 648FFB183E0870A2
1 changed files with 21 additions and 2 deletions

View File

@ -18,9 +18,10 @@
//! //!
pub use secp256k1::schnorrsig::{PublicKey, KeyPair}; pub use secp256k1::schnorrsig::{PublicKey, KeyPair};
use secp256k1::{Secp256k1, Verification}; use secp256k1::{Secp256k1, Verification, constants};
use hashes::Hash; use hashes::Hash;
use util::taproot::{TapBranchHash, TapTweakHash}; use util::taproot::{TapBranchHash, TapTweakHash};
use core::fmt;
/// Untweaked Schnorr public key /// Untweaked Schnorr public key
pub type UntweakedPublicKey = PublicKey; pub type UntweakedPublicKey = PublicKey;
@ -29,6 +30,18 @@ pub type UntweakedPublicKey = PublicKey;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TweakedPublicKey(PublicKey); pub struct TweakedPublicKey(PublicKey);
impl fmt::LowerHex for TweakedPublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
}
impl fmt::Display for TweakedPublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
/// A trait for tweaking Schnorr public keys /// A trait for tweaking Schnorr public keys
pub trait TapTweak { pub trait TapTweak {
/// Tweaks an untweaked public key given an untweaked key and optional script tree merkle root. /// Tweaks an untweaked public key given an untweaked key and optional script tree merkle root.
@ -66,7 +79,6 @@ impl TapTweak for UntweakedPublicKey {
} }
} }
impl TweakedPublicKey { impl TweakedPublicKey {
/// Creates a new [`TweakedPublicKey`] from a [`PublicKey`]. No tweak is applied, consider /// Creates a new [`TweakedPublicKey`] from a [`PublicKey`]. No tweak is applied, consider
/// calling `tap_tweak` on an [`UntweakedPublicKey`] instead of using this constructor. /// calling `tap_tweak` on an [`UntweakedPublicKey`] instead of using this constructor.
@ -84,4 +96,11 @@ impl TweakedPublicKey {
&self.0 &self.0
} }
/// Serialize the key as a byte-encoded pair of values. In compressed form
/// the y-coordinate is represented by only a single bit, as x determines
/// it up to one bit.
#[inline]
pub fn serialize(&self) -> [u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE] {
self.0.serialize()
}
} }