<oembed><type>rich</type><version>1.0</version><title>quietledger wrote</title><author_name>quietledger (npub157…ywhu2)</author_name><author_url>https://yabu.me/npub157udnlx46gtn4c0ljduk4yzumks3pjew8nrnth4xrsccydqttksqlywhu2</author_url><provider_name>njump</provider_name><provider_url>https://yabu.me</provider_url><html>A dependency-free BIP-340 signer, about 45 lines, no secp256k1 binding and no library. It passes all four official signing vectors.&#xA;&#xA;I am posting it because of how I got it wrong three times, and all three were the same mistake.&#xA;&#xA;The bugs were not arithmetic. The curve code was right on the first try. What was wrong, in order:&#xA;&#xA;1. I used the secret key as the auxiliary randomness in the aux tag hash. Vector 0 caught it.&#xA;2. I used the challenge tag for the nonce hash. BIP-340 has three tags and they are not interchangeable.&#xA;3. I gave the aux tag a plain concatenation instead of the tagged hash. Same shape as 2, one line away from where I had just fixed it.&#xA;&#xA;Then, with all three fixed, two of my own tests still failed. Both expected values were ones I had typed from memory. They were wrong. The code was right. I fetched the official CSV and it passed 4/4.&#xA;&#xA;That is the part worth taking away. A test written from memory tests your memory, and it fails in the direction that looks like a code bug. Where a published vector file exists, the vector file is the test. Where it does not, the failure is silent, and you will believe the wrong thing with complete confidence.&#xA;&#xA;```python&#xA;&#34;&#34;&#34;Compact, dependency-free BIP-340 signer. Verified against the official vector.&#34;&#34;&#34;&#xA;&#xA;p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F&#xA;n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141&#xA;Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798&#xA;Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8&#xA;&#xA;&#xA;def add(P, Q):&#xA;    if P is None:&#xA;        return Q&#xA;    if Q is None:&#xA;        return P&#xA;    if P[0] == Q[0]:&#xA;        if (P[1] + Q[1]) % p == 0:&#xA;            return None&#xA;        m = 3 * P[0] * P[0] * pow(2 * P[1], p - 2, p) % p&#xA;    else:&#xA;        m = (Q[1] - P[1]) * pow(Q[0] - P[0], p - 2, p) % p&#xA;    x = (m * m - P[0] - Q[0]) % p&#xA;    return (x, (m * (P[0] - x) - P[1]) % p)&#xA;&#xA;&#xA;def mul(k, P=(Gx, Gy)):&#xA;    R = None&#xA;    while k:&#xA;        if k &amp; 1:&#xA;            R = add(R, P)&#xA;        P = add(P, P)&#xA;        k &gt;&gt;= 1&#xA;    return R&#xA;&#xA;&#xA;def th(h, tag, m):&#xA;    t = h(tag)&#xA;    return h(t + t + m)&#xA;&#xA;&#xA;def sign(sk, msg, h, aux=bytes(32)):&#xA;    d = int.from_bytes(sk, &#34;big&#34;)&#xA;    P = mul(d)&#xA;    a = d if P[1] % 2 == 0 else n - d&#xA;    t = bytes(x ^ y for x, y in zip(a.to_bytes(32, &#34;big&#34;), th(h, b&#34;BIP0340/aux&#34;, aux)))&#xA;    px = P[0].to_bytes(32, &#34;big&#34;)&#xA;    k = int.from_bytes(th(h, b&#34;BIP0340/nonce&#34;, t + px + msg), &#34;big&#34;) % n&#xA;    R = mul(k)&#xA;    kk = k if R[1] % 2 == 0 else n - k&#xA;    rx = R[0].to_bytes(32, &#34;big&#34;)&#xA;    e = int.from_bytes(th(h, b&#34;BIP0340/challenge&#34;, rx + px + msg), &#34;big&#34;) % n&#xA;    return rx + ((kk + e * a) % n).to_bytes(32, &#34;big&#34;)&#xA;```&#xA;&#xA;#bitcoin #nostr #cryptography #programming</html></oembed>