"""Minimal BIP-340 Schnorr signing + Nostr event builder for tests. Pure Python, no external dependencies beyond stdlib. """ import hashlib import json import os import time # secp256k1 curve parameters. _P = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F _N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 _G = ( 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8, ) def _modinv(a, m): g, x, _ = _egcd(a % m, m) return x % m def _egcd(a, b): if a == 0: return b, 0, 1 g, x, y = _egcd(b % a, a) return g, y - (b // a) * x, x def _point_add(p1, p2): """Affine addition; one modular inverse. Kept for callers/tests.""" if p1 is None: return p2 if p2 is None: return p1 if p1[0] == p2[0] and p1[1] != p2[1]: return None if p1 == p2: lam = (3 * p1[0] * p1[0] * _modinv(2 * p1[1], _P)) % _P else: lam = ((p2[1] - p1[1]) * _modinv(p2[0] - p1[0], _P)) % _P x = (lam * lam - p1[0] - p2[0]) % _P y = (lam * (p1[0] - x) - p1[1]) % _P return (x, y) def _jac_double(p): """Double a Jacobian point (X, Y, Z); a = 0 curve, so no a*Z^4 term.""" x, y, z = p if y == 0 or z == 0: return (0, 1, 0) yy = (y * y) % _P s = (4 * x * yy) % _P m = (3 * x * x) % _P nx = (m * m - 2 * s) % _P ny = (m * (s - nx) - 8 * yy * yy) % _P nz = (2 * y * z) % _P return (nx, ny, nz) def _jac_add(p1, p2): """Add two Jacobian points; no inverse.""" x1, y1, z1 = p1 x2, y2, z2 = p2 if z1 == 0: return p2 if z2 == 0: return p1 z1z1 = (z1 * z1) % _P z2z2 = (z2 * z2) % _P u1 = (x1 * z2z2) % _P u2 = (x2 * z1z1) % _P s1 = (y1 * z2z2 * z2) % _P s2 = (y2 * z1z1 * z1) % _P if u1 == u2: if s1 != s2: return (0, 1, 0) return _jac_double(p1) h = (u2 - u1) % _P r = (s2 - s1) % _P hh = (h * h) % _P hhh = (h * hh) % _P v = (u1 * hh) % _P nx = (r * r - hhh - 2 * v) % _P ny = (r * (v - nx) - s1 * hhh) % _P nz = (h * z1 * z2) % _P return (nx, ny, nz) def _jac_to_affine(p): x, y, z = p if z == 0: return None zi = _modinv(z, _P) zi2 = (zi * zi) % _P return ((x * zi2) % _P, (y * zi2 * zi) % _P) def _point_mul(k, point): """k*point. Jacobian ladder: one modular inverse for the whole multiplication instead of one per bit. The affine version cost ~65ms per signature, which made the high-volume relay tests spend 130s signing 2000 events before the relay saw any of them. """ k %= _N if k == 0 or point is None: return None r = (0, 1, 0) a = (point[0], point[1], 1) while k: if k & 1: r = _jac_add(r, a) a = _jac_double(a) k >>= 1 return _jac_to_affine(r) def _tagged_hash(tag, msg): t = hashlib.sha256(tag.encode()).digest() return hashlib.sha256(t + t + msg).digest() def _int_from_bytes(b): return int.from_bytes(b, "big") def _bytes_from_int(x): return x.to_bytes(32, "big") def pubkey_from_seckey(seckey: bytes) -> bytes: pt = _point_mul(_int_from_bytes(seckey), _G) return _bytes_from_int(pt[0]) def schnorr_sign(msg: bytes, seckey: bytes) -> bytes: d = _int_from_bytes(seckey) pt = _point_mul(d, _G) if pt[1] % 2 != 0: d = _N - d aux = os.urandom(32) t = bytes( a ^ b for a, b in zip(_bytes_from_int(d), _tagged_hash("BIP0340/aux", aux)) ) rand = _tagged_hash("BIP0340/nonce", t + _bytes_from_int(pt[0]) + msg) k = _int_from_bytes(rand) % _N if k == 0: raise ValueError("bad nonce") r = _point_mul(k, _G) if r[1] % 2 != 0: k = _N - k e = ( _int_from_bytes( _tagged_hash( "BIP0340/challenge", _bytes_from_int(r[0]) + _bytes_from_int(pt[0]) + msg, ) ) % _N ) return _bytes_from_int(r[0]) + _bytes_from_int((k + e * d) % _N) def make_event(seckey: bytes, content: str, kind=1, tags=None, created_at=None): """Create a fully signed Nostr event.""" if tags is None: tags = [] if created_at is None: created_at = int(time.time()) pubkey = pubkey_from_seckey(seckey).hex() serial = json.dumps( [0, pubkey, created_at, kind, tags, content], separators=(",", ":") ) event_id = hashlib.sha256(serial.encode()).digest() sig = schnorr_sign(event_id, seckey) return { "id": event_id.hex(), "pubkey": pubkey, "created_at": created_at, "kind": kind, "tags": tags, "content": content, "sig": sig.hex(), } # Fixed test keypair — deterministic for reproducible tests. TEST_SECKEY = hashlib.sha256(b"smesh-test-key-do-not-use").digest() TEST_PUBKEY = pubkey_from_seckey(TEST_SECKEY).hex()