Discrete Logarithm Calculator

Find a small integer x such that gˣ ≡ h (mod p) by educational brute force. This educational calculator shows the formula, result, and step-by-step interpretation.

Discrete MathCryptography

Calculator

What this calculator teaches

The discrete logarithm problem is easy for tiny examples but hard for large cryptographic groups.

Use the result as a learning aid. For classwork, still show the formula and intermediate reasoning so the final answer is not just a black-box number.

What a Discrete Logarithm Means

A discrete logarithm asks for an integer exponent x satisfying gˣ ≡ h (mod p). It is analogous to an ordinary logarithm because x is the unknown exponent, but all arithmetic takes place in a finite modular system. For small teaching examples, the simplest method is to list powers of g, reduce each modulo p, and stop when the residue equals h.

A solution is not guaranteed for every choice of g, h, and modulus. The powers of g generate a subgroup of the nonzero residues, and h must lie in that subgroup. If p is prime and g is a primitive root modulo p, then every nonzero residue occurs among g⁰,g¹,…,g^(p−2), so each nonzero h has a discrete logarithm modulo p−1. If g has smaller order, only some targets can be reached.

IdeaRule
Target equationgˣ ≡ h (mod p)
Search sequence1, g, g², g³, … (mod p)
Periodpowers repeat according to the order of g

Real cryptographic systems use enormous groups where brute-force search is infeasible. This page is intentionally educational and suited only to small examples. Efficient algorithms such as baby-step giant-step or Pollard rho are used for larger research examples, while cryptographic security depends on choosing groups where the discrete logarithm problem remains computationally difficult.

Checking a discrete-log answer is easy even when finding it is hard. Once a candidate x is known, compute gˣ mod p and verify that the result equals h mod p. This asymmetry—easy verification but potentially difficult search—is one reason discrete logarithms are important in cryptography.

For a small modulus, also list the cycle of powers of g. The number of distinct nonzero residues before the sequence returns to 1 is the order of g. If the target h never appears in that cycle, there is no solution for that base. If it does appear, all solutions repeat modulo the order of g, so the first occurrence gives the smallest nonnegative answer.

Formula & Symbols

ConceptFormula or rule
Discrete logarithmgˣ ≡ h (mod p)
Brute-force searchtry x = 0,1,2,...

Worked example

Example: For small p, the calculator can search powers of g until it reaches h.
Example 2: Solve 2ˣ ≡ 8 (mod 13). Since 2³=8, x=3.
Example 3: Solve 3ˣ ≡ 5 (mod 7). The residues 1,3,2,6,4,5 show that x=5.
Example 4: Solve 2ˣ ≡ 3 (mod 7). Powers 1,2,4 repeat, so 3 is never reached; there is no solution.
Example 5: Solve 5ˣ ≡ 1 (mod 11). The smallest nonnegative solution is x=0.

Common mistakes

⚠️
Using the wrong input format

Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.

⚠️
Ignoring assumptions

Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.

⚠️
Assuming every target has a solution

If h is outside the subgroup generated by g, no exponent will produce it.

⚠️
Confusing ordinary and modular logarithms

Taking ln(h)/ln(g) does not solve a modular exponent equation because modular reduction changes the structure completely.

FAQ

Can I use decimals?
Most numerical calculators allow decimals, but modular arithmetic and coding-theory tools usually require integers or binary strings.
Is this for homework checking?
Yes. The page is designed to show both the answer and the reasoning pattern.
Why does the result sometimes say approximate?
Some probability, floating-point, and numerical methods naturally produce approximations.
What is the smallest discrete logarithm?
Usually the smallest nonnegative exponent x satisfying the congruence is reported.
Why can several exponents work?
Powers repeat modulo p. If g has order r, then exponents differing by multiples of r give the same residue.
Does p have to be prime?
No, but prime moduli give a cleaner multiplicative-group structure and are common in introductory examples.
Is brute force suitable for cryptographic sizes?
No. This calculator is for small educational inputs only; cryptographic groups are far too large for a simple linear search.

Related calculators

These links will work after the calculators are registered in the final Math layout update.

Formula Explorer connections

Interpretation: This formula describes an algebraic relationship among variables, functions, equations, roots or sequences. Assumption: Respect the expression’s domain and excluded values. Check roots in the original equation because transformations can introduce extraneous solutions.

Domain and Range Calculator →Ellipse Equation Calculator →Equation of a Line Calculator →Math Formula Explorer →