Discrete Logarithm

lean4-proofnumber-theoryvisualization
logga=k    gka(modp)\log_g a = k \iff g^k \equiv a \pmod{p}

Statement

Let pp be prime and gg a primitive root mod pp. The discrete logarithm of a(Z/pZ)×a \in (\mathbb{Z}/p\mathbb{Z})^\times base gg is the unique integer kk with 0k<p10 \le k < p-1 satisfying

gka(modp)g^k \equiv a \pmod{p}

Because gg generates (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times (by Primitive RootsPrimitive Rootsg is a primitive root mod p    {g0,g1,,gp2}=(Z/pZ)×g \text{ is a primitive root mod } p \iff \{g^0, g^1, \ldots, g^{p-2}\} = (\mathbb{Z}/p\mathbb{Z})^\timesA primitive root mod p is a generator of the cyclic group (Z/pZ)*, existing for every prime pRead more →), such kk always exists and is unique mod p1p-1. The hardness of computing kk from gg, aa, and pp is the Discrete Logarithm Problem (DLP), the security foundation of Diffie-Hellman and elliptic-curve cryptography.

Visualization

Discrete log table for g=2g = 2 in (Z/11Z)×(\mathbb{Z}/11\mathbb{Z})^\times:

kk2kmod112^k \bmod 11
01
12
24
38
45
510
69
77
83
96

Reading the table backwards: log25=4\log_2 5 = 4, log210=5\log_2 10 = 5, log27=7\log_2 7 = 7. Note that 2 hits all 10 nonzero residues, confirming it is a primitive root mod 11.

Proof Sketch

  1. (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is cyclic of order p1p-1, generated by gg.
  2. The map kgkk \mapsto g^k (for kZ/(p1)Zk \in \mathbb{Z}/(p-1)\mathbb{Z}) is a group isomorphism from (Z/(p1)Z,+)(\mathbb{Z}/(p-1)\mathbb{Z}, +) to ((Z/pZ)×,×)((\mathbb{Z}/p\mathbb{Z})^\times, \times).
  3. Therefore every element aa has a unique preimage kk — the discrete logarithm.
  4. The discrete log satisfies logg(ab)=logga+loggb(modp1)\log_g(ab) = \log_g a + \log_g b \pmod{p-1}, turning multiplication into addition.

Connections

Discrete logarithms exist in any cyclic group; the same concept applies to elliptic curve groups. The existence follows from the Primitive RootsPrimitive Rootsg is a primitive root mod p    {g0,g1,,gp2}=(Z/pZ)×g \text{ is a primitive root mod } p \iff \{g^0, g^1, \ldots, g^{p-2}\} = (\mathbb{Z}/p\mathbb{Z})^\timesA primitive root mod p is a generator of the cyclic group (Z/pZ)*, existing for every prime pRead more → theorem. The analogue over R\mathbb{R} is the ordinary logarithm from the Fundamental Theorem of CalculusFundamental Theorem of Calculusabf(x)dx=f(b)f(a)\int_a^b f'(x)\,dx = f(b) - f(a)Integration and differentiation are inverse operationsRead more → viewpoint.

Lean4 Proof

/-- If g generates (ZMod p)ˣ, then every element a has a discrete log. -/
theorem dlog_exists {p : } [Fact p.Prime]
    (g : (ZMod p)ˣ)
    (hg :  x : (ZMod p)ˣ, x  Subgroup.zpowers g)
    (a : (ZMod p)ˣ) :  n : , g ^ n = a := by
  obtain n, hn := hg a
  exact n, hn