Skip to content

Instantly share code, notes, and snippets.

@notcod
Last active October 22, 2024 05:38
Show Gist options
  • Select an option

  • Save notcod/b30de1f48edfbad9b5d2fda1f50e722b to your computer and use it in GitHub Desktop.

Select an option

Save notcod/b30de1f48edfbad9b5d2fda1f50e722b to your computer and use it in GitHub Desktop.
def secp256k1(z):
z = str(bin(z))[2:]
l = len(z)
p = 115792089237316195423570985008687907853269984665640564039457584007908834671663
x = Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
y = Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
for i in range(1, l):
lm, hm = 1, 0
lw, hi = 2*y % p, p
while lw > 1:
r = hi//lw
nm, nw = hm-lm*r, hi-lw*r
lm, lw, hm, hi = nm, nw, lm, lw
R = lm % p
o = 3*x*x*R % p
u = o*o-2*x
y = (o*(x-u % p)-y) % p
x = u % p
if z[i] == "1":
lm, hm = 1, 0
lw, hi = (Gx - x) % p, p
while lw > 1:
r = hi//lw
nm, nw = hm-lm*r, hi-lw*r
lm, lw, hm, hi = nm, nw, lm, lw
R = lm % p
o = (Gy-y)*R % p
u = o*o-x-Gx
y = (o*(x-u % p)-y) % p
x = u % p
return (x,y)
print(secp256k1(32592575621351777380295131014550050576823494298654980010178247371840571660671))
#OUTPUT: (94203404061421058009312340410868627152452458145746879189182041812498811783078, 42201476396565815024119219966581192970316115428508342197075279408811375234858)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment