Lesson 14 of 15

Finite Fields

Finite Fields

A finite field (or Galois field) GF(q)\text{GF}(q) has exactly qq elements, where qq must be a prime power: q=pkq = p^k.

GF(p)\text{GF}(p) — Prime Fields

When k=1k = 1, GF(p)=Zp\text{GF}(p) = \mathbb{Z}_p. These are the simplest finite fields.

Multiplicative Group Structure

The nonzero elements of GF(p)\text{GF}(p) form a cyclic group under multiplication. A primitive root (generator of this group) gg satisfies:

{g1,g2,,gp1}={1,2,,p1}\{g^1, g^2, \ldots, g^{p-1}\} = \{1, 2, \ldots, p-1\}

Finding Primitive Roots

An element gg is a primitive root mod pp if and only if g(p1)/q≢1(modp)g^{(p-1)/q} \not\equiv 1 \pmod{p} for every prime factor qq of p1p - 1.

Discrete Logarithm

Given a primitive root gg, the discrete logarithm logg(a)\log_g(a) is the unique kk such that gka(modp)g^k \equiv a \pmod{p}.

def discrete_log(g, a, p):
    power = 1
    for k in range(p - 1):
        if power == a:
            return k
        power = (power * g) % p
    return -1

The difficulty of computing discrete logarithms in large fields is the basis of many cryptographic systems.

Your Task

Implement is_primitive_root(g, p), find_primitive_roots(p) returning all sorted primitive roots, and discrete_log(g, a, p).

Pyodide loading...
Loading...
Click "Run" to execute your code.