P2TR address from untweaked key#691
P2TR address from untweaked key#691dr-orlovsky merged 1 commit intorust-bitcoin:masterfrom nlanson:p2tr_address
Conversation
Kixunil
left a comment
There was a problem hiding this comment.
Alternative API idea:
- Have
TweakedPublicKeyandUntweakedPublicKey UntweakedPublicKeycan be created fromshnorrsig::PublicKeyor is a type alias ofshnorrsig::PublicKey- Allow conversion between them by calling
tweak()method. - Allow direct conversion with
dangerous_assume_tweaked()method
|
Having a tweaked and untweaked type alias sounds like a good idea since it is ambiguous. Something like this might work? It is my first time using type aliases so not sure what the convention is surrounding them. type UntweakedPublicKey = PublicKey;
type TweakedPublicKey = PublicKey;
trait KeyTweaking {
/// Tweaks an untweaked public key.
fn tweak<C: Verification>(&self, secp: Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedPublicKey;
/// Directly convert an UntweakedPublicKey to a TweakedPublicKey
fn dangerous_assume_tweaked(&self) -> TweakedPublicKey;
}
impl KeyTweaking for UntweakedPublicKey {
fn tweak<C: Verification>(&self, secp: Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedPublicKey {
// Applies the tweak to self
}
fn dangerous_assume_tweaked(&self) -> TweakedPublicKey {
// Direct converstion
}
}and /// Tweaks the internal key
pub fn p2tr<C: Verification>(
secp: Secp256k1<C>,
internal_key: UntweakedPublicKey,
merkle_root: Option<TapBranchHash>,
network: Network
) -> Address {
Address {
network: network,
payload: Payload::WitnessProgram {
version: WitnessVersion::V1,
program: internal_key.tweak(secp, merkle_root).serialize().to_vec()
}
}
}
/// Does not tweak the given key assuming it has already been tweaked
pub fn dangerous_p2tr<C: Verification>(
internal_key: TweakedPublicKey,
network: Network
) -> Address {
Address {
network: network,
payload: Payload::WitnessProgram {
version: WitnessVersion::V1,
program: internal_key.serialize().to_vec()
}
}
} |
|
Also I don't see why trait is useful and consuming pub type UntweakedPublicKey = PublicKey;
pub struct TweakedPublicKey(PublicKey);
impl UntweakedPublicKey {
/// Tweaks an untweaked public key.
pub fn tweak<C: Verification>(self, secp: Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedPublicKey {
// Applies the tweak to self
}
/// Directly convert an UntweakedPublicKey to a TweakedPublicKey
fn dangerous_assume_tweaked(self) -> TweakedPublicKey {
TweakedPublicKey(self)
}
}
impl TweakedPublicKey {
fn into_raw(self) -> PublicKey {
self.0
}
} |
|
+1 for the current suggestion from @Kixunil . |
|
Most recent commits include changes to reflect suggestions from @Kixunil and @sanket1729. Changes-Added two new structs in Please let me know if there is anything is wrong or missing. |
I believe it should be the other way around? Or rather, only have non-dangerous API? |
#687 points out that operating on tweaked keys is discouraged so I believe it is correct that |
|
The API I suggested should remove the danger but maybe I'm missing something? |
|
Just to note that we still need API working on keys assumed to be tweaked (dangerous) for supporting pay-to-contract schemes with taproot, which will not follow exact BIP-341 recommendation and will tweak internal key not with its hash, but with some contract (non-script) hash. |
src/util/schnorr.rs
Outdated
There was a problem hiding this comment.
I think this is not needed
There was a problem hiding this comment.
Better to have internal panic than incorrect results?
There was a problem hiding this comment.
How we can have an incorrect result if it is checked in line 58 already and this check adds nothing to that?
There was a problem hiding this comment.
Yes, it should. my bad! I would still prefer the regular |
|
Most recent commits include changes requested by @dr-orlovsky Changes
|
|
I am still stuck on deciding whether If it is going to be a type alias it will need to implement a trait as I cannot directly implement to I am leaning towards using a type alias though as it will make using the |
|
@nlanson I am up to have a type alias since it will simplify API usage overall (it's easier to |
dr-orlovsky
left a comment
There was a problem hiding this comment.
LGTM. Will ACK once we will have a final code with type alias (if everyone agree upon that) and old code comments removed
|
We could also add |
|
Most recent commits now use a type alias instead of a new type for Changes
|
dr-orlovsky
left a comment
There was a problem hiding this comment.
utACK 1a990b9b126ce8cf0b1f26b419cabc1d8621979c modulo several nets
But we need commits to be squashed before merging
sanket1729
left a comment
There was a problem hiding this comment.
This looks great, left a couple of nits. I agree with the overall API, I think commits can be squashed and we are good to go.
Ambiguous TweakedPublicKey and UntweakedPublicKey type aliases and methods to convert Use structs for Untweaked and Tweaked key type swap dangerous api to work on tweaked keys remove unecessary allocations and rename methods Use type alias for UntweakedPublicKey TweakedPublicKey::new(...) method added minor naming and doc changes
|
Managed to squash the commits after battling with git for a good hour. |
…g tweak for UntweakedPublicKey 5b21a9c Use TapTweakHash method for computing tweak (Noah) Pull request description: Quick follow up PR to #691 using a method from #677. ### Changes - Updated `UntweakedPublicKey::tap_tweak(...)` to use `TapTweakHash::from_key_and_tweak(...)` ACKs for top commit: Kixunil: ACK 5b21a9c dr-orlovsky: utACK 5b21a9c Tree-SHA512: d00455bba51981e9ec942a6cf69672666e227850d073b1fdcd92d2eb6ad553659fb2967aec2ce12d3ed109cee5fa125cdda649cddb25404f08adae2bfd3e19bb
7aacc37 Add tests from BIP341 (sanket1729) 61629cc Make taproot hashes forward display (sanket1729) Pull request description: Add tests for taproot. - ~Also fixes one bug in #677, namely, I was returning `LeafVersion::default()` instead of given version~ - ~ Fixes a bug in #691 about taking secp context as a reference instead of consuming it. This should have not passed my review, but this is easy to miss. ~ - Makes the display on taproot hashes forward instead of the reverse (because the BIP prints in a forward way, I think we should too and it is more natural. ) ACKs for top commit: RCasatta: ACK 7aacc37 apoelstra: ACK 7aacc37 Tree-SHA512: 2e0442131fc036ffa10f88c91c8fc02d9b67ff6c16c592aa6f4e6a220c26a00fc6ca95a288f14aa40667a289fb0446219fd6c76c0196ead766252356592b9941
rebeccahermanreedroe-cpu
left a comment
There was a problem hiding this comment.
What is this ?Is this done
This PR was merged years ago. |
|
@nlanson they have internet in the mountains huh? |
|
Yep. And it's fast. |
|
Now you winding me up. |
Addressing issue #687.
Changes:
Address::dangerous_p2trthat creates an address given an untweaked internal key and optional merkle root of a script tree. The method utilizes existing hash methods from within the library and key tweaking from therust-secp256k1library.Questions:
secp256k1key tweaking method does not hash the data, there could be a method that does the tweaking and commitment part of theAddress::dangerous_p2trmethod.Address::dangerous_p2trreturn if an error occurs while tweaking the internal key?I would love feedback as this is my first PR to this repository.
See updated changes below