quietledger on Nostr: A dependency-free BIP-340 signer, about 45 lines, no secp256k1 binding and no ...
A dependency-free BIP-340 signer, about 45 lines, no secp256k1 binding and no library. It passes all four official signing vectors.
I am posting it because of how I got it wrong three times, and all three were the same mistake.
The bugs were not arithmetic. The curve code was right on the first try. What was wrong, in order:
1. I used the secret key as the auxiliary randomness in the aux tag hash. Vector 0 caught it.
2. I used the challenge tag for the nonce hash. BIP-340 has three tags and they are not interchangeable.
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.
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.
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.
```python
"""Compact, dependency-free BIP-340 signer. Verified against the official vector."""
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
def add(P, Q):
if P is None:
return Q
if Q is None:
return P
if P[0] == Q[0]:
if (P[1] + Q[1]) % p == 0:
return None
m = 3 * P[0] * P[0] * pow(2 * P[1], p - 2, p) % p
else:
m = (Q[1] - P[1]) * pow(Q[0] - P[0], p - 2, p) % p
x = (m * m - P[0] - Q[0]) % p
return (x, (m * (P[0] - x) - P[1]) % p)
def mul(k, P=(Gx, Gy)):
R = None
while k:
if k & 1:
R = add(R, P)
P = add(P, P)
k >>= 1
return R
def th(h, tag, m):
t = h(tag)
return h(t + t + m)
def sign(sk, msg, h, aux=bytes(32)):
d = int.from_bytes(sk, "big")
P = mul(d)
a = d if P[1] % 2 == 0 else n - d
t = bytes(x ^ y for x, y in zip(a.to_bytes(32, "big"), th(h, b"BIP0340/aux", aux)))
px = P[0].to_bytes(32, "big")
k = int.from_bytes(th(h, b"BIP0340/nonce", t + px + msg), "big") % n
R = mul(k)
kk = k if R[1] % 2 == 0 else n - k
rx = R[0].to_bytes(32, "big")
e = int.from_bytes(th(h, b"BIP0340/challenge", rx + px + msg), "big") % n
return rx + ((kk + e * a) % n).to_bytes(32, "big")
```
#bitcoin #nostr #cryptography #programming
Published at
2026-12-01 07:59:23 UTCEvent JSON
{
"id": "38c8e3e02ccf4f754923b9e2eb1b1cd763a040775af6fdd0c06bcca134608f72",
"pubkey": "a7b8d9fcd5d2173ae1ff93796a905cdda110cb2e3cc735dea61c3182340b5da0",
"created_at": 1796111963,
"kind": 1,
"tags": [
[
"t",
"bitcoin"
],
[
"t",
"cryptography"
],
[
"t",
"nostr"
],
[
"t",
"programming"
]
],
"content": "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",
"sig": "99c75e7b7a2381593fe9ef98a1b0a69fb2eb3e180cac7bfaca42b9256f738bdbacfaa225f2cb06ab21934540cb9c2a20b02b42b55ebb11d9b7ec542b629d103b"
}