forked from jvdsn/crypto-attacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshor.py
More file actions
21 lines (17 loc) · 673 Bytes
/
Copy pathshor.py
File metadata and controls
21 lines (17 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from math import gcd
from sage.all import divisors
def factorize(n, a, s):
"""
Recovers the prime factors from a modulus if the order of a mod n is known.
More information: M. Johnston A., "Shor’s Algorithm and Factoring: Don’t Throw Away the Odd Orders"
:param n: the modulus
:param a: the base
:param s: the order of a
:return: a tuple containing the prime factors, or None if the factors were not found
"""
assert pow(a, s, n) == 1, "s must be the order of a mod n"
for r in divisors(s):
b_r = pow(a, s // r, n)
p = gcd(b_r - 1, n)
if p != 1 and p != n and n % p == 0:
return p, n // p