Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

CastToBool is 'nonzero as a script number'

Proved
BitcoinScript.castToBool_iff

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

bitcoinbitcoin-mainnetconsensuscscriptnumformal-verificationinterpreterscript

For every byte list vvv, castToBool(v)=true  ⟺  setVch(v)≠0\mathrm{castToBool}(v) = \mathtt{true} \iff \mathrm{setVch}(v) \ne 0castToBool(v)=true⟺setVch(v)=0. In particular both encodings of zero — all-zero bytes, and zero bytes followed by 0x80 (negative zero) — are false, and every other byte list is true, whatever its length.

Preamble
import Definitions.Def_BitcoinScript
Formal statement
theorem BitcoinScript.castToBool_iff (v : Bytes) : BitcoinScript.castToBool v = true ↔ BitcoinScript.setVch v ≠ 0 := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: CastToBool (lines 46-59), EvalScript (417-1249), EvalChecksigPreTapscript, VerifyScript (2012-2110), src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp; CScriptNum (227-382), opcodetype, MAX_SCRIPT_ELEMENT_SIZE, MAX_OPS_PER_SCRIPT, MAX_PUBKEYS_PER_MULTISIG, MAX_STACK_SIZE, src/script/script.h; GetScriptOp, IsPushOnly, IsPayToScriptHash, src/script/script.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/script.h
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: BitcoinScript.castToBool_iff

Binders and hypothesis. There is a single universally quantified variable vvv, a finite list of bytes (8-bit words), of any length including zero. There are no hypotheses, no flags, no oracle, and no other parameters.

Conclusion. The statement is a biconditional between two Boolean/propositional facts about vvv:

castToBool(v)=true⟺setVch(v)≠0.\mathrm{castToBool}(v) = \text{true} \quad\Longleftrightarrow\quad \mathrm{setVch}(v) \neq 0 .castToBool(v)=true⟺setVch(v)=0.

Unfolding the left-hand side. castToBool\mathrm{castToBool}castToBool is the recursive function with castToBool([])=false\mathrm{castToBool}([]) = \text{false}castToBool([])=false; castToBool([b])=(b≠0∧b≠0x80)\mathrm{castToBool}([b]) = (b \neq 0 \wedge b \neq \mathtt{0x80})castToBool([b])=(b=0∧b=0x80) for a single byte; and castToBool(b::r)=(b≠0)∨castToBool(r)\mathrm{castToBool}(b :: r) = (b \neq 0) \vee \mathrm{castToBool}(r)castToBool(b::r)=(b=0)∨castToBool(r) when rrr is non-empty. Spelled out: castToBool(v)\mathrm{castToBool}(v)castToBool(v) is true exactly when there is some byte of vvv that is non-zero, except in the special case where every byte before the last is 0x00\mathtt{0x00}0x00 and the last byte is exactly 0x80\mathtt{0x80}0x80, which is false. In particular [0x80][\mathtt{0x80}][0x80], [0x00,0x80][\mathtt{0x00},\mathtt{0x80}][0x00,0x80], [][][], and [0x00,0x00][\mathtt{0x00},\mathtt{0x00}][0x00,0x00] are all false, while [0x80,0x00][\mathtt{0x80},\mathtt{0x00}][0x80,0x00], [0x01][\mathtt{0x01}][0x01] and [0x00,0x01][\mathtt{0x00},\mathtt{0x01}][0x00,0x01] are true.

Unfolding the right-hand side. setVch(v)\mathrm{setVch}(v)setVch(v) is the integer

setVch(v)={− leNat(clearTop(v))if the top bit 0x80 of the last byte of v is set,−leNat(v)otherwise (including v=[]),\mathrm{setVch}(v) = \begin{cases} -\,\mathrm{leNat}(\mathrm{clearTop}(v)) & \text{if the top bit } \mathtt{0x80} \text{ of the last byte of } v \text{ is set,} \\ \phantom{-}\mathrm{leNat}(v) & \text{otherwise (including } v = []),\end{cases}setVch(v)={−leNat(clearTop(v))−leNat(v)​if the top bit 0x80 of the last byte of v is set,otherwise (including v=[]),​

where leNat\mathrm{leNat}leNat is the little-endian unsigned value (leNat(b::r)=b+256⋅leNat(r)\mathrm{leNat}(b :: r) = b + 256\cdot\mathrm{leNat}(r)leNat(b::r)=b+256⋅leNat(r), leNat([])=0\mathrm{leNat}([]) = 0leNat([])=0) and clearTop\mathrm{clearTop}clearTop clears bit 0x80\mathtt{0x80}0x80 of the last byte only. Hence setVch(v)=0\mathrm{setVch}(v) = 0setVch(v)=0 exactly when every byte of vvv is 0x00\mathtt{0x00}0x00 after ignoring the sign bit of the last byte — i.e. when vvv is empty, or all-zero, or all-zero except that its last byte is 0x80\mathtt{0x80}0x80. The right-hand side setVch(v)≠0\mathrm{setVch}(v) \neq 0setVch(v)=0 is the negation of that: some byte of vvv has a bit set other than the sign bit of the final byte.

The claim, in words. For every byte list vvv, the truthiness test (some non-zero byte, with "negative zero" 0⋯0 0x800\cdots0\,\mathtt{0x80}0⋯00x80 counted as false) coincides with the numeric value under the sign-magnitude little-endian reading being non-zero. Both directions are asserted: whenever the Boolean test is true the integer is non-zero, and whenever the integer is non-zero the Boolean test is true. Equivalently, castToBool(v)=false\mathrm{castToBool}(v) = \text{false}castToBool(v)=false if and only if setVch(v)=0\mathrm{setVch}(v) = 0setVch(v)=0.

Edge cases included by the quantifier. v=[]v = []v=[] (both sides false: castToBool([])=false\mathrm{castToBool}([]) = \text{false}castToBool([])=false and setVch([])=0\mathrm{setVch}([]) = 0setVch([])=0). v=[0x80]v = [\mathtt{0x80}]v=[0x80] (both sides false: the single-byte clause excludes 0x80\mathtt{0x80}0x80, and setVch([0x80])=−leNat([0x00])=0\mathrm{setVch}([\mathtt{0x80}]) = -\mathrm{leNat}([\mathtt{0x00}]) = 0setVch([0x80])=−leNat([0x00])=0). Lists of arbitrary length, including lists far longer than any numeric size limit used elsewhere in the file (4 or 5 bytes): the statement does not restrict length, so it concerns setVch\mathrm{setVch}setVch on lists of, say, 1000 bytes just as much as on short ones. Lists with trailing zero bytes such as [0x01,0x00][\mathtt{0x01},\mathtt{0x00}][0x01,0x00] (both sides true) and non-minimal encodings are all included.

What is not said. The theorem does not mention toNum, minimal encodings, nonMinimal, or any size limit; it says nothing about how vvv was produced, and nothing about serialize. It does not assert that castToBool(v)\mathrm{castToBool}(v)castToBool(v) equals castToBool(serialize(setVch(v)))\mathrm{castToBool}(\mathrm{serialize}(\mathrm{setVch}(v)))castToBool(serialize(setVch(v))) or any round-trip property. It does not say which non-zero value setVch(v)\mathrm{setVch}(v)setVch(v) takes, only that it is non-zero; in particular it does not relate the sign or magnitude of setVch(v)\mathrm{setVch}(v)setVch(v) to anything. It does not state the contrapositive form separately, though that follows logically from the biconditional. It makes no claim about the interpreter (execOp, step, run) or about how the result of castToBool is used by OP_IF, OP_VERIFY, OP_IFDUP or verifyScriptPlain; in particular it does not assert that a script's final truth value is determined by a numeric value. It does not involve any oracle or flags.

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

    Confirmed by the moderator at approval.

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