-
-
Save tatdig/288c4fedb0e3200f3a7aa9b8a3984349 to your computer and use it in GitHub Desktop.
create Bitcoin public key from private key
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
#! /usr/bin/env python | |
class Point(object): | |
def __init__(self, _x, _y, _order = None): self.x, self.y, self.order = _x, _y, _order | |
def calc(self, top, bottom, other_x): | |
l = (top * inverse_mod(bottom)) % p | |
x3 = (l * l - self.x - other_x) % p | |
return Point(x3, (l * (self.x - x3) - self.y) % p) | |
def double(self): | |
if self == INFINITY: return INFINITY | |
return self.calc(3 * self.x * self.x, 2 * self.y, self.x) | |
def __add__(self, other): | |
if other == INFINITY: return self | |
if self == INFINITY: return other | |
if self.x == other.x: | |
if (self.y + other.y) % p == 0: return INFINITY | |
return self.double() | |
return self.calc(other.y - self.y, other.x - self.x, other.x) | |
def __mul__(self, e): | |
if self.order: e %= self.order | |
if e == 0 or self == INFINITY: return INFINITY | |
result, q = INFINITY, self | |
while e: | |
if e&1: result += q | |
e, q = e >> 1, q.double() | |
return result | |
def __str__(self): | |
if self == INFINITY: return "infinity" | |
return "04 %x %x" % (self.x, self.y) | |
def inverse_mod(a): | |
if a < 0 or a >= p: a = a % p | |
c, d, uc, vc, ud, vd = a, p, 1, 0, 0, 1 | |
while c: | |
q, c, d = divmod(d, c) + (c,) | |
uc, vc, ud, vd = ud - q*uc, vd - q*vc, uc, vc | |
if ud > 0: return ud | |
return ud + p | |
p, INFINITY = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL, Point(None, None) # secp256k1 | |
g = Point(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L, 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L, | |
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L) | |
secret = 0x2EE42A735AE3D0C1A7E435EF3B4731B0205A7839015E100BCC8472EE989EC887L | |
print ' privkey: %x\n pubkey: %s' % (secret, g * secret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment