Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

OP_CHECKMULTISIG checks every signature against the code with all its signatures deleted (L10 closed)

Proved
BitcoinScriptV2.multisig_deletes_all

by pitimespi · Oct 29, 2026 · Mathlib 0df444a (Lean v4.33.1)

bitcoinbitcoin-mainnetcheckmultisigcodeseparatorconsensusfindanddeleteformal-verificationinterpreterscriptsighash

The oracle at an OP_CHECKMULTISIG(VERIFY) checks each sig against fadAll v scriptCode (multisigSigs stack): Core deletes all nSigsCount signature pushes before any check.

Preamble
import Definitions.Def_BitcoinScriptV2
import Definitions.Def_BitcoinSig
import Definitions.Def_BitcoinScript
open BitcoinScriptV2 BitcoinScript BitcoinSig BitcoinVerify
Formal statement
theorem BitcoinScriptV2.multisig_deletes_all (v : BitcoinSig.SigVersion) (o : BitcoinScriptV2.OracleV2)
    (s : BitcoinScriptV2.StateV2) (c : BitcoinScript.Byte)
    (hc : c = BitcoinScript.OP_CHECKMULTISIG ∨ c = BitcoinScript.OP_CHECKMULTISIGVERIFY) (sig pk : BitcoinScript.Bytes) :
    (BitcoinScriptV2.oracleAt v o s (.op c)).checkSig sig pk =
      o.checkSig (BitcoinScriptV2.fadAll v s.scriptCode (BitcoinScriptV2.multisigSigs s.st.stack)) sig pk := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: EvalScript (pbegincodehash, OP_CODESEPARATOR at interpreter.cpp:1058-1066, OP_CHECKSIG's scriptCode and FindAndDelete at :339-343 / the CHECKSIG case, OP_CHECKMULTISIG's deletion of every signature at :1152-1159), SignatureHash (SigVersion::BASE), VerifyScript (src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp); Core's script_tests.json, tx_valid.json, tx_invalid.json at the same revision
Read-back

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.multisig_deletes_all

Statement. For every signature version vvv (base\mathsf{base}base or witnessV0\mathsf{witnessV0}witnessV0), every V2 oracle ooo, every V2 state SSS, every byte ccc with

c=0xae (OP_CHECKMULTISIG)orc=0xaf (OP_CHECKMULTISIGVERIFY),c = \texttt{0xae}\ (\texttt{OP\_CHECKMULTISIG}) \quad\text{or}\quad c = \texttt{0xaf}\ (\texttt{OP\_CHECKMULTISIGVERIFY}),c=0xae (OP_CHECKMULTISIG)orc=0xaf (OP_CHECKMULTISIGVERIFY),

and all byte strings sig,pk\mathit{sig}, \mathit{pk}sig,pk:

oracleAt(v,o,S,op(c)).checkSig(sig,pk)=o.checkSig(fadAll(v,S.sc,multisigSigs(stack(S))), sig, pk).\mathrm{oracleAt}(v, o, S, \mathrm{op}(c)).\mathrm{checkSig}(\mathit{sig}, \mathit{pk}) = o.\mathrm{checkSig}\Big(\mathrm{fadAll}\big(v, S.\mathrm{sc}, \mathrm{multisigSigs}(\mathrm{stack}(S))\big),\ \mathit{sig},\ \mathit{pk}\Big).oracleAt(v,o,S,op(c)).checkSig(sig,pk)=o.checkSig(fadAll(v,S.sc,multisigSigs(stack(S))), sig, pk).

In words: for these two opcodes the signature check supplied by the V2 interpreter at state SSS checks every (sig,pk)(\mathit{sig}, \mathit{pk})(sig,pk) against one script code: S.scS.\mathrm{sc}S.sc with the canonical pushes of all strings in multisigSigs(stack(S))\mathrm{multisigSigs}(\mathrm{stack}(S))multisigSigs(stack(S)) deleted one after another in list order (for v=basev = \mathsf{base}v=base), or S.scS.\mathrm{sc}S.sc unchanged (for v=witnessV0v = \mathsf{witnessV0}v=witnessV0). That script code does not depend on sig\mathit{sig}sig or pk\mathit{pk}pk, and sig\mathit{sig}sig need not be among the deleted strings. The statement involves neither whether the opcode is executed nor whether the stack is well formed for a multisig (when it is not, multisigSigs\mathrm{multisigSigs}multisigSigs returns the value described below, possibly [ ][\,][]). Only the checkSig\mathrm{checkSig}checkSig component of the oracle is compared.


Unfolded definitions used above

Basic objects and notation

  • A byte is an 8-bit word, identified with its value in {0,…,255}\{0,\dots,255\}{0,…,255} and written in hexadecimal (e.g. 0xab\texttt{0xab}0xab). B∗\mathcal B^*B∗ is the set of finite byte strings (lists of bytes); ∣x∣|x|∣x∣ is the length, x+ ⁣ ⁣+yx \mathbin{+\!\!+} yx++y concatenation, ε\varepsilonε the empty string.
  • takek(x)\mathrm{take}_k(x)takek​(x) is the first min⁡(k,∣x∣)\min(k,|x|)min(k,∣x∣) entries of a list xxx; dropk(x)\mathrm{drop}_k(x)dropk​(x) is what is left after removing the first kkk entries, which is the empty list when k≥∣x∣k \ge |x|k≥∣x∣.
  • For a 0-based index ppp, the statement "x[p]=cx[p] = cx[p]=c" means: p<∣x∣p < |x|p<∣x∣ and the entry of xxx at index ppp is ccc. Indexing out of range yields no value, so such an equation can only hold with p<∣x∣p<|x|p<∣x∣.
  • A partial result is either none\mathsf{none}none or some(y)\mathsf{some}(y)some(y). Applying a function ggg "under some\mathsf{some}some" (mapg\mathrm{map}_gmapg​) sends none↦none\mathsf{none}\mapsto\mathsf{none}none↦none and some(y)↦some(g(y))\mathsf{some}(y)\mapsto\mathsf{some}(g(y))some(y)↦some(g(y)).
  • An instruction is either push(d)\mathrm{push}(d)push(d), carrying a data string d∈B∗d\in\mathcal B^*d∈B∗, or op(c)\mathrm{op}(c)op(c), carrying an opcode byte ccc. A script (parsed) is a finite list of instructions.
  • Opcode bytes named below: OP_16=0x60\texttt{OP\_16}=\texttt{0x60}OP_16=0x60, OP_IF=0x63\texttt{OP\_IF}=\texttt{0x63}OP_IF=0x63, OP_ENDIF=0x68\texttt{OP\_ENDIF}=\texttt{0x68}OP_ENDIF=0x68, OP_CODESEPARATOR=0xab\texttt{OP\_CODESEPARATOR}=\texttt{0xab}OP_CODESEPARATOR=0xab, OP_CHECKSIG=0xac\texttt{OP\_CHECKSIG}=\texttt{0xac}OP_CHECKSIG=0xac, OP_CHECKSIGVERIFY=0xad\texttt{OP\_CHECKSIGVERIFY}=\texttt{0xad}OP_CHECKSIGVERIFY=0xad, OP_CHECKMULTISIG=0xae\texttt{OP\_CHECKMULTISIG}=\texttt{0xae}OP_CHECKMULTISIG=0xae, OP_CHECKMULTISIGVERIFY=0xaf\texttt{OP\_CHECKMULTISIGVERIFY}=\texttt{0xaf}OP_CHECKMULTISIGVERIFY=0xaf.
  • A signature version vvv is one of exactly two values: base\mathsf{base}base (legacy) or witnessV0\mathsf{witnessV0}witnessV0.

Reading one instruction from bytes: getOp\mathrm{getOp}getOp

getOp(x)\mathrm{getOp}(x)getOp(x) returns either none\mathsf{none}none or some(i,y)\mathsf{some}(i, y)some(i,y) with iii an instruction and yyy the remaining bytes:

  • x=εx = \varepsilonx=ε: none\mathsf{none}none.
  • Otherwise let bbb be the first byte and y0y_0y0​ the rest.
    • If b≤0x4eb \le \texttt{0x4e}b≤0x4e (a push opcode), a data length nnn and a position are determined: if b<0x4cb < \texttt{0x4c}b<0x4c, n=bn = bn=b and the data starts right after bbb; if b=0x4cb = \texttt{0x4c}b=0x4c, nnn is the next byte (none\mathsf{none}none if there is none); if b=0x4db = \texttt{0x4d}b=0x4d, nnn is the little-endian value of the next 2 bytes (none\mathsf{none}none if fewer than 2 remain); if b=0x4eb=\texttt{0x4e}b=0x4e, nnn is the little-endian value of the next 4 bytes (none\mathsf{none}none if fewer than 4 remain). Then, if fewer than nnn bytes remain, none\mathsf{none}none; otherwise some(push(next n bytes), the bytes after them)\mathsf{some}(\mathrm{push}(\text{next } n \text{ bytes}),\ \text{the bytes after them})some(push(next n bytes), the bytes after them). (So b=0x00b=\texttt{0x00}b=0x00 gives push(ε)\mathrm{push}(\varepsilon)push(ε).)
    • If b>0x4eb > \texttt{0x4e}b>0x4e: some(op(b),y0)\mathsf{some}(\mathrm{op}(b), y_0)some(op(b),y0​).

On success the remainder is a suffix of xxx that is at least one byte shorter than xxx. getOp\mathrm{getOp}getOp never returns op(c)\mathrm{op}(c)op(c) with c≤0x4ec \le \texttt{0x4e}c≤0x4e.

Signature-push deletion: pushBytes\mathrm{pushBytes}pushBytes, findAndDelete\mathrm{findAndDelete}findAndDelete, fadSig\mathrm{fadSig}fadSig, fadAll\mathrm{fadAll}fadAll

  • pushBytes(d)\mathrm{pushBytes}(d)pushBytes(d), the canonical push of ddd: if ∣d∣<0x4c|d| < \texttt{0x4c}∣d∣<0x4c it is the byte ∣d∣|d|∣d∣ followed by ddd; else if ∣d∣≤255|d| \le 255∣d∣≤255 it is 0x4c\texttt{0x4c}0x4c, the byte ∣d∣|d|∣d∣, then ddd; else if ∣d∣≤65535|d| \le 65535∣d∣≤65535 it is 0x4d\texttt{0x4d}0x4d, ∣d∣|d|∣d∣ as 2 little-endian bytes, then ddd; otherwise 0x4e\texttt{0x4e}0x4e, ∣d∣ mod 232|d| \bmod 2^{32}∣d∣mod232 as 4 little-endian bytes, then ddd. It is never empty (pushBytes(ε)=[0x00]\mathrm{pushBytes}(\varepsilon) = [\texttt{0x00}]pushBytes(ε)=[0x00]).
  • findAndDelete(x,b)\mathrm{findAndDelete}(x, b)findAndDelete(x,b) returns a pair (resulting string, count); only the first component (written FD(x,b)\mathrm{FD}(x,b)FD(x,b)) is used here. If b=εb = \varepsilonb=ε the result is (x,0)(x, 0)(x,0). Otherwise xxx is scanned from the start: at the current instruction boundary, copies of bbb are removed as long as the remaining string begins with bbb (each removal counted); then one instruction is read with getOp\mathrm{getOp}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|x|+1∣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)\mathrm{fadSig}(v, \mathit{sc}, \mathit{sig})fadSig(v,sc,sig):
fadSig(base,sc,sig)=FD(sc,pushBytes(sig)),fadSig(witnessV0,sc,sig)=sc.\mathrm{fadSig}(\mathsf{base}, \mathit{sc}, \mathit{sig}) = \mathrm{FD}\big(\mathit{sc}, \mathrm{pushBytes}(\mathit{sig})\big), \qquad \mathrm{fadSig}(\mathsf{witnessV0}, \mathit{sc}, \mathit{sig}) = \mathit{sc}.fadSig(base,sc,sig)=FD(sc,pushBytes(sig)),fadSig(witnessV0,sc,sig)=sc.
  • fadAll(v,sc,[σ1,…,σm])\mathrm{fadAll}(v, \mathit{sc}, [\sigma_1,\dots,\sigma_m])fadAll(v,sc,[σ1​,…,σm​]) is the left fold of fadSig(v,⋅,⋅)\mathrm{fadSig}(v,\cdot,\cdot)fadSig(v,⋅,⋅) over the list, in list order:
fadAll(v,sc,[σ1,…,σm])=fadSig(v,⋯fadSig(v,fadSig(v,sc,σ1),σ2)⋯ ,σm),\mathrm{fadAll}(v, \mathit{sc}, [\sigma_1,\dots,\sigma_m]) = \mathrm{fadSig}\big(v, \cdots \mathrm{fadSig}(v, \mathrm{fadSig}(v, \mathit{sc}, \sigma_1), \sigma_2) \cdots, \sigma_m\big),fadAll(v,sc,[σ1​,…,σm​])=fadSig(v,⋯fadSig(v,fadSig(v,sc,σ1​),σ2​)⋯,σm​),

and fadAll(v,sc,[ ])=sc\mathrm{fadAll}(v, \mathit{sc}, [\,]) = \mathit{sc}fadAll(v,sc,[])=sc.

Script numbers and multisigSigs\mathrm{multisigSigs}multisigSigs

  • toNum(x)\mathrm{toNum}(x)toNum(x) (the call with "minimal encoding not required" and maximum size 444): none\mathsf{none}none if ∣x∣>4|x| > 4∣x∣>4; otherwise some(z)\mathsf{some}(z)some(z) with z∈Zz \in \mathbb Zz∈Z obtained by reading xxx little-endian, the bit 0x80\texttt{0x80}0x80 of the last byte being a sign bit: if it is set, zzz is minus the little-endian value of xxx with that bit cleared; else zzz is the little-endian value of xxx. toNum(ε)=some(0)\mathrm{toNum}(\varepsilon) = \mathsf{some}(0)toNum(ε)=some(0). No minimality check is performed.

  • For an integer zzz, z+:=max⁡(z,0)z^{+} := \max(z, 0)z+:=max(z,0) (negative integers convert to the natural number 000).

  • multisigSigs(Σ)\mathrm{multisigSigs}(\Sigma)multisigSigs(Σ), for a stack Σ\SigmaΣ listed top first, is a list of byte strings:

    • Σ\SigmaΣ empty: [ ][\,][].
    • Σ=[nk]+ ⁣ ⁣+R\Sigma = [n_k] \mathbin{+\!\!+} RΣ=[nk​]++R: if toNum(nk)=none\mathrm{toNum}(n_k) = \mathsf{none}toNum(nk​)=none, [ ][\,][]. If toNum(nk)=some(NK)\mathrm{toNum}(n_k) = \mathsf{some}(N_K)toNum(nk​)=some(NK​), let R′=dropNK+(R)R' = \mathrm{drop}_{N_K^{+}}(R)R′=dropNK+​​(R). If R′R'R′ is empty, [ ][\,][]. If R′=[ns]+ ⁣ ⁣+R′′R' = [n_s] \mathbin{+\!\!+} R''R′=[ns​]++R′′: if toNum(ns)=none\mathrm{toNum}(n_s) = \mathsf{none}toNum(ns​)=none, [ ][\,][]; if toNum(ns)=some(NS)\mathrm{toNum}(n_s) = \mathsf{some}(N_S)toNum(ns​)=some(NS​), the result is takeNS+(R′′)\mathrm{take}_{N_S^{+}}(R'')takeNS+​​(R′′) (all of R′′R''R′′ if it has fewer than NS+N_S^{+}NS+​ elements).

    No range checks are made on NKN_KNK​, NSN_SNS​ (e.g. NS>NKN_S > N_KNS​>NK​, or negative counts, are accepted).

Oracles

  • A published oracle OOO consists of five functions B∗→B∗\mathcal B^*\to\mathcal B^*B∗→B∗ (RIPEMD-160, SHA-1, SHA-256, HASH160, HASH256), a function O.checkSig(sig,pk)∈{none,some(true),some(false)}O.\mathrm{checkSig}(\mathit{sig}, \mathit{pk}) \in \{\mathsf{none}, \mathsf{some}(\mathrm{true}), \mathsf{some}(\mathrm{false})\}O.checkSig(sig,pk)∈{none,some(true),some(false)}, and two predicates checkLockTime,checkSequence:Z→{true,false}\mathrm{checkLockTime}, \mathrm{checkSequence} : \mathbb Z \to \{\mathrm{true},\mathrm{false}\}checkLockTime,checkSequence:Z→{true,false}.
  • A V2 oracle ooo 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)}o.\mathrm{checkSig}(\mathit{sc}, \mathit{sig}, \mathit{pk}) \in \{\mathsf{none}, \mathsf{some}(\mathrm{true}), \mathsf{some}(\mathrm{false})\}o.checkSig(sc,sig,pk)∈{none,some(true),some(false)}. All components are arbitrary functions.
  • toOracle(o)\mathrm{toOracle}(o)toOracle(o): the published oracle with ooo's hashes and predicates and the signature check that always returns none\mathsf{none}none.
  • withCode(o,sc)\mathrm{withCode}(o, \mathit{sc})withCode(o,sc): toOracle(o)\mathrm{toOracle}(o)toOracle(o) with signature check (sig,pk)↦o.checkSig(sc,sig,pk)(\mathit{sig}, \mathit{pk}) \mapsto o.\mathrm{checkSig}(\mathit{sc}, \mathit{sig}, \mathit{pk})(sig,pk)↦o.checkSig(sc,sig,pk).
  • withCodeSig(o,v,sc)\mathrm{withCodeSig}(o, v, \mathit{sc})withCodeSig(o,v,sc): toOracle(o)\mathrm{toOracle}(o)toOracle(o) with signature check (sig,pk)↦o.checkSig(fadSig(v,sc,sig),sig,pk)(\mathit{sig}, \mathit{pk}) \mapsto o.\mathrm{checkSig}\big(\mathrm{fadSig}(v, \mathit{sc}, \mathit{sig}), \mathit{sig}, \mathit{pk}\big)(sig,pk)↦o.checkSig(fadSig(v,sc,sig),sig,pk).

V2 states and oracleAt\mathrm{oracleAt}oracleAt

  • A V2 state SSS is a pair (S.st,S.sc)(S.\mathrm{st}, S.\mathrm{sc})(S.st,S.sc) of a published interpreter state S.stS.\mathrm{st}S.st (a main stack and an alt stack of byte strings listed top first, a boolean condition stack vfExec\mathrm{vfExec}vfExec, and an operation counter) and a byte string S.scS.\mathrm{sc}S.sc, the script code. Write stack(S)\mathrm{stack}(S)stack(S) and vfExec(S)\mathrm{vfExec}(S)vfExec(S) for the main stack and condition stack of S.stS.\mathrm{st}S.st.
  • oracleAt(v,o,S,i)\mathrm{oracleAt}(v, o, S, i)oracleAt(v,o,S,i), the published oracle used for instruction iii from state SSS:
    • if i=op(c)i = \mathrm{op}(c)i=op(c) with c=0xaec = \texttt{0xae}c=0xae or c=0xafc = \texttt{0xaf}c=0xaf: withCode(o, fadAll(v,S.sc,multisigSigs(stack(S))))\mathrm{withCode}\big(o,\ \mathrm{fadAll}(v, S.\mathrm{sc}, \mathrm{multisigSigs}(\mathrm{stack}(S)))\big)withCode(o, fadAll(v,S.sc,multisigSigs(stack(S))));
    • for every other op(c)\mathrm{op}(c)op(c), and for every push(d)\mathrm{push}(d)push(d): withCodeSig(o,v,S.sc)\mathrm{withCodeSig}(o, v, S.\mathrm{sc})withCodeSig(o,v,S.sc).
Human review
  • Endorsed by Shuze Chen · Oct 30, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 30, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me