比特币secp256k1,私钥转公钥详解。用python语言编写。
这里是白币私钥:
WsBLkkXYyjXo4JsJULLQGS76py36yAJVhkNuBLu7Jy1622KNgrVW
免费赠送,先到先得。
1.相关环境
本人测试环境:win10 64位
开发环境:VS2015
python版本:3.5.2
2.代码示例:
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#!/usr/bin/python
#coding=gbk
import math
def GetSqrtRange(num):#求开方
a = 1
b = num - 1
while(1):
c = (a + b) // 2
if(c ** 2 > num):
b =
c
if(a ** 2
== num):
return (a,a)
if(a + 1
== b):
return (a,b)
elif (c ** 2 < num):
a =
c
if(b ** 2
== num):
return(b,b)
if(a + 1
== b):
return(a,b)
else:
return
(c,c)
def quick_algorithm(a,b,c): #求乘法逆元
a = a % c
ans = 1
#这里我们不需要考虑b<0,因为分数没有取模运算
while b != 0:
#费马小定理
if b & 1:
ans = (ans
* a) % c
b>>=1
a = (a * a) % c
return ans
def P_Q(P,Q):#计算P+Q P和Q不能相等
if True:
p =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
ret = [0 ,0]
k = (P[1] - Q[1]) * quick_algorithm(P[0] -
Q[0],p - 2,p) % p#P+Q 类似P点和Q和两个点的斜率
ret[0] = (k * k - P[0] - Q[0]) % p#P+Q
ret[1] = (k * (P[0] - ret[0]) - P[1]) %
p#P+Q
return ret;
#k = ((3 * yuanzu1[0] * yuanzu1[0]) *
quick_algorithm(2 * yuanzu1[1],CL
#- 2,CL)) % CL#P+P
#yuanzu3[0] = (k * k - 2 * yuanzu1[0]) %
CL#P+P
#yuanzu3[1] = (k * (yuanzu1[0] - yuanzu3[0]) -
yuanzu1[1]) % CL#P+P
def P_P(P):#计算2P
if True:
p =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
ret = [0 ,0]
k = ((3 * P[0] * P[0]) * quick_algorithm(2 *
P[1],p
- 2,p)) % p#P+P 类似P点切线斜率
ret[0] = (k * k - 2 * P[0]) % p#P+P
ret[1] = (k * (P[0] - ret[0]) - P[1]) %
p#P+P
return ret;
def GetKByPrivateNum(privatenum):#根据随机数(私钥)求坐标(公钥)
n=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
if (privatenum<=0 or
privatenum>=n):
return (0,0)
p=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
G =
[0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8]#1
if(privatenum==1):
return G;
ret = [0,0]
while privatenum != 0:
if privatenum & 1:
if(ret[0]==0 and ret[1]==0):
ret=G
else:
ret=P_Q(ret,G)
privatenum>>=1
G=P_P(G)
return ret
if __name__ == "__main__":
#E : y^2 ≡ x^3 + ax + b
(mod p)
#T = (p, a, b, G, n,
h)
#p=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
#代表有限域Fp的那个质数
#G =04
#79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
#483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
#a=0,b=7:椭圆方程的参数
#G: 椭圆曲线上的一个基点G = (xG,
yG)
#n=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141:G在Fp中规定的序号,一个质数。私钥范围(0,n)
#h=01:余因数(cofactor),控制选取点的密度。h = #E(Fp) / n。
if True:
yuanzu1 =
[0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8]#私钥是1
#yuanzu1
=[0xC6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5,0x1AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A]#私钥是2
yuanzu2 =
[0xC6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5,0x1AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A]#私钥是2
#yuanzu2
=[0x97DE6003A550284732D1C7FA7DB3083BA4245FAAD278CEC04FAEA50454211E63,0xE250F63A1B3F2710A409AF09A04F8C8207C3AB50E81C3CD62F25C1EB8D86D3F5]#私钥是00000000000000000000000000000000fffffffffffffffffffffffffffffffe
#yuanzu2 =
[0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,0xB7C52588D95C3B9AA25B0403F1EEF75702E84BB7597AABE663B82F6F04EF2777]#私钥是最大值
print("P+Q
P和Q不能相同-------------------------------------------------------1+2")
ret = P_Q(yuanzu1,yuanzu2)#测试P+Q,P和Q不能相同
print("x = "+("%x" % (ret[0])))
print("y = "+("%x" % (ret[1])))
print("私钥是:"+("%x" % 3))
print("公钥是:04"+("%x" % (ret[0])).zfill(64)+("%x"
% (ret[1])).zfill(64))
x = ret[0]
y = ret[1]
p =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
zuo = (y * y) % p
you = (x * x * x + 7) % p
print(zuo)
print(you)
print("2P----------------------------------------------------1+1")
ret = P_P(yuanzu1)#测试2P
print("x = "+("%x" % (ret[0])))
print("y = "+("%x" % (ret[1])))
print("私钥是:"+("%x" % 2))
print("公钥是:04"+("%x" % (ret[0])).zfill(64)+("%x"
% (ret[1])).zfill(64))
x = ret[0]
y = ret[1]
p =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
zuo = (y * y) % p
you = (x * x * x + 7) % p
print(zuo)
print(you)
print("private to public
-----------------------------------------------------私钥最大值")
ret =
GetKByPrivateNum(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140)
print("x = "+("%x" % (ret[0])))
print("y = "+("%x" % (ret[1])))
print("私钥是:"+("%x" %
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140))
print("公钥是:04"+("%x" % (ret[0])).zfill(64)+("%x"
% (ret[1])).zfill(64))
x = ret[0]
y = ret[1]
p =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
zuo = (y * y) % p
you = (x * x * x + 7) % p
print(zuo)
print(you)
input()
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
3.测试效果:
如果觉得有用,就打赏点虚拟币给我,谢谢。
白币(XWC)打赏地址:WYkBw4qveiFGgkCJbKmz55SAK79qJwGrQv
比特币(BTC)打赏地址:1KckCJAiMnwbQLSSSWY7uJBfcgvrMsMJiZ
无限币(IFC)打赏地址:i66xPVPgZXWuAPPMbqh4q1fhJABUFwHi9q
狗狗币(DOGE)打赏地址:DJBF5UwQwuPJj72XsQi62cwDooyPLY1i2e
加载中,请稍候......