I have an infinite loop Python function for measuring how fast are SECP256K1 public Keys are generated.
The script:
from time import time
a = 0
b = 7
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
prime = 2**256 - 2**32 - 977
def addition(currentX, currentY, gx, gy, a, b, prime):
if gy == 0:
return (None, None)
elif currentX is None and currentY is None:
return (gx, gy)
elif currentX == gx and currentY != gy:
return (None, None)
elif currentX == gx and currentY == gy and currentY == 0:
return (None, None)
elif currentX == gx and currentY == gy:
s1 = (3 * pow(gx, 2, prime) + a) % prime
s2 = (gy * 2) % prime
s = (s1 * pow(s2, (prime - 2), prime)) % prime
currentX = (s ** 2 - 2 * gx) % prime
currentY = (s * (gx - currentX) - gy) % prime
elif currentX != gx:
s1 = (currentY - gy)
s2 = (currentX - gx)
s = (s1 * pow(s2, (prime - 2), prime)) % prime
currentX = ((s ** 2) - gx - currentX) % prime
currentY = ((s * (gx - currentX)) - gy) % prime
return (currentX, currentY)
def secp256k1BinaryExpansion(privateKey, gx, gy, a, b, prime):
#if pow(gy, 2, prime) != (pow(gx, 3, prime) + a * gx + b) % prime:
#return "The point is not on the curve"
coef = privateKey
currentX, currentY = gx, gy
resultX, resultY = None, None
while coef:
if coef & 1:
resultX, resultY = addition(resultX, resultY, currentX, currentY, a, b, prime)
currentX, currentY = addition(currentX, currentY, currentX, currentY, a, b, prime)
coef >>= 1
return (resultX, resultY)
def testLoop(gx, gy, a, b, prime):
count = 1 #Count is the number of all calculations
counter = 0 #Counter is for measuring the speed of the function
timeOne = time()
pubX, pubY = None, None
while True:
pubX, pubY = secp256k1BinaryExpansion(count, gx, gy, a, b, prime)
#print("Case ", count,":", pubX,pubY)
count += 1
counter += 1
timeTwo = time()
if (timeTwo - timeOne) >= 10:
print("The speed is: ", counter / (timeTwo - timeOne), "c/s")
timeOne = time()
counter = 0
testLoop(gx, gy, a, b, prime)
Whenever I am launching the script on Pycharm, it outputs aroud 100 c/s on Windows and 300 c/s on Ubuntu.
When it happens, on both os, only 1 core out ouf 4 gets loaded with this task for 100%, hence only 25% of CPU power is allocated to this. The CPU: intel core i5-4440 cpu @ 3.10ghz
I'd like to allocate 2-3 cores to the task, so it gets loaded like: 50-75%.
The truth is I've read documentation and watched tutorials on Python Parallelism/Multithreading and it's confusing.
Not really sure how to allocate a single job across the cores.
May be you could help out?