fixups to taptweaking code

This commit is contained in:
sanket1729 2021-11-12 13:36:31 -08:00
parent df72500465
commit e4774e74eb
2 changed files with 18 additions and 13 deletions

View File

@ -514,9 +514,9 @@ impl Address {
/// Create a pay to taproot address from untweaked key
pub fn p2tr<C: Verification>(
secp: Secp256k1<C>,
secp: &Secp256k1<C>,
internal_key: UntweakedPublicKey,
merkle_root: Option<TapBranchHash>,
merkle_root: Option<&TapBranchHash>,
network: Network
) -> Address {
Address {
@ -539,7 +539,7 @@ impl Address {
network: network,
payload: Payload::WitnessProgram {
version: WitnessVersion::V1,
program: output_key.into_inner().serialize().to_vec()
program: output_key.as_inner().serialize().to_vec()
}
}
}
@ -1196,7 +1196,8 @@ mod tests {
fn p2tr_from_untweaked(){
//Test case from BIP-086
let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap();
let address = Address::p2tr(Secp256k1::new(), internal_key,None, Network::Bitcoin);
let secp = Secp256k1::verification_only();
let address = Address::p2tr(&secp, internal_key, None, Network::Bitcoin);
assert_eq!(address.to_string(), "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr");
assert_eq!(address.address_type(), Some(AddressType::P2tr));
roundtrips(&address);

View File

@ -38,7 +38,7 @@ pub trait TapTweak {
/// * H is the hash function
/// * c is the commitment data
/// * G is the generator point
fn tap_tweak<C: Verification>(&self, secp: Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedPublicKey;
fn tap_tweak<C: Verification>(&self, secp: &Secp256k1<C>, merkle_root: Option<&TapBranchHash>) -> TweakedPublicKey;
/// Directly convert an UntweakedPublicKey to a TweakedPublicKey
///
@ -48,14 +48,13 @@ pub trait TapTweak {
}
impl TapTweak for UntweakedPublicKey {
fn tap_tweak<C: Verification>(&self, secp: Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedPublicKey {
fn tap_tweak<C: Verification>(&self, secp: &Secp256k1<C>, merkle_root: Option<&TapBranchHash>) -> TweakedPublicKey {
// Compute the tweak
let mut engine = TapTweakHash::engine();
engine.input(&self.serialize());
merkle_root.map(|hash| engine.input(&hash));
let tweak_value: [u8; 32] = TapTweakHash::from_engine(engine).into_inner();
//Tweak the internal key by the tweak value
let mut output_key = self.clone();
let parity = output_key.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed");
@ -64,7 +63,6 @@ impl TapTweak for UntweakedPublicKey {
} else { unreachable!("Tap tweak failed") }
}
fn dangerous_assume_tweaked(self) -> TweakedPublicKey {
TweakedPublicKey(self)
}
@ -81,4 +79,10 @@ impl TweakedPublicKey {
pub fn into_inner(self) -> PublicKey {
self.0
}
/// Returns a reference to underlying public key
pub fn as_inner(&self) -> &PublicKey {
&self.0
}
}