Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
duyuefeng0708
GitHub Repository: duyuefeng0708/Cryptography-From-First-Principle
Path: blob/main/foundations/02-rings-fields-polynomials/sage/02e-polynomial-division-irreducibility.ipynb
483 views
unlisted
Kernel: SageMath 10.5

Polynomial Division & Irreducibility

Module 02 | 02-rings-fields-polynomials

divmod for polys, is_irreducible(), the primes of polynomial rings

Objectives

By the end of this notebook you will be able to:

  1. Perform polynomial long division over Fp[x]\mathbb{F}_p[x] and verify the division algorithm.

  2. Test irreducibility of polynomials over finite fields using SageMath.

  3. Explain why irreducibility depends on the base field.

  4. Articulate the parallel: irreducible polynomials are to polynomial rings what primes are to integers.

  5. Understand why irreducible polynomials are the key ingredient for constructing extension fields GF(pn)\mathrm{GF}(p^n).

Prerequisites

  • Completion of Polynomial Rings (polynomial arithmetic in Fp[x]\mathbb{F}_p[x]).

  • Completion of What Is a Field? (fields, Z/pZ\mathbb{Z}/p\mathbb{Z}).

  • Comfort with modular arithmetic from Module 01.

Motivating Question

You already know that x2+1x^2 + 1 has no real roots (since x20x^2 \ge 0 for all xRx \in \mathbb{R}). Over R\mathbb{R}, we cannot factor it, so we call it irreducible.

But what happens if we change the field?

  • Over C\mathbb{C}, we get x2+1=(x+i)(xi)x^2 + 1 = (x + i)(x - i). Reducible!

  • Over F2={0,1}\mathbb{F}_2 = \{0, 1\}, is x2+1x^2 + 1 irreducible? Let's check: 02+1=100^2 + 1 = 1 \ne 0, and 12+1=2=01^2 + 1 = 2 = 0 in F2\mathbb{F}_2. So x=1x = 1 is a root, meaning (x1)=(x+1)(x - 1) = (x + 1) divides it. In fact, x2+1=(x+1)2x^2 + 1 = (x+1)^2 over F2\mathbb{F}_2.

  • Over F3\mathbb{F}_3? We check: 02+1=10^2 + 1 = 1, 12+1=21^2 + 1 = 2, 22+1=5=22^2 + 1 = 5 = 2. No roots! For a degree-2 polynomial, no roots means irreducible. So x2+1x^2 + 1 is irreducible over F3\mathbb{F}_3.

The same polynomial can be irreducible over one field and reducible over another. This is not a minor technicality, it is the central idea that makes finite field extensions (and therefore AES, elliptic curves, and post-quantum crypto) work.

To understand irreducibility properly, we first need the tool that underpins it: polynomial division.


1. Polynomial Long Division

Bridge from 02c: In the polynomial rings notebook, we learned how to add and multiply polynomials in Fp[x]\mathbb{F}_p[x]. Now we add the final arithmetic operation: division with remainder.

The Division Algorithm for Polynomials

Recall the division algorithm for integers: given a,bZa, b \in \mathbb{Z} with b0b \ne 0, there exist unique q,rq, r such that a=qb+r,0r<b.a = q \cdot b + r, \qquad 0 \le r < |b|.

The polynomial version is almost identical. Given polynomials f,gF[x]f, g \in F[x] with g0g \ne 0, there exist unique polynomials qq (quotient) and rr (remainder) such that:

f=qg+r,deg(r)<deg(g).f = q \cdot g + r, \qquad \deg(r) < \deg(g).

The role of "less than" in the integer case (r<br < |b|) is played by "lower degree" in the polynomial case (deg(r)<deg(g)\deg(r) < \deg(g)).

Step-by-step example: dividing over Q[x]\mathbb{Q}[x]

Let's divide f=x4+x2+1f = x^4 + x^2 + 1 by g=x2+x+1g = x^2 + x + 1 step by step.

Step 1: Leading term of ff is x4x^4, leading term of gg is x2x^2. Divide: x4/x2=x2x^4 / x^2 = x^2. Multiply gg by x2x^2: x4+x3+x2x^4 + x^3 + x^2. Subtract from ff: (x4+x2+1)(x4+x3+x2)=x3+1( x^4 + x^2 + 1 ) - ( x^4 + x^3 + x^2 ) = -x^3 + 1

Step 2: Leading term of remainder is x3-x^3. Divide: x3/x2=x-x^3 / x^2 = -x. Multiply gg by x-x: x3x2x-x^3 - x^2 - x. Subtract: (x3+1)(x3x2x)=x2+x+1( -x^3 + 1 ) - ( -x^3 - x^2 - x ) = x^2 + x + 1

Step 3: Leading term is x2x^2. Divide: x2/x2=1x^2 / x^2 = 1. Multiply gg by 11: x2+x+1x^2 + x + 1. Subtract: (x2+x+1)(x2+x+1)=0(x^2 + x + 1) - (x^2 + x + 1) = 0

So q=x2x+1q = x^2 - x + 1 and r=0r = 0. The remainder is zero, meaning gg divides ff exactly.

Let's verify with SageMath:

# Polynomial long division over Q[x] R.<x> = PolynomialRing(QQ) f = x^4 + x^2 + 1 g = x^2 + x + 1 q, r = f.quo_rem(g) # quotient and remainder print(f'f = {f}') print(f'g = {g}') print(f'Quotient q = {q}') print(f'Remainder r = {r}') print() # Verify the division algorithm: f == q*g + r print(f'Verification: q*g + r = {q*g + r}') print(f'Equals f? {q*g + r == f}')

SageMath provides three ways to do polynomial division:

OperationSageMath syntaxReturns
Full divisionf.quo_rem(g)Tuple (q, r)
Quotient onlyf // gq
Remainder onlyf % gr

These mirror Python's integer divmod, //, and %.

# The //, %, and divmod shortcuts R.<x> = PolynomialRing(QQ) f = x^3 + 2*x^2 - x + 3 g = x - 2 print(f'f // g = {f // g}') # quotient print(f'f % g = {f % g}') # remainder print(f'divmod = {f.quo_rem(g)}') # both print() # Note: f(2) should equal the remainder (Remainder Theorem!) print(f'f(2) = {f(2)}') print(f'Remainder = {f % g}') print(f'Match? {f(2) == (f % g)}') # Polynomial Remainder Theorem

Division over finite fields Fp[x]\mathbb{F}_p[x]

Everything works identically over Fp\mathbb{F}_p. The only difference is that all coefficients are reduced mod pp. This is exactly where cryptography lives.

# Polynomial division over F_5[x] R.<x> = PolynomialRing(GF(5)) f = x^4 + 3*x^3 + 2*x + 1 g = x^2 + 4*x + 2 q, r = f.quo_rem(g) print(f'Over F_5[x]:') print(f'f = {f}') print(f'g = {g}') print(f'q = {q}') print(f'r = {r}') print(f'deg(r) = {r.degree()} < deg(g) = {g.degree()}? {r.degree() < g.degree()}') print(f'Verification: q*g + r == f? {q*g + r == f}')

Checkpoint: Before running the next cell, compute by hand: in F3[x]\mathbb{F}_3[x], divide x3+2x+1x^3 + 2x + 1 by x+2x + 2. What are qq and rr?

Hint: The leading coefficient of gg is 11, so no inversions needed. Remember all arithmetic is mod 3.

# Check your hand calculation R.<x> = PolynomialRing(GF(3)) f = x^3 + 2*x + 1 g = x + 2 q, r = f.quo_rem(g) print(f'q = {q}') print(f'r = {r}') print(f'f = q*g + r? {q*g + r == f}')

2. Irreducible Polynomials: The "Primes" of Polynomial Rings

Bridge from 02d: We saw that Z/pZ\mathbb{Z}/p\mathbb{Z} is a field precisely when pp is prime. Primes are the "atoms" of the integers, they cannot be broken into smaller factors. Polynomials have an exact analogue.

Definition

A polynomial fF[x]f \in F[x] of degree 1\ge 1 is irreducible over FF if it cannot be written as f=ghf = g \cdot h where both gg and hh have degree 1\ge 1.

In other words: the only way to factor ff is trivially, as f=c(c1f)f = c \cdot (c^{-1} f) for some constant cFc \in F^*.

The parallel

Integers Z\mathbb{Z}Polynomials F[x]F[x]
Primes ppIrreducible polynomials
Every integer factors uniquely into primesEvery polynomial factors uniquely into irreducibles
Z/pZ\mathbb{Z}/p\mathbb{Z} is a field iff pp is primeF[x]/(f)F[x]/(f) is a field iff ff is irreducible
Primes are the building blocks of Z\mathbb{Z}Irreducibles are the building blocks of F[x]F[x]

The last row in this table is exactly what we will use in notebook 02f to build extension fields like GF(28)\mathrm{GF}(2^8).

Testing irreducibility with SageMath

SageMath provides two key methods:

  • f.is_irreducible(), returns True/False

  • f.factor(), returns the complete factorization

# Irreducibility testing over GF(2) R.<x> = PolynomialRing(GF(2)) test_polys = [x^2 + 1, x^2 + x + 1, x^3 + x + 1, x^3 + x^2 + 1, x^3 + 1] print('Polynomial Irreducible? Factorization')for f in test_polys: print(f'{str(f)} {str(f.is_irreducible())} {f.factor()}')

Notice that x2+1=(x+1)2x^2 + 1 = (x+1)^2 over F2\mathbb{F}_2. This is because 1=1-1 = 1 in F2\mathbb{F}_2, so x2+1=x21=(x1)(x+1)=(x+1)(x+1)x^2 + 1 = x^2 - 1 = (x-1)(x+1) = (x+1)(x+1).

Meanwhile, x2+x+1x^2 + x + 1 is irreducible over F2\mathbb{F}_2: we check f(0)=10f(0) = 1 \ne 0 and f(1)=1+1+1=10f(1) = 1 + 1 + 1 = 1 \ne 0, so no roots, and for degree 2, no roots implies irreducible.


3. Irreducibility Depends on the Field!

This is the single most important thing to understand about irreducibility: it is not a property of the polynomial alone, but of the polynomial together with the field.

Let's see x2+1x^2 + 1 across several fields:

# x^2 + 1 over different fields print('Field Irreducible? Factorization') for F, name in [(GF(2), 'F_2'), (GF(3), 'F_3'), (GF(5), 'F_5'), (GF(7), 'F_7'), (GF(11), 'F_11'), (GF(13), 'F_13')]: R.<x> = PolynomialRing(F) f = x^2 + 1 print(f'{name} {str(f.is_irreducible())} {f.factor()}')

Checkpoint: Look at the pattern above. Over which primes pp is x2+1x^2 + 1 irreducible?

Hint: x2+1x^2 + 1 has a root in Fp\mathbb{F}_p iff 1-1 is a quadratic residue mod pp, which happens iff p1(mod4)p \equiv 1 \pmod{4} (or p=2p = 2). So x2+1x^2+1 is irreducible over Fp\mathbb{F}_p precisely when p3(mod4)p \equiv 3 \pmod{4}.

Verify: 333 \equiv 3, 737 \equiv 3, 113(mod4)11 \equiv 3 \pmod{4}, all irreducible. 515 \equiv 1, 131(mod4)13 \equiv 1 \pmod{4}, all reducible. And p=2p=2 is special (1=1-1 = 1).

Misconception Alert

"If a polynomial has no roots, then it must be irreducible."

This is only true for polynomials of degree 2 or 3! For degree 4\ge 4, a polynomial can have no roots and still factor into two polynomials of degree 2\ge 2.

Example: over F2\mathbb{F}_2, the polynomial x4+x2+1x^4 + x^2 + 1 has no roots (f(0)=1f(0) = 1, f(1)=1f(1) = 1), but it is not irreducible:

# Misconception buster: no roots does NOT always mean irreducible R.<x> = PolynomialRing(GF(2)) f = x^4 + x^2 + 1 # Check roots print(f'f(0) = {f(0)}') print(f'f(1) = {f(1)}') print(f'Has any roots? No!') print() # But is it irreducible? print(f'Is irreducible? {f.is_irreducible()}') print(f'Factorization: {f.factor()}') print() print('It factors as a product of two degree-2 polynomials!') print('Neither factor has degree 1, so the original polynomial has no LINEAR factors (= no roots),') print('but it is still reducible.')

Takeaway: For degree 2 or 3, checking for roots is sufficient to test irreducibility (because if it factors, at least one factor must have degree 1, i.e., it has a root). For degree 4\ge 4, you must use proper factorization or is_irreducible().


4. Unique Factorization: Every Polynomial Factors into Irreducibles

Just as the Fundamental Theorem of Arithmetic says every integer >1> 1 factors uniquely into primes, we have:

Unique Factorization in F[x]F[x]: Every polynomial of degree 1\ge 1 in F[x]F[x] can be written uniquely (up to ordering and constant multiples) as a product of irreducible polynomials.

SageMath's .factor() gives us this decomposition directly.

# Unique factorization in F_5[x] R.<x> = PolynomialRing(GF(5)) polys = [ x^4 + 1, x^5 - x, # Hint: Fermat's little theorem! x^6 + x^3 + 2*x + 1, ] for f in polys: print(f'{f} = {f.factor()}') print()

Notice x5x=x(x1)(x2)(x3)(x4)=x(x+4)(x+3)(x+2)(x+1)x^5 - x = x(x-1)(x-2)(x-3)(x-4) = x(x+4)(x+3)(x+2)(x+1) over F5\mathbb{F}_5. Every element of F5\mathbb{F}_5 is a root! This is Fermat's Little Theorem in disguise: ap=aa^p = a for all aFpa \in \mathbb{F}_p, so xpxx^p - x has all pp elements as roots.


5. Counting Irreducibles: How Many Are There?

If we want to build a field GF(pn)\mathrm{GF}(p^n), we need an irreducible polynomial of degree nn over Fp\mathbb{F}_p. But do they always exist? How many are there?

The number of monic irreducible polynomials of degree nn over Fp\mathbb{F}_p is given by the necklace formula:

Np(n)=1ndnμ(n/d)pdN_p(n) = \frac{1}{n} \sum_{d \mid n} \mu(n/d) \, p^d

where μ\mu is the Mobius function. For large nn, this is approximately pn/np^n / n.

The key fact: Np(n)1N_p(n) \ge 1 for all pp and n1n \ge 1. So irreducible polynomials of every degree always exist, and we can always build GF(pn)\mathrm{GF}(p^n).

# Count monic irreducibles of each degree over F_2 print('Monic irreducible polynomials over F_2 by degree:') print('Degree Count Approx 2^n/n Examples') for n in range(1, 9): R.<x> = PolynomialRing(GF(2)) # List all monic polynomials of degree n and filter irreducibles count = 0 examples = [] for f in R.polynomials(of_degree=n): if f.is_monic() and f.is_irreducible(): count += 1 if len(examples) < 2: examples.append(str(f)) approx = round(2^n / n, 1) print(f'{n} {count} {approx} {" , ".join(examples)}')

For degree 8 over F2\mathbb{F}_2, there are 30 monic irreducible polynomials. AES uses one specific choice: x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1. Let's verify:

# The AES irreducible polynomial R.<x> = PolynomialRing(GF(2)) aes_poly = x^8 + x^4 + x^3 + x + 1 print(f'AES polynomial: {aes_poly}') print(f'Is irreducible over F_2? {aes_poly.is_irreducible()}') print(f'Degree: {aes_poly.degree()}') print() print('This polynomial is used to construct GF(2^8) = GF(256),') print('the field in which all AES byte-level operations take place.') print(f'GF(2^8) has {2^8} elements, one for each possible byte value.')

Crypto Foreshadowing: In Module 03, we will build GF(28)\mathrm{GF}(2^8) explicitly using this polynomial and see how AES performs its MixColumns and SubBytes operations as polynomial arithmetic modulo x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1. The choice of irreducible polynomial does not affect the field structure (all choices give isomorphic fields), but it does affect implementation efficiency.


Exercises

Exercise 1: Division Algorithm Verification (Fully Worked)

Task: Divide f=x5+3x3+x+2f = x^5 + 3x^3 + x + 2 by g=x2+1g = x^2 + 1 over F7[x]\mathbb{F}_7[x]. Verify the division algorithm.

# Exercise 1: Fully worked solution R.<x> = PolynomialRing(GF(7)) f = x^5 + 3*x^3 + x + 2 g = x^2 + 1 # Step 1: Compute quotient and remainder q, r = f.quo_rem(g) print(f'f = {f}') print(f'g = {g}') print(f'q = {q}') print(f'r = {r}') print() # Step 2: Verify f = q*g + r print(f'q * g + r = {q*g + r}') print(f'Equals f? {q*g + r == f}') print() # Step 3: Verify deg(r) < deg(g) print(f'deg(r) = {r.degree()}') print(f'deg(g) = {g.degree()}') print(f'deg(r) < deg(g)? {r.degree() < g.degree()}') print() # Step 4: Alternative syntax print(f'f // g = {f // g}') # same as q print(f'f % g = {f % g}') # same as r

Exercise 2: Finding Irreducibles (Guided)

Task: Find all monic irreducible polynomials of degree 3 over F3\mathbb{F}_3. There should be 8 of them (since N3(3)=(333)/3=8N_3(3) = (3^3 - 3)/3 = 8).

Fill in the TODOs below.

# Exercise 2: Find all monic irreducible cubics over F_3 R.<x> = PolynomialRing(GF(3)) irreducibles = [] # TODO 1: Loop over all monic polynomials of degree 3 over F_3. # A monic cubic has the form x^3 + a*x^2 + b*x + c # where a, b, c are each in {0, 1, 2}. # # for a in GF(3): # for b in GF(3): # for c in GF(3): # f = x^3 + a*x^2 + b*x + c # TODO 2: For each polynomial, check if it is irreducible. # If so, append it to the irreducibles list. # # if f.is_irreducible(): # irreducibles.append(f) # Print results print(f'Found {len(irreducibles)} monic irreducible cubics over F_3:') for f in irreducibles: print(f' {f}') # TODO 3: Verify: for each irreducible you found, check that it has # no roots in F_3. (Why is this sufficient for degree 3?) # # for f in irreducibles: # roots = [a for a in GF(3) if f(a) == 0] # print(f'{f} has roots: {roots}')

Exercise 3: Field-Dependent Irreducibility (Independent)

Task: Consider the polynomial f=x4+1f = x^4 + 1.

  1. Factor ff over F2\mathbb{F}_2, F3\mathbb{F}_3, F5\mathbb{F}_5, F7\mathbb{F}_7, F11\mathbb{F}_{11}, and F13\mathbb{F}_{13}.

  2. Is ff irreducible over any of these fields?

  3. Investigate: is x4+1x^4 + 1 irreducible over Fp\mathbb{F}_p for any prime pp? What do you observe?

Hint: This is a famous example. The answer may surprise you, x4+1x^4 + 1 is irreducible over Q\mathbb{Q} but reducible over every finite field Fp\mathbb{F}_p.

# Exercise 3: Your code here # Factor x^4 + 1 over various finite fields and investigate.

Summary

ConceptKey idea
Division algorithmFor any f,gF[x]f, g \in F[x] with g0g \ne 0, unique q,rq, r exist with f=qg+rf = q \cdot g + r and deg(r)<deg(g)\deg(r) < \deg(g). SageMath: f.quo_rem(g), f // g, f % g
Irreducible polynomialsThe polynomial analogue of prime numbers. They cannot be factored into lower-degree polynomials, and they build all polynomials via unique factorization
Irreducibility depends on the fieldThe same polynomial can be irreducible over one field and reducible over another. Always specify the field!
No-roots test (degree 2 and 3 only)For degree 4\ge 4, a polynomial can have no roots yet still be reducible, factoring into two quadratics for example
Counting irreduciblesThere are approximately pn/np^n / n monic irreducible polynomials of degree nn over Fp\mathbb{F}_p, and at least one always exists. This guarantees we can always construct GF(pn)\mathrm{GF}(p^n)
Crypto connectionAES uses the irreducible x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1 over F2\mathbb{F}_2 to build GF(28)\mathrm{GF}(2^8), the field where all byte-level AES operations live

Next: Quotient Rings and Field Extensions, where we use irreducible polynomials to actually construct new fields.