What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: BitcoinToLocal.spends_iff
Statement. Let f be a script flag record (booleans f.cltv,f.csv,f.nulldummy), o an oracle (a record of five arbitrary hash functions on byte strings, an arbitrary o.checkSig:bytes×bytes→OptionBool taking the signature first and the key second, and arbitrary o.checkLockTime,o.checkSequence:Z→Bool; no properties assumed), R (rev) and D (dpk) byte strings, δ∈Z (delay), and st an arbitrary list of byte strings (an initial stack, top-first). Assume
∣R∣≤520,∣D∣≤520,∣δ∣<232
(520 is MAX_SCRIPT_ELEMENT_SIZE; ∣δ∣ the absolute value as a natural). Then
(∃res. evalScript(f,o,st,toLocalScript(R,δ,D))=some res ∧ ∣res∣=1 ∧ topTrue(res)=true)⟺(∃b sig. st=[b,sig] ∧ SpendCondition(f,o,R,δ,D,b,sig)).
On the left, res ranges over lists of byte strings; topTrue(res) is false for the empty list and castToBool of the first (top) element otherwise, so with ∣res∣=1 the left side says: evaluation succeeds with a one-element stack [x] and castToBool(x)=true. On the right, b and sig range over byte strings; st=[b,sig] means the initial stack has exactly two elements, b on top and sig beneath. No hypothesis constrains st (its length or element sizes) — the two-element shape is part of the conclusion, not an assumption.
Unfolded notions.
- castToBool(x): false for x=[]; for [y], true iff y=0 and y=0x80; for y::rest with rest=[], true iff y=0 or castToBool(rest).
- CsvPasses(f,o,δ) is f.csv=false∨(0≤δ∧((toNat(δ)&231)=0∨o.checkSequence(δ)=true)), where toNat truncates negatives to 0 and & is bitwise AND on naturals.
- SpendCondition(f,o,R,δ,D,b,sig) is
(castToBool(b)=true∧o.checkSig(sig,R)=sometrue) ∨ (castToBool(b)=false∧CsvPasses(f,o,δ)∧o.checkSig(sig,D)=sometrue);
in each disjunct the oracle's verdict must be exactly sometrue (none and somefalse both fail).
- toLocalScript(R,δ,D) is the nine-instruction list [op0x63, pushR, op0x67, numInstr(δ), op0xb2, op0x75, pushD, op0x68, op0xac], where numInstr(δ) is the opcode with byte δ+80 when δ=−1 or 1≤δ≤16 (bytes 0x4f, 0x51–0x60) and otherwise the data push of serialize(δ) (serialize(0)=[]; for δ=0 the little-endian magnitude bytes of ∣δ∣ without trailing zeros, plus an appended 0x80/0x00 byte when the top bit of the last byte is set, else the top bit set on the last byte when δ<0).
The interpreter evalScript. evalScript(f,o,S,P) starts from the state (main stack S, alt-stack [], condition stack vfExec=[], opCount=0), applies one step per instruction of P in order, and returns some of the final main stack if every step succeeds and vfExec is empty at the end; otherwise none. A step is:
- Push of data x. Fail if ∣x∣>520. Otherwise, if every entry of vfExec is true, put x on top of the main stack (else leave the state unchanged). Then fail if ∣stack∣+∣alt-stack∣>1000.
- Opcode c. Let e be "every entry of vfExec is true". If c>0x60: fail if opCount+1>201, else increment opCount. Fail if c is one of the fifteen disabled opcodes (none occur here). If e holds or 0x63≤c≤0x68, apply execOp with flag e and then fail if ∣stack∣+∣alt-stack∣>1000; otherwise leave the state unchanged apart from that size check.
execOp for the opcodes of this script:
- 0x4f, 0x51–0x60 (opcode form of numInstr(δ)): push serialize(c−80).
- 0x63 (
OP_IF): if e, fail on an empty main stack, else pop the top x and push castToBool(x) onto vfExec; if not e, push false onto vfExec without popping.
- 0x67 (
OP_ELSE): fail if vfExec is empty, else negate its head.
- 0x68 (
OP_ENDIF): fail if vfExec is empty, else remove its head.
- 0xb2 (
OP_CHECKSEQUENCEVERIFY): if f.csv=false, unchanged. Else fail on an empty main stack; let x be the top (not removed); fail if ∣x∣>5; let n be the signed little-endian value of x (top bit of last byte is the sign; empty is 0); fail if n<0; if (toNat(n)&231)=0, unchanged; else fail if o.checkSequence(n)=false; else unchanged.
- 0x75 (
OP_DROP): fail on an empty main stack, else remove the top.
- 0xac (
OP_CHECKSIG): the main stack must be pk::sg::rest (pk on top), else fail; then o.checkSig(sg,pk): none fails, someok yields the stack boolBytes(ok)::rest with boolBytes(true)=[0x01], boolBytes(false)=[].
The theorem is a two-sided equivalence: a successful run of this script from st ending with exactly one element that casts to true happens precisely when st is a two-element stack [b,sig] satisfying SpendCondition.
Confirmed by the moderator at approval.