sry for necro-up, but this topic is consistent( search tag: python, btc, bitcoin , ecc , ecdsa , secp256k1, lib , library, compare )Comparing bitcoin libraries, generation speed of rand privkey to pubkey
(maybe someone will need)
Python37
C:\Python37>python.exe _benchmark_bitcoin_lib.py
[timeit] 10 loops, 1.14888 sec ecdsa
[timeit] 10 loops, 0.613698 sec pycoin
[timeit] 10 loops, 0.0630259 sec starkbank-ecdsa
[timeit] 10 loops, 0.0613364 sec pybitcointools
[timeit] 10 loops, 0.0381225 sec fastecdsa
[timeit] 10 loops, 0.0427408 sec python-bitcoinlib
[timeit] 10 loops, 0.0214886 sec openssl
[timeit] 10 loops, 0.000839192 sec coincurve
Python27
C:\Python27>python.exe _benchmark_bitcoin_lib.py
[timeit] 10 loops, 1.33738 sec ecdsa
[timeit] 10 loops, 0.660795 sec pycoin
[timeit] 10 loops, 0.00585777 sec starkbank-ecdsa
[timeit] 10 loops, 0.0984026 sec pybitcointools
[timeit] 10 loops, 0.0389676 sec python-bitcoinlib
[timeit] 10 loops, 0.0010109 sec coincurve
in short:
- coincurve lib the undisputed leader (
https://github.com/ofek/coincurve)
- starkbank anomal x10 faster under python2
- pycoin have problem mode openssl (i dont only for me local or global)
for python3
#!/usr/bin/env python3
#####################
import timeit
rounds = 10
import random
randrange = random.SystemRandom().randrange
#####################
# ecdsa
# (use python)
# python -m pip install ecdsa==0.13
def test_ecdsa(test_name = 'ecdsa', rounds = 10):
loops = rounds
try:
import ecdsa
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from ecdsa.ecdsa import generator_secp256k1, Public_key, Private_key
#from ecdsa.util import string_to_number, number_to_string
g = generator_secp256k1
pubkey = Public_key( g, g * secret ).point
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from ecdsa.ecdsa import generator_secp256k1, Public_key, Private_key; g=generator_secp256k1; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'Public_key(g,g*random.randint(1,n)).point'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# starkbank-ecdsa
# (use python with quick jacobian multiply)
# python -m pip install starkbank-ecdsa==0.1.4
def test_starkbank_ecdsa(test_name = 'starkbank-ecdsa', rounds = 10):
loops = rounds
try:
import ellipticcurve
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from ellipticcurve.privateKey import PrivateKey
pubkey = PrivateKey.fromString(secret).publicKey().toString()
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from ellipticcurve.privateKey import PrivateKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret = random.randint(1,n); secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big"); PrivateKey.fromString(secret).publicKey().toString();'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# bitcoin (pybitcointools)
# (use python with quick jacobian multiply)
# (Author: Vitalik Buterin)
# python -m pip install bitcoin==1.1.42
# python -m pip install pybitcointools==1.1.15
def test_pybitcointools(test_name = 'pybitcointools', rounds = 10):
loops = rounds
try:
import pybitcointools
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from pybitcointools import fast_multiply, G
pubkey = fast_multiply(G, secret)
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from pybitcointools import fast_multiply, G; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'fast_multiply(G, random.randint(1,n))'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# pycoin
# (use python, use openssl(fail))
# python -m pip install pycoin==0.80
def test_pycoin(test_name = 'pycoin', rounds = 10):
loops = rounds
#if os.getenv("PYCOIN_NATIVE") != "openssl":
try:
import pycoin
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
#from pycoin.key import Key
#pubkey = Key(secret_exponent=secret).public_pair()
#print('[pubkey] {0}'.format(pubkey));exit(0);
from pycoin.ecdsa import public_pair_for_secret_exponent, generator_secp256k1
pubkey = public_pair_for_secret_exponent(generator_secp256k1, secret)
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from pycoin.ecdsa import public_pair_for_secret_exponent, generator_secp256k1; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'public_pair_for_secret_exponent(generator_secp256k1, random.randint(1,n))'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# bit
# (use coincurve)
# python -m pip install bit==0.4.3
#####################
# pybitcoin
# (use pybitcointools + use ecdsa)
# python -m pip install pybitcoin==0.9.9
#####################
# python-bitcoin
# (use ecdsa)
# python -m pip install python-bitcoin==0.0.10
#####################
# bitcoinlib
# (use ecdsa)
# python -m pip install bitcoinlib==0.4.4
#####################
# python-bitcoinlib
# (use openssl)
# python -m pip install python-bitcoinlib==0.10.1
# (inst path to PythonXX/Lib/site-packages/bitcoin , its conflict with pybitcointools)
def test_python_bitcoinlib(test_name = 'python-bitcoinlib', rounds = 10):
loops = rounds
try:
import bitcoin
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from bitcoin.wallet import CKey
pubkey = CKey(secret,compressed=False).pub
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from bitcoin.wallet import CKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret = random.randint(1,n);secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big");CKey(secret,compressed=False).pub'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
from bitcoin.core.key import CECKey, CPubKey
pubkey = CECKey()
pubkey.set_secretbytes(secret)
pubkey.set_compressed(compressed=False)
pubkey = CPubKey(pubkey.get_pubkey(), pubkey)
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from bitcoin.core.key import CECKey, CPubKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret = random.randint(1,n);secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big");pubkey = CECKey();pubkey.set_secretbytes(secret);pubkey = CPubKey(pubkey.get_pubkey(), pubkey);'
#print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# py_ecc
# (use python with quick jacobian multiply)
# python -m pip install py_ecc==1.4.7
# (bitcoin_easy)
#####################
# ecc-0.0.1
#####################
# bitcoin-utils
# (use ecdsa)
# python -m pip install bitcoin-utils==0.2.2
#####################
# bitcoin_tools
# (use python)
# python -m pip install bitcoin_tools==0.0.13
#####################
# python_bitcoin_tools
# (use ecdsa)
# python -m pip install python_bitcoin_tools==0.2.3
#####################
#
# (use )
# python -m pip install
#exit(0);
#####################
# openssl
def test_openssl(test_name = 'openssl', rounds = 10):
loops = rounds
try:
import ctypes
ssl_library = ctypes.cdll.LoadLibrary("libeay32.dll")
#ssl_library = ctypes.cdll.LoadLibrary(r"C:\Windows\System32\libeay32.dll")
#ssl_library = ctypes.cdll.LoadLibrary(r"C:\Windows\SysWOW64\libeay32.dll")
#ssl_library = ctypes.cdll.LoadLibrary("libssl.so")
except Exception:
print('[error] import {} failed; need libeay32.dll/.so'.format(test_name,test_name))
return(1)
NID_secp256k1 = 714
def get_public_key1(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
if ssl_library.EC_KEY_generate_key(k) != 1:
raise Exception("internal error")
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_KEY_free(k)
return public_key
def get_public_key2(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
storage = ctypes.create_string_buffer(private_key)
bignum_private_key = ssl_library.BN_new()
ssl_library.BN_bin2bn(storage, 32, bignum_private_key)
group = ssl_library.EC_KEY_get0_group(k)
point = ssl_library.EC_POINT_new(group)
ssl_library.EC_POINT_mul(group, point, bignum_private_key, None, None, None)
ssl_library.EC_KEY_set_private_key(k, bignum_private_key)
ssl_library.EC_KEY_set_public_key(k, point)
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_POINT_free(point)
ssl_library.BN_free(bignum_private_key)
ssl_library.EC_KEY_free(k)
return public_key
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, 'big')
#pubkey = get_public_key1(secret)
pubkey = get_public_key2(secret)
#print('[pubkey] {0}'.format(pubkey.hex()));exit(0);
setup = 'import random; import ctypes; ssl_library = ctypes.cdll.LoadLibrary("libeay32.dll"); n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = '''
if 1:
NID_secp256k1 = 714
def get_public_key1(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
if ssl_library.EC_KEY_generate_key(k) != 1:
raise Exception("internal error")
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_KEY_free(k)
return public_key
def get_public_key2(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
storage = ctypes.create_string_buffer(private_key)
bignum_private_key = ssl_library.BN_new()
ssl_library.BN_bin2bn(storage, 32, bignum_private_key)
group = ssl_library.EC_KEY_get0_group(k)
point = ssl_library.EC_POINT_new(group)
ssl_library.EC_POINT_mul(group, point, bignum_private_key, None, None, None)
ssl_library.EC_KEY_set_private_key(k, bignum_private_key)
ssl_library.EC_KEY_set_public_key(k, point)
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_POINT_free(point)
ssl_library.BN_free(bignum_private_key)
ssl_library.EC_KEY_free(k)
return public_key
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
secret=random.randint(1,n);
secret=secret.to_bytes((secret.bit_length()+7)//8 or 1, "big");
pubkey = get_public_key2(secret);
'''
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# coincurve
# python -m pip install coincurve==9.0.0
# (use libsecp256k1.dll/.so)
def test_coincurve(test_name = 'coincurve', rounds = 10):
loops = rounds
try:
import coincurve
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from coincurve.keys import PrivateKey, PublicKey
pubkey = PublicKey.from_valid_secret(secret).point()
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from coincurve.keys import PrivateKey, PublicKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret=random.randint(1,n); secret=secret.to_bytes((secret.bit_length()+7)//8 or 1, "big"); PublicKey.from_valid_secret(secret).point();'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#stmt = "secret=random.randint(1,n); secret=secret.to_bytes((secret.bit_length()+7)//8 or 1, 'big'); PublicKey.from_valid_secret(secret).format(False);"
#print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# fastecdsa
# python -m pip install fastecdsa==1.6.5
# (use Cython)
def test_fastecdsa(test_name = 'fastecdsa', rounds = 10):
loops = rounds
try:
import fastecdsa
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from fastecdsa import keys, curve
pubkey = keys.get_public_key(secret, curve.P256)
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from fastecdsa import keys, curve; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret=random.randint(1,n); keys.get_public_key(secret, curve.P256);'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
#####################
#####################
test_ecdsa()
test_pycoin()
test_starkbank_ecdsa()
test_pybitcointools()
test_fastecdsa()
test_python_bitcoinlib()
test_openssl()
test_coincurve()
need some fix for python2
#!/usr/bin/env python2
#####################
import timeit
rounds = 10
import random
randrange = random.SystemRandom().randrange
#####################
# ecdsa
# (use python)
# python -m pip install ecdsa==0.13
def test_ecdsa(test_name = 'ecdsa', rounds = 10):
loops = rounds
try:
import ecdsa
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from ecdsa.ecdsa import generator_secp256k1, Public_key, Private_key
#from ecdsa.util import string_to_number, number_to_string
g = generator_secp256k1
pubkey = Public_key( g, g * secret ).point
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from ecdsa.ecdsa import generator_secp256k1, Public_key, Private_key; g=generator_secp256k1; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'Public_key(g,g*random.randint(1,n)).point'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# starkbank-ecdsa
# (use python with quick jacobian multiply)
# python -m pip install starkbank-ecdsa==0.1.4
def test_starkbank_ecdsa(test_name = 'starkbank-ecdsa', rounds = 10):
loops = rounds
try:
import ellipticcurve
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
secret = bytes((secret.bit_length()+7)//8 or 1)
from ellipticcurve.privateKey import PrivateKey
pubkey = PrivateKey.fromString(secret).publicKey().toString()
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from ellipticcurve.privateKey import PrivateKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret = random.randint(1,n); secret = bytes((secret.bit_length()+7)//8 or 1); PrivateKey.fromString(secret).publicKey().toString();'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# bitcoin (pybitcointools)
# (use python with quick jacobian multiply)
# (Author: Vitalik Buterin)
# python -m pip install bitcoin==1.1.42
# python -m pip install pybitcointools==1.1.15
def test_pybitcointools(test_name = 'pybitcointools', rounds = 10):
loops = rounds
try:
import pybitcointools
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
from pybitcointools import fast_multiply, G
pubkey = fast_multiply(G, secret)
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from pybitcointools import fast_multiply, G; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'fast_multiply(G, random.randint(1,n))'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# pycoin
# (use python, use openssl(fail))
# python -m pip install pycoin==0.80
def test_pycoin(test_name = 'pycoin', rounds = 10):
loops = rounds
#if os.getenv("PYCOIN_NATIVE") != "openssl":
try:
import pycoin
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
#secret = secret.to_bytes((secret.bit_length()+7)//8 or 1, "big")
#from pycoin.key import Key
#pubkey = Key(secret_exponent=secret).public_pair()
#print('[pubkey] {0}'.format(pubkey));exit(0);
from pycoin.ecdsa import public_pair_for_secret_exponent, generator_secp256k1
pubkey = public_pair_for_secret_exponent(generator_secp256k1, secret)
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from pycoin.ecdsa import public_pair_for_secret_exponent, generator_secp256k1; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'public_pair_for_secret_exponent(generator_secp256k1, random.randint(1,n))'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# bit
# (use coincurve)
# python -m pip install bit==0.4.3
#####################
# pybitcoin
# (use pybitcointools + use ecdsa)
# python -m pip install pybitcoin==0.9.9
#####################
# python-bitcoin
# (use ecdsa)
# python -m pip install python-bitcoin==0.0.10
#####################
# bitcoinlib
# (use ecdsa)
# python -m pip install bitcoinlib==0.4.4
#####################
# python-bitcoinlib
# (use openssl)
# python -m pip install python-bitcoinlib==0.10.1
# (inst path to PythonXX/Lib/site-packages/bitcoin , its conflict with pybitcointools)
def test_python_bitcoinlib(test_name = 'python-bitcoinlib', rounds = 10):
loops = rounds
try:
import bitcoin
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
secret = bytes((secret.bit_length()+7)//8 or 1)
from bitcoin.wallet import CKey
pubkey = CKey(secret,compressed=False).pub
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from bitcoin.wallet import CKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret = random.randint(1,n);secret = bytes((secret.bit_length()+7)//8 or 1);CKey(secret,compressed=False).pub'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
from bitcoin.core.key import CECKey, CPubKey
pubkey = CECKey()
pubkey.set_secretbytes(secret)
pubkey.set_compressed(compressed=False)
pubkey = CPubKey(pubkey.get_pubkey(), pubkey)
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from bitcoin.core.key import CECKey, CPubKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret = random.randint(1,n);secret = bytes((secret.bit_length()+7)//8 or 1, "big");pubkey = CECKey();pubkey.set_secretbytes(secret);pubkey = CPubKey(pubkey.get_pubkey(), pubkey);'
#print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# py_ecc
# (use python with quick jacobian multiply)
# python -m pip install py_ecc==1.4.7
# (bitcoin_easy)
#####################
# ecc-0.0.1
#####################
# bitcoin-utils
# (use ecdsa)
# python -m pip install bitcoin-utils==0.2.2
#####################
# bitcoin_tools
# (use python)
# python -m pip install bitcoin_tools==0.0.13
#####################
# python_bitcoin_tools
# (use ecdsa)
# python -m pip install python_bitcoin_tools==0.2.3
#####################
#
# (use )
# python -m pip install
#exit(0);
#####################
# openssl
def test_openssl(test_name = 'openssl', rounds = 10):
loops = rounds
try:
import ctypes
ssl_library = ctypes.cdll.LoadLibrary("libeay32.dll")
#ssl_library = ctypes.cdll.LoadLibrary(r"C:\Windows\System32\libeay32.dll")
#ssl_library = ctypes.cdll.LoadLibrary(r"C:\Windows\SysWOW64\libeay32.dll")
#ssl_library = ctypes.cdll.LoadLibrary("libssl.so")
except Exception:
print('[error] import {} failed; need libeay32.dll/.so'.format(test_name,test_name))
return(1)
NID_secp256k1 = 714
def get_public_key1(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
if ssl_library.EC_KEY_generate_key(k) != 1:
raise Exception("internal error")
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_KEY_free(k)
return public_key
def get_public_key2(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
storage = ctypes.create_string_buffer(private_key)
bignum_private_key = ssl_library.BN_new()
ssl_library.BN_bin2bn(storage, 32, bignum_private_key)
group = ssl_library.EC_KEY_get0_group(k)
point = ssl_library.EC_POINT_new(group)
ssl_library.EC_POINT_mul(group, point, bignum_private_key, None, None, None)
ssl_library.EC_KEY_set_private_key(k, bignum_private_key)
ssl_library.EC_KEY_set_public_key(k, point)
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_POINT_free(point)
ssl_library.BN_free(bignum_private_key)
ssl_library.EC_KEY_free(k)
return public_key
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
secret = bytes((secret.bit_length()+7)//8 or 1)
#pubkey = get_public_key1(secret)
pubkey = get_public_key2(secret)
#print('[pubkey] {0}'.format(pubkey.hex()));exit(0);
setup = 'import random; import ctypes; ssl_library = ctypes.cdll.LoadLibrary("libeay32.dll"); n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = '''
if 1:
NID_secp256k1 = 714
def get_public_key1(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
if ssl_library.EC_KEY_generate_key(k) != 1:
raise Exception("internal error")
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_KEY_free(k)
return public_key
def get_public_key2(private_key, curve_name=NID_secp256k1):
k = ssl_library.EC_KEY_new_by_curve_name(curve_name)
storage = ctypes.create_string_buffer(private_key)
bignum_private_key = ssl_library.BN_new()
ssl_library.BN_bin2bn(storage, 32, bignum_private_key)
group = ssl_library.EC_KEY_get0_group(k)
point = ssl_library.EC_POINT_new(group)
ssl_library.EC_POINT_mul(group, point, bignum_private_key, None, None, None)
ssl_library.EC_KEY_set_private_key(k, bignum_private_key)
ssl_library.EC_KEY_set_public_key(k, point)
size = ssl_library.i2o_ECPublicKey(k, 0)
storage = ctypes.create_string_buffer(size)
ssl_library.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(storage)))
public_key = storage.raw
ssl_library.EC_POINT_free(point)
ssl_library.BN_free(bignum_private_key)
ssl_library.EC_KEY_free(k)
return public_key
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
secret=random.randint(1,n);
secret=bytes((secret.bit_length()+7)//8 or 1);
pubkey = get_public_key2(secret);
'''
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
# coincurve
# python -m pip install coincurve==9.0.0
# (use libsecp256k1.dll/.so)
def test_coincurve(test_name = 'coincurve', rounds = 10):
loops = rounds
try:
import coincurve
except Exception:
print('[error] import {} failed; try: python -m pip install {}'.format(test_name,test_name))
return(1)
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
#secret = random.randint(1,n)
secret = 0xebaaedce6af48a03bbfd25e8cd0364141fffffffffffffffffffffffffffffff;
secret = bytes((secret.bit_length()+7)//8 or 1)
from coincurve.keys import PrivateKey, PublicKey
pubkey = PublicKey.from_valid_secret(secret).point()
#print('[pubkey] {0}'.format(pubkey));exit(0);
setup = 'import random; from coincurve.keys import PrivateKey, PublicKey; n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;'
stmt = 'secret=random.randint(1,n); secret=bytes((secret.bit_length()+7)//8 or 1); PublicKey.from_valid_secret(secret).point();'
print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#stmt = "secret=random.randint(1,n); secret=secret.to_bytes((secret.bit_length()+7)//8 or 1, 'big'); PublicKey.from_valid_secret(secret).format(False);"
#print('[timeit] {0} loops, {1:.6} sec '.format( loops, timeit.timeit(stmt, setup, number=loops) ).ljust(50,' ') + test_name )
#exit(0);
#####################
#####################
#####################
test_ecdsa()
test_pycoin()
test_starkbank_ecdsa()
test_pybitcointools()
test_python_bitcoinlib()
#test_openssl()
test_coincurve()