Transaction intent: the signature-free content of a transaction, and spends of native witness programs
DefinitionBitcoinMalleabilityThree names over the serialization mission's Tx, the block mission's ledger, and the VerifyScript mission's isWitnessProgram; nothing is re-transcribed.
Intent — a record of a version, a list of (outpoint, sequence number) pairs, a list of outputs, and a lock time: what the sender decides. scriptSigs and witnesses are not part of it.
intentOf tx — the intent of a transaction: its version, the (prevout, nSequence) of each input in order, its outputs, its lock time.
ofIntent t — the canonical transaction of an intent: the same fields, every scriptSig empty, every witness empty.
SpendsWitnessPrograms u tx — every input of tx spends, in the ledger u, an output whose scriptPubKey is a native witness program (CScript::IsWitnessProgram: OP_0 or OP_1..OP_16 followed by a single push of 2 to 40 bytes).
Formalization Note. txid is the serialization mission's double SHA-256 of the witness-stripped serialization; inputScriptsOk is the block mission's CheckInputScripts (the complete VerifyScript for every input against the spent outputs); verifyScript is the VerifyScript mission's dispatch. Only Std is imported.
import Definitions.Def_BitcoinBlockV2 import Definitions.Def_BitcoinVerify /-! # Transaction identifiers under SegWit The txid of a transaction hashes its witness-stripped serialization, which still contains every `scriptSig`. Before SegWit a third party could alter a signature's encoding inside a `scriptSig` and change the txid of a valid transaction without invalidating it. For an input that spends a native witness program, `VerifyScript` requires the `scriptSig` to be empty (`SCRIPT_ERR_WITNESS_MALLEATED`), so nothing a signer or a third party chooses remains in the hashed bytes. This file names the sender's intent, the part of a transaction that is not a signature, so that the theorems can say: for such spends the txid is a function of the intent. -/ namespace BitcoinMalleability open BitcoinSer BitcoinBlockV2 /-- What the sender decides: the version, the outpoint and sequence number of every input, the outputs, and the lock time. The `scriptSig`s and witnesses are not part of it. -/ structure Intent where version : Nat inputs : List (OutPoint × Nat) vout : List TxOut nLockTime : Nat deriving DecidableEq, Repr /-- The intent of a transaction. -/ def intentOf (tx : Tx) : Intent := ⟨tx.version, tx.vin.map fun i => (i.prevout, i.nSequence), tx.vout, tx.nLockTime⟩ /-- The canonical transaction of an intent: empty `scriptSig`s and empty witnesses. -/ def ofIntent (t : Intent) : Tx := ⟨t.version, t.inputs.map fun p => ⟨p.1, [], p.2, []⟩, t.vout, t.nLockTime⟩ /-- Every input of `tx` spends, in the ledger `u`, an output whose script is a native witness program (`CScript::IsWitnessProgram`). -/ def SpendsWitnessPrograms (u : Ledger) (tx : Tx) : Prop := ∀ i ∈ tx.vin, BitcoinVerify.isWitnessProgram (accessCoin u i.prevout).out.scriptPubKey ≠ none end BitcoinMalleability
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: Def_BitcoinMalleability.lean
The file declares one structure and three definitions, inside a namespace BitcoinMalleability, on top of the transaction and ledger types of the imported files. The notions those declarations use are unfolded first; then each declaration is read back, in file order.
Imported notions, unfolded
-
A byte is an 8-bit value; a byte string is a finite list of bytes (possibly empty).
-
An outpoint is a pair with a byte string (of any length; no 32-byte constraint is part of the type) and (no bound is part of the type).
-
A transaction input is a quadruple : an outpoint, a byte string, a natural number, and a list of byte strings.
-
A transaction output is a pair with (any integer) and a byte string.
-
A transaction is a quadruple : a natural number, a list of inputs, a list of outputs, a natural number. Nothing in the type requires or to be non-empty or any field to be in range.
-
A coin is a triple : an output, a natural number, a Boolean. The dummy coin is : value , empty script.
-
A ledger is a finite list of pairs (outpoint, coin). It is an association list; keys are not required to be distinct and the order matters for lookup.
-
walks from the front and returns the coin of the first pair whose outpoint equals exactly (same hash bytes, same index), or nothing if no pair matches.
-
is when it exists, and otherwise the dummy coin. In particular, for an outpoint absent from , and .
-
, for a byte string of length , is defined (returns a pair) exactly when all three hold:
- ;
- , or ;
- (so ).
When defined, it returns with if and otherwise (so ), and . Otherwise it is undefined ("none"). Note that the empty string and every string of length are not witness programs.
Declaration 1: structure Intent
An intent is a record with exactly four fields and no constraints between them:
where ; is a finite list of pairs with an outpoint and ; is a finite list of transaction outputs ; and . Equality of intents is decidable and is field-wise equality: two intents are equal if and only if their versions are equal, their input lists are equal as lists (same length, same pairs in the same order), their output lists are equal as lists, and their lock times are equal. There is no field for script signatures, witnesses, or any ledger. The lists may be empty; there are no range bounds on any natural number and no length bound on any byte string. Nothing in the structure refers to a transaction.
Declaration 2: intentOf
For a transaction with ,
That is: the version is copied; each input is replaced by the pair of its outpoint and its sequence number, in the same order and with the same multiplicity (duplicates are kept); the output list is copied unchanged (values and scripts included); the lock time is copied. The fields and of every input are discarded. Consequently, two transactions have the same intent if and only if they have the same version, the same number of inputs with pairwise equal outpoints and sequence numbers, identical output lists, and the same lock time; they may differ arbitrarily in the and of each input. For a transaction with no inputs, the intent's input list is empty. No ledger is involved and no validity is checked.
Declaration 3: ofIntent
For an intent ,
a transaction whose version is , whose -th input has outpoint , empty , sequence number and empty witness list, whose outputs are exactly , and whose lock time is . By construction for every intent , and for every transaction , is with every input's replaced by the empty byte string and every input's witness replaced by the empty list, all other data unchanged. No validity, well-formedness or ledger condition is imposed.
Declaration 4: SpendsWitnessPrograms
For a ledger and a transaction , the proposition states:
Unfolded: for every input that occurs in the input list of (membership, so position and repetition are irrelevant), let be the output script of the first ledger entry whose outpoint equals , or the empty string if there is no such entry; then , the first byte of is or lies in , and the second byte of equals . Edge cases: if is empty the proposition holds vacuously; since the empty string is not a witness program, the proposition fails for any input whose outpoint has no entry in , so it entails that every input's outpoint is a key of ; any witness version to and any program length to is accepted, with no distinction among P2WPKH, P2WSH, Taproot or unknown versions; nothing is said about the inputs' , witness, sequence number, about coinbase status, about duplicate keys in (only the first match counts), or about the transaction's outputs.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.