-
Notifications
You must be signed in to change notification settings - Fork 121
/
frey_ruck_attack.py
50 lines (41 loc) · 1.59 KB
/
frey_ruck_attack.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
import logging
import os
import sys
from math import gcd
from sage.all import GF
path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(os.path.abspath(__file__)))))
if sys.path[1] != path:
sys.path.insert(1, path)
from shared.ecc import get_embedding_degree
def attack(P, R, max_k=6, max_tries=10):
"""
Solves the discrete logarithm problem using the Frey-Ruck attack.
More information: Harasawa R. et al., "Comparing the MOV and FR Reductions in Elliptic Curve Cryptography" (Section 3)
:param P: the base point
:param R: the point multiplication result
:param max_k: the maximum value of embedding degree to try (default: 6)
:param max_tries: the maximum amount of times to try to find l (default: 10)
:return: l such that l * P == R, or None if l was not found
"""
E = P.curve()
q = E.base_ring().order()
n = P.order()
assert gcd(n, q) == 1, "GCD of base point order and curve base ring order should be 1."
logging.info("Calculating embedding degree...")
k = get_embedding_degree(q, n, max_k)
if k is None:
return None
logging.info(f"Found embedding degree {k}")
Ek = E.base_extend(GF(q ** k))
Pk = Ek(P)
Rk = Ek(R)
for _ in range(max_tries):
S = Ek.random_point()
T = Ek.random_point()
if (gamma := Pk.tate_pairing(S, n, k) / Pk.tate_pairing(T, n, k)) == 1:
continue
delta = Rk.tate_pairing(S, n, k) / Rk.tate_pairing(T, n, k)
logging.info(f"Computing {delta}.log({gamma})...")
l = delta.log(gamma)
return int(l)
return None