-
Notifications
You must be signed in to change notification settings - Fork 831
Remove redundant code computing tap hashes #926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,21 @@ impl TapLeafHash { | |
} | ||
} | ||
|
||
impl TapBranchHash { | ||
/// Computes branch hash given two hashes of the nodes underneath it. | ||
pub fn from_node_hashes(a: sha256::Hash, b: sha256::Hash) -> TapBranchHash { | ||
let mut eng = TapBranchHash::engine(); | ||
if a < b { | ||
eng.input(&a); | ||
eng.input(&b); | ||
} else { | ||
eng.input(&b); | ||
eng.input(&a); | ||
}; | ||
TapBranchHash::from_engine(eng) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this, but I won't hold up the PR for it. |
||
} | ||
|
||
/// Maximum depth of a taproot tree script spend path. | ||
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L229 | ||
pub const TAPROOT_CONTROL_MAX_NODE_COUNT: usize = 128; | ||
|
@@ -561,16 +576,9 @@ impl NodeInfo { | |
b_leaf.merkle_branch.push(a.hash)?; // add hashing partner | ||
all_leaves.push(b_leaf); | ||
} | ||
let mut eng = TapBranchHash::engine(); | ||
if a.hash < b.hash { | ||
eng.input(&a.hash); | ||
eng.input(&b.hash); | ||
} else { | ||
eng.input(&b.hash); | ||
eng.input(&a.hash); | ||
}; | ||
let hash = TapBranchHash::from_node_hashes(a.hash, b.hash); | ||
Ok(Self { | ||
hash: sha256::Hash::from_engine(eng), | ||
hash: sha256::Hash::from_inner(hash.into_inner()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a 'normal' thing to do, convert between the various sha256 hash types? Should be be providing conversion functions over in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably provide a conveience method for "wrapped hashes" to their generic form (and vice-versa), yeah. Sometimes hash conversions reflect perversions in Bitcoin, and these should appear perversely in our code :) but I don't think this is one of them. In any case, for purposes of this PR the explicit conversion is fine. |
||
leaves: all_leaves, | ||
}) | ||
} | ||
|
@@ -775,16 +783,11 @@ impl ControlBlock { | |
let mut curr_hash = TapBranchHash::from_inner(leaf_hash.into_inner()); | ||
// Verify the proof | ||
for elem in self.merkle_branch.as_inner() { | ||
let mut eng = TapBranchHash::engine(); | ||
if curr_hash.as_inner() < elem.as_inner() { | ||
eng.input(&curr_hash); | ||
eng.input(elem); | ||
} else { | ||
eng.input(elem); | ||
eng.input(&curr_hash); | ||
} | ||
// Recalculate the curr hash as parent hash | ||
curr_hash = TapBranchHash::from_engine(eng); | ||
curr_hash = TapBranchHash::from_node_hashes( | ||
sha256::Hash::from_inner(curr_hash.into_inner()), | ||
*elem | ||
); | ||
} | ||
// compute the taptweak | ||
let tweak = TapTweakHash::from_key_and_tweak(self.internal_key, Some(curr_hash)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Referring to the previous discussion on whether this function should be public: https://github.com/rust-bitcoin/rust-bitcoin/pull/922/files#r838943645
I am using this function downstream in OP_RETURN taproot commitment code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I like having it be public. It seems like a generally useful thing to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this parameter be
TapBranchHash
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that for leaves it is
TapLeafHash
and for nodes, it isTapBranchHash
. Maybe not worth having it hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are three hash types involved in taproot trees:
TapBranchHash
,TapLeafHash
andsha256::Hash
for hidden branches/leaves or for parameters generic over branch/leaves. To solve the abiguity we have the following optionspub type TapNodeHash = sha256::Hash
and use it in APIimpl Into<TapNodeHash>
in arguments which are generic over specific hash typeFrom<TapBranch|LeafHash> for TapNodeHash
convertors.But even if we decide to do so, this should be a scope of separate (quite large) PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be nit-picky here. I would like this to be
left
andright
instead ofa
andb
.