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
|
||||
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);
|
||||
|
|
|
@ -31,30 +31,29 @@ pub struct TweakedPublicKey(PublicKey);
|
|||
/// A trait for tweaking Schnorr public keys
|
||||
pub trait TapTweak {
|
||||
/// Tweaks an untweaked public key given an untweaked key and optional script tree merkle root.
|
||||
///
|
||||
/// This is done by using the equation Q = P + H(P|c)G, where
|
||||
///
|
||||
/// This is done by using the equation Q = P + H(P|c)G, where
|
||||
/// * Q is the tweaked key
|
||||
/// * P is the internal key
|
||||
/// * 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
|
||||
///
|
||||
///
|
||||
/// This method is dangerous and can lead to loss of funds if used incorrectly.
|
||||
/// Specifically, in multi-party protocols a peer can provide a value that allows them to steal.
|
||||
fn dangerous_assume_tweaked(self) -> TweakedPublicKey;
|
||||
}
|
||||
|
||||
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();
|
||||
|
@ -64,7 +63,6 @@ impl TapTweak for UntweakedPublicKey {
|
|||
} else { unreachable!("Tap tweak failed") }
|
||||
}
|
||||
|
||||
|
||||
fn dangerous_assume_tweaked(self) -> TweakedPublicKey {
|
||||
TweakedPublicKey(self)
|
||||
}
|
||||
|
@ -76,9 +74,15 @@ impl TweakedPublicKey {
|
|||
pub fn new(key: PublicKey) -> TweakedPublicKey {
|
||||
TweakedPublicKey(key)
|
||||
}
|
||||
|
||||
/// Returns the underlying public key
|
||||
|
||||
/// Returns the underlying public key
|
||||
pub fn into_inner(self) -> PublicKey {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Returns a reference to underlying public key
|
||||
pub fn as_inner(&self) -> &PublicKey {
|
||||
&self.0
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue