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_handler
Binders. spent : a list of transaction outputs (integer value, byte-string script); c : a verification context BitcoinVerify.Ctx with fields flags (the consensus flag set: Booleans p2sh, witness, taproot, dersig, cltv, csv, nulldummy), tx (a transaction), nIn∈N and amount∈Z; sig,q : byte strings.
Hypotheses. None.
Conclusion (an equality of Booleans).
taprootHandler(spent)(c,[sig],q)=checkSchnorrSignature(c.tx,spent,c.nIn,sig,q,false,(false,[],[],0xFFFFFFFF)),
where the last argument is the ExecData record with annexPresent =false, empty annexHash, empty tapleafHash, and codeseparatorPos =232−1.
The left side, unfolded. taprootHandler(spent) is the function of a context c, a witness list W and a program byte string q that: returns false if W is empty; otherwise forms the Tapscript context (flags cltv, csv, nulldummy taken from c.flags, c.tx, spent, c.nIn); decides hasAnnex(W), which is "∣W∣≥2 and the last element of W is non-empty and its first byte is 0x50"; sets the annex to the last element when hasAnnex(W) and removes it from W to get the stack, else the stack is W; builds e0=(annex present,SHA256(CompactSize(∣annex∣)∥annex) or [],[],0xFFFFFFFF); and, if the stack has exactly one element x, returns checkSchnorrSignature(c.tx,spent,c.nIn,x,q,false,e0) (the key path); otherwise it takes the script path (control block and script popped from the end, control-block size checks, verifyTaprootCommitment, and either executeTapscript for leaf version 0xc0 or true for any other leaf version). For W=[sig]: W is non-empty, ∣W∣=1<2 so there is no annex, the stack is [sig] of length 1, e0=(false,[],[],0xFFFFFFFF), and the key path is taken. The flag c.flags.taproot and c.amount are not consulted by the handler.
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.
Edge cases the quantifiers include. sig of any length, including empty or 65 bytes ending in 0x00 (both sides false); sig beginning with 0x50 (no annex, since ∣W∣=1); q of any length; c.nIn beyond vin or spent (defaults substituted); spent of length unrelated to the inputs; c.flags arbitrary, including taproot off.
Confirmed by the moderator at approval.