Maths & numbers

Discrete logarithm calculator using baby-step giant-step

Solve baseˣ ≡ target modulo a prime, report the smallest nonnegative exponent and show the matching search steps.

Your numbers

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A discrete logarithm reverses modular exponentiation: the base, target remainder and prime modulus are given, and the exponent is unknown. Ordinary logarithms do not invert reduction modulo a prime. Baby-step giant-step divides the search into short powers of the base and larger inverse strides from the target. Matching the two lists produces an exponent that is checked by an exact modular power.

THE FORMULAtarget × base^(−i·w) ≡ baseʲ; x = i·w + j; w = ceil(√(p−1)).

A worked example

Solve 5ˣ ≡ 8 modulo 23. Width five gives baby residues 1, 5, 2, 10 and 4 at exponents zero through four. Since 5⁵ ≡ 20 and its inverse is 15, one giant step from 8 gives 8 × 15 ≡ 5. This matches baby exponent one, so x = 1 × 5 + 1 = 6. Indeed 5⁶ = 15,625 leaves remainder 8.

Good to know

Supports prime moduli up to one billion with nonzero base and target residues. Composite-modulus logarithms and noninvertible inputs require different algorithms. Memory and work grow roughly with the square root of p. This bounded educational calculation is not a solver for modern cryptographic-size groups.

A table collision recovers the exponent

Write a candidate exponent as i·w + j, where j lies from zero to w − 1. The equation base^(i·w+j) = target becomes base^j = target × base^(−i·w). The left side is stored in a baby-step map. The right side is generated by repeated giant strides. A collision identifies both i and j, and therefore a concrete exponent.

The result table shows the first giant strides and the matching baby index when it occurs within those displayed rows. Final metrics report the answer even when the trace is truncated. The search keeps the smallest baby index for duplicate residues and examines giant blocks in increasing order. This returns the smallest nonnegative exponent in the complete range zero through p − 2, rather than an arbitrary representative.

Baby-step giant-step finds five to the sixth congruent to eight modulo twenty-three1j=05j=12j=210j=34j=4Giant i = 1 meets baby j = 1Exponent = 1 × 5 + 1 = 6
Width five gives baby residues 1, 5, 2, 10, 4. One giant step turns target 8 into residue 5, matching baby index 1. The exponent is 1 × 5 + 1 = 6.

A prime modulus does not guarantee a solution

The nonzero residues modulo a prime form a group, but a chosen base may generate only a subgroup. Modulo seven, powers of two visit 1, 2 and 4. Target three never occurs, so the discrete logarithm has no solution for that base. The calculator reports this mathematical outcome instead of asking for a larger exponent. Zero residues are rejected because they are outside the multiplicative group used by the method.

If x is one solution, adding the multiplicative order of the base gives another. This periodicity explains why the page reports a smallest representative. Use the primitive-root calculator to find a base that generates every nonzero target; use the existing multiplicative-order calculator to find the repetition period of a particular base. The existing modular exponentiation page computes a remainder from a known exponent, while this page solves the inverse task.

A few questions, answered

Can the smallest exponent be zero?

Yes. For every nonzero base, exponent zero gives residue one. Target one therefore returns zero.

Why not divide ordinary logarithms?

The congruence identifies numbers that differ by multiples of the modulus. Taking ordinary logarithms does not preserve that equivalence or reveal the missing multiple.

Find your calculator

Start typing to explore the library.