Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/Discrete-Logarithm-Problem/Elliptic-Curve-DLP/brute_ecdlp.py
1402 views
1
from sage.all import *
2
import random
3
4
def brute_ecdlp(P, Q, E):
5
"""
6
Brute Force algorithm to solve ECDLP and retrieve value of secret key `x`.
7
Use only if group is very small.
8
Includes memoization for faster computation
9
[Warning]: Tested for Weierstrass Curves only
10
11
:parameters:
12
E : sage.schemes.elliptic_curves.ell_finite_field.EllipticCurve_finite_field_with_category
13
Elliptic Curve defined as y^2 = x^3 + a*x + b mod p
14
P : sage.schemes.elliptic_curves.ell_point.EllipticCurvePoint_finite_field
15
Base point of the Elliptic Curve E
16
Q : sage.schemes.elliptic_curves.ell_point.EllipticCurvePoint_finite_field
17
Q = x*P, where `x` is the secret key
18
"""
19
_order = P.order()
20
try:
21
assert P == E((P[0], P[1]))
22
except TypeError:
23
print "[-] Point does not lie on the curve"
24
return -1
25
26
res = 2*P
27
if res == Q:
28
return 2
29
if Q == E((0, 1, 0)):
30
return _order
31
if Q == P:
32
return 1
33
i = 3
34
while i <= P.order() - 1:
35
res = res + P
36
if res == Q:
37
return i
38
i += 1
39
return -1
40
41
42
if __name__ == "__main__":
43
E = EllipticCurve(GF(17), [2, 2])
44
P = E((5, 1))
45
try:
46
for _ in range(100):
47
x = random.randint(2, 18)
48
assert brute_ecdlp(E((5, 1)), x*P, E) == x
49
except Exception as e:
50
print e
51
print "[-] Function inconsistent and incorrect"
52
print "[-] Check your implementation"
53
54