Skip to content

Keep OP_0 OP_IF data envelopes byte for byte - #1

Merged
codenlighten merged 2 commits into
mainfrom
fix/preserve-dead-branch-data
Sep 17, 2026
Merged

codenlighten merged 2 commits into
mainfrom
fix/preserve-dead-branch-data

Conversation

@codenlighten

@codenlighten codenlighten commented Sep 17, 2026

Copy link
Copy Markdown
Owner

Problem

An OP_0 OP_IF … OP_ENDIF block 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:

before: OP_0 OP_IF 6f7264 OP_1 746578742f706c61696e OP_0 <60B> OP_0 <60B> OP_ENDIF OP_DUP OP_HASH160 … OP_CHECKSIG
after:  0    OP_IF 6f7264 OP_1 746578742f706c61696e 0    <60B> 0    OP_OVER OP_ENDIF OP_DUP OP_HASH160 … OP_CHECKSIG

The second field became OP_OVER, so an indexer no longer sees the inscription. A body like OP_1 OP_1 OP_ADD OP_DROP was also removed from inside an envelope entirely.

Fix

  • parse turns an OP_0 OP_IF … OP_ENDIF block into one verbatim op (DEAD), the same way the tail after a top-level OP_RETURN is kept. It only does this when the block has no OP_ELSE of its own (that branch would run) and has a matching OP_ENDIF with no truncated pushes, and contains no OP_VERIF/OP_VERNOTIF. A nested IF/ELSE inside the block is fine.
  • Those two opcodes are excluded because, in a branch that does not run, they open a conditional after Chronicle but do nothing between Genesis and Chronicle, so the end of the block depends on the era. Counting them as openers let 1 IF 0 IF VERIF ELSE DROP ENDIF ENDIF be taken as one dead block, although its DROP runs before Chronicle: OP_DUP OP_DROP after it was removed, and a script that failed on a one-item stack came out succeeding, with the symbolic proof passing (ed56b3d).
  • In analysis, the block is a barrier with stack effect [g, ga]: it pushes OP_0 and 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_DROP still minimizes to OP_1 <envelope>.
  • toAsm prints the block's real ASM, so --asm output round-trips. profile reports it as data (OP_0 OP_IF block), and push re-encoding skips it.
  • The README section on barriers now mentions these blocks.

Tests

  • New unit test: the envelope comes out byte for byte, the code around it is still optimized, the block parses, encodes and prints as expected, and the OP_ELSE, nested ELSE and unterminated cases are handled.
  • New fuzz test (wide opcode fuzz with stacks that pass checks), 150 scripts. It covers every modelled opcode including CHECKSIG, CODESEPARATOR, Chronicle opcodes and shifts (with small size operands); non-minimal pushes, negative zero and 4/5-byte number boundaries; OP_NOTIF; OP_RETURN and OP_0 OP_IF blocks inside branches; and PICK/ROLL with a computed index. Starting stacks are biased toward passing checks.
  • Unit test for the OP_VERIF case: it fails without ed56b3d.
  • npm test: 25/25 pass.
  • A fuzz of OP_0 OP_IF blocks containing OP_VERIF/OP_VERNOTIF inside other conditionals, checked on the interpreter under both Chronicle and Genesis-only flags: 2 mismatches in 6,000 checks before ed56b3d, 0 after.
  • Separately, a longer run of a similar generator (without the OP_0 OP_IF blocks) found no mismatches: 1,600 scripts on main and 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

  • toAsm still prints the tail after a top-level OP_RETURN as <tail N bytes>, so --asm -o output loses that data. Hex and binary output are fine.
  • The Node 24 job took 7m21s on ed56b3d because the wide fuzz test generated a script with a constant ROLL index 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 to main as well.

codenlighten 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.
@codenlighten
codenlighten merged commit 6d4dabc into main Sep 17, 2026
5 checks passed
@codenlighten
codenlighten deleted the fix/preserve-dead-branch-data branch September 17, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant