python 的 ecdsa 库出现问题
Having troubles with the ecdsa library for python
我想测试 Andreas Antonopolous 的《精通比特币》一书中的椭圆曲线代码(从私钥派生出 public 密钥)是否可行...所以我复制并粘贴了代码在书中给我的 VSC 编辑器。它提示了一些错误,使我删除了参数末尾的“L”并在打印语句后插入了必要的括号。现在我仍然遇到 "unable to import ecdsa.util"...
等错误
下面是我正在使用的代码,附件是它显示的错误的快照:
import ecdsa
import random
from ecdsa.util import string_to_number, number_to_string
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
_b = 0x0000000000000000000000000000000000000000000000000000000000000007
_a = 0x0000000000000000000000000000000000000000000000000000000000000000
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)
generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r)
oid_secp256k1 = (1, 3, 132, 0, 10)
SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1,
oid_secp256k1)
ec_order = _r
curve = curve_secp256k1
generator = generator_secp256k1
def random_secret():
random_char = lambda: chr(random.randint(0, 255))
convert_to_int = lambda array: int("".join(array).encode("hex"), 16)
byte_array = [random_char() for i in range(32)]
return convert_to_int(byte_array)
def get_point_pubkey(point):
if point.y() & 1:
key = '03' + '%064x' % point.x()
else:
key = '02' + '%064x' % point.x()
return key.decode('hex')
def get_point_pubkey_uncompressed(point):
key = '04' + \
'%064x' % point.x() + \
'%064x' % point.y()
return key.decode('hex')
secret = random_secret()
print ("Secret: ", secret)
point = secret * generator
print ("EC point:", point)
print ("BTC public key:", get_point_pubkey(point).encode("hex"))
point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)
assert point1 == point
因为掌握比特币的python代码基于python2
python3 更改了字符串编码和解码。
这是正确的激活码python3
import ecdsa
import os
import binascii
from ecdsa.util import string_to_number, number_to_string
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
_b = 0x0000000000000000000000000000000000000000000000000000000000000007
_a = 0x0000000000000000000000000000000000000000000000000000000000000000
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
_Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)
generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r)
oid_secp256k1 = (1, 3, 132, 0, 10)
SECP256k1 = ecdsa.curves.Curve(
"SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1
)
ec_order = _r
curve = curve_secp256k1
generator = generator_secp256k1
def random_secret():
convert_to_int = lambda array: int(binascii.hexlify(array), 16)
byte_array = os.urandom(32)
return convert_to_int(byte_array)
def get_point_pubkey(point):
if point.y() & 1:
key = "03" + "%064x" % point.x()
else:
key = "02" + "%064x" % point.x()
return binascii.unhexlify(key)
def get_point_pubkey_uncompressed(point):
key = "04" + "%064x" % point.x() + "%064x" % point.y()
return binascii.unhexlify(key)
secret = random_secret()
print("Secret: ", secret)
point = secret * generator
print("EC point: ", point)
print("BTC public key: ", binascii.hexlify(get_point_pubkey(point)))
point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)
assert point1 == point
结果是这样
Secret: 45144777120122405140812796301557992803916792041985925168483786699489296135626
EC point: (97022596039543818601440182171533278426801313043337446429800654678890979695199,53586405348987121115764110810505844890885198959743315511392863714433787991327)
BTC public key: b'03d680d7e3daf984534507c6ddd7d7cfa07864daf6b78d5b52bf657ecb636a9a5f'
我想测试 Andreas Antonopolous 的《精通比特币》一书中的椭圆曲线代码(从私钥派生出 public 密钥)是否可行...所以我复制并粘贴了代码在书中给我的 VSC 编辑器。它提示了一些错误,使我删除了参数末尾的“L”并在打印语句后插入了必要的括号。现在我仍然遇到 "unable to import ecdsa.util"...
等错误下面是我正在使用的代码,附件是它显示的错误的快照:
import ecdsa
import random
from ecdsa.util import string_to_number, number_to_string
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
_b = 0x0000000000000000000000000000000000000000000000000000000000000007
_a = 0x0000000000000000000000000000000000000000000000000000000000000000
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)
generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r)
oid_secp256k1 = (1, 3, 132, 0, 10)
SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1,
oid_secp256k1)
ec_order = _r
curve = curve_secp256k1
generator = generator_secp256k1
def random_secret():
random_char = lambda: chr(random.randint(0, 255))
convert_to_int = lambda array: int("".join(array).encode("hex"), 16)
byte_array = [random_char() for i in range(32)]
return convert_to_int(byte_array)
def get_point_pubkey(point):
if point.y() & 1:
key = '03' + '%064x' % point.x()
else:
key = '02' + '%064x' % point.x()
return key.decode('hex')
def get_point_pubkey_uncompressed(point):
key = '04' + \
'%064x' % point.x() + \
'%064x' % point.y()
return key.decode('hex')
secret = random_secret()
print ("Secret: ", secret)
point = secret * generator
print ("EC point:", point)
print ("BTC public key:", get_point_pubkey(point).encode("hex"))
point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)
assert point1 == point
因为掌握比特币的python代码基于python2 python3 更改了字符串编码和解码。
这是正确的激活码python3
import ecdsa
import os
import binascii
from ecdsa.util import string_to_number, number_to_string
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
_b = 0x0000000000000000000000000000000000000000000000000000000000000007
_a = 0x0000000000000000000000000000000000000000000000000000000000000000
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
_Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)
generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r)
oid_secp256k1 = (1, 3, 132, 0, 10)
SECP256k1 = ecdsa.curves.Curve(
"SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1
)
ec_order = _r
curve = curve_secp256k1
generator = generator_secp256k1
def random_secret():
convert_to_int = lambda array: int(binascii.hexlify(array), 16)
byte_array = os.urandom(32)
return convert_to_int(byte_array)
def get_point_pubkey(point):
if point.y() & 1:
key = "03" + "%064x" % point.x()
else:
key = "02" + "%064x" % point.x()
return binascii.unhexlify(key)
def get_point_pubkey_uncompressed(point):
key = "04" + "%064x" % point.x() + "%064x" % point.y()
return binascii.unhexlify(key)
secret = random_secret()
print("Secret: ", secret)
point = secret * generator
print("EC point: ", point)
print("BTC public key: ", binascii.hexlify(get_point_pubkey(point)))
point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)
assert point1 == point
结果是这样
Secret: 45144777120122405140812796301557992803916792041985925168483786699489296135626
EC point: (97022596039543818601440182171533278426801313043337446429800654678890979695199,53586405348987121115764110810505844890885198959743315511392863714433787991327)
BTC public key: b'03d680d7e3daf984534507c6ddd7d7cfa07864daf6b78d5b52bf657ecb636a9a5f'