What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: BitcoinHtlc.parseScript_offered
Statement. Let r,Kr,Kl,h be any four byte strings with ∣r∣≤520, ∣Kr∣≤520, ∣Kl∣≤520, ∣h∣≤520. The theorem asserts
parse(OffB(r,Kr,Kl,h))=some(Off(r,Kr,Kl,h)),
i.e. the byte-level parser applied to the serialized offered-HTLC byte string returns (without failure) exactly the 26-instruction list Off(r,Kr,Kl,h); both sides are unfolded below. Equality is equality of optional instruction lists, where an instruction is either a data push carrying a byte string or a non-push opcode byte.
Definitions unfolded
Basic notions used throughout. A byte is an 8-bit word and a byte string (written Bytes) is a finite list of bytes; ∣b∣ denotes the length of a byte string b. The constant Lmax=520 is the interpreter's maximum size of one stack element. A flag set f consists of three Booleans f.cltv, f.csv, f.nulldummy. An oracle o is an arbitrary structure supplying eight functions with no constraints whatsoever on their behaviour: o.ripemd160,o.sha1,o.sha256,o.hash160,o.hash256:Bytes→Bytes (so in particular o.hash160 is any function from byte strings to byte strings, not necessarily RIPEMD160∘SHA256, not necessarily 20-byte-valued), o.checkSig:Bytes→Bytes→OptionBool (first argument the signature, second the public key; the value none represents a script-aborting failure, someb a verdict b), o.checkLockTime:Z→Bool and o.checkSequence:Z→Bool. Stacks are lists written top element first. castToBool(b) is: false for the empty string; for a one-byte string [x], true iff x=0 and x=0x80; for x::r with r non-empty, true iff x=0 or castToBool(r) (so a string of zero bytes whose last byte is 0x80 is false). boolBytes(true)=[0x01] and boolBytes(false)=[] (the empty string). The script-number encoding ser(v) of an integer v is: [] if v=0; otherwise the little-endian bytes of ∣v∣ with no trailing zero byte, then, if the last of those bytes has its top bit (0x80) set, an extra byte 0x80 (for v<0) or 0x00 (for v>0) is appended, and otherwise, for v<0, the top bit of the last byte is set. For example ser(2)=[0x02] and ser(32)=[0x20]. The decoding numk(b) ("script number of at most k bytes, non-minimal encodings allowed") is none if ∣b∣>k, and otherwise some of the little-endian value of b with the top bit of the last byte read as a sign (the empty string decodes to 0).
The number push numInstr(n) / numBytes(n). For an integer n, numInstr(n) is the single opcode with byte value n+0x50 when n=−1 or 1≤n≤16 (so numInstr(−1) is the opcode 0x4f, numInstr(2) is the opcode 0x52, i.e. OP_2), and otherwise it is the data push of ser(n) (so numInstr(32) is the push of the one-byte string [0x20], and numInstr(0) is the push of the empty string). numBytes(n) is the byte-level form: the one opcode byte n+0x50 in the first case, and pushBytes(ser(n)) otherwise, where pushBytes(d) is the canonical push encoding of a byte string d: [∣d∣]++d if ∣d∣<0x4c; [0x4c,∣d∣]++d if ∣d∣≤0xff; [0x4d]++(2 little-endian bytes of ∣d∣)++d if ∣d∣≤0xffff; and [0x4e]++(4 little-endian bytes of ∣d∣)++d otherwise.
The two witness scripts as byte strings. OffB(r,Kr,Kl,h) is the byte string
[0x76,0xa9]++pushBytes(r)++[0x87,0x63,0xac,0x67]++pushBytes(Kr)++[0x7c,0x82]++numBytes(32)++[0x87,0x64,0x75]++numBytes(2)++[0x7c]++pushBytes(Kl)++numBytes(2)++[0xae,0x67,0xa9]++pushBytes(h)++[0x88,0xac,0x68,0x68]
(the opcode bytes are OP_DUP OP_HASH160 … OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE … OP_SWAP OP_SIZE … OP_EQUAL OP_NOTIF OP_DROP … OP_SWAP … OP_CHECKMULTISIG OP_ELSE OP_HASH160 … OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF; numBytes(32)=[0x01,0x20] and numBytes(2)=[0x52]). RecB(r,Kr,Kl,h,e) is the byte string
[0x76,0xa9]++pushBytes(r)++[0x87,0x63,0xac,0x67]++pushBytes(Kr)++[0x7c,0x82]++numBytes(32)++[0x87,0x63,0xa9]++pushBytes(h)++[0x88]++numBytes(2)++[0x7c]++pushBytes(Kl)++numBytes(2)++[0xae,0x67,0x75]++numBytes(e)++[0xb1,0x75,0xac,0x68,0x68]
(… OP_EQUAL OP_IF OP_HASH160 … OP_EQUALVERIFY … OP_SWAP … OP_CHECKMULTISIG OP_ELSE OP_DROP <e> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF OP_ENDIF), where numBytes(e) is the single byte e+0x50 if e=−1 or 1≤e≤16 and pushBytes(ser(e)) otherwise.
The two witness scripts as instruction lists. Writing ⟨d⟩ for the data push of the byte string d, the offered script Off(r,Kr,Kl,h), for byte strings r (the "revocation hash"), Kr (the "remote key"), Kl (the "local key") and h (the "payment hash"), all four being completely unconstrained parameters (any byte strings, not necessarily 20 bytes, not necessarily hashes of anything), is the 26-instruction list
OP_DUP OP_HASH160 ⟨r⟩ OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE ⟨Kr⟩ OP_SWAP OP_SIZE ⟨[0x20]⟩ OP_EQUALOP_NOTIF OP_DROP OP_2 OP_SWAP ⟨Kl⟩ OP_2 OP_CHECKMULTISIG OP_ELSE OP_HASH160 ⟨h⟩ OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF,
where ⟨[0x20]⟩ is numInstr(32) and OP_2 is numInstr(2), the opcode byte 0x52. The received script Rec(r,Kr,Kl,h,e) additionally takes an integer e (the "expiry", any integer, possibly negative or zero) and is the 28-instruction list
OP_DUP OP_HASH160 ⟨r⟩ OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE ⟨Kr⟩ OP_SWAP OP_SIZE ⟨[0x20]⟩ OP_EQUALOP_IF OP_HASH160 ⟨h⟩ OP_EQUALVERIFY OP_2 OP_SWAP ⟨Kl⟩ OP_2 OP_CHECKMULTISIGOP_ELSE OP_DROP numInstr(e) OP_CHECKLOCKTIMEVERIFY OP_DROP OP_CHECKSIG OP_ENDIF OP_ENDIF,
where numInstr(e) is the opcode e+0x50 if e=−1 or 1≤e≤16, and the data push of ser(e) otherwise (the empty push when e=0; a push carrying a sign bit when e<−1).
Byte-level parsing parse(s). parse reads a byte string instruction by instruction with a fuel of ∣s∣ steps and yields none on any malformed push and some of the instruction list otherwise. One instruction is read from a first byte b as follows: if b≤0x4e it is a data push, whose length is b itself when b<0x4c, the next one byte when b=0x4c, the next two bytes little-endian when b=0x4d, and the next four bytes little-endian when b=0x4e (failing if those length bytes are missing), and the push fails if fewer bytes than the announced length remain; otherwise (b>0x4e) it is the non-push opcode b. The empty string parses to the empty instruction list.
Confirmed by the moderator at approval.