Merge bitcoin/bitcoin#22748: refactor: Avoid temporary vectors/uint256s in VerifyTaprootCommitment

2f0190320ddf45ff35f07950e8f01e8f15538043 Avoid temporary vectors/uint256s in VerifyTaprootCommitment (Pieter Wuille)

Pull request description:

  As XOnlyPubKey has a Span-based constructor, that can be used directly without needing to first convert the byte sequence into a vector, only to convert that to a uint256, which only then can then be passed as a span to the constructor.

  Reported by @ roconnor-blockstream.

ACKs for top commit:
  Zero-1729:
    crACK 2f0190320ddf45ff35f07950e8f01e8f15538043
  theStack:
    re-ACK 2f0190320ddf45ff35f07950e8f01e8f15538043
  jonatack:
    ACK 2f0190320ddf45ff35f07950e8f01e8f15538043

Tree-SHA512: f5e809d693cf6f6e899278cd706548eb4341e73b3f7ca8926b5fb50afb2098077d691579aea84fd7db2a7edd76be8e400aa2ed886091ee3416651b8a36efba37
This commit is contained in:
fanquake 2021-08-23 11:13:49 +08:00
commit a93e7a4422
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

View File

@ -1874,9 +1874,9 @@ static bool VerifyTaprootCommitment(const std::vector<unsigned char>& control, c
assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE);
assert(program.size() >= uint256::size());
//! The internal 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{Span<const unsigned char>{control.data() + 1, control.data() + TAPROOT_CONTROL_BASE_SIZE}};
//! The output pubkey (taken from the scriptPubKey).
const XOnlyPubKey q{uint256(program)};
const XOnlyPubKey q{program};
// Compute the Merkle root from the leaf and the provided path.
const uint256 merkle_root = ComputeTaprootMerkleRoot(control, tapleaf_hash);
// Verify that the output pubkey matches the tweaked internal pubkey, after correcting for parity.