Discrete Logarithm Calculator

Number Theory

Discrete logarithmx = 7
Order of the base modulo p22
Search space (p - 1)22
Verification g^x mod p17
How this was calculated
  1. The discrete logarithm problem asks for the exponent x with 5^x = 17 (mod 23): raise the base 5 to an unknown power until it lands on the target 17 modulo the prime 23.
  2. The baby-step giant-step method uses a block size of m = 5, storing 5 baby steps of the base and then walking the target down by the giant-step factor until the two meet.
  3. The smallest solution is x = 7, and it checks out: 5^7 mod 23 = 17.
  4. Solutions repeat with period equal to the order of the base, here 22. For cryptographic-size primes this search is infeasible, which is exactly what Diffie-Hellman key exchange relies on.

Exponentiation is easy; undoing it is one of the hardest problems in mathematics. Given 5^x mod 23 = 17, find x. Trying powers one by one works for a toy modulus - the answer is 7 - but the same problem with a 300-digit prime is the wall that Diffie-Hellman key exchange leans against: no known classical method climbs it in reasonable time.

This calculator solves the discrete logarithm for moduli up to 100,000 using the baby-step giant-step algorithm, the classic square-root speedup: instead of up to p trials, it needs about sqrt(p) stored values and sqrt(p) steps. It also reports the order of the base - the length of its power cycle - and verifies its own answer by recomputing g^x mod p.

Some instances have no answer at all, and the calculator says so: modulo 7, the powers of 2 cycle through 1, 2 and 4 forever, so 2^x = 5 (mod 7) is unsolvable - not hard, impossible.

Worked examples

5^x = 17 (mod 23)

(5 · 17 · 23)

Discrete logarithmx = 7
Order of the base modulo p22
Search space (p - 1)22
Verification g^x mod p17

2^x = 13 (mod 23)

(2 · 13 · 23)

Discrete logarithmx = 7
Order of the base modulo p11
Search space (p - 1)22
Verification g^x mod p13

2^x = 5 (mod 7) - no solution

(2 · 5 · 7)

Discrete logarithmNo solution
Order of the base modulo p3
Search space (p - 1)6
Verification g^x mod pNo match

The problem in plain terms

Write the powers of 5 modulo 23 in a row: 5, 2, 10, 4, 20, 8, 17 - there it is, at position 7. The discrete logarithm is the position of the target in that row. For small moduli the row is short and the search is trivial; the entire difficulty is that the row's length grows with the modulus while each entry still looks random.

That one-way character - cheap to exponentiate, expensive to invert - is exactly what public-key cryptography purchases. Diffie-Hellman publishes g and g^x openly precisely because extracting x is the discrete logarithm problem.

Baby-step giant-step

The algorithm, due to Shanks, splits the unknown exponent as x = i x m - j with m about sqrt(p). It first tabulates the baby steps g^j for j up to m, then computes the giant-step factor g^(-m) and walks the target h down by it, checking after each stride whether the result sits in the baby-step table. A match assembles the exponent from the two indices.

For the worked example modulo 23, m is 5: five baby steps are stored, and the target is found after a single giant stride combined with baby step 2, giving x = 1 x 5 + 2 = 7. The same idea at cryptographic scale still fails - sqrt of a 300-digit number is astronomically large - which is the point.

Orders and unreachable targets

The powers of a base modulo a prime form a cycle whose length is the order of the base. Modulo 23, the base 5 has order 22 - its powers visit every nonzero residue, so every target is reachable. The base 2 modulo 23 has order 11: only eleven residues ever appear, and a target outside them has no logarithm.

The starkest case on this page is modulo 7, where 2 has order 3 and its entire universe is {1, 2, 4}. Asking for the logarithm of 5 is asking for a position in a row that never contains it. The calculator detects this by exhausting the cycle and reporting no solution rather than a wrong number.

Uniqueness up to the order

When a solution exists it is never unique as an integer: adding the order of the base gives another, because g^(x + order) = g^x x g^order = g^x. The calculator reports the smallest non-negative solution, the canonical representative - for 5^x = 17 (mod 23), x = 7, with 29, 51 and so on equally valid as exponents.

This periodicity is built into cryptographic practice: exponents are always understood modulo the order of the base, and well-chosen groups use a prime order so that every non-identity element generates the whole group.

Why the problem is hard

Decades of attacks have improved on brute force - baby-step giant-step, Pollard's rho, index calculus, and the number field sieve for prime fields - yet the best classical methods remain subexponential at best and utterly infeasible at cryptographic sizes. That track record, not a proof, is the security foundation.

The foundation has a known expiry condition: Shor's quantum algorithm solves discrete logarithms in polynomial time. It is the reason post-quantum cryptography exists, and why the toy problem on this page - solvable in microseconds - is simultaneously a live research frontier.

Using the calculator

Enter the base, target and a prime modulus. Composite moduli are accepted arithmetically but the theory - orders dividing p-1, guaranteed cycles - is stated for primes, and the calculator's framing assumes them. Keep the base and modulus coprime; a base sharing a factor with the modulus never cycles back to 1.

The verification line is worth reading every time: it recomputes g^x mod p from the reported x, so the answer carries its own proof - a luxury the cryptographic-scale problem conspicuously lacks.

Pollard's rho: the smarter search

Baby-step giant-step trades memory for time: it stores about sqrt(p) values. Pollard's rho algorithm achieves the same square-root expected time with almost no memory, by walking pseudorandomly through the group and waiting for a collision - the birthday paradox doing the heavy lifting. For the toy moduli on this page both are instant; at serious sizes rho is the workhorse for generic groups.

Neither touches the structure of prime fields, where index calculus and the number field sieve exploit the integers themselves and run in subexponential time. That gap - generic groups hard, prime fields less hard - shapes real protocol choices, including the preference for well-chosen elliptic curve groups.

Elliptic curve version

Replace modular multiplication by point addition on an elliptic curve and the same problem reappears: given points P and Q = xP, find x. No index calculus is known for well-chosen curves, so the elliptic curve discrete logarithm supports smaller keys at equal security - the reason modern TLS certificates are a fraction of the size RSA would need.

The calculator's prime-field version is the right training ground: orders, cycles, unreachable targets and square-root algorithms all transfer to the curve setting with only the group operation changed.

Choosing parameters in practice

Real Diffie-Hellman groups are chosen so the order of the base has a large prime factor - otherwise the Pohlig-Hellman algorithm reduces the logarithm to small, easy pieces modulo each small factor. The order output on this page is the toy echo of that design rule: an order like 22 = 2 x 11 would be a warning sign in production, because the problem splits into trivial parts.

Safe primes, where (p - 1) / 2 is itself prime, exist precisely to force the order question to have a good answer for almost every base.

A hand-worked tiny case

Solve 3^x = 4 (mod 7) by listing: 3, 2, 6, 4 - position 4. The listing is the algorithm at its most honest, and everything else - tables, strides, rho walks - is bookkeeping to avoid listing at scale. Notice also what the list shows: 3 has order 6 modulo 7, visiting every nonzero residue, so modulo 7 every target has a logarithm base 3.

Contrast base 2 modulo 7 with its cycle of length 3: the same modulus, a different universe of solvability. The base is not a detail; it is half the problem.

Discrete logs beyond cryptography

The same mathematics indexes points on elliptic curves in coding theory, underlies some zero-knowledge proof systems, and appears in the analysis of pseudorandom generators - anywhere a one-way exponentiation is needed. Chemists meet a cousin of it in NMR phase problems; the structural idea recurs wherever cycles and hidden exponents meet.

Learning it on toy primes, with a calculator that shows its verification, builds the intuition those applications assume: cycles, orders, and the cliff between computing a power and recovering one.

FAQs

What is a discrete logarithm?

Given a base g, a target h and a modulus p, it is the exponent x such that g^x = h (mod p) - the inverse operation of modular exponentiation.

What is the discrete log of 17 base 5 modulo 23?

7, because 5^7 mod 23 = 17. The calculator verifies this by recomputing the power.

Can a discrete logarithm have no solution?

Yes. If h never appears among the powers of g - like 5 among the powers of 2 modulo 7, which cycle through 1, 2 and 4 - no exponent exists.

What is baby-step giant-step?

Shanks' algorithm: it splits the exponent into blocks of about sqrt(p), storing baby steps in a table and striding the target down by giant steps until they meet - a square-root speedup over brute force.

What is the order of the base?

The number of distinct powers of g modulo p before the cycle repeats - 5 has order 22 modulo 23, while 2 has order 11.

Why is the discrete logarithm important?

Its hardness secures Diffie-Hellman key exchange and related cryptography; no efficient classical algorithm is known, though Shor's quantum algorithm would break it.

What is Pohlig-Hellman in one sentence?

An algorithm that solves a discrete logarithm cheaply when the order of the base factors into small primes - which is why cryptographic groups are chosen with a large prime order factor.

What is the discrete log of 4 base 3 modulo 7?

4 - the powers of 3 modulo 7 run 3, 2, 6, 4, so the target appears at position 4.

Sources

Related calculators

Correlation Coefficient Calculator
Pearson's r for up to six paired observations, with r² and the deviation sums shown — the full workings, not just the headline number.
Lognormal Distribution Calculator
Compute the CDF, PDF, mean and median of a lognormal distribution from its log-scale mu and sigma.
Sphere Volume Calculator
Volume and surface area of a sphere from its radius — plus diameter, great-circle circumference and the hemisphere figure.