Skip to content

Commit

Permalink
Use TapTweakHash method for computing tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
nlanson committed Nov 24, 2021
1 parent 5286d0a commit 5b21a9c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/util/address.rs
Expand Up @@ -524,7 +524,7 @@ impl Address {
pub fn p2tr<C: Verification>(
secp: &Secp256k1<C>,
internal_key: UntweakedPublicKey,
merkle_root: Option<&TapBranchHash>,
merkle_root: Option<TapBranchHash>,
network: Network
) -> Address {
Address {
Expand Down
11 changes: 4 additions & 7 deletions src/util/schnorr.rs
Expand Up @@ -19,7 +19,7 @@

pub use secp256k1::schnorrsig::{PublicKey, KeyPair};
use secp256k1::{Secp256k1, Verification};
use hashes::{Hash, HashEngine};
use hashes::Hash;
use util::taproot::{TapBranchHash, TapTweakHash};

/// Untweaked Schnorr public key
Expand All @@ -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
///
Expand All @@ -48,12 +48,9 @@ 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();
let tweak_value = TapTweakHash::from_key_and_tweak(self, merkle_root).into_inner();

//Tweak the internal key by the tweak value
let mut output_key = self.clone();
Expand Down

0 comments on commit 5b21a9c

Please sign in to comment.