Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/factorization/shor.py
2589 views
1
from math import gcd
2
3
from sage.all import divisors
4
5
6
def factorize(N, a, s):
7
"""
8
Recovers the prime factors from a modulus if the order of a mod n is known.
9
More information: M. Johnston A., "Shor's Algorithm and Factoring: Don't Throw Away the Odd Orders"
10
:param N: the modulus
11
:param a: the base
12
:param s: the order of a
13
:return: a tuple containing the prime factors, or None if the factors were not found
14
"""
15
assert pow(a, s, N) == 1, "s must be the order of a mod N"
16
17
for r in divisors(s):
18
b_r = pow(a, s // r, N)
19
p = gcd(b_r - 1, N)
20
if 1 < p < N and N % p == 0:
21
return p, N // p
22
23
return None
24
25