-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial_factorization_in_finite_field.sf
More file actions
159 lines (119 loc) · 3.43 KB
/
polynomial_factorization_in_finite_field.sf
File metadata and controls
159 lines (119 loc) · 3.43 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
7#!/usr/bin/ruby
# Factorize a polynomial in Z_n[x], using the Cantor-Zassenhaus algorithm.
# Reference:
# Lecture 15, Week 8, 1hr - Polynomial factorization
# https://youtube.com/watch?v=tMqKqsMb-Ro
func distinct_degree_factorization(f) {
var p = f.modulus
var x_poly = PolyMod(1 => 1, p) # monomial x
var w = x_poly
var factors = []
for k in (1..f.degree) {
w = w.powmod(p, f) # w = x^(p^k) mod f
var diff = (w - x_poly)
var gk = diff.monic_gcd(f)
factors << [k, gk]
f /= gk # exact division
break if (f==1)
}
# Safety net for non-squarefree or degenerate input
if (f != 1) {
factors << [f.degree, f]
}
return factors
}
func distinct_degree_split(f, d) {
var p = f.modulus
var deg_f = f.degree
return [] if (deg_f == 0)
return [f] if (deg_f == d)
var is_char2 = (p == 2)
var exp = (is_char2 ? nil : ((p**d - 1)/2))
var g = nil
var h = nil
for (1..200) {
# Random non-zero polynomial of degree < deg_f
var t = PolyMod(deg_f.of { irand(1, p-1) }, p)
next if (t == 0)
if (is_char2) {
# Absolute trace map
h = t%f
var ti = h
for (1 .. d-1) {
ti = ti.powmod(p, f)
h = ((h+ti) % f)
}
} else {
# Quadratic-residue split
h = (t.powmod(exp, f) - 1)
}
g = h.monic_gcd(f)
break if ((g != 1) && (g != f))
}
return [f] if !(defined(g) && (g != 1) && (g != f))
__FUNC__(g, d) + __FUNC__(f/g, d)
}
func cantor_zassenhaus_polynomial_factorization(original) {
# Stage 1 – preprocessing
var lc = original.lift.leading_coefficient
var f = original.normalize_to_monic
var df = f.derivative
var sq = f.monic_gcd(df)
f = f/sq
f = f.normalize_to_monic if !(f==0 || f==1)
# Stage 2 – distinct degree factorization
var ddf_groups = (f==1 ? [] : distinct_degree_factorization(f))
# Stage 3 – equal degree factorization
var irreducibles = []
ddf_groups.each_2d {|d, fk|
next if (fk == 1)
irreducibles << distinct_degree_split(fk, d)...
}
# Stage 4 – multiplicities in the original polynomial
var remainder = original
var factors = []
for irred in (irreducibles) {
var e = 0
loop {
var (q,r) = remainder.divmod(irred)
break if (r != 0)
e += 1
remainder = q
}
next if (e == 0)
factors << [irred, e]
}
[[lc, 1], factors.sort...]
}
for f in (
PolyMod("8*x^4", 101),
PolyMod("8*x^4 + 18*x^3 - 63*x^2 - 162*x - 81", 5),
PolyMod("7*x^3 + 2*x^2 + 8*x + 1", 17)*PolyMod("x^2+x+1", 17)
) {
say "\nFactoring: #{f} in Z_#{f.modulus}[x]:"
var F = cantor_zassenhaus_polynomial_factorization(f)
assert_eq(F.prod_2d{|a,b| a**b }, f)
assert_eq(f.factor_exp, F)
F.each_2d{|factor, exp|
if (exp > 1) {
say "(#{factor})^#{exp}"
}
else {
say factor
}
}
}
__END__
Factoring: 8*x^4 (mod 101) in Z_101[x]:
8
(x (mod 101))^4
Factoring: 3*x^4 + 3*x^3 + 2*x^2 + 3*x + 4 (mod 5) in Z_5[x]:
3
(x + 2 (mod 5))^2
x + 3 (mod 5)
x + 4 (mod 5)
Factoring: 7*x^5 + 9*x^4 + 11*x^2 + 9*x + 1 (mod 17) in Z_17[x]:
7
x + 8 (mod 17)
x^2 + x + 1 (mod 17)
x^2 + 2*x + 7 (mod 17)