Discrete Logarithms and Diffie-Hellman
Diffie-Hellman key exchange is the public-key revolution in its most compact form: two parties who share no secret can agree on a shared secret over an eavesdropped channel. The protocol relies on a group where exponentiation is efficient but discrete logarithms and related Diffie-Hellman problems are hard.
Katz and Lindell separate the number-theory assumptions from the key-exchange protocol, which is the right proof-aware view. Smart gives more algorithmic background for discrete logarithms and signatures based on the same structures. Together they show that the protocol transcript is simple, but the security claim depends on the chosen group, parameter sizes, validation rules, and authentication layer.
Definitions
Let be a cyclic group of order generated by . Elements are written multiplicatively. For , exponentiation gives .
The discrete logarithm problem is: given where , find .
The computational Diffie-Hellman problem, or CDH, is: given
compute
The decisional Diffie-Hellman problem, or DDH, is: distinguish tuples
from random-looking tuples
where are uniform in .
The basic Diffie-Hellman key exchange is:
- Alice samples and sends .
- Bob samples and sends .
- Alice computes .
- Bob computes .
A key derivation function, or KDF, hashes the group element and transcript into usable symmetric keys.
An authenticated key exchange binds the DH values to party identities, usually with signatures, certificates, MACs from previous keys, or password-authenticated methods.
Key results
Correctness is immediate from exponent laws:
Security against passive eavesdroppers is based on CDH or DDH-type assumptions. An eavesdropper sees and but should not learn or distinguish the derived key from random. In many protocol proofs, DDH is convenient because it directly says is indistinguishable from a random group element given the transcript.
Unauthenticated Diffie-Hellman is vulnerable to man-in-the-middle attacks. An active adversary can replace Alice's with to Bob and replace Bob's with to Alice. Alice then shares a key with the adversary, and Bob shares a different key with the adversary. Both honest parties believe they are secure, but the attacker relays and modifies traffic.
Group choice matters. In , the full multiplicative group has order , which may have small factors. Implementations often use a prime-order subgroup generated by , with validation that received elements lie in the correct subgroup. Elliptic-curve DH uses groups of curve points and has smaller keys for comparable classical security, but requires point validation and careful curve choices.
Ephemeral Diffie-Hellman gives forward secrecy. If Alice and Bob use fresh exponents for a session and later erase them, compromise of a long-term signing key does not reveal past session keys. TLS 1.3 relies on ephemeral (EC)DHE modes for this reason.
The discrete-log problem is not uniformly hard in every group. Index-calculus methods affect finite-field groups, while generic algorithms such as baby-step/giant-step and Pollard rho work in any group in about time. Elliptic-curve groups are attractive partly because no subexponential classical discrete-log algorithm is known for well-chosen curves.
There is also a useful hierarchy among assumptions. If an algorithm can solve discrete logarithms, then it can solve CDH by recovering from and computing . If an algorithm can solve CDH, then it can usually solve the key-computation part of basic DH. DDH is a different kind of assumption: it asks whether the shared group element is distinguishable from random. Some groups make DDH easy because pairings or group structure leak a test, while CDH may still be hard. Protocol proofs must use the assumption their security claim actually needs.
Subgroup attacks are a practical boundary between algebra and implementation. If a receiver exponentiates an attacker-chosen element of small order, the result may leak information about the secret exponent modulo that small order. Repeating the attack across subgroups can recover the exponent. Standard defenses include validating group membership, using prime-order groups, clearing cofactors in elliptic-curve settings when appropriate, and deriving keys with transcript-aware KDFs rather than exposing raw exponentiation results.
Static Diffie-Hellman and ephemeral Diffie-Hellman offer different security properties. A static DH key can authenticate implicitly in some designs, but compromise of the static exponent may expose many sessions. Ephemeral DH creates fresh exponents per session and supports forward secrecy, but it must be authenticated by signatures, PSKs, or certificates to stop active substitution. TLS 1.3's preference for ephemeral exchange reflects this tradeoff.
After DH computes a group element, a protocol should feed an encoded form of that element into a KDF along with the transcript. The raw element may have representation quirks, leading zeros, cofactors, or multiple encodings. A KDF extracts uniform-looking key material and binds the resulting key to the exact messages that created it. Without this binding, the same mathematical shared secret might be replayed or misinterpreted across algorithm choices and identities.
Parameter generation is part of the assumption. A statement such as "DDH is hard in " is meaningful only for a specified family of groups. Safe-prime finite-field groups, standardized elliptic curves, and modern prime-order abstractions are attempts to make the group choice reviewable. Inventing a group for an application is usually far riskier than using a well-studied standard group.
A final practical point is contributory behavior. In a good key exchange, both parties' fresh contributions should affect the final key. If one side can force a constant shared secret or a small set of possible secrets, the KDF may output attacker-predictable keys. Public-key validation, transcript binding, and checks for invalid or identity elements all support this contributory property.
These checks are especially important in libraries, because the library may not know whether the caller is building a toy demo, a VPN, a messenger, or a long-lived certificate protocol.
Defaults should therefore be conservative, validated, and tied to named parameter sets.
That keeps deployments reviewable.
Visual
| Assumption | Given | Challenge | Typical role |
|---|---|---|---|
| Discrete log | find | base hardness problem | |
| CDH | compute | shared-secret secrecy | |
| DDH | decide if | indistinguishable key proofs | |
| Gap DH | DDH oracle plus CDH hardness | compute | some random-oracle protocols |
Worked example 1: toy Diffie-Hellman exchange
Problem: use prime , generator , Alice secret , and Bob secret . Compute the shared key.
Method:
- Alice sends:
Compute:
So .
- Bob sends:
Compute powers:
Since :
So .
- Alice computes:
, , so
- Bob computes:
This also gives .
Checked answer: shared group element . The parameters are toy-sized and insecure.
Worked example 2: man-in-the-middle structure
Problem: explain how an active attacker Mallory breaks unauthenticated DH without solving discrete logs.
Method:
-
Alice sends to Bob. Mallory intercepts it and sends to Bob.
-
Bob sends to Alice. Mallory intercepts it and sends to Alice.
-
Alice computes:
Mallory can compute the same key as:
- Bob computes:
Mallory can compute the same key as:
- Mallory now decrypts Alice's traffic under , reads or modifies it, and re-encrypts to Bob under .
Checked answer: the attack uses message substitution, not discrete-log solving. Authentication is required.
Code
def dh_public(p: int, g: int, secret: int) -> int:
return pow(g, secret, p)
def dh_shared(p: int, peer_public: int, secret: int) -> int:
return pow(peer_public, secret, p)
p = 23
g = 5
a = 6
b = 15
A = dh_public(p, g, a)
B = dh_public(p, g, b)
print(A, B)
print(dh_shared(p, B, a), dh_shared(p, A, b))
Common pitfalls
- Using unauthenticated Diffie-Hellman and assuming passive security handles active attackers.
- Skipping validation of received group elements or curve points.
- Reusing ephemeral exponents across sessions.
- Treating the raw group element as an application key instead of applying a KDF with transcript context.
- Choosing groups with small subgroups or obsolete parameters.
- Confusing CDH and DDH; some groups make DDH easy while CDH may still be hard.