What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: BitcoinScriptV2.legacyOracle_eq
Statement. For all signature flags g (four arbitrary booleans dersig,lowS,strictenc,witnessPubkeyType), every transaction tx, input index nin∈N, amount a∈Z, signature version v (base or witnessV0) and byte string sc, the two published oracles are equal:
legacyOracle(g,tx,nin,a,v,sc)=withCodeSig(legacyOracleV2(g,tx,nin,a,v), v, sc).
This is equality of the whole eight-component structures, with function components equal as functions. Unfolded, the components are: the same five hash functions and lock-time/sequence checks on both sides, and signature checks which for every (sig,pk) are, on the left, "none if the signature- or public-key-encoding check fails, else some of the ECDSA check with script code sc′", where sc′=FD(sc,pushBytes(sig)) for base and sc for witnessV0; on the right, legacyOracleV2's check applied to the script code fadSig(v,sc,sig), which is the same encoding tests followed by the same ECDSA check on that script code. No hypotheses.
Unfolded definitions used above
Basic objects and notation
- A byte is an 8-bit word, identified with its value in {0,…,255} and written in hexadecimal (e.g. 0xab). B∗ is the set of finite byte strings (lists of bytes); ∣x∣ is the length, x++y concatenation, ε the empty string.
- takek(x) is the first min(k,∣x∣) entries of a list x; dropk(x) is what is left after removing the first k entries, which is the empty list when k≥∣x∣.
- For a 0-based index p, the statement "x[p]=c" means: p<∣x∣ and the entry of x at index p is c. Indexing out of range yields no value, so such an equation can only hold with p<∣x∣.
- A partial result is either none or some(y). Applying a function g "under some" (mapg) sends none↦none and some(y)↦some(g(y)).
- An instruction is either push(d), carrying a data string d∈B∗, or op(c), carrying an opcode byte c. A script (parsed) is a finite list of instructions.
- Opcode bytes named below: OP_16=0x60, OP_IF=0x63, OP_ENDIF=0x68, OP_CODESEPARATOR=0xab, OP_CHECKSIG=0xac, OP_CHECKSIGVERIFY=0xad, OP_CHECKMULTISIG=0xae, OP_CHECKMULTISIGVERIFY=0xaf.
- A signature version v is one of exactly two values: base (legacy) or witnessV0.
Reading one instruction from bytes: getOp
getOp(x) returns either none or some(i,y) with i an instruction and y the remaining bytes:
- x=ε: none.
- Otherwise let b be the first byte and y0 the rest.
- If b≤0x4e (a push opcode), a data length n and a position are determined: if b<0x4c, n=b and the data starts right after b; if b=0x4c, n is the next byte (none if there is none); if b=0x4d, n is the little-endian value of the next 2 bytes (none if fewer than 2 remain); if b=0x4e, n is the little-endian value of the next 4 bytes (none if fewer than 4 remain). Then, if fewer than n bytes remain, none; otherwise some(push(next n bytes), the bytes after them). (So b=0x00 gives push(ε).)
- If b>0x4e: some(op(b),y0).
On success the remainder is a suffix of x that is at least one byte shorter than x. getOp never returns op(c) with c≤0x4e.
Signature-push deletion: pushBytes, findAndDelete, fadSig, fadAll
- pushBytes(d), the canonical push of d: if ∣d∣<0x4c it is the byte ∣d∣ followed by d; else if ∣d∣≤255 it is 0x4c, the byte ∣d∣, then d; else if ∣d∣≤65535 it is 0x4d, ∣d∣ as 2 little-endian bytes, then d; otherwise 0x4e, ∣d∣mod232 as 4 little-endian bytes, then d. It is never empty (pushBytes(ε)=[0x00]).
- findAndDelete(x,b) returns a pair (resulting string, count); only the first component (written FD(x,b)) is used here. If b=ε the result is (x,0). Otherwise x is scanned from the start: at the current instruction boundary, copies of b are removed as long as the remaining string begins with b (each removal counted); then one instruction is read with getOp: if that fails, the whole remaining string is kept verbatim and the scan stops; if it succeeds, that instruction's bytes are kept and the scan continues right after it. The result is the concatenation of the kept bytes. (The recursion carries fuel ∣x∣+1, and when fuel is exhausted the remaining bytes are kept verbatim; every iteration of the scan consumes at least one byte.)
- fadSig(v,sc,sig):
fadSig(base,sc,sig)=FD(sc,pushBytes(sig)),fadSig(witnessV0,sc,sig)=sc.
- fadAll(v,sc,[σ1,…,σm]) is the left fold of fadSig(v,⋅,⋅) over the list, in list order:
fadAll(v,sc,[σ1,…,σm])=fadSig(v,⋯fadSig(v,fadSig(v,sc,σ1),σ2)⋯,σm),
and fadAll(v,sc,[])=sc.
Oracles
- A published oracle O consists of five functions B∗→B∗ (RIPEMD-160, SHA-1, SHA-256, HASH160, HASH256), a function O.checkSig(sig,pk)∈{none,some(true),some(false)}, and two predicates checkLockTime,checkSequence:Z→{true,false}.
- A V2 oracle o has the same five hash functions and two predicates, but its signature check takes a script code as an extra first argument: o.checkSig(sc,sig,pk)∈{none,some(true),some(false)}. All components are arbitrary functions.
- toOracle(o): the published oracle with o's hashes and predicates and the signature check that always returns none.
- withCode(o,sc): toOracle(o) with signature check (sig,pk)↦o.checkSig(sc,sig,pk).
- withCodeSig(o,v,sc): toOracle(o) with signature check (sig,pk)↦o.checkSig(fadSig(v,sc,sig),sig,pk).
Concrete oracles
- Signature flags g=(g.dersig,g.lowS,g.strictenc,g.witnessPubkeyType), four booleans. SigEncg(sig) (signature-encoding check): true if sig=ε; false if one of dersig,lowS,strictenc is set and sig is not strict DER plus a hash-type byte; false if lowS is set and the signature does not have low S; false if strictenc is set and the hash type is undefined; true otherwise. PkEncg,v(pk) (public-key encoding check): false if strictenc is set and pk is not a 33-byte compressed (02/03) or 65-byte uncompressed (04) key; false if witnessPubkeyType is set, v=witnessV0 and pk is not compressed; true otherwise.
- ECDSA(tx,nin,a,v,sig,pk,sc) (the transaction checker's ECDSA check, with tx a transaction, nin∈N an input index, a∈Z an amount): false if pk's length does not match its header byte or sig=ε; otherwise the last byte of sig is the hash type, the signature hash of tx for input nin with script code sc is computed (legacy algorithm for base, BIP143 algorithm with amount a for witnessV0), the result is false if v=witnessV0 and a<0, and otherwise it is the secp256k1 ECDSA verification of sig without its last byte against pk and that hash.
- legacyOracleV2(g,tx,nin,a,v) is the V2 oracle with Bitcoin Core's RIPEMD-160, SHA-1, SHA-256, HASH160, HASH256, the lock-time and sequence checks of tx at input nin, and
checkSig(sc,sig,pk)=noneif ¬SigEncg(sig) or ¬PkEncg,v(pk),
and otherwise
checkSig(sc,sig,pk)=some(ECDSA(tx,nin,a,v,sig,pk,sc)).
The script code sc is used as given (no deletion inside the oracle).
- legacyOracle(g,tx,nin,a,v,sc) is the published oracle with the same hashes and lock-time/sequence checks and signature check checkSigPreTapscript: for (sig,pk), let sc′=FD(sc,pushBytes(sig)) if v=base and sc′=sc if v=witnessV0; the result is none if ¬SigEncg(sig) or ¬PkEncg,v(pk), and some(ECDSA(tx,nin,a,v,sig,pk,sc′)) otherwise.
Confirmed by the moderator at approval.