forked from andreacorbellini/ecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbruteforce.py
executable file
·52 lines (37 loc) · 1.12 KB
/
bruteforce.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
#!/usr/bin/env python3
# This script makes use of another module: common.py, which can be
# found on GitHub:
#
# https://github.com/andreacorbellini/ecc/blob/master/logs/common.py
#
# You must place that module on the same directory of this script
# prior to running it.
import random
from common import tinycurve as curve
def log(p, q):
assert curve.is_on_curve(p)
assert curve.is_on_curve(q)
start = random.randrange(curve.n)
r = curve.mult(start, p)
for x in range(curve.n):
if q == r:
logarithm = (start + x) % curve.n
steps = x + 1
return logarithm, steps
r = curve.add(r, p)
raise AssertionError('logarithm not found')
def main():
x = random.randrange(curve.n)
p = curve.g
q = curve.mult(x, p)
print('Curve: {}'.format(curve))
print('Curve order: {}'.format(curve.n))
print('p = (0x{:x}, 0x{:x})'.format(*p))
print('q = (0x{:x}, 0x{:x})'.format(*q))
print(x, '* p = q')
y, steps = log(p, q)
print('log(p, q) =', y)
print('Took', steps, 'steps')
assert x == y
if __name__ == '__main__':
main()