Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

A push-only scriptSig parses to its pushes

Proved
BitcoinVerify.parseScript_pushes

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

bip141bitcoinbitcoin-mainnetconsensusformal-verificationp2shsegwitverifyscript

For elements each within 520 bytes, parseScript (pushesBytes ps) = some (ps.map push): the canonical pushes (direct, PUSHDATA1, PUSHDATA2) parse back to the pushed elements.

Preamble
import Definitions.Def_BitcoinVerify
import Definitions.Def_BitcoinScript
import Definitions.Def_BitcoinSig
open BitcoinVerify BitcoinSer BitcoinSig
open BitcoinScript hiding Bytes Byte
Formal statement
theorem BitcoinVerify.parseScript_pushes (ps : List Bytes) (hp : ∀ p ∈ ps, p.length ≤ BitcoinScript.MAX_SCRIPT_ELEMENT_SIZE) :
    BitcoinSig.parseScript (BitcoinVerify.pushesBytes ps) = some (ps.map Instr.push) := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: VerifyScript, VerifyWitnessProgram, ExecuteWitnessScript, src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp; CScript::IsPayToScriptHash, IsWitnessProgram, IsPushOnly, IsPayToAnchor, DecodeOP_N, src/script/script.cpp and script.h; MAX_SCRIPT_SIZE, WITNESS_V0_SCRIPTHASH_SIZE, WITNESS_V0_KEYHASH_SIZE, WITNESS_V1_TAPROOT_SIZE; BIP16, BIP141, BIP143 at bips revision 100ba872c808946155b1eeb8c4568058f2eea0a7
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: BitcoinVerify.parseScript_pushes

Binders and hypothesis. One universally quantified variable pspsps, a finite list of byte strings (each a finite list of 8-bit bytes). One hypothesis hp: every element ppp of pspsps satisfies ∣p∣≤520|p| \le 520∣p∣≤520 (MAX_SCRIPT_ELEMENT_SIZE). The list may be empty, in which case the hypothesis holds vacuously.

Conclusion.

parseScript(pushesBytes(ps))=some(map push ps),\mathrm{parseScript}\big(\mathrm{pushesBytes}(ps)\big) = \mathrm{some}\big(\mathrm{map}\ \mathrm{push}\ ps\big),parseScript(pushesBytes(ps))=some(map push ps),

i.e. the byte-level parser succeeds on the concatenated canonical pushes and returns exactly the instruction list consisting of one push instruction per element of pspsps, carrying that element's bytes, in the same order. For ps=[]ps = []ps=[] this says parseScript [] = some [].

Unfolding pushesBytes. pushesBytes ps concatenates pushBytes p over the elements ppp of pspsps in order. pushBytes p is: for ∣p∣<76|p| < 76∣p∣<76, the single byte ∣p∣|p|∣p∣ followed by ppp (for p=[]p = []p=[], the lone byte 0x00\mathtt{0x00}0x00); for 76≤∣p∣≤25576 \le |p| \le 25576≤∣p∣≤255, 0x4c\mathtt{0x4c}0x4c, one length byte, then ppp; for 256≤∣p∣≤65535256 \le |p| \le 65535256≤∣p∣≤65535, 0x4d\mathtt{0x4d}0x4d, two little-endian length bytes, then ppp; otherwise 0x4e\mathtt{0x4e}0x4e, four little-endian length bytes, then ppp. Under ∣p∣≤520|p| \le 520∣p∣≤520 only the first three shapes occur.

Unfolding parseScript. parseScript s runs parseScriptAux with fuel ∣s∣|s|∣s∣: on the empty string it returns some []; with zero fuel on a non-empty string it returns nothing; otherwise it calls getOp on sss and, on success with instruction iii and remainder rrr, recursively parses rrr with one less fuel and prepends iii; any failure propagates as nothing. getOp on a non-empty string with first byte ooo: if o≤0x4eo \le \mathtt{0x4e}o≤0x4e it is a push — for o<0x4co < \mathtt{0x4c}o<0x4c the push length is ooo itself; for o=0x4co = \mathtt{0x4c}o=0x4c the next byte is the length (nothing if absent); for o=0x4do = \mathtt{0x4d}o=0x4d the next two bytes little-endian (nothing if fewer than two remain); for o=0x4eo = \mathtt{0x4e}o=0x4e the next four (nothing if fewer than four) — and then nothing if fewer than that many bytes remain, else the push of those bytes and the rest; if o>0x4eo > \mathtt{0x4e}o>0x4e it is the non-push opcode ooo. Each successful getOp consumes at least one byte, so fuel ∣s∣|s|∣s∣ never runs out early.

Logical content. The theorem asserts that the encoder pushBytes and the decoder getOp are inverse along concatenations: parsing re-splits the byte string exactly at the boundaries the encoder wrote, and recovers each element's bytes unchanged as a push instruction, provided every element has at most 520 bytes. It is an equality of optional instruction lists, so it also asserts that the parse does not fail.

What is not said. The theorem does not say anything about elements longer than 520 bytes (the PUSHDATA2/PUSHDATA4 shapes are then outside the statement, even though the hypothesis is not visibly used by the parser); it does not say that the parsed instructions execute successfully or satisfy the interpreter's 520-byte push limit — it is only about parsing; it does not bound the total length (no MAX_SCRIPT_SIZE condition, and parseScript has none); it does not assert the converse (that every byte string parsing to a list of pushes is of the form pushesBytes ps — other encodings of the same pushes exist); and it does not relate to isPushOnly or verifyScript.

Human review
  • Endorsed by Shuze Chen · Oct 27, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 27, 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