Keep OP_0 OP_IF data envelopes byte for byte - #1
Merged
Merged
Conversation
added 2 commits
September 17, 2026 09:12
An OP_0 OP_IF ... OP_ENDIF block never runs, so the optimizer could rewrite its contents and still pass the equivalence proof. That is where inscription envelopes carry their data: two identical 60-byte fields became a push followed by OP_OVER, which indexers no longer read as the inscription. parse now keeps such a block (with no OP_ELSE of its own) as one verbatim op, like the tail after a top-level OP_RETURN. It is a barrier with no stack effect, so the code after it keeps the stack guarantee from before it. toAsm prints its real ASM, profile reports it as data, and push re-encoding skips it. Also adds a wider fuzz test: every modelled opcode including signatures, Chronicle opcodes and shifts, unusual push encodings, boundary numbers, OP_NOTIF, OP_RETURN and OP_0 OP_IF blocks inside branches, computed PICK/ROLL indices, and starting stacks that pass checks more often.
In an unexecuted branch OP_VERIF and OP_VERNOTIF open a conditional after Chronicle but do nothing between Genesis and Chronicle. Counting them as openers let an OP_ELSE or OP_ENDIF of the enclosing block be taken as theirs, so code that runs pre-Chronicle was kept as a dead block with no stack effect, and redundant-looking ops after it were removed: a script that failed with an empty stack came out succeeding, with the symbolic proof passing. Where such a block ends depends on the era, and envelopes do not use these opcodes, so it is left to the ordinary barrier handling.
codenlighten
pushed a commit
that referenced
this pull request
Sep 17, 2026
A constant ROLL or PICK index in the tens of thousands makes a fragment need that many inputs. When the operation moves the deepest of them, finish() arranges every one back into place, scanning the stack for each: quadratic. OP_DUP OP_DROP 0x0159e3 OP_ROLL OP_SWAP OP_DROP took 75 s to optimize, and the wide fuzz test in #1 hit a script like it in CI (410 s on Node 24). A schedule is only used when it is smaller than the fragment, so the machine now stops once it has emitted more than twice the fragment's size plus 16 bytes, and that variant is skipped. The generic arrangement of a short tail is exempt, because the search that may beat it runs afterwards. That script now takes 3.8 s with the same 8-byte result. The Miller loop and the final exponentiation come out byte for byte as before, as do the 19 module scripts checked, field-300.hex, sha256.block and g1.inSubgroup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An
OP_0 OP_IF … OP_ENDIFblock never runs, so rewriting what is inside it does not change what the script does, and the symbolic proof and differential tests pass. But that is where inscription envelopes (1Sat Ordinals, BSV-21 and similar) carry their data, and the optimizer was rewriting it:The second field became
OP_OVER, so an indexer no longer sees the inscription. A body likeOP_1 OP_1 OP_ADD OP_DROPwas also removed from inside an envelope entirely.Fix
parseturns anOP_0 OP_IF … OP_ENDIFblock into one verbatim op (DEAD), the same way the tail after a top-levelOP_RETURNis kept. It only does this when the block has noOP_ELSEof its own (that branch would run) and has a matchingOP_ENDIFwith no truncated pushes, and contains noOP_VERIF/OP_VERNOTIF. A nestedIF/ELSEinside the block is fine.1 IF 0 IF VERIF ELSE DROP ENDIF ENDIFbe taken as one dead block, although itsDROPruns before Chronicle:OP_DUP OP_DROPafter it was removed, and a script that failed on a one-item stack came out succeeding, with the symbolic proof passing (ed56b3d).analysis, the block is a barrier with stack effect[g, ga]: it pushesOP_0and pops it. Code after the envelope keeps the stack guarantee it had before, so the optimizer still removes redundant ops there.OP_1 OP_DUP OP_DROP <envelope> OP_DUP OP_DROPstill minimizes toOP_1 <envelope>.toAsmprints the block's real ASM, so--asmoutput round-trips.profilereports it asdata (OP_0 OP_IF block), and push re-encoding skips it.Tests
OP_ELSE, nestedELSEand unterminated cases are handled.wide opcode fuzz with stacks that pass checks), 150 scripts. It covers every modelled opcode includingCHECKSIG,CODESEPARATOR, Chronicle opcodes and shifts (with small size operands); non-minimal pushes, negative zero and 4/5-byte number boundaries;OP_NOTIF;OP_RETURNandOP_0 OP_IFblocks inside branches; andPICK/ROLLwith a computed index. Starting stacks are biased toward passing checks.OP_VERIFcase: it fails without ed56b3d.npm test: 25/25 pass.OP_0 OP_IFblocks containingOP_VERIF/OP_VERNOTIFinside other conditionals, checked on the interpreter under both Chronicle and Genesis-only flags: 2 mismatches in 6,000 checks before ed56b3d, 0 after.OP_0 OP_IFblocks) found no mismatches: 1,600 scripts onmainand 200 on this branch, about 430k interpreter runs in total under consensus and policy flags (MINIMALDATA, MINIMALIF, NULLFAIL), about 78k of them successful.Not in this PR
toAsmstill prints the tail after a top-levelOP_RETURNas<tail N bytes>, so--asm -ooutput loses that data. Hex and binary output are fine.ROLLindex of 88,547, which made the scheduler quadratic. That is fixed in Abandon schedules far larger than the fragment they would replace #2 and applies tomainas well.