2 suggestions
Optional improvements
[suggestion] · Long negative zero is missing from word-scan coverage
The current implementation preserves Script truth values by inspection, but the short false-value IFDUP fixtures and unchecked fuzz call do not protect long negative zero through the new word loop.
Location: src/script/interpreter.cpp:51 (head).
Details and suggested fix
script_tests.json:21 already checks eight zero bytes followed by 0x10, providing word-to-tail coverage. That case would still pass if the nonzero-word branch returned true instead of resuming the scalar scan, while seven zero bytes followed by 0x80 would then be misclassified. The one- and two-byte false-value IFDUP fixtures bypass the word loop, and script_interpreter discards CastToBool's result. Add a test-only copy of the original bytewise rule and assert equality in the fuzz target, together with fixed cases at lengths 7/8/9, 15/16/17 and 519/520. Check all-zero and zero-padded terminal-0x80 inputs as false, and earlier-nonzero/terminal-0x80 and word-boundary nonzero inputs as true.
[suggestion] · The reported IFDUP/VERIFY workload is absent from the benchmark suite
The checked-in suite lacks VerifyIfDupVerifyFullLast, so it cannot directly rerun the workload behind the reported 3.1x speedup or track that workload across changes.
Location: src/script/interpreter.cpp:51 (head).
Details and suggested fix
The existing standard-script benchmarks do not repeat long scans, and VerifyNestedIfScript only tests one-byte OP_1 operands. Check in the reported harness next to it, using the 520-byte element of 519 zero bytes followed by 0x01 with repeated OP_IFDUP OP_VERIFY pairs. Each pair leaves the original element on the stack, allowing repeated scans without stack growth. Select SigVersion::TAPSCRIPT, since flags cannot lift the script-size and 201-opcode limits for BASE or WITNESS_V0. Clear the stack in setup() and assert successful execution so samples cannot silently measure early rejection or accumulated state.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
Script's negative zero follows its explicit signed byte-vector representation, rather than the host language's integer representation. The sign bit belongs in the final byte because Script numbers use little-endian encoding.
Both pinned versions of CastToBool treat an empty vector, all-zero bytes, and zero bytes ending in 0x80 as false. BIP62 documents the signed encoding; issue #1638 specifically resolved a question about why CastToBool checks the final byte. This explains the representation behind the special case retained alongside the head's word-sized scan.
Negative-zero handling is an established consensus constraint: changing its boolean interpretation can change which outputs are spendable.
In the historical discussion of PR #18236, Pieter Wuille explained that changing CastToBool would make the script 0x0180 spendable where it previously was not, constituting a hard fork. The affected helper is used not only by IFDUP and VERIFY but also by final script-success checks, so its truth table has protocol significance beyond those individual opcodes.
Minimal IF arguments were introduced to remove malleability, with compatibility determining where the restriction could safely apply.
PR #8526 proposed accepting only the empty vector or 0x01 for P2WSH IF/NOTIF arguments because a relay node could otherwise substitute arbitrary data. It explicitly avoided applying the restriction to existing non-segwit scripts because incompatible P2SH outputs might already exist. BIP342 later states that requiring MINIMALIF eliminates a potential malleability vector. The pinned code reflects this separation: witness-v0 IF minimality is flag-controlled policy, tapscript IF minimality is mandatory, and IFDUP/VERIFY still use general CastToBool semantics.
Source 1 · Source 2 · Source 3
Historical interpreter optimization explicitly considered adversarial per-opcode cost and the interaction between implementation complexity and protocol limits.
PR #16902 investigated scripts maximizing verification work and replaced nesting-depth-dependent conditional bookkeeping with an O(1) representation. Its proposal explained that the existing 201-opcode limit bounded the immediate impact, while lifting that limit without changing the algorithm would permit unreasonable quadratic growth. Both pinned versions contain that optimized ConditionStack; they also apply the 201-opcode limit to legacy and witness-v0 scripts, but not tapscript. This is relevant precedent for examining repeated boolean conversions as validation work, rather than only ordinary wallet execution.
Ralph's inferences
General Script truth testing is deliberately distinct from parsing a minimally encoded arithmetic operand; numeric minimality cannot be assumed for every CastToBool input.
The pinned interpreter uses CScriptNum, with size and optional minimal-encoding checks, for arithmetic operations. IFDUP and VERIFY instead pass byte vectors directly to CastToBool, and witness completion also tests the final vector directly. Consequently, the affected behavior includes long byte strings and multiple encodings of false, even though tapscript IF arguments are narrowly constrained. The historical encoding and malleability sources support this distinction, but do not establish an original design intention for every caller.
Source 1 · Source 2 · Source 3
Bounded stack storage does not eliminate incentives to maximize repeated validation work per script byte.
In the pinned code, a true-valued element survives an IFDUP/VERIFY pair: IFDUP duplicates it and VERIFY consumes the duplicate. Repeating the pair therefore reuses the same data without growing the stack, while repeatedly invoking boolean conversion. The 520-byte element limit bounds each scan, but not the number of scans by itself; tapscript also lacks the legacy 201-opcode ceiling. This explains why the PR's long-zero-prefix workload is meaningful as adversarial resource context, without establishing its claimed speedup or assessing the patch.