Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The SIGHASH_SINGLE bug returns the hash 1

Proved
BitcoinSig.sigHash_single_bug

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

bitcoinbitcoin-mainnetconsensusderecdsaformal-verificationschnorrsecp256k1sighash

For every script code, transaction, input index nInnInnIn and hash type with (hashType & 0x1f)=SIGHASH_SINGLE(\text{hashType} \,\&\, \mathtt{0x1f}) = \mathtt{SIGHASH\_SINGLE}(hashType&0x1f)=SIGHASH_SINGLE and ∣vout∣≤nIn|\mathrm{vout}| \le nIn∣vout∣≤nIn: the legacy signature hash is uint256::ONE — the historical quirk by which such a signature commits to nothing.

Preamble
import Definitions.Def_BitcoinSig
open BitcoinSig BitcoinSer
Formal statement
theorem BitcoinSig.sigHash_single_bug (scriptCode : Bytes) (tx : Tx) (nIn hashType : Nat)
    (h1 : hashType &&& 0x1f = BitcoinSig.SIGHASH_SINGLE) (h2 : tx.vout.length ≤ nIn) :
    BitcoinSig.sigHashLegacy scriptCode tx nIn hashType = BitcoinSig.uint256One := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: IsValidSignatureEncoding, IsLowDERSignature, IsCompressedOrUncompressedPubKey, IsCompressedPubKey, IsDefinedHashtypeSignature, CheckSignatureEncoding, CheckPubKeyEncoding, FindAndDelete, CTransactionSignatureSerializer, SignatureHash (BASE and WITNESS_V0), EvalChecksigPreTapscript, GenericTransactionSignatureChecker::CheckECDSASignature/CheckLockTime/CheckSequence, src/script/interpreter.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/script/interpreter.cpp; GetScriptOp, src/script/script.cpp; CPubKey::IsValid/Verify/CheckLowS and ecdsa_signature_parse_der_lax, src/pubkey.cpp and pubkey.h; secp256k1_eckey_pubkey_parse (src/secp256k1/src/eckey_impl.h), secp256k1_ecdsa_sig_verify (ecdsa_impl.h), secp256k1_schnorrsig_verify (modules/schnorrsig/main_impl.h); BIP66, BIP143, BIP340 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: BitcoinSig.sigHash_single_bug

Binders and hypothesis. Universally quantified over a byte string scriptCodescriptCodescriptCode, a transaction txtxtx, and natural numbers nInnInnIn and hashTypehashTypehashType (the latter unbounded, not restricted to one byte). Two hypotheses:

  • h1h1h1: hashType&31=3hashType \mathbin{\&} 31 = 3hashType&31=3, i.e. the low five bits of hashTypehashTypehashType (bitwise AND on natural numbers with 0x1f) equal the constant SIGHASH_SINGLE =3= 3=3; the remaining bits, in particular the ANYONECANPAY bit 128128128 and any bits above the eighth, are unconstrained;
  • h2h2h2: ∣tx.vout∣≤nIn|tx.\text{vout}| \leq nIn∣tx.vout∣≤nIn, the input index is at least the number of outputs (so there is no output at position nInnInnIn; this includes the case of a transaction with no outputs at all, for every nInnInnIn).

Conclusion.

sigHashLegacy scriptCode tx nIn hashType=uint256One,\text{sigHashLegacy}\ scriptCode\ tx\ nIn\ hashType = \text{uint256One},sigHashLegacy scriptCode tx nIn hashType=uint256One,

where uint256One is the 32-byte string consisting of the byte 111 followed by 31 zero bytes.

Unfolding. sigHashLegacy sc tx nIn htsc\,tx\,nIn\,htsctxnInht is defined as: if ht&31=3ht \mathbin{\&} 31 = 3ht&31=3 and ∣tx.vout∣≤nIn|tx.\text{vout}| \leq nIn∣tx.vout∣≤nIn then uint256One, else the double SHA-256 of the legacy serialization followed by the 4-byte hash type. The two hypotheses are literally the two conjuncts of the guard, so the conclusion is the first branch of the definition. The script code, the inputs, version, lock time, and the number of inputs play no role; nInnInnIn need not be a valid input index either.

Edge cases. With tx.vout=[ ]tx.\text{vout} = [\,]tx.vout=[] the conclusion holds for every nInnInnIn including 000. With ∣tx.vout∣=nIn|tx.\text{vout}| = nIn∣tx.vout∣=nIn exactly, the bound is met (it is ≤\leq≤, not <<<). Hash types 333, 131131131 (3+1283 + 1283+128), 353535, 259259259, etc. all satisfy h1h1h1.

What is not said. The statement does not say what the hash is when nIn<∣tx.vout∣nIn < |tx.\text{vout}|nIn<∣tx.vout∣ or when the low five bits are not 333. It does not say that uint256One is "the number one" in any particular reading: as a byte string it begins with the byte 111; read big-endian by pubkeyVerify it is the scalar 2248 mod n2^{248} \bmod n2248modn — the theorem asserts only equality of byte strings. It does not say anything about whether a signature over this value verifies, nor about checkECDSASignature or checkSigPreTapscript. It does not say the BIP143 hash has any analogous behaviour (by its definition it does not; it uses a zero hashOutputs instead). It does not assert that this is a "bug" despite the theorem's name — the name is not part of the statement.

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