FLINT3.7.0-dev
Algorithm

Factoring polynomials over finite fields

Cost
For ff of degree nn over Fq\mathbb{F}_q: Cantor–Zassenhaus O(nM(n)log⁡q)O(n M(n) \log q) expected; Kaltofen–Shoup with Brent–Kung composition O(nM(n)+n1/2C(n)+M(n)log⁡qlog⁡n)O(n M(n) + n^{1/2} C(n) + M(n) \log q \log n) expected, C(n)=O(n1/2M(n)+n(ω+1)/2)C(n) = O(n^{1/2} M(n) + n^{(\omega+1)/2}); Berlekamp O(nω+nM(n)log⁡n+M(n)log⁡qlog⁡n)O(n^\omega + n M(n) \log n + M(n) \log q \log n) expected.

Let Fq\mathbb{F}_q be a finite field with q=pkq = p^k elements and let f∈Fq[x]f \in \mathbb{F}_q[x] have degree nn. Every factorisation algorithm used by FLINT reduces the problem to three steps, which go back to Berlekamp [Ber1967] [Ber1970] and to Cantor and Zassenhaus [CanZas1981]:

  1. Squarefree factorisation writes f=c∏igieif = c \prod_i g_i^{e_i} with squarefree, pairwise coprime gig_i and distinct eie_i (Squarefree factorisation).
  2. Distinct-degree factorisation (DDF) splits a squarefree gg into g=∏dg[d]g = \prod_d g_{[d]}, where g[d]g_{[d]} is the product of the irreducible factors of gg of degree dd.
  3. Equal-degree factorisation (EDF) splits each g[d]g_{[d]} into its irreducible factors, by a randomised algorithm.

The algorithms differ in how they perform steps 2 and 3. The Cantor–Zassenhaus algorithm computes the DDF with one exponentiation by qq per degree; the Kaltofen–Shoup algorithm [KalSho1998] [Sho1995] uses a baby-step giant-step method in which the exponentiations are replaced by modular compositions; Berlekamp's algorithm skips the DDF and splits gg with the kernel of a linear map. Textbook accounts are [GG2003, Ch. 14] and [Sho2009, Ch. 20].

All of them are implemented once, generically, in gr_poly (gr_poly_factor_finite_field and the functions it calls), for any gr context that is a finite field: nmod, fmpz_mod, fq, fq_nmod and fq_zech. The modules nmod_poly_factor, fmpz_mod_poly_factor and the fq_*_poly_factor modules are wrappers. Throughout, M(n)M(n) denotes the cost of multiplying polynomials of degree nn over Fq\mathbb{F}_q, counted in field operations, and C(n)C(n) the cost of one modular composition a(b) mod fa(b) \bmod f for a,ba, b of degree less than nn (Modular composition).

Finite fields and the Frobenius map#

Theorem 1. For every d≥1d \ge 1,

xqd−x=∏g monic irreducibledeg⁡g∣dgx^{q^d} - x = \prod_{\substack{g \text{ monic irreducible} \\ \deg g \mid d}} g

in Fq[x]\mathbb{F}_q[x].

Proof. The polynomial xqd−xx^{q^d} - x is squarefree, since its derivative is −1-1. Its roots in an algebraic closure are the qdq^d elements of Fqd\mathbb{F}_{q^d}. A monic irreducible gg of degree ee divides it if and only if a root α\alpha of gg lies in Fqd\mathbb{F}_{q^d}, that is, if and only if Fq(α)≅Fqe\mathbb{F}_q(\alpha) \cong \mathbb{F}_{q^e} is a subfield of Fqd\mathbb{F}_{q^d}, which happens exactly when e∣de \mid d [LN1997, Ch. 2].

Corollary 2. If gg is squarefree, gcd⁡(g,xqd−x)\gcd(g, x^{q^d} - x) is the product of the irreducible factors of gg whose degree divides dd. In particular, gg of degree nn is irreducible if and only if gcd⁡(g,xqd−x)=1\gcd(g, x^{q^d} - x) = 1 for 1≤d≤n/21 \le d \le n/2.

The gcd is computed with xqd mod gx^{q^d} \bmod g, never with the huge polynomial xqd−xx^{q^d} - x itself. The powers xqd mod gx^{q^d} \bmod g are linked by the Frobenius endomorphism a↦aqa \mapsto a^q of Fq[x]\mathbb{F}_q[x]:

Lemma 3. For a∈Fq[x]a \in \mathbb{F}_q[x], a(x)q=a(xq)a(x)^q = a(x^q). Consequently, if hi=xqi mod gh_i = x^{q^i} \bmod g, then hi+j=hi(hj) mod gh_{i+j} = h_i(h_j) \bmod g and, more generally, aqi mod g=a(hi) mod ga^{q^i} \bmod g = a(h_i) \bmod g.

Proof. The map a↦aqa \mapsto a^q is a ring endomorphism in characteristic pp ((a+b)p=ap+bp(a + b)^p = a^p + b^p) and fixes the coefficients, since cq=cc^q = c for c∈Fqc \in \mathbb{F}_q. Hence (∑cjxj)q=∑cjxjq(\sum c_j x^j)^q = \sum c_j x^{jq}. Applying this ii times gives aqi=a(xqi)a^{q^i} = a(x^{q^i}), and reducing modulo gg gives aqi≡a(hi)a^{q^i} \equiv a(h_i). With a=xqja = x^{q^j} this is hi+j≡hj(hi)≡hi(hj)h_{i+j} \equiv h_j(h_i) \equiv h_i(h_j) (the two orders are equal by symmetry).

So there are two ways to step from hih_i to hi+1h_{i+1}: an exponentiation hi+1=hiq mod gh_{i+1} = h_i^q \bmod g, which costs about 32log⁡2q\tfrac{3}{2} \log_2 q multiplications modulo gg, and a composition hi+1=hi(h1) mod gh_{i+1} = h_i(h_1) \bmod g, which costs C(n)C(n) independently of qq. With the Brent–Kung algorithm and the matrix of powers of h1h_1 precomputed, a composition costs about n\sqrt{n} multiplications modulo gg plus a product of an n×n\sqrt{n} \times \sqrt{n} and an n×n\sqrt{n} \times n matrix, so composition wins when log⁡q\log q is large compared to n\sqrt{n}. (The Frobenius map on elements of Fq\mathbb{F}_q itself is discussed in Frobenius map, norm, trace and roots.) The generic code uses composition when qq has more than 34(⌊n⌋+1)\tfrac{3}{4}(\lfloor \sqrt{n} \rfloor + 1) bits.

Squarefree factorisation and deflation#

Squarefree factorisation needs only gcds, divisions, derivatives and, in characteristic pp, pp-th roots of coefficients; it costs O(M(n)log⁡n)O(M(n) \log n) operations per gcd and is described in Squarefree factorisation. It reduces the problem to monic squarefree polynomials, and all steps below assume this.

Before the squarefree factorisation, _gr_poly_factor_finite_field deflates the input: if F(x)=G(xm)F(x) = G(x^m) for some m≥2m \ge 2 (all exponents of nonzero terms are multiples of mm), it factors GG, and then factors gi(xm)g_i(x^m) for each factor gig_i of GG (without deflating again). Each factor of FF divides exactly one gi(xm)g_i(x^m), so this splits the problem into independent problems of smaller size; polynomials like xm−ax^m - a or x2m+axm+bx^{2m} + a x^m + b benefit most.

Distinct-degree factorisation#

The classical algorithm#

Algorithm 1: Classical distinct-degree factorisation

Input: gg monic squarefree of degree nn.

Output: the pairs (g[d],d)(g_{[d]}, d) with g[d]≠1g_{[d]} \ne 1.

  1. v←gv \gets g, h←xh \gets x, i←0i \gets 0.
  2. While deg⁡v≥2(i+1)\deg v \ge 2(i + 1):

    1. i←i+1i \gets i + 1; h←hq mod vh \gets h^q \bmod v.
    2. u←gcd⁡(v,h−x)u \gets \gcd(v, h - x); if u≠1u \ne 1, output (u,i)(u, i), set v←v/uv \gets v / u and h←h mod vh \gets h \bmod v.
  3. If v≠1v \ne 1, output (v,deg⁡v)(v, \deg v).

By induction on ii, when uu is computed vv has no factors of degree less than ii, so by Corollary 2 uu is the product of the factors of degree exactly ii. When deg⁡v<2(i+1)\deg v < 2(i + 1), the remaining vv has no factor of degree at most ii and therefore cannot be a product of two or more factors; it is irreducible. The loop runs at most n/2n/2 times, and each iteration costs an exponentiation, O(M(n)log⁡q)O(M(n) \log q), and a gcd, for a total of O(nM(n)log⁡q)O(n M(n) \log q).

Baby steps and giant steps#

Shoup [Sho1995] and Kaltofen and Shoup [KalSho1998] remove the factor log⁡q\log q by grouping the degrees into intervals. Choose ℓ≈n/2\ell \approx \sqrt{n/2} and m=⌈n/(2ℓ)⌉m = \lceil n / (2\ell) \rceil, so that the mm intervals (ℓj,ℓ(j+1)](\ell j, \ell (j+1)], 0≤j<m0 \le j < m, cover all degrees up to n/2n/2.

Lemma 4. Let hi=xqi mod gh_i = x^{q^i} \bmod g and Hj=xqℓ(j+1) mod gH_j = x^{q^{\ell(j+1)}} \bmod g. An irreducible factor uu of gg of degree dd divides Hj−hiH_j - h_i if and only if d∣ℓ(j+1)−id \mid \ell(j + 1) - i.

Proof. Modulo uu, Hj−hi≡xqℓ(j+1)−xqi=(xqℓ(j+1)−i−x)qiH_j - h_i \equiv x^{q^{\ell(j+1)}} - x^{q^i} = (x^{q^{\ell(j+1) - i}} - x)^{q^i}, since the qiq^i-th power map is additive. Since uu is irreducible, it divides a qiq^i-th power if and only if it divides the base, and by Theorem 1 that happens if and only if d∣ℓ(j+1)−id \mid \ell(j+1) - i.

Algorithm 2: Baby-step giant-step distinct-degree factorisation

Input: gg monic squarefree of degree n≥2n \ge 2, and h1=xq mod gh_1 = x^q \bmod g.

Output: the pairs (g[d],d)(g_{[d]}, d) with g[d]≠1g_{[d]} \ne 1.

  1. Baby steps: hi←xqi mod gh_i \gets x^{q^i} \bmod g for 0≤i≤ℓ0 \le i \le \ell.
  2. Coarse DDF: v←gv \gets g. For j=0,1,…,m−1j = 0, 1, \ldots, m - 1:

    1. Hj←hℓH_j \gets h_\ell if j=0j = 0, else Hj←Hj−1(hℓ) mod vH_j \gets H_{j-1}(h_\ell) \bmod v (a modular composition with the precomputed matrix of powers of hℓh_\ell).
    2. Ij←∏i=0ℓ−1(Hj−hi) mod vI_j \gets \prod_{i=0}^{\ell - 1} (H_j - h_i) \bmod v, omitting the terms for degrees ℓ(j+1)−i>12deg⁡v\ell(j+1) - i > \tfrac12 \deg v.
    3. Fj←gcd⁡(v,Ij)F_j \gets \gcd(v, I_j) (or Fj←vF_j \gets v if Ij=0I_j = 0); v←v/Fjv \gets v / F_j.
    4. Stop if deg⁡v<2(ℓ(j+1)+1)\deg v < 2(\ell(j + 1) + 1).
  3. If v≠1v \ne 1, output (v,deg⁡v)(v, \deg v).
  4. Fine DDF: for each jj with Fj≠1F_j \ne 1: if j=0j = 0 or deg⁡Fj>ℓ(j+1)\deg F_j > \ell(j+1), then for i=ℓ−1,…,0i = \ell - 1, \ldots, 0: u←gcd⁡(Fj,Hj−hi)u \gets \gcd(F_j, H_j - h_i); if u≠1u \ne 1, output (u,ℓ(j+1)−i)(u, \ell(j+1) - i) and set Fj←Fj/uF_j \gets F_j / u. Otherwise output (Fj,deg⁡Fj)(F_j, \deg F_j).

Theorem 5. Algorithm 2 is correct. With Brent–Kung modular composition it uses ℓ+m≈2n\ell + m \approx \sqrt{2n} modular compositions (or exponentiations, see below), at most ℓm≈n/2\ell m \approx n/2 multiplications modulo gg, and O(n)O(\sqrt{n}) gcds, in total

O(nM(n)+n C(n))O\bigl(n M(n) + \sqrt{n}\, C(n)\bigr)

operations, plus O(M(n)log⁡q)O(M(n) \log q) for h1h_1.

Proof. By Lemma 4, the factors of vv that divide IjI_j are those whose degree divides some integer in (ℓj,ℓ(j+1)](\ell j, \ell(j+1)]. By induction on jj, when FjF_j is computed every factor of vv has degree greater than ℓj\ell j (smaller degrees dd divide dd itself, which lies in an earlier interval), so FjF_j is the product of the factors with degree in (ℓj,ℓ(j+1)](\ell j, \ell(j+1)]. Omitting the terms for degrees above 12deg⁡v\tfrac12 \deg v loses nothing, because if vv had a factor of such a degree dd and were reducible, it would also have a factor of degree ≤deg⁡v−d<d\le \deg v - d < d, already removed. The stopping rule and step 3 are justified as in the classical algorithm. Reducing Hj−1H_{j-1} and the matrix of powers of hℓh_\ell modulo the new vv after a split is valid since vv divides the old modulus.

In the fine phase, for j≥1j \ge 1 the factors of FjF_j have degrees in (ℓj,ℓ(j+1)](\ell j, \ell(j+1)], an interval in which no degree divides another (since 2(ℓj+1)>ℓ(j+1)2(\ell j + 1) > \ell(j+1)); so gcd⁡(Fj,Hj−hi)\gcd(F_j, H_j - h_i) is exactly the product of the factors of degree ℓ(j+1)−i\ell(j+1) - i. If deg⁡Fj≤ℓ(j+1)\deg F_j \le \ell(j+1), FjF_j has a single factor, since two factors would have total degree greater than 2ℓj≥ℓ(j+1)2\ell j \ge \ell(j+1). For j=0j = 0 the degrees 1,2,…,ℓ1, 2, \ldots, \ell are processed in increasing order and the factors found are removed, so the gcd for degree dd only contains factors of degree exactly dd, as in the classical algorithm.

The cost bound counts the compositions of steps 1 and 2.1, the ℓ\ell multiplications per interval of step 2.2, and one gcd per interval and per degree in the fine phase, O(nM(n)log⁡n)O(\sqrt{n} M(n) \log n) in total, which is dominated by the compositions.

The baby steps are computed in _gr_poly_iterated_frobenius_preinv, by repeated exponentiation by qq if qq is small (see Finite fields and the Frobenius map) and otherwise by modular composition using a doubling scheme: given h1,…,hsh_1, \ldots, h_s, the vector composition hs+i=hi(hs)h_{s+i} = h_i(h_s), 1≤i≤s1 \le i \le s, is a single matrix product with the powers of hsh_s (gr_poly_preinv_compose_mod_brent_kung_vec). The giant steps always use composition with the precomputed matrix of powers of hℓh_\ell.

With C(n)=O(nM(n)+n(ω+1)/2)C(n) = O(\sqrt{n} M(n) + n^{(\omega+1)/2}) the bound of Theorem 5 is O(nM(n)+n(ω+2)/2)O(n M(n) + n^{(\omega+2)/2}) and no longer depends on qq except through h1h_1; this is what makes the algorithm fast for large qq. Kaltofen and Shoup also show how to make the interval products subquadratic, which together with fast matrix multiplication gives a factorisation algorithm with O(n1.815log⁡q)O(n^{1.815} \log q) operations [KalSho1998]; the asymptotically fastest known method, based on the modular composition algorithm of Kedlaya and Umans, uses O(n1.5+o(1)+n1+o(1)log⁡q)O(n^{1.5 + o(1)} + n^{1 + o(1)} \log q) bit operations per field operation [KU2011]. FLINT uses neither of these refinements.

Equal-degree factorisation#

Let now gg be monic, squarefree, of degree n=rdn = rd, the product of r≥2r \ge 2 irreducible factors u1,…,uru_1, \ldots, u_r of degree dd. By the Chinese remainder theorem

R=Fq[x]/(g)≅∏i=1rFq[x]/(ui)≅Fqdr,a↦(a mod u1,…,a mod ur).R = \mathbb{F}_q[x]/(g) \cong \prod_{i=1}^r \mathbb{F}_q[x]/(u_i) \cong \mathbb{F}_{q^d}^r, \qquad a \mapsto (a \bmod u_1, \ldots, a \bmod u_r).

The Berlekamp subalgebra B={b∈R:bq=b}B = \{ b \in R : b^q = b \} consists of the elements whose components all lie in Fq\mathbb{F}_q, so B≅FqrB \cong \mathbb{F}_q^r. Every b∈Bb \in B whose components are not all equal gives a proper factor gcd⁡(g,b−c)\gcd(g, b - c) for some c∈Fqc \in \mathbb{F}_q; the algorithms below produce such bb at random and then find a cc, or a polynomial in bb playing the same role.

Lemma 6 (Trace map). The map Tr⁡(a)=a+aq+⋯+aqd−1\operatorname{Tr}(a) = a + a^q + \cdots + a^{q^{d-1}} is an Fq\mathbb{F}_q-linear surjection R→BR \to B. If a∈Ra \in R is uniformly random, then Tr⁡(a)\operatorname{Tr}(a) is uniformly random in BB.

Proof. Componentwise, Tr⁡\operatorname{Tr} is the trace of Fqd\mathbb{F}_{q^d} over Fq\mathbb{F}_q, which is Fq\mathbb{F}_q-linear and surjective [LN1997, §2.3]. A surjective linear map sends the uniform distribution to the uniform distribution, since all fibres are cosets of the kernel.

Lemma 7 (Splitting probability). Let qq be odd and let b∈Bb \in B be uniformly random. Then gcd⁡(g,b(q−1)/2−1)\gcd(g, b^{(q-1)/2} - 1) is a proper factor of gg with probability at least 4/94/9. For q=2kq = 2^k and the absolute trace T(b)=b+b2+⋯+b2k−1T(b) = b + b^2 + \cdots + b^{2^{k-1}}, gcd⁡(g,T(b))\gcd(g, T(b)) is a proper factor with probability 1−21−r≥1/21 - 2^{1-r} \ge 1/2.

Proof. For odd qq, a component c∈Fqc \in \mathbb{F}_q of bb satisfies c(q−1)/2=1c^{(q-1)/2} = 1 if and only if cc is a nonzero square, which happens with probability s=(q−1)/(2q)s = (q - 1)/(2q), independently for the rr components. The gcd is the product of the uiu_i for which this holds, so it is trivial with probability sr+(1−s)r≤s2+(1−s)2=1−2s(1−s)s^r + (1 - s)^r \le s^2 + (1 - s)^2 = 1 - 2s(1 - s), and 2s(1−s)=(q2−1)/(2q2)≥4/92s(1-s) = (q^2 - 1)/(2q^2) \ge 4/9 for q≥3q \ge 3. For even qq, TT is the trace of Fq\mathbb{F}_q over F2\mathbb{F}_2 on each component, so the components of T(b)T(b) are independent and uniform in {0,1}\{0, 1\}, and the gcd is trivial only if they are all 0 or all 1.

Algorithm 3: Equal-degree splitting

Input: gg as above, h1=xq mod gh_1 = x^q \bmod g.

Output: the irreducible factors of gg.

  1. If deg⁡g=d\deg g = d, return gg.
  2. Choose a∈Ra \in R at random and compute b←Tr⁡(a)b \gets \operatorname{Tr}(a); repeat if bb is constant.
  3. t←b(q−1)/2−1 mod gt \gets b^{(q-1)/2} - 1 \bmod g for odd qq, t←T(b) mod gt \gets T(b) \bmod g for even qq; u←gcd⁡(g,t)u \gets \gcd(g, t).
  4. If uu is a proper factor, recurse on uu and g/ug / u (with h1h_1 reduced modulo each); otherwise go to step 2.

By Lemma 7, each attempt succeeds with probability at least 4/94/9, and because each factor goes to either side independently, the expected depth of the recursion is O(log⁡r)O(\log r). The cost of an attempt is that of the trace, an exponentiation, O(M(n)log⁡q)O(M(n) \log q), and a gcd. FLINT computes the trace in one of three ways (_gr_poly_trace):

  • for small qq (the same test as in Finite fields and the Frobenius map), as a+aq+⋯a + a^q + \cdots with d−1d - 1 exponentiations by qq;
  • for d<16d < 16, with d−1d - 1 compositions aqi+1=aqi(h1)a^{q^{i+1}} = a^{q^i}(h_1) using the precomputed matrix of powers of h1h_1;
  • otherwise by the doubling method of von zur Gathen and Shoup [GatSho1992]: with Ts(a)=a+aq+⋯+aqs−1T_s(a) = a + a^q + \cdots + a^{q^{s-1}}, the identities T2s(a)=Ts(a)+Ts(a)(xqs)T_{2s}(a) = T_s(a) + T_s(a)(x^{q^s}), Ts+1(a)=a+Ts(a)(h1)T_{s+1}(a) = a + T_s(a)(h_1) and xq2s=xqs(xqs)x^{q^{2s}} = x^{q^s}(x^{q^s}) compute Td(a)T_d(a) along the binary expansion of dd with O(log⁡d)O(\log d) compositions.

Roots (d=1d = 1). Every element of RR is then in BB, so no trace is needed, and b=x+cb = x + c with random cc suffices (for even qq, b=αx+cb = \alpha x + c with random α≠0\alpha \ne 0): the components of bb are the values ρ+c\rho + c at the roots ρ\rho of gg, so gcd⁡(g,(x+c)(q−1)/2−1)\gcd(g, (x + c)^{(q-1)/2} - 1) collects the roots for which ρ+c\rho + c is a nonzero square. For qq of more than 16 bits, the power is computed as (x+c)e mod g=R(x+c)(x + c)^e \bmod g = R(x + c) where R=xe mod g(x−c)R = x^e \bmod g(x - c), because powers of xx modulo a polynomial are cheaper (the multiplications by xx in the exponentiation are shifts); this costs two Taylor shifts. See Roots of polynomials over finite fields.

Splitting by the minimal polynomial. When qq is large and the number rr of factors is moderate, it is cheaper to split gg into all its factors at once [Mar2014]. If b∈Bb \in B has components c1,…,crc_1, \ldots, c_r, its minimal polynomial over Fq\mathbb{F}_q is μ(y)=∏c∈C(y−c)\mu(y) = \prod_{c \in C} (y - c), where CC is the set of distinct values cic_i, and

g=∏c∈Cgcd⁡(g,b−c).g = \prod_{c \in C} \gcd(g, b - c).

FLINT computes the sequence si=w(bi mod g)s_i = w(b^i \bmod g), 0≤i<2r0 \le i < 2r, for a random linear form ww, obtains its minimal polynomial with the Berlekamp–Massey algorithm [Mas1969] (which equals μ\mu unless ww is degenerate, in which case the attempt is repeated), finds the roots CC of μ\mu by the d=1d = 1 case above, and splits gg by divide and conquer: with μ1=∏c∈C1(y−c)\mu_1 = \prod_{c \in C_1} (y - c) for the first half C1C_1 of the roots, gcd⁡(g,μ1(b) mod g)\gcd(g, \mu_1(b) \bmod g) is the product of the uiu_i with ci∈C1c_i \in C_1, and μ1(b) mod g\mu_1(b) \bmod g is a modular composition. This uses 2r2r multiplications modulo gg and no exponentiation by (q−1)/2(q-1)/2 for the splitting element. A random bb has rr distinct components with probability at least 1−r2/(2q)1 - r^2/(2q), in which case the factorisation is complete after one round. The generic code uses this method when d>1d > 1, q≥27q \ge 2^7 and r≤8λ(d+1)+λ(q)r \le 8 \lambda(d + 1) + \lambda(q), where λ\lambda denotes the bit length.

Berlekamp's algorithm#

Berlekamp's algorithm [Ber1967] [Ber1970] does not need the DDF. Let gg be monic squarefree of degree nn with rr irreducible factors (of arbitrary degrees). The map ϕ(a)=aq−a\phi(a) = a^q - a on R=Fq[x]/(g)R = \mathbb{F}_q[x]/(g) is Fq\mathbb{F}_q-linear, and its matrix Q−IQ - I in the basis 1,x,…,xn−11, x, \ldots, x^{n-1} has as column ii the coefficients of xqi mod gx^{qi} \bmod g minus the unit vector eie_i.

Theorem 8. ker⁡ϕ=B\ker \phi = B has dimension rr. In particular gg is irreducible if and only if the nullity of Q−IQ - I is 1.

Proof. By the Chinese remainder theorem R≅∏iFqdiR \cong \prod_i \mathbb{F}_{q^{d_i}}, and aq=aa^q = a holds in Fqdi\mathbb{F}_{q^{d_i}} exactly for a∈Fqa \in \mathbb{F}_q. So ker⁡ϕ≅Fqr\ker \phi \cong \mathbb{F}_q^r.

gr_poly_factor_berlekamp computes xq mod gx^q \bmod g, the nn columns xqi mod gx^{qi} \bmod g by repeated multiplication, and a basis of the kernel with gr_mat_nullspace. If the nullity is 1, gg is irreducible. Otherwise it takes random linear combinations bb of the basis vectors (uniform in BB), tries gcd⁡(g,b)\gcd(g, b) and then gcd⁡(g,b(q−1)/2−1)\gcd(g, b^{(q-1)/2} - 1) (or gcd⁡(g,T(b))\gcd(g, T(b)) for even qq), and recurses on the two parts of the first proper split, recomputing the matrix for each part. The kernel costs O(nω)O(n^\omega), the matrix O(nM(n))O(n M(n)), and each split attempt O(M(n)log⁡q)O(M(n) \log q); since random splits are balanced on average, the expected total is O(nω+nM(n)log⁡r+M(n)log⁡qlog⁡r)O(n^\omega + n M(n) \log r + M(n) \log q \log r). The cubic linear algebra makes the algorithm uncompetitive for large nn in FLINT, where it serves mainly as an independent check.

The complete algorithms and FLINT's choice#

All three algorithms begin with gr_poly_factor_squarefree and treat each squarefree factor separately.

gr_poly_factor_finite_field (after deflation) uses Cantor–Zassenhaus for degree

n<125/(log⁡2q)1.3n < 125 / (\log_2 q)^{1.3}

and Kaltofen–Shoup otherwise. The rule fits measured crossovers for dense random polynomials over nmod: about n=125n = 125 for q=2q = 2, 70 for q=3q = 3, 32 for q=7q = 7, 16 for q=31q = 31, 8 for q≈28q \approx 2^8 and 4 for q≈216q \approx 2^{16}. For small qq the exponentiation by qq is a few squarings and the classical DDF has the smaller constant; for large qq the log⁡q\log q factor dominates and the modular compositions win.

Parallelism. When threads are available (flint_set_num_threads) and the context is thread-safe, polynomials of degree above 200 (GR_POLY_FACTOR_THREADED_CUTOFF) use _gr_poly_factor_distinct_deg_with_frob_threaded: the giant steps are computed in blocks of BB (the number of threads) independent compositions Hj+B=Hj(HB−1)H_{j+B} = H_j(H_{B-1}) with a precomputed matrix for HB−1H_{B-1}, the interval polynomials of a block in parallel, and the fine DDF in parallel over the intervals; vv is only reduced after each block, which costs a little extra work when there are factors of small degree. The equal-degree factorisation processes the pieces on the top of its work stack in parallel, each with its own random state. Matrix products use the threads of gr_mat_mul.

Randomness. Each top-level function initialises its own flint_rand_t with the default seed, so results, including the order of the factors, are reproducible from run to run (but not across FLINT versions). The factors are returned in no particular order.

Use in FLINT#

The generic implementations are gr_poly_factor_finite_field (with _gr_poly_factor_finite_field for a fixed algorithm), gr_poly_factor_cantor_zassenhaus, gr_poly_factor_kaltofen_shoup, gr_poly_factor_berlekamp, gr_poly_factor_squarefree, gr_poly_factor_distinct_deg, gr_poly_factor_equal_deg and gr_poly_factor_equal_deg_prob, with the helpers gr_poly_iterated_frobenius_preinv, _gr_poly_factor_distinct_deg_with_frob and _gr_poly_factor_equal_deg_with_frob. The gr method GR_METHOD_POLY_FACTOR of the finite field contexts points to gr_poly_factor_finite_field. The type-specific modules wrap these functions without copying the polynomials:

The fq_poly_factor, fq_nmod_poly_factor and fq_zech_poly_factor modules are generated from the same template and wrap the same functions; the arithmetic of the finite fields themselves is described in Arithmetic in finite fields. The modular factorisations used by the Zassenhaus algorithm over Z\mathbb{Z} (Factoring polynomials over the integers) and by the multivariate factorisation code (Factoring over finite fields) call nmod_poly_factor and fmpz_mod_poly_factor. Irreducibility tests are described in Irreducibility testing over finite fields, and root finding, which is the case d=1d = 1 with a cheaper first split, on Roots of polynomials over finite fields.

Referenced from#

References#

  • [Ber1967] E. R. Berlekamp. Factoring polynomials over finite fields. Bell System Technical Journal 46 (1967), 1853–1859.
  • [Ber1970] E. R. Berlekamp. Factoring polynomials over large finite fields. Mathematics of Computation 24 (1970), 713–735.
  • [CanZas1981] D. G. Cantor and H. Zassenhaus. A new algorithm for factoring polynomials over finite fields. Mathematics of Computation 36 (1981), 587–592. https://doi.org/10.1090/S0025-5718-1981-0606517-5
  • [KalSho1998] E. Kaltofen and V. Shoup, "Subquadratic-time factoring of polynomials over finite fields", Mathematics of Computation 67 (1998), 1179-1197. https://doi.org/10.1090/S0025-5718-98-00944-2
  • [Sho1995] V. Shoup, "A new polynomial factorization algorithm and its implementation", Journal of Symbolic Computation 20:4 (1995), 363–397
  • [GG2003] J. von zur Gathen and J. Gerhard, Modern Computer Algebra, second edition, Cambridge University Press (2003)
  • [Sho2009] V. Shoup. A Computational Introduction to Number Theory and Algebra. Second edition, Cambridge University Press, 2009. https://shoup.net/ntb/
  • [LN1997] R. Lidl and H. Niederreiter. Finite Fields. Second edition, Encyclopedia of Mathematics and its Applications 20, Cambridge University Press, 1997.
  • [KU2011] K. S. Kedlaya and C. Umans, "Fast polynomial factorization and modular composition", SIAM Journal on Computing 40:6 (2011), 1767–1802
  • [GatSho1992] J. von zur Gathen and V. Shoup, "Computing Frobenius maps and factoring polynomials", Computational Complexity 2 (1992), 187-224. https://doi.org/10.1007/BF01272074
  • [Mar2014] D. Marquis. Deterministic factorization of polynomials over finite fields. Master's thesis, Carleton University, 2014.
  • [Mas1969] J. L. Massey. Shift-register synthesis and BCH decoding. IEEE Transactions on Information Theory 15 (1969), 122–127.
Cite this page

The FLINT team, “Factoring polynomials over finite fields,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/polynomials/factoring-finite-fields.html (generated 2026-11-02).