<p>If you just want to benchmark to see the performance when loading multiple cores, just run multiple instances of the benchmark simultaneously, and sum their rates.</p>
<p>Other than that, you're out of luck. While Python supports multiple threads within a single process, only one can actually be executing Python code at a time.</p>

CC BY-SA 4.0
2022-11-12T18:52:18.390Z
You think that C++ is much better and easier for speeding up a computation like this?
Emma Lincoln
CC BY-SA 4.0
2022-11-12T18:58:28.213Z
Python just isn't low-level enough to get good performance; even ignoring issues with parallellism, Python will be orders of magnitude slower than what you can do in C-like languages.
Pieter Wuille
CC BY-SA 4.0
2022-11-06T14:50:34.303Z
2022-11-06T15:22:18.383Z
2022-11-06T15:22:18.383Z
Pieter Wuille
<p>I have an infinite loop Python function for measuring how fast are SECP256K1 public Keys are generated.</p>
<p>The script:</p>
<pre><code>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 &quot;The point is not on the curve&quot;
    coef = privateKey
    currentX, currentY = gx, gy
    resultX, resultY = None, None
    while coef:
        if coef &amp; 1:
            resultX, resultY = addition(resultX, resultY, currentX, currentY, a, b, prime)
        currentX, currentY = addition(currentX, currentY, currentX, currentY, a, b, prime)
        coef &gt;&gt;= 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(&quot;Case &quot;, count,&quot;:&quot;, pubX,pubY)
        count += 1
        counter += 1
        timeTwo = time()
        if (timeTwo - timeOne) &gt;= 10:
            print(&quot;The speed is: &quot;, counter / (timeTwo - timeOne), &quot;c/s&quot;)
            timeOne = time()
            counter = 0

testLoop(gx, gy, a, b, prime)
</code></pre>
<p>Whenever I am launching the script on Pycharm, it outputs aroud 100 c/s on Windows and 300 c/s on Ubuntu.</p>
<p>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</p>
<p>I'd like to allocate 2-3 cores to the task, so it gets loaded like: 50-75%.</p>
<p>The truth is I've read documentation and watched tutorials on Python Parallelism/Multithreading and it's confusing.</p>
<p>Not really sure how to allocate a single job across the cores.</p>
<p>May be you could help out?</p>

<public-key><python><secp256k1>
How to add multihreading to a continuous SECP256K1 Python function?
Emma Lincoln
# How to add multihreading to a continuous SECP256K1 Python function?

(Asked by: username_0 on 2022-11-06)

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?

**Tags:** public-key, python, secp256k1


---

## 1 answers

### Answer by username_1 on 2022-11-06. Score: 2 | Accepted answer

If you just want to benchmark to see the performance when loading multiple cores, just run multiple instances of the benchmark simultaneously, and sum their rates.

Other than that, you're out of luck. While Python supports multiple threads within a single process, only one can actually be executing Python code at a time.

> **Comments:**
> **username_0**: You think that C++ is much better and easier for speeding up a computation like this?  
> **username_1**: Python just isn't low-level enough to get good performance; even ignoring issues with parallellism, Python will be orders of magnitude slower than what you can do in C-like languages.  

---

