[N.B.: This PR does not change the consensus. It only adds assert
statements according to the current consensus in consensus-sensitive code (interpreter.cpp
). So that’s why the bot added the “consensus” tag and I prefixed the PR title with “consensus”.]
BIP 341 specifies constraints on the size of the control block c used to compute the taproot merkle root.
The last stack element is called the control block c, and must have length 33 + 32m, for a value of m that is an integer between 0 and 128, inclusive. Fail if it does not have such a length.
The actual merkle root is computed in ComputeTaprootMerkleRoot
(interpreter.cpp@1833) - this code does not check these constraints.
All the callers do check the constraints before calling ComputeTaprootMerkleRoot
. But in the future there may be more callers, and these checks may be inadvertently omitted at those future calls. Also, code at/near the current call sites may also change and skip these checks. Therefore this PR adds those checks as asserts
directly in ComputeTaprootMerkleRoot
to help prevent that error.
No unit tests provided: they’d have to be death tests as these are assert
statements which raise SIGABRT
and kill the program. Boost Test has a way to implement death tests (see the in-progress draft PR #25097 at this code (you may have to click to expand the diff) and could be added here if desired by reviewers.
Current callers of ComputeTaprootMerkleRoot
:
InferTaprootTree
(standard.cpp@1552)VerifyTaprootCommittment
(interpreter.cpp@1859) does a partial check, but it is called fromVerifyWitnessProgram
(interpreter.cpp@1922) where a full check is done