forked from andreacorbellini/ecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparelogs.py
executable file
·75 lines (51 loc) · 1.63 KB
/
comparelogs.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import functools
import multiprocessing
import sys
import time
import bruteforce
import babygiantstep
import pollardsrho
from common import tinycurve as curve
def compute_one(func, x):
p = curve.g
q = curve.mult(x, p)
try:
y, steps = func(p, q)
except Exception as exc:
return x, str(exc)
return x, y, steps
def compute_all(func):
total_steps = 0
compute_func = functools.partial(compute_one, func)
with multiprocessing.Pool() as pool:
results = pool.imap_unordered(compute_func, range(curve.n))
for i, (x, y, steps) in enumerate(results):
total_steps += steps
if x != y:
print('\nERROR: expected {}, got: {}'.format(x, y))
if i % 100 == 0:
print('\rComputing all logarithms: {:.2f}% done'
.format(100 * i / (curve.n - 1)), end='')
sys.stdout.flush()
print('\rComputing all logarithms: 100.00% done')
return total_steps / curve.n
def main():
all_mods = [
bruteforce,
babygiantstep,
pollardsrho,
]
print('Curve order: {}'.format(curve.n))
for mod in all_mods:
print('Using {}'.format(mod.__name__))
start = time.monotonic()
average_steps = compute_all(mod.log)
stop = time.monotonic()
total_seconds = stop - start
minutes = int(total_seconds // 60)
seconds = round(total_seconds - 60 * minutes)
print('Took {}m {}s ({} steps on average)'
.format(minutes, seconds, round(average_steps)))
if __name__ == '__main__':
main()