Skip to content

Commit 5464616

Browse files
committed
Merge #18388: Make VerifyWitnessProgram use a Span stack
2b0fcff Make VerifyWitnessProgram use a Span stack (Pieter Wuille) Pull request description: Here is a follow-up to #18002, again with the goal of simplifying (potential) BIP341 code. Instead of passing a begin and end iterator of the initial stack to `ExecuteWitnessScript`, they are turned into a `Span<const valtype>`, representing a span of `valtype`s in memory. This allows `VerifyWitnessProgram` to operate on that span directly, instead of juggling iterators around (which would be exacerbated by #17977 if trying to avoid copying the stack). ACKs for top commit: ajtowns: ACK 2b0fcff elichai: ReACK on the diff 2b0fcff instagibbs: re-ACK bitcoin/bitcoin@2b0fcff theStack: re-ACK bitcoin/bitcoin@2b0fcff Empact: ACK bitcoin/bitcoin@2b0fcff jnewbery: utACK 2b0fcff Tree-SHA512: 38eb4ce17f1947674c1c274caa40feb6ea8266bd96134d9cf1bc41e6fbf1114d4dde6c7a9e26e1ca8f3d0155429ef0911cc8ec0c1037d8fe7d6ec7f9e7184e93
2 parents e3154aa + 2b0fcff commit 5464616

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/script/interpreter.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,9 +1478,9 @@ bool GenericTransactionSignatureChecker<T>::CheckSequence(const CScriptNum& nSeq
14781478
template class GenericTransactionSignatureChecker<CTransaction>;
14791479
template class GenericTransactionSignatureChecker<CMutableTransaction>;
14801480

1481-
static bool ExecuteWitnessScript(std::vector<valtype>::const_iterator begin, std::vector<valtype>::const_iterator end, const CScript& scriptPubKey, unsigned int flags, SigVersion sigversion, const BaseSignatureChecker& checker, ScriptError* serror)
1481+
static bool ExecuteWitnessScript(const Span<const valtype>& stack_span, const CScript& scriptPubKey, unsigned int flags, SigVersion sigversion, const BaseSignatureChecker& checker, ScriptError* serror)
14821482
{
1483-
std::vector<valtype> stack{begin, end};
1483+
std::vector<valtype> stack{stack_span.begin(), stack_span.end()};
14841484

14851485
// Disallow stack item size > MAX_SCRIPT_ELEMENT_SIZE in witness stack
14861486
for (const valtype& elem : stack) {
@@ -1499,27 +1499,29 @@ static bool ExecuteWitnessScript(std::vector<valtype>::const_iterator begin, std
14991499
static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion, const std::vector<unsigned char>& program, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror)
15001500
{
15011501
CScript scriptPubKey;
1502+
Span<const valtype> stack = MakeSpan(witness.stack);
15021503

15031504
if (witversion == 0) {
15041505
if (program.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
15051506
// Version 0 segregated witness program: SHA256(CScript) inside the program, CScript + inputs in witness
1506-
if (witness.stack.size() == 0) {
1507+
if (stack.size() == 0) {
15071508
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY);
15081509
}
1509-
scriptPubKey = CScript(witness.stack.back().begin(), witness.stack.back().end());
1510+
const valtype& script_bytes = SpanPopBack(stack);
1511+
scriptPubKey = CScript(script_bytes.begin(), script_bytes.end());
15101512
uint256 hashScriptPubKey;
15111513
CSHA256().Write(&scriptPubKey[0], scriptPubKey.size()).Finalize(hashScriptPubKey.begin());
15121514
if (memcmp(hashScriptPubKey.begin(), program.data(), 32)) {
15131515
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH);
15141516
}
1515-
return ExecuteWitnessScript(witness.stack.begin(), witness.stack.end() - 1, scriptPubKey, flags, SigVersion::WITNESS_V0, checker, serror);
1517+
return ExecuteWitnessScript(stack, scriptPubKey, flags, SigVersion::WITNESS_V0, checker, serror);
15161518
} else if (program.size() == WITNESS_V0_KEYHASH_SIZE) {
15171519
// Special case for pay-to-pubkeyhash; signature + pubkey in witness
1518-
if (witness.stack.size() != 2) {
1520+
if (stack.size() != 2) {
15191521
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH); // 2 items in witness
15201522
}
15211523
scriptPubKey << OP_DUP << OP_HASH160 << program << OP_EQUALVERIFY << OP_CHECKSIG;
1522-
return ExecuteWitnessScript(witness.stack.begin(), witness.stack.end(), scriptPubKey, flags, SigVersion::WITNESS_V0, checker, serror);
1524+
return ExecuteWitnessScript(stack, scriptPubKey, flags, SigVersion::WITNESS_V0, checker, serror);
15231525
} else {
15241526
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH);
15251527
}

src/span.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <type_traits>
99
#include <cstddef>
1010
#include <algorithm>
11+
#include <assert.h>
1112

1213
/** A Span is an object that can refer to a contiguous sequence of objects.
1314
*
@@ -27,6 +28,8 @@ class Span
2728
constexpr C* data() const noexcept { return m_data; }
2829
constexpr C* begin() const noexcept { return m_data; }
2930
constexpr C* end() const noexcept { return m_data + m_size; }
31+
constexpr C& front() const noexcept { return m_data[0]; }
32+
constexpr C& back() const noexcept { return m_data[m_size - 1]; }
3033
constexpr std::ptrdiff_t size() const noexcept { return m_size; }
3134
constexpr C& operator[](std::ptrdiff_t pos) const noexcept { return m_data[pos]; }
3235

@@ -57,4 +60,15 @@ constexpr Span<A> MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
5760
template<typename V>
5861
constexpr Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type> MakeSpan(V& v) { return Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type>(v.data(), v.size()); }
5962

63+
/** Pop the last element off a span, and return a reference to that element. */
64+
template <typename T>
65+
T& SpanPopBack(Span<T>& span)
66+
{
67+
size_t size = span.size();
68+
assert(size > 0);
69+
T& back = span[size - 1];
70+
span = Span<T>(span.data(), size - 1);
71+
return back;
72+
}
73+
6074
#endif

0 commit comments

Comments
 (0)