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 read

    Intuition

    Every public-key exchange needs a computation that is easy to do in one direction and, as far as anyone knows, infeasible to undo in the other. Multiplication and factoring are one such pair for RSA; exponentiation and its inverse are the pair here. Raise a number to a power modulo a prime, and the answer falls out in a handful of squarings, however large the exponent (the previous page covers exactly that). Run the process backwards, given only the base, the modulus and the result, and there is no known shortcut: an attacker is reduced to something close to trying values one at a time. 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.

    Mechanism

    For a prime pp and an integer aa not divisible by pp, the order of aa modulo pp is the smallest positive nn with an1(modp)a^n \equiv 1 \pmod{p}. By Fermat’s little theorem, ap11(modp)a^{p-1} \equiv 1 \pmod{p} always holds, so the order of aa is guaranteed to be at most p1p - 1. For most aa, though, it is smaller than that, 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 p1p - 1, the maximum possible, is a primitive root of pp. Its powers g1,g2,,gp1g^1, g^2, \ldots, g^{p-1} run through every nonzero residue modulo pp exactly once before returning to 11.

    Worked example

    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. 21mod19=22^1 \bmod 19 = 2
    2. 22mod19=42^2 \bmod 19 = 4
    3. 23mod19=82^3 \bmod 19 = 8
    4. 24mod19=162^4 \bmod 19 = 16
    5. 25mod19=32mod19=132^5 \bmod 19 = 32 \bmod 19 = 13
    6. 26mod19=26mod19=72^6 \bmod 19 = 26 \bmod 19 = 7
    7. 27mod19=142^7 \bmod 19 = 14
    8. 28mod19=28mod19=92^8 \bmod 19 = 28 \bmod 19 = 9
    9. 29mod19=182^9 \bmod 19 = 18
    10. 210mod19=36mod19=172^{10} \bmod 19 = 36 \bmod 19 = 17
    11. 211mod19=34mod19=152^{11} \bmod 19 = 34 \bmod 19 = 15
    12. 212mod19=30mod19=112^{12} \bmod 19 = 30 \bmod 19 = 11
    13. 213mod19=22mod19=32^{13} \bmod 19 = 22 \bmod 19 = 3
    14. 214mod19=62^{14} \bmod 19 = 6
    15. 215mod19=122^{15} \bmod 19 = 12
    16. 216mod19=24mod19=52^{16} \bmod 19 = 24 \bmod 19 = 5
    17. 217mod19=102^{17} \bmod 19 = 10
    18. 218mod19=20mod19=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 25mod19=132^5 \bmod 19 = 13. The Week 4 supplement prints this row as 25mod19=122^5 \bmod 19 = 12, which is wrong: 25=322^5 = 32, and 3219=1332 - 19 = 13, not 1212. This is a source arithmetic slip, not a different convention. Every other explicitly printed row (rows 11 to 44, 66, 1717, 1818) matches the computation above exactly, and the source’s own next line, 26mod19=72^6 \bmod 19 = 7, is only reachable by doubling the correct 1313, not the printed 1212 (doubling 1212 gives 24mod19=524 \bmod 19 = 5, not 77). Rows 77 through 1616 are not printed in the source at all: it shows the first six rows, an ellipsis, then jumps to rows 1717 and 1818. They are filled in here by the same repeated-doubling computation so the order-1818 claim can be checked in full.

    Aside

    The six primitive roots of 1919 are not a separate fact to memorise: they 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 rather than a shorter one. The values of 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.

    Mechanism

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

    Formula

    Discrete logarithm problem (DLP)

    y=gxmodpy = 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.

    Mechanism

    Two directions, two different costs.

    Forward: exponentiation, easy. Given gg, xx and pp, computing y=gxmodpy = g^x \bmod p is fast: the previous page’s square-and-multiply method does it in about log2x\log_2 x modular multiplications, regardless of how large xx is. For p=19p = 19, g=2g = 2, x=7x = 7: y=27mod19=128mod19=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=2xmod198 = 2^x \bmod 19 for xx means scanning the table until 2x2^x lands on 88. Row 33 gives 23mod19=82^3 \bmod 19 = 8, so x=3x = 3. That search only finishes at a glance here because p=19p = 19 is small enough to hold the whole table in view. The lecture’s point survives the toy scale: for a prime with hundreds of digits, the equivalent search space is far too large to scan, and no faster general algorithm is known.

    Exam detail

    “No known faster algorithm” is doing real work in that last sentence: it is a computational hardness assumption, not a proof. Nobody has proven the discrete logarithm problem is hard in the way PNPP \neq NP would make precise; 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. This is the same status RSA’s hardness assumption has, and the exam draws the same distinction for both: believed hard, not proven hard.

    Mechanism

    This asymmetry, 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 gamodpg^a \bmod p and gbmodpg^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.