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
# From public key to address | |
# Reference: https://medium.freecodecamp.org/how-to-create-a-bitcoin-wallet-address-from-a-private-key-eca3ddd9c05f | |
# https://docs.python.org/2/library/hashlib.html | |
import codecs #If not installed: "pip3 install codecs" | |
import hashlib | |
# UK0 is a demo public key. | |
UK0 = ['3a56bd64573c28050bfe202c57e56b46c63744a253d1430e2b737876fa883b19','73c2f565444dc62151562993ff4b566c826010befb289fa2fc749293266066c0'] | |
UK1 = "04" + UK0[0] + UK0[1] | |
UK2 = hashlib.sha256(codecs.decode(UK1, 'hex')) | |
h = hashlib.new('ripemd160') |
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
# From private key(hex) to Wallet Import Format(WIF) | |
# Reference: https://medium.freecodecamp.org/how-to-create-a-bitcoin-wallet-address-from-a-private-key-eca3ddd9c05f | |
# https://docs.python.org/2/library/hashlib.html | |
import codecs #If not installed: "pip3 install codecs" | |
import hashlib | |
# PK0 is a demo private key. | |
PK0 = "841846de7afbe32ee7ded837872c6e0825db095275b8afed0000000000000000" | |
PK1 = '80'+ PK0 | |
PK2 = hashlib.sha256(codecs.decode(PK1, 'hex')) | |
PK3 = hashlib.sha256(PK2.digest()) |
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
#These are Bitcoin parameters. See [Recommended Elliptic Curve Domain Parameters: page 15](http://www.secg.org/SEC2-Ver-1.0.pdf). | |
a = 0; b = 7 # Define a elliptic curve. y**2 = a*x**3 + b*x | |
P = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 -1 # A prime. | |
x1 = int("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",16) | |
y1 = int("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8",16) | |
G = (x1,y1) # Base point | |
N = int("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",16) # The order of the base point which is equal to the order of the curve in this case. | |
# Note: A and P must share no factor greater than 1. | |
def modular_inverse(A,P): |
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
# a,b define a elliptic curve. y**2 = a*x**3 + b*x | |
# G(x1,y1) is the base point. | |
# P is a large prime. N is the order of the base point and usually equals to the order of the curve. | |
def ECMultiplication(G,a,b,P,N,privateKey): | |
#if privateKey < 1 or privateKey >= N: raise Exception("ECMultiplication(G,a,b,P,privateKey), privateKey should >0 and <N.") | |
n_binary = str(bin(privateKey))[2:] | |
D = G | |
for i in range (1, len(n_binary)): | |
D = ECdouble(D,a,b,P) |
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
# Predfined parameters (x1,y1,x2,y2 are integers) | |
A = (x1,y1); B = (x2,y2) | |
# Function | |
def ECadd(A,B): | |
lambda_mod = (B[1]-A[1])% P * modular_inverse(B[0]-A[0], P) | |
x3 = (lambda_mod * lambda_mod - A[0] - B[0]) % P | |
y3 = (lambda_mod * (A[0] - x3) - A[1]) % P | |
return (x3,y3) |
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
# An example of predfined parameters | |
a = 1; b = 1 | |
P = 23 | |
x1 = 0; y1 = 1 | |
G = (x1,y1) | |
# Function | |
def ECdouble(G,a,b,P): # G = (x1,y1) where x1,y1 are integers. | |
lambda_mod = (3*G[0]** 2 + a)% P * modular_inverse(2*G[1],P) | |
x3 = (lambda_mod * lambda_mod - G[0] - G[0]) % P |
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
# Note: A and P must share no factor greater than 1. | |
def modular_inverse(A,P): | |
p0, a0 = 1, 0 | |
p1, a1 = 0, 1 | |
R0, R1 = P, A%P | |
while R1 != 1 and R1 != 0: | |
n, R2 = divmod(R0, R1) | |
p2 = p0 - n*p1 ; a2 = a0 - n*a1 | |
p0 = p1; a0 = a1; p1 = p2; a1 = a2 | |
R0 = R1; R1 = R2 |