-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwrapper.py
More file actions
executable file
·58 lines (48 loc) · 1.81 KB
/
wrapper.py
File metadata and controls
executable file
·58 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import subprocess
from secrets import randbelow
import time
import sys
from multiprocessing import cpu_count
# read the order from the header file
# this is the simplest method I found so the user doesn't need to modify 2 files or use a config file
with open("pollard_rho_worker.h", "r") as f:
for line in f.readlines():
if line.startswith("#define ORDER"):
order = int(line.split('"')[1].split('"')[0], 16)
DPOINTS = {}
# choose how many parallel processes you want to launch (default: number of CPU)
num_processes = cpu_count()
def newP():
return subprocess.Popen(["./worker", str(randbelow(2**64))], stdout=subprocess.PIPE)
def parseStdout(p):
# U.x,a,b
stdout, _ = process.communicate()
return [int(e, 16) for e in stdout.decode().strip().split(",")]
def checkFound(p):
x, a, b = parseStdout(process)
known = DPOINTS.get(x)
if known is not None:
a_, b_ = known
if b_ != b:
priv = (a-a_)*pow(b_-b, -1, order)
priv %= order
print(f"Found private key ! x = {priv}")
return True
else:
DPOINTS[x] = (a, b)
if __name__ == '__main__':
processes = [newP() for _ in range(num_processes)]
while True:
for i, process in enumerate(processes):
if process.poll() == 0:
# Respawn a new process because the previous one has finished
processes[i] = newP()
if checkFound(process):
# kill all previous processes and exit
for remaining_process in processes:
if remaining_process.poll() is None:
remaining_process.terminate()
sys.exit(0)
# sleep to not consume CPU power endlessly polling
time.sleep(1)