# Pohlig–Hellman algorithm

In group theory, the Pohlig–Hellman algorithm, sometimes credited as the Silver–Pohlig–Hellman algorithm, is a special-purpose algorithm for computing discrete logarithms in a finite abelian group whose order is a smooth integer.
The algorithm was introduced by Roland Silver, but first published by Stephen Pohlig and Martin Hellman, who credit Silver with its earlier independent but unpublished discovery. Pohlig and Hellman also list Richard Schroeppel and H. Block as having found the same algorithm, later than Silver, but again without publishing it.

## Groups of prime-power order

As an important special case, which is used as a subroutine in the general algorithm (see below), the Pohlig–Hellman algorithm applies to groups whose order is a prime power. The basic idea of this algorithm is to iteratively compute the $p$-adic digits of the logarithm by repeatedly "shifting out" all but one unknown digit in the exponent, and computing that digit by elementary methods.
(Note that for readability, the algorithm is stated for cyclic groups — in general, $G$ must be replaced by the subgroup $\langle g\rangle$ generated by $g$, which is always cyclic.)
    Input. A cyclic group $G$ of order $n=p^{e}$ with generator $g$ and an element $h\in G$.
    Output. The unique integer $x\in \{0,\dots ,n-1\}$ such that $g^{x}=h$.
   * Initialize $x_{0}:=0.$
   * Compute $\gamma :=g^{p^{e-1}}$. By Lagrange's theorem, this element has order $p$.
   * For all $k\in \{0,\dots ,e-1\}$, do:
   ** Compute $h_{k}:=(g^{-x_{k}}h)^{p^{e-1-k}}$. By construction, the order of this element must divide $p$, hence $h_{k}\in \langle \gamma \rangle$.
   ** Using the baby-step giant-step algorithm, compute $d_{k}\in \{0,\dots ,p-1\}$ such that $\gamma ^{d_{k}}=h_{k}$. It takes time $O({\sqrt {p}})$.
   ** Set $x_{k+1}:=x_{k}+p^{k}d_{k}$.
   * Return $x_{e}$.
The algorithm computes discrete logarithms in time complexity $O(e{\sqrt {p}})$, far better than the baby-step giant-step algorithm's $O({\sqrt {p^{e}}})$ when $e$ is large.

## The general algorithm

In this section, we present the general case of the Pohlig–Hellman algorithm. The core ingredients are the algorithm from the previous section (to compute a logarithm modulo each prime power in the group order) and the Chinese remainder theorem (to combine these to a logarithm in the full group).
(Again, we assume the group to be cyclic, with the understanding that a non-cyclic group must be replaced by the subgroup generated by the logarithm's base element.)
    Input. A cyclic group $G$ of order $n$ with generator $g$, an element $h\in G$, and a prime factorization n=∏$_{i=1}$$^r$p$_i$$^{ei}${\textstyle n=\prod _{i=1}^{r}p_{i}^{e_{i}}}.
    Output. The unique integer $x\in \{0,\dots ,n-1\}$ such that $g^{x}=h$.
   * For each $i\in \{1,\dots ,r\}$, do:
   ** Compute $g_{i}:=g^{n/p_{i}^{e_{i}}}$. By Lagrange's theorem, this element has order $p_{i}^{e_{i}}$.
   ** Compute $h_{i}:=h^{n/p_{i}^{e_{i}}}$. By construction, $h_{i}\in \langle g_{i}\rangle$.
   ** Using the algorithm above in the group $\langle g_{i}\rangle$, compute $x_{i}\in \{0,\dots ,p_{i}^{e_{i}}-1\}$ such that $g_{i}^{x_{i}}=h_{i}$.
   * Solve the simultaneous congruence $x\equiv x_{i}{\pmod {p_{i}^{e_{i}}}}\quad \forall i\in \{1,\dots ,r\}{\text{.}}$The Chinese remainder theorem guarantees there exists a unique solution $x\in \{0,\dots ,n-1\}$.
   * Return $x$.
The correctness of this algorithm can be verified via the classification of finite abelian groups: Raising $g$ and $h$ to the power of $n/p_{i}^{e_{i}}$ can be understood as the projection to the factor group of order $p_{i}^{e_{i}}$.

## Complexity

The worst-case input for the Pohlig–Hellman algorithm is a group of prime order: In that case, it degrades to the baby-step giant-step algorithm, hence the worst-case time complexity is ${\mathcal {O}}({\sqrt {n}})$. However, it is much more efficient if the order is smooth: Specifically, if $\prod _{i}p_{i}^{e_{i}}$ is the prime factorization of $n$, then the algorithm's complexity is ${\mathcal {O}}\left(\sum _{i}{e_{i}(\log n+{\sqrt {p_{i}}})}\right)$ group operations.