Merge rust-bitcoin/rust-bitcoin#707: P2tr fixes
e4774e74eb
fixups to taptweaking code (sanket1729) Pull request description: This was my bad for not clearly stating the expected spec #687 . Changed values to references so that we only take ownership where it is required. This should simplify the #697 ACKs for top commit: Kixunil: ACKe4774e74eb
dr-orlovsky: utACKe4774e74eb
Tree-SHA512: adacbfa8a77f46b2c85720f3760ed12a437f40d8422731d0207662d7947c95dda79d576923f6056c77f57977a3dcd25afd270f0ee11e9c3be9d067ccdc63371a
This commit is contained in:
commit
435298c427
|
@ -514,9 +514,9 @@ impl Address {
|
||||||
|
|
||||||
/// Create a pay to taproot address from untweaked key
|
/// Create a pay to taproot address from untweaked key
|
||||||
pub fn p2tr<C: Verification>(
|
pub fn p2tr<C: Verification>(
|
||||||
secp: Secp256k1<C>,
|
secp: &Secp256k1<C>,
|
||||||
internal_key: UntweakedPublicKey,
|
internal_key: UntweakedPublicKey,
|
||||||
merkle_root: Option<TapBranchHash>,
|
merkle_root: Option<&TapBranchHash>,
|
||||||
network: Network
|
network: Network
|
||||||
) -> Address {
|
) -> Address {
|
||||||
Address {
|
Address {
|
||||||
|
@ -539,7 +539,7 @@ impl Address {
|
||||||
network: network,
|
network: network,
|
||||||
payload: Payload::WitnessProgram {
|
payload: Payload::WitnessProgram {
|
||||||
version: WitnessVersion::V1,
|
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(){
|
fn p2tr_from_untweaked(){
|
||||||
//Test case from BIP-086
|
//Test case from BIP-086
|
||||||
let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap();
|
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.to_string(), "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr");
|
||||||
assert_eq!(address.address_type(), Some(AddressType::P2tr));
|
assert_eq!(address.address_type(), Some(AddressType::P2tr));
|
||||||
roundtrips(&address);
|
roundtrips(&address);
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub trait TapTweak {
|
||||||
/// * H is the hash function
|
/// * H is the hash function
|
||||||
/// * c is the commitment data
|
/// * c is the commitment data
|
||||||
/// * G is the generator point
|
/// * 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
|
/// Directly convert an UntweakedPublicKey to a TweakedPublicKey
|
||||||
///
|
///
|
||||||
|
@ -48,14 +48,13 @@ pub trait TapTweak {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TapTweak for UntweakedPublicKey {
|
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
|
// Compute the tweak
|
||||||
let mut engine = TapTweakHash::engine();
|
let mut engine = TapTweakHash::engine();
|
||||||
engine.input(&self.serialize());
|
engine.input(&self.serialize());
|
||||||
merkle_root.map(|hash| engine.input(&hash));
|
merkle_root.map(|hash| engine.input(&hash));
|
||||||
let tweak_value: [u8; 32] = TapTweakHash::from_engine(engine).into_inner();
|
let tweak_value: [u8; 32] = TapTweakHash::from_engine(engine).into_inner();
|
||||||
|
|
||||||
|
|
||||||
//Tweak the internal key by the tweak value
|
//Tweak the internal key by the tweak value
|
||||||
let mut output_key = self.clone();
|
let mut output_key = self.clone();
|
||||||
let parity = output_key.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed");
|
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") }
|
} else { unreachable!("Tap tweak failed") }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn dangerous_assume_tweaked(self) -> TweakedPublicKey {
|
fn dangerous_assume_tweaked(self) -> TweakedPublicKey {
|
||||||
TweakedPublicKey(self)
|
TweakedPublicKey(self)
|
||||||
}
|
}
|
||||||
|
@ -81,4 +79,10 @@ impl TweakedPublicKey {
|
||||||
pub fn into_inner(self) -> PublicKey {
|
pub fn into_inner(self) -> PublicKey {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to underlying public key
|
||||||
|
pub fn as_inner(&self) -> &PublicKey {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue