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.checkSchnorr_iff
Binders. tx : a transaction (natural-number version, list of inputs vin — each an outpoint (byte-string hash, natural index), a scriptSig, a natural sequence and a witness list —, list of outputs vout — each an integer value and a byte-string script —, natural nLockTime); spent : a list of outputs; nIn∈N; sig,q : byte strings; tapscript : a Boolean; ed : an ExecData record (a,A,L,κ) — Boolean annexPresent, byte strings annexHash and tapleafHash, natural codeseparatorPos.
Hypotheses. None.
Conclusion (an equivalence).
checkSchnorrSignature(tx,spent,nIn,sig,q,tapscript,ed)=true
if and only if there exist a byte string body, a natural number ht and a byte string h such that all of the following hold:
- ∣body∣=64;
- either (sig=body and ht=0), or there exists an 8-bit value b with sig=body∥[b], b=ht (as naturals) and ht=0;
- sigHashSchnorr(tx,spent,nIn,ht,tapscript,ed)=some(h);
- schnorrVerify(q,h,body)=true.
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. q of length other than 32 (then schnorrVerify is false and both sides are false); sig of 65 bytes ending in 0x00 (left side false; right side has no witness since clause 2 forbids it); sig of length other than 64/65; nIn≥∣vin∣ or ≥∣spent∣ (default input / default output (−1,[]) substituted inside the hash); spent of length unrelated to ∣vin∣; ht in the witness is uniquely determined by sig when a witness exists; empty vin/vout; ed arbitrary (its annexHash/tapleafHash of any length).
Confirmed by the moderator at approval.