From 9b40838f25c6a64f289b1bd07db9e0326486c8ec Mon Sep 17 00:00:00 2001 From: bmng-dev Date: Wed, 21 May 2014 11:08:15 +0000 Subject: [PATCH] Use pointMult instead of arithmetic.privtopub pointMult is faster than the pure python arithmetic.privtopub Additionally in makeCryptor the call to a.privtopub could have just simply be changed to call the local privToPub but then privkey would have been dehexified twice (once in makeCryptor, then again in privToPub) and privToPub would have hexified its result only for makeCryptor to immediately dehexify it. This sort of unnecessary hexifying/dehexifying seems to occur throughout PyBitmessage. --- src/highlevelcrypto.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/highlevelcrypto.py b/src/highlevelcrypto.py index 26143d59..28439c8d 100644 --- a/src/highlevelcrypto.py +++ b/src/highlevelcrypto.py @@ -1,9 +1,10 @@ import pyelliptic from pyelliptic import arithmetic as a, OpenSSL def makeCryptor(privkey): - privkey_bin = '\x02\xca\x00 '+a.changebase(privkey,16,256,minlen=32) - pubkey = a.changebase(a.privtopub(privkey),16,256,minlen=65)[1:] - pubkey_bin = '\x02\xca\x00 '+pubkey[:32]+'\x00 '+pubkey[32:] + private_key = a.changebase(privkey, 16, 256, minlen=32) + public_key = pointMult(private_key) + privkey_bin = '\x02\xca\x00\x20' + private_key + pubkey_bin = '\x02\xca\x00\x20' + public_key[1:-32] + '\x00\x20' + pubkey[-32:] cryptor = pyelliptic.ECC(curve='secp256k1',privkey=privkey_bin,pubkey=pubkey_bin) return cryptor def hexToPubkey(pubkey): @@ -15,7 +16,9 @@ def makePubCryptor(pubkey): return pyelliptic.ECC(curve='secp256k1',pubkey=pubkey_bin) # Converts hex private key into hex public key def privToPub(privkey): - return a.privtopub(privkey) + private_key = a.changebase(privkey, 16, 256, minlen=32) + public_key = pointMult(private_key) + return public_key.encode('hex') # Encrypts message with hex public key def encrypt(msg,hexPubkey): return pyelliptic.ECC(curve='secp256k1').encrypt(msg,hexToPubkey(hexPubkey))