mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
Add comments to VerifyTaprootCommitment
This commit is contained in:
parent
cdf900cbf2
commit
8dbb7de67c
1 changed files with 6 additions and 0 deletions
|
@ -1834,9 +1834,13 @@ static bool ExecuteWitnessScript(const Span<const valtype>& stack_span, const CS
|
||||||
static bool VerifyTaprootCommitment(const std::vector<unsigned char>& control, const std::vector<unsigned char>& program, const CScript& script, uint256& tapleaf_hash)
|
static bool VerifyTaprootCommitment(const std::vector<unsigned char>& control, const std::vector<unsigned char>& program, const CScript& script, uint256& tapleaf_hash)
|
||||||
{
|
{
|
||||||
const int path_len = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE;
|
const int path_len = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE;
|
||||||
|
//! The inner pubkey (x-only, so no Y coordinate parity).
|
||||||
const XOnlyPubKey p{uint256(std::vector<unsigned char>(control.begin() + 1, control.begin() + TAPROOT_CONTROL_BASE_SIZE))};
|
const XOnlyPubKey p{uint256(std::vector<unsigned char>(control.begin() + 1, control.begin() + TAPROOT_CONTROL_BASE_SIZE))};
|
||||||
|
//! The output pubkey (taken from the scriptPubKey).
|
||||||
const XOnlyPubKey q{uint256(program)};
|
const XOnlyPubKey q{uint256(program)};
|
||||||
|
// Compute the tapleaf hash.
|
||||||
tapleaf_hash = (CHashWriter(HASHER_TAPLEAF) << uint8_t(control[0] & TAPROOT_LEAF_MASK) << script).GetSHA256();
|
tapleaf_hash = (CHashWriter(HASHER_TAPLEAF) << uint8_t(control[0] & TAPROOT_LEAF_MASK) << script).GetSHA256();
|
||||||
|
// Compute the Merkle root from the leaf and the provided path.
|
||||||
uint256 k = tapleaf_hash;
|
uint256 k = tapleaf_hash;
|
||||||
for (int i = 0; i < path_len; ++i) {
|
for (int i = 0; i < path_len; ++i) {
|
||||||
CHashWriter ss_branch{HASHER_TAPBRANCH};
|
CHashWriter ss_branch{HASHER_TAPBRANCH};
|
||||||
|
@ -1848,7 +1852,9 @@ static bool VerifyTaprootCommitment(const std::vector<unsigned char>& control, c
|
||||||
}
|
}
|
||||||
k = ss_branch.GetSHA256();
|
k = ss_branch.GetSHA256();
|
||||||
}
|
}
|
||||||
|
// Compute the tweak from the Merkle root and the inner pubkey.
|
||||||
k = (CHashWriter(HASHER_TAPTWEAK) << MakeSpan(p) << k).GetSHA256();
|
k = (CHashWriter(HASHER_TAPTWEAK) << MakeSpan(p) << k).GetSHA256();
|
||||||
|
// Verify that the output pubkey matches the tweaked inner pubkey, after correcting for parity.
|
||||||
return q.CheckPayToContract(p, k, control[0] & 1);
|
return q.CheckPayToContract(p, k, control[0] & 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue