taproot: add TapNodeHash getter method on TapTree and NodeInfo#2467
Merged
Conversation
Pull Request Test Coverage Report for Build 7879485491Details
💛 - Coveralls |
Member
|
I think the method on But I think you're correct that this is an API gap. I agree there's a mild footgun in the |
Collaborator
|
Concept ACK. Maybe |
Fixes a gap in the API of the taproot module. Callers can now use TapTree::root_hash or NodeInfo::node_hash to extract the taproot tree merkle root hash for fast validation without any ECC overhead.
2c9d911 to
1384330
Compare
Contributor
Author
|
I renamed Then squashed, rebased, and added a more detailed commit message. |
apoelstra
approved these changes
Feb 13, 2024
tcharding
approved these changes
Feb 13, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Submitting this to fix what I think is an API hole. Please correct me if I'm mistaken here and there is an easier way to do what I'm after.
Problem
From what I can tell of the existing 0.31.1 API, there doesn't seem to be any way for a consumer to build a taproot tree using
TaprootBuilderand then simply output the resulting tap tree merkle rootTapNodeHash.Instead, the API forces me to do
TaprootBuilder::finalize(secp_ctx, internal_key)first to get aTaprootSpendInfo, and then callTaprootSpendInfo::merkle_root()to get the rootTapNodehash. This requires ECC point addition/multiplication for the tweak operation (insideTaprootBuilder::finalize), so it is a lot less performant than the simple hashing operations needed to build a taproot tree.Obviously if I want to spend the taproot tree, I'll need to tweak an internal key. But if all I want is to examine the merkle root hashes of taproot trees (e.g. for quick validation), there should be a faster and more direct option for me.
Suggested Solution
My suggestion, demonstrated in this PR, would be to add a couple of simple getter methods:
TapTree::node_hash() -> TapNodeHashNodeInfo::node_hash() -> TapNodeHashThese rhyme with the existing
LeafNode::node_hash()method. These provide a roundabout way for downstream consumers to extract a taptree merkle rootTapNodeHashwhile still using the safe API provided byTaprootBuilder. I would simply useTaprootBuilderto build aTapTreeorNodeInfo, and then invoke thenode_hashmethod on that object. No point addition required.Footguns
This does open up more opportunities for consumers to footgun themselves by, for example, committing a P2TR script pubkey to a leaf node by accident, instead of the root node. I'd argue this possibility already exists in the form of
LeafNode::node_hash(). We're not making that problem much worse here.If the caller is using
TaprootBuilderto construct their tree, the only way they'll be able to get aNodeInfoorTapTreein the first place would be to finalize the builder into it, which seems like an acceptable and intuitive-enough usage path to me.