CSEC3616Cybersecurity Engineering

    Discrete logarithms and primitive roots

    The order of an element modulo a prime, the primitive-root condition, the discrete logarithm problem, and why one direction is easy while the other is believed hard.

    • Define the order of an element modulo p and state the condition that makes it a primitive root.
    • Verify that 2 is a primitive root of 19 by computing its full sequence of powers.
    • State the discrete logarithm problem and identify which direction is easy and which is hard.
    • Explain why that asymmetry is what Diffie-Hellman key exchange relies on.

    16 min readJump to the quiz ↓

    A primitive root has the maximum order, so its powers visit every nonzero residue

    Every public-key exchange needs a computation that is easy in one direction and, as far as anyone knows, infeasible to undo in the other. Raise a number to a power modulo a prime and the answer falls out in a handful of squarings, however large the exponent. Run the process backwards, given only the base, the modulus and the result, and there is no known shortcut. That one-way street is called the discrete logarithm problem, and Diffie-Hellman key exchange, covered in Module 6, is built directly on top of it.

    For a prime pp and an integer aa not divisible by pp, the order of aa modulo pp is the smallest positive nn with an≡1(modp)a^n \equiv 1 \pmod{p}. By Fermat’s little theorem, ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p} always holds, so the order of aa is at most p−1p - 1. For most aa it is smaller, meaning the powers of aa cycle back to 11 early and never touch some of the other nonzero residues.

    An element whose order is exactly p−1p - 1, the maximum possible, is a primitive root of pp. Its powers g1,g2,…,gp−1g^1, g^2, \ldots, g^{p-1} run through every nonzero residue modulo pp exactly once before returning to 11.

    Try it — worked example

    Is 2 a primitive root of 19? Find its order.

    Show answer and steps

    Answer2 is a primitive root of 19, with order 18

    The lecture demonstrates this for p=19p = 19, g=2g = 2, printing rows 11 through 66 of the table, an ellipsis, and then rows 1717 and 1818. The full sequence, computed the same way at every step (multiply the previous result by 22 and reduce), is below.

    1. 21 mod 19=22^1 \bmod 19 = 2
    2. 22 mod 19=42^2 \bmod 19 = 4
    3. 23 mod 19=82^3 \bmod 19 = 8
    4. 24 mod 19=162^4 \bmod 19 = 16
    5. 25 mod 19=32 mod 19=132^5 \bmod 19 = 32 \bmod 19 = 13
    6. 26 mod 19=26 mod 19=72^6 \bmod 19 = 26 \bmod 19 = 7
    7. 27 mod 19=142^7 \bmod 19 = 14
    8. 28 mod 19=28 mod 19=92^8 \bmod 19 = 28 \bmod 19 = 9
    9. 29 mod 19=182^9 \bmod 19 = 18
    10. 210 mod 19=36 mod 19=172^{10} \bmod 19 = 36 \bmod 19 = 17
    11. 211 mod 19=34 mod 19=152^{11} \bmod 19 = 34 \bmod 19 = 15
    12. 212 mod 19=30 mod 19=112^{12} \bmod 19 = 30 \bmod 19 = 11
    13. 213 mod 19=22 mod 19=32^{13} \bmod 19 = 22 \bmod 19 = 3
    14. 214 mod 19=62^{14} \bmod 19 = 6
    15. 215 mod 19=122^{15} \bmod 19 = 12
    16. 216 mod 19=24 mod 19=52^{16} \bmod 19 = 24 \bmod 19 = 5
    17. 217 mod 19=102^{17} \bmod 19 = 10
    18. 218 mod 19=20 mod 19=12^{18} \bmod 19 = 20 \bmod 19 = 1
    19. All eighteen values are distinct, and 2182^{18} is the first power to return to 11. So the order of 22 modulo 1919 is 18=φ(19)18 = \varphi(19), and 22 is a primitive root.

    Pitfall

    Row 55 above is 25 mod 19=132^5 \bmod 19 = 13. The Week 4 supplement prints 25 mod 19=122^5 \bmod 19 = 12, which is wrong: 25=322^5 = 32, and 32−19=1332 - 19 = 13. The source’s own next line, 26 mod 19=72^6 \bmod 19 = 7, is only reachable by doubling the correct 1313 (doubling 1212 gives 55). Rows 77 through 1616 are not printed in the source at all, and are filled in here by the same repeated-doubling computation so the order-1818 claim can be checked in full.

    Optional aside

    The six primitive roots of 1919 fall out of the table above. A power 2k2^k is itself a primitive root exactly when gcd⁡(k,18)=1\gcd(k, 18) = 1, since only then does 2k2^k generate the full cycle. The kk from 11 to 1818 coprime to 1818 are 1,5,7,11,13,171, 5, 7, 11, 13, 17, and there are φ(18)=6\varphi(18) = 6 of them. Reading those rows off the table: 21=22^1 = 2, 25=132^5 = 13, 27=142^7 = 14, 211=152^{11} = 15, 213=32^{13} = 3, 217=102^{17} = 10. Sorted, that set is {2,3,10,13,14,15}\{2, 3, 10, 13, 14, 15\}, exactly the primitive roots the lecture lists.

    Exponentiation is easy and its inverse, the discrete logarithm, is not

    Ordinary logarithms invert exponentiation: given y=gxy = g^x, log⁡gy\log_g y recovers xx. The discrete logarithm is the modular analogue. For a prime pp and a primitive root gg of pp, y=gx mod p.y = g^x \bmod p.

    Formula

    Discrete logarithm problem (DLP)

    y=gx mod py = g^x \bmod p
    pp
    a prime
    gg
    a primitive root of p
    xx
    the discrete logarithm of y to base g
    yy
    the result, given

    Given g, x, p, finding y is easy. Given g, y, p, finding x is not: the DLP asks for the second direction.

    The two directions have different costs.

    Forward: exponentiation, easy. Given gg, xx and pp, computing y=gx mod py = g^x \bmod p is fast: the previous page’s square-and-multiply method does it in about log⁡2x\log_2 x modular multiplications, however large xx is. For p=19p = 19, g=2g = 2, x=7x = 7: y=27 mod 19=128 mod 19=14y = 2^7 \bmod 19 = 128 \bmod 19 = 14, matching row 77 of the table above.

    Reverse: the discrete logarithm, hard. Given gg, yy and pp, finding xx has no known method faster than something close to exhaustive search over every possible exponent. For p=19p = 19, g=2g = 2, y=8y = 8: solving 8=2x mod 198 = 2^x \bmod 19 means scanning the table until 2x2^x lands on 88. Row 33 gives 23 mod 19=82^3 \bmod 19 = 8, so x=3x = 3. That search only finishes at a glance because p=19p = 19 is small enough to hold the whole table in view. For a prime with hundreds of digits the search space is far too large to scan, and no faster general algorithm is known.

    Exam detail

    “No known faster algorithm” is a computational hardness assumption, not a proof. Nobody has proven the discrete logarithm problem hard; the best known general attacks (index calculus and its relatives) are still exponential or sub-exponential in the size of pp, and every cryptosystem built on the DLP is only as secure as that gap holding up. RSA’s hardness assumption has the same status, and the exam draws the same distinction for both: believed hard, not proven hard.

    That asymmetry is what Diffie-Hellman builds on

    Cheap to compute forward and expensive to invert is precisely what a public-key exchange needs. Diffie-Hellman key exchange, covered in Diffie-Hellman key exchange, has each party publish ga mod pg^a \bmod p and gb mod pg^b \bmod p over an open channel while keeping aa and bb private. Anyone listening sees gg, pp, and both public values, and reconstructing either private exponent from those is exactly the discrete logarithm problem. Public-key cryptography and trapdoor functions covers where this fits alongside the RSA problem as one of the two hardness assumptions Module 6 is built on.

    Recall

    Why does knowing the discrete logarithm problem is hard tell you nothing about how hard it is to compute g^x mod p in the forward direction?

    They are different computations with different costs. Forward exponentiation has an efficient algorithm (square-and-multiply) regardless of how large xx is. The discrete logarithm problem is about the absence of an efficient algorithm for the reverse direction: recovering xx from gg, yy and pp. A problem can be trivially easy one way and believed infeasible the other way at the same time; that gap is the entire point.

    Check yourself