What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: BitcoinTaproot.keypath_correct
Binders. spent : a list of transaction outputs (integer value, byte-string script); c : a verification context BitcoinVerify.Ctx (fields flags — Booleans p2sh, witness, taproot, dersig, cltv, csv, nulldummy —, tx a transaction with natural version, inputs vin, outputs vout, natural nLockTime, nIn∈N, amount∈Z); sig,q : byte strings.
Hypotheses. None.
Conclusion (an equivalence).
taprootHandler(spent)(c,[sig],q)=true
if and only if there exist a byte string body, a natural ht and a byte string h with
- ∣body∣=64;
- either (sig=body and ht=0) or (there is an 8-bit value b with sig=body∥[b], b=ht as naturals, and ht=0);
- sigHashSchnorr(c.tx,spent,c.nIn,ht,false,(false,[],[],0xFFFFFFFF))=some(h), the last argument being the
ExecData with no annex, empty annex hash, empty tapleaf hash and code-separator position 232−1;
- schnorrVerify(q,h,body)=true.
The left side, unfolded. taprootHandler(spent)(c,W,q) returns false for empty W; otherwise computes hasAnnex(W) ("∣W∣≥2 and the last element is non-empty and starts with 0x50"), strips the annex if present, builds e0 = (annex present, SHA-256 of the CompactSize-prefixed annex or [], [], 0xFFFFFFFF), and if the remaining stack has exactly one element x returns checkSchnorrSignature(c.tx,spent,c.nIn,x,q,false,e0); otherwise it takes the script path. For W=[sig] there is no annex (∣W∣=1), the stack is [sig], e0=(false,[],[],0xFFFFFFFF), and the left side equals checkSchnorrSignature(c.tx,spent,c.nIn,sig,q,false,e0)=true. The flag c.flags.taproot and c.amount play no role.
The signature check, unfolded. checkSchnorrSignature(tx,S,i,sig,pk,τ,e) (with S the spent-output list, i the input index, τ the tapscript flag, e the ExecData) is the Boolean computed as follows: if ∣sig∣=64 and ∣sig∣=65, false; otherwise let t:= the last byte of sig as a natural if ∣sig∣=65, else t:=0, and sig′:=sig without its last byte if ∣sig∣=65, else sig; if ∣sig∣=65 and t=0, false; otherwise compute sigHashSchnorr(tx,S,i,t,τ,e) — if that is none, false; if it is some(h), the result is schnorrVerify(pk,h,sig′).
schnorrVerify(pk,m,σ) is: false if ∣pk∣=32 or ∣σ∣=64; let r:=BE(σ[0:32]) and s:=BE(σ[32:64]) (big-endian values); false if p≤r or n≤s (p=2256−232−977 the field prime, n the secp256k1 group order); let P:=liftX(BE(pk)), which is none if BE(pk)≥p, and otherwise, with x:=BE(pk) and y:=(x3+7)(p+1)/4modp, is the point (x,y) if y is even, or (x,p−y) if y is odd, provided y2≡x3+7(modp), and none otherwise; false if P is none; let c:=BE(tagged("BIP0340/challenge",σ[0:32]∥pk∥m))modn; let R:=s⋅G+((n−c)modn)⋅P computed with the bundle's affine addition/doubling formulas (with the point at infinity as none) and a 257-step double-and-add; false if R is none; otherwise true iff yR is even and xR=r.
sigHashSchnorr(tx,S,i,t,τ,e), with e=(a,A,L,κ) (annex present, annex hash, tapleaf hash, code-separator position), is: none unless t∈{0,1,2,3,0x81,0x82,0x83}; let o:=1 if t=0 else t&3; ι:=t&0x80; ς:=2[τ]+[a] (so 0, 1, 2 or 3); I:= the i-th input of tx, or, if i≥∣vin∣, the default input (prevout with empty hash and index 0, empty scriptSig, sequence 0, empty witness); if o=3 then single:= none when ∣vout∣≤i and otherwise SHA256(serTxOut(vouti)), else single:=[]; if single is none the result is none; otherwise the result is some of
tagged("TapSighash", [0x00,tmod256]∥le32(version)∥le32(nLockTime)∥Hin∥Hout∥[ς]∥D∥(A if a else [])∥single∥(L∥[0x00]∥le32(κ) if τ else []))
where Hin is empty if ι=0x80 and otherwise is the concatenation of four SHA-256 digests: of all inputs' serialized outpoints (hash ∥ le32 index), of all elements of S's values as 8-byte little-endian two's complement, of all elements of S's scriptPubKeys each prefixed by its CompactSize length, and of all inputs' le32 sequences; Hout is the SHA-256 of all outputs serialized (le64 two's-complement value ∥ CompactSize-prefixed script) if o=1, and empty otherwise; D is serOutPoint(I.prevout)∥serTxOut(Si or the default output (−1,[]) if i≥∣S∣)∥le32(I.nSequence) if ι=0x80, and le32(i) otherwise. tagged(T,m)=SHA256(SHA256(T)∥SHA256(T)∥m) with T the UTF-8 bytes of the tag; le32(x) is the 4-byte little-endian encoding of xmod232; CompactSize is 1 byte below 253, else 0xfd + 2 bytes up to 0xffff, else 0xfe + 4 bytes up to 0xffffffff, else 0xff + 8 bytes. All SHA-256 computations are the bundle's own implementation.
In this instance τ=false and a=false, so ς=0, no annex hash and no tapscript extension appear in the message, and t=ht must lie in {0,1,2,3,0x81,0x82,0x83} for clause 3 to hold; with ht∈{3,0x83} it additionally requires c.nIn<∣vout∣.
Edge cases the quantifiers include. q not of length 32 (both sides false); sig empty, of length =64,65, or 65 bytes ending in 0x00 (both sides false); c.nIn≥∣vin∣ (default input with empty prevout hash used in D when ht≥0x81) or ≥∣spent∣ (default output (−1,[])); spent of any length; empty vin or vout; ht, when it exists, is determined by sig.
Confirmed by the moderator at approval.