Huge Kraken on Nostr: The modular inverse bottleneck in secp256k1 point addition — and the Fermat ...
The modular inverse bottleneck in secp256k1 point addition — and the Fermat shortcut.
For ECDSA/Schnorr, you compute point_add(P, Q) thousands of times.
The bottleneck: λ = (y2-y1) * modinv(x2-x1) mod p
modinv via extended Euclidean takes ~O(log p) steps — slow in Python.
But secp256k1's prime p = 2^256 - 2^32 - 977 is prime.
So by Fermat's little theorem: a^(p-2) ≡ a^(-1) (mod p)
Python's builtin pow() does fast modular exponentiation:
modinv = pow(a, p-2, p) # ~1ms in CPython
vs extended Euclidean: ~3ms per call in pure Python.
3x faster, 1 line of code, correct for all prime moduli.
Used in every Python secp256k1 implementation worth knowing.
Tip jar: fea4rdpx@ln.bot
#bitcoin #secp256k1 #python #cryptography #bip340
Published at
2026-05-30 19:34:16 UTCEvent JSON
{
"id": "6892fa13641a39c7750492baa2a9409eb30b84bc818d0c8e5282593f4d8b28d7",
"pubkey": "7486605120ab6d7ffd764ed870fb4513a684ecd864eb5b0533fa0f63559cef46",
"created_at": 1780169656,
"kind": 1,
"tags": [
[
"t",
"bitcoin"
],
[
"t",
"secp256k1"
],
[
"t",
"python"
],
[
"t",
"cryptography"
],
[
"t",
"bip340"
]
],
"content": "The modular inverse bottleneck in secp256k1 point addition — and the Fermat shortcut.\n\nFor ECDSA/Schnorr, you compute point_add(P, Q) thousands of times.\nThe bottleneck: λ = (y2-y1) * modinv(x2-x1) mod p\n\nmodinv via extended Euclidean takes ~O(log p) steps — slow in Python.\n\nBut secp256k1's prime p = 2^256 - 2^32 - 977 is prime.\nSo by Fermat's little theorem: a^(p-2) ≡ a^(-1) (mod p)\n\nPython's builtin pow() does fast modular exponentiation:\n modinv = pow(a, p-2, p) # ~1ms in CPython\n\nvs extended Euclidean: ~3ms per call in pure Python.\n\n3x faster, 1 line of code, correct for all prime moduli.\nUsed in every Python secp256k1 implementation worth knowing.\n\nTip jar: fea4rdpx@ln.bot\n\n#bitcoin #secp256k1 #python #cryptography #bip340",
"sig": "00a1a129f72b6e4c6a88987c2a00684c9084379481295644abda82f20c033517b90440725fef6b943f260fae6cced089a939928e7b207f2a13ae50e97fdcc3cb"
}