Discrete Logarithms and Diffie-Hellman
August 25, 20267 min readbeginner
The second classical hard problem looks nothing like factoring. It is about exponents rather than products, and the trapdoor is placed differently.
The second classical hard problem looks nothing like factoring. It is about exponents rather than products, and the trapdoor is placed differently. Underneath, it satisfies the same three-clause contract, and it falls to the same quantum algorithm.
01.The forward direction
Fix a prime and a base , and raise to a power modulo . Take and , and list the powers as they come:
Each step is one multiplication by followed by one reduction modulo , which is the arithmetic from Modular Arithmetic. Check the fourth one by hand: , so .
Continuing to the end of the cycle:
Something worth noticing: the sixteen powers produce all sixteen non-zero values modulo , each exactly once, before returning to . A base that does this is called a generator, and it is the right kind of base to build a cryptosystem on, because it means the output could be anything.
02.Making the forward direction fast
Computing by sixteen separate multiplications is fine. Computing that way is not, and real exponents are astronomically larger than a million.
The trick is repeated squaring, and it is the same trick that made RSA encryption fast in the previous note. Instead of stepping up one power at a time, square repeatedly to leap:
Each squaring doubles the exponent for the price of one multiplication. To reach an arbitrary exponent, write it in binary and multiply together the squarings whose positions carry a .
Work an example modulo . To compute , write , so
Then , so that is , and . The table above gives , which matches.
The cost is about multiplications rather than of them. Reaching an exponent of a million takes about twenty multiplications. At cryptographic sizes, with several hundred digits long, the forward direction stays well under a millisecond.
03.The backward direction
Now reverse the question. You are told , , and that . Find .
With there are only sixteen candidates, so read the table: , giving . Trivial, and trivial for the same reason factoring was trivial. The modulus is tiny.
This backward problem is called the discrete logarithm problem. The name is an analogy with ordinary logarithms, where because . Over the real numbers that is easy, because the powers of increase steadily and you can home in on the answer by bisection. Modulo they do not increase at all. The sequence jumps around with no usable order. Knowing that and that tells you nothing about whether is above or below . Every bisection strategy is dead on arrival.
At cryptographic sizes the candidate space has around elements, and no classical algorithm does substantially better than subexponential time. Pollard's rho, baby-step giant-step, index calculus and the number field sieve for discrete logarithms are all far better than checking every candidate, and all far too slow at deployed sizes.
There is a variant worth naming because it is what your browser actually uses. The same exponentiation can be performed on the points of an elliptic curve rather than on integers modulo a prime. The best known classical attack there runs in about time, which is worse for the attacker than in the integer setting, so the keys can be much smaller. A -bit elliptic-curve key gives roughly the same security as a -bit RSA key. That size advantage is why elliptic curves displaced RSA across most of the modern web.
04.Where the trapdoor lives: Diffie-Hellman key exchange
The trapdoor sits in a different place from RSA, and the scheme it produces solves the key-distribution problem from the first note directly.
Alice and Bob agree publicly on and . Eve knows both. Then:
Alice picks a secret and sends .
Bob picks a secret and sends .
Alice computes . Bob computes .
Both arrive at the same value, because
That shared value is their key, and neither of them ever transmitted it.
Work it fully with , . Alice picks and sends . Bob picks and sends .
Alice computes . By repeated squaring: , then , then .
Bob computes . Again: , , and . Now , and .
Both get . The shared secret is , and it was never sent.
Eve has heard , , and . To compute she needs or , and getting either from or is exactly the discrete logarithm problem. At she reads it off a table in seconds. At a few hundred digits she does not.
The signature version puts the same hardness to a different use. In ECDSA the signer knows a secret exponent , publishes , and produces signatures only someone knowing could produce. Forging one means recovering from , once again the discrete logarithm problem.
05.Two problems, one shared fate
Factoring and discrete logarithms look unrelated. One is about pulling a product apart, the other about undoing an exponentiation. They were discovered separately, they have different best-known attacks, and for decades the fact that deployed cryptography rested on two independent problems felt like healthy insurance. If one fell, the world could migrate to the other.
HTTPS, VPNs, secure email, software update signatures, payment networks and interbank settlement all rest on one or both of them.
That insurance turned out to be worth nothing, and the next note explains why. Both problems have a hidden structural property in common, and one algorithm published in 1994 exploits it in both.