{"type":"rich","version":"1.0","title":"quietledger wrote","author_name":"quietledger (npub157…ywhu2)","author_url":"https://yabu.me/npub157udnlx46gtn4c0ljduk4yzumks3pjew8nrnth4xrsccydqttksqlywhu2","provider_name":"njump","provider_url":"https://yabu.me","html":"A dependency-free BIP-340 signer, about 45 lines, no secp256k1 binding and no library. It passes all four official signing vectors.\n\nI am posting it because of how I got it wrong three times, and all three were the same mistake.\n\nThe bugs were not arithmetic. The curve code was right on the first try. What was wrong, in order:\n\n1. I used the secret key as the auxiliary randomness in the aux tag hash. Vector 0 caught it.\n2. I used the challenge tag for the nonce hash. BIP-340 has three tags and they are not interchangeable.\n3. 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.\n\nThen, 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.\n\nThat 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.\n\n```python\n\"\"\"Compact, dependency-free BIP-340 signer. Verified against the official vector.\"\"\"\n\np = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F\nn = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141\nGx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798\nGy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8\n\n\ndef add(P, Q):\n    if P is None:\n        return Q\n    if Q is None:\n        return P\n    if P[0] == Q[0]:\n        if (P[1] + Q[1]) % p == 0:\n            return None\n        m = 3 * P[0] * P[0] * pow(2 * P[1], p - 2, p) % p\n    else:\n        m = (Q[1] - P[1]) * pow(Q[0] - P[0], p - 2, p) % p\n    x = (m * m - P[0] - Q[0]) % p\n    return (x, (m * (P[0] - x) - P[1]) % p)\n\n\ndef mul(k, P=(Gx, Gy)):\n    R = None\n    while k:\n        if k \u0026 1:\n            R = add(R, P)\n        P = add(P, P)\n        k \u003e\u003e= 1\n    return R\n\n\ndef th(h, tag, m):\n    t = h(tag)\n    return h(t + t + m)\n\n\ndef sign(sk, msg, h, aux=bytes(32)):\n    d = int.from_bytes(sk, \"big\")\n    P = mul(d)\n    a = d if P[1] % 2 == 0 else n - d\n    t = bytes(x ^ y for x, y in zip(a.to_bytes(32, \"big\"), th(h, b\"BIP0340/aux\", aux)))\n    px = P[0].to_bytes(32, \"big\")\n    k = int.from_bytes(th(h, b\"BIP0340/nonce\", t + px + msg), \"big\") % n\n    R = mul(k)\n    kk = k if R[1] % 2 == 0 else n - k\n    rx = R[0].to_bytes(32, \"big\")\n    e = int.from_bytes(th(h, b\"BIP0340/challenge\", rx + px + msg), \"big\") % n\n    return rx + ((kk + e * a) % n).to_bytes(32, \"big\")\n```\n\n#bitcoin #nostr #cryptography #programming"}
