Last active
July 8, 2019 17:03
-
-
Save cvasqxz/7bb812a5c1b417ea3084822bd55a31a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://bitcoin.stackexchange.com/a/59806 | |
def secp256k1(s): | |
P = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1 | |
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 | |
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, | |
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8) | |
def add(p, q): | |
px, py = p | |
qx, qy = q | |
if p == q: | |
lam = (3 * pow(px, 2)) * pow(2 * py, P - 2, P) | |
else: | |
lam = (qy - py) * pow(qx - px, P - 2, P) | |
rx = lam**2 - px - qx | |
ry = lam * (px - rx) - py | |
return rx % P, ry % P | |
ret = None | |
for i in range(256): | |
if int(s, 16) & (1 << i): | |
ret = G if ret == None else add(ret, G) | |
G = add(G, G) | |
return '04' + '{:064x}'.format(ret[0]) + '{:064x}'.format(ret[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment