-
Notifications
You must be signed in to change notification settings - Fork 128
/
base_conversion.py
41 lines (34 loc) · 1.21 KB
/
base_conversion.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
import logging
from sage.all import ZZ
def factorize(N, coefficient_threshold=32):
"""
Recovers the prime factors from a modulus by converting it to different bases.
:param N: the modulus
:param coefficient_threshold: the threshold of coefficients below which we will try to factor a base k polynomial
:return: a tuple containing the prime factors
"""
R = ZZ["x"]
base = 2
while True:
logging.debug(f"Trying {base = }...")
poly = R(ZZ(N).digits(base))
logging.debug(f"Got {len(poly.coefficients())} coefficients")
if len(poly.coefficients()) < coefficient_threshold:
facs = poly.factor()
return tuple(map(lambda f: int(f[0](base)), facs))
base += 1
def factorize_base_2x(N):
"""
Recovers the prime factors from a modulus by converting it to different bases of the form 2^x.
:param N: the modulus
:return: a tuple containing the prime factors
"""
R = ZZ["x"]
base = 2
while True:
logging.debug(f"Trying {base = }...")
poly = R(ZZ(N).digits(base))
facs = poly.factor()
if len(facs) > 1:
return tuple(map(lambda f: int(f[0](base)), facs))
base *= 2