-
Notifications
You must be signed in to change notification settings - Fork 124
/
hensel.py
47 lines (41 loc) · 1.22 KB
/
hensel.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
from sage.all import ZZ
from sage.all import Zmod
def hensel_lift_linear(f, p, k, roots):
"""
Uses Hensel lifting to lift the roots of f mod p^k to f mod p^(k + 1)
:param f: the polynomial
:param p: the prime
:param k: the power
:param roots: the roots of f mod p^k
:return: a generator generating the roots of f mod p^k
"""
pk = p ** k
pk1 = p ** (k + 1)
for root in roots:
for i in range(p):
new_root = root + i * pk
if f(new_root) % pk1 == 0:
yield new_root
def hensel_roots(f, p, k):
"""
Uses Hensel lifting to generate the roots of f mod p^k.
:param f: the polynomial
:param p: the prime
:param k: the power
:return: a list containing the roots of f mod p^k, or an empty list if no roots were found
"""
f_ = f.change_ring(Zmod(p))
if f_ == 0:
roots = list(range(p))
elif f_.is_constant():
return []
else:
roots = []
for root in f_.roots(multiplicities=False):
roots.append(int(root))
f = f.change_ring(ZZ)
for i in range(1, k):
roots = list(hensel_lift_linear(f, p, i, roots))
if len(roots) == 0:
return []
return roots