Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Diffie–Hellman, ElGamal, knapsack and ECC

Diffie–Hellman key exchange, ElGamal public-key encryption, Merkle–Hellman knapsack (superincreasing) and basic elliptic-curve cryptography (ECC): definitions, formulas, step-by-step derivations and exam-standard worked examples.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: Engineers use these public-key schemes to agree secrets, encrypt messages and sign or authenticate in network protocols (TLS/SSH/PGP) where symmetric keys must be established without pre-shared secrets. ECC gives the same security as RSA with much smaller keys, saving bandwidth and CPU in constrained devices.

Key ideas

  • Discrete logarithm problem (DLP): given prime p, generator g and value h in Z_p^*, find x such that g^x ≡ h (mod p). DLP must be hard (computationally infeasible) for security.

  • Diffie–Hellman key exchange (DH): two parties, Alice and Bob, choose private exponents a and b and exchange A = g^a mod p, B = g^b mod p. The shared secret s = g^{ab} mod p. Derivation: A^b = (g^a)^b = g^{ab} = (g^b)^a = B^a.

    Assumptions: p large prime, g a generator of a large prime‑order subgroup; let q be the subgroup order and choose a and b uniformly in {1,…,q−1} and keep them private. Security relies on the CDH/DDH assumptions in the chosen group; unauthenticated DH is vulnerable to man‑in‑the‑middle (so authenticate or sign the exchange in practice).

    Draw the diagram examiners expect: two vertical boxes labelled Alice (left) and Bob (right). Under Alice write private a, compute A = g^a mod p, arrow right labelled A. Under Bob write private b, compute B = g^b mod p, arrow left labelled B. Inside both boxes show computation of shared s = B^a = A^b.

  • ElGamal public-key encryption (on group Z_p^): key generation: choose private x, public h = g^x mod p. Encryption of message m (as an integer in Z_p^): choose random k, compute c1 = g^k mod p and c2 = m·h^k mod p; ciphertext is (c1,c2). Decryption: compute m = c2 · (c1^{-x}) mod p (equivalently c2 · (c1^x)^{-1}). Proof: c2 · c1^{-x} = m·h^k·(g^k)^{-x} = m·(g^{x})^k · g^{-kx} = m. (Reusing k across encryptions makes c1 identical and leaks relations between plaintexts; always use fresh, unpredictable k.)

    Assumptions: random k each encryption, p prime, message encoded into Z_p^*.

  • Merkle–Hellman knapsack (superincreasing): a superincreasing sequence w = (w1,...,wn) satisfies each wi > sum_{j< i} wj. Private key: superincreasing w, modulus n > sum(w), multiplier m coprime to n, and optional permutation. Public key: b_i = m·w_i (mod n) (and permute). Encryption: for bit vector x (length n) compute ciphertext S = sum x_i·b_i. Decryption: multiply S by m^{-1} mod n to get S' which is sum x_i·w_i; recover bits greedily from largest w_n down because of superincreasing property.

    Assumptions: w superincreasing and kept secret; m chosen so gcd(m,n)=1.

  • Elliptic-curve cryptography (ECC): work over finite field F_p (p prime). An elliptic curve in short Weierstrass form: E: y^2 ≡ x^3 + a x + b (mod p) with discriminant 4a^3 + 27b^2 ≠ 0 (mod p) to avoid singularities. The set of solutions (x,y) plus a point at infinity O form an abelian group under a geometric law.

    Group law (algebraic): for P=(x1,y1) and Q=(x2,y2), if P ≠ Q then

    • λ = (y2 - y1)·(x2 - x1)^{-1} (mod p), x3 = λ^2 - x1 - x2 (mod p), y3 = λ·(x1 - x3) - y1 (mod p). For doubling P=Q: λ = (3x1^2 + a)·(2y1)^{-1} (mod p). If x2 ≡ x1 and y2 ≡ −y1 (mod p), then P + Q = O; for doubling, if y1 ≡ 0 (mod p) then 2P = O. These short‑Weierstrass formulas assume p > 3.

    Scalar multiplication k·P means adding P to itself k times; compute with double-and-add (binary method). ECC security relies on the elliptic-curve discrete logarithm problem (ECDLP): given P and Q=kP, find k, believed much harder per-bit than integer DLP.

    Diagram to draw: curve (qualitative), points P and Q, line through P and Q intersecting curve at R, reflect R about x-axis to get P+Q. For doubling draw tangent.

Formulas

A = g^a mod p — A is Alice's public value; g generator (integer), a private exponent (integer); p prime modulus; applies in Z_p^*.

B = g^b mod p — Bob's public value; b private exponent.

s = A^b mod p = B^a mod p = g^{ab} mod p — shared secret; same units (integer mod p).

c1 = g^k mod p — ElGamal first component; k random.

c2 = m · h^k mod p — ElGamal second component; m plaintext in Z_p^*; h = g^x.

m = c2 · (c1^x)^{-1} mod p — ElGamal decryption; inverse in Z_p^*.

b_i = m · w_i mod n — Merkle–Hellman public key element; m multiplier, n modulus (> sum w_i).

S = sum_i x_i · b_i — ciphertext sum for knapsack.

S' = S · m^{-1} mod n — recover weighted sum with superincreasing w.

y^2 ≡ x^3 + a x + b (mod p) — elliptic-curve equation; p prime.

For point addition (P ≠ Q): λ = (y2 - y1)·(x2 - x1)^{-1} (mod p) x3 = λ^2 - x1 - x2 (mod p) y3 = λ·(x1 - x3) - y1 (mod p)

For point doubling: λ = (3 x1^2 + a) · (2 y1)^{-1} (mod p)

Define every inverse as a modular inverse with respect to the modulus in context (mod p for Z_p^* and ECC; mod n for the knapsack).

Worked examples

  1. Diffie–Hellman (numerical). Given p = 23, g = 5. Alice private a = 6, Bob private b = 15. Compute shared secret. Step 1: A = g^a mod p = 5^6 mod 23. 5^2 = 25 ≡ 2, 5^4 ≡ 2^2 = 4, 5^6 ≡ 4·2 = 8. So A = 8. Step 2: B = g^b mod p = 5^15. Compute 5^8 = 16, 5^4 = 4, 5^2 = 2, 5^15 = 5^8·5^4·5^2·5 = 16·4·2·5 ≡ 19. So B = 19. Step 3: shared s = B^a mod p = 19^6 mod 23. Compute 19 ≡ -4: (-4)^2 = 16, (-4)^4 = 3, (-4)^6 = 3·16 = 48 ≡ 2. So s = 2.

  2. ElGamal (numerical). Use same p = 23, g = 5. Private key x = 6 => public h = g^x = 8. Encrypt m = 13 with random k = 10. c1 = g^k mod p = 5^10 ≡ 9. h^k = 8^10; compute 8^2 ≡ 18, 8^4 ≡ 2, 8^8 ≡ 4, 8^10 ≡ 4·18 = 72 ≡ 3. So c2 = 13·3 ≡ 16. Ciphertext (9,16). Decrypt: compute c1^x = 9^6 ≡ 3, inverse of 3 mod 23 is 8, so m = 16·8 ≡ 13 (recovered). Final answer 13.

  3. Merkle–Hellman knapsack. Private w = [2,3,7,14] (superincreasing). Choose modulus n = 31 (n > sum(w)=26). Multiplier m = 17 (gcd(17,31)=1). Public b = [3,20,26,21] computed as (17·2 mod31 =3, 17·3=20, 17·7=26, 17·14=21). Encrypt bits x = [1,0,1,1] => S = 3 + 0 + 26 + 21 = 50. Compute m^{-1} mod 31 = 11 (since 17·11 = 187 ≡ 1). S' = 50·11 mod31 = 23. Recover bits greedily: 14 ≤ 23 => bit4=1, remainder 9; 7 ≤ 9 => bit3=1, remainder 2; 3 > 2 => bit2=0; 2 ≤ 2 => bit1=1. Recovered [1,0,1,1].

  4. ECC point doubling (small). Curve y^2 = x^3 + 2x + 3 mod 97. Check P = (3,6) lies on curve: 6^2 = 36, RHS = 27 + 6 + 3 = 36. Compute 2P: λ = (3·x1^2 + a)·(2·y1)^{-1} = (3·9 + 2)·(12)^{-1} = 29·(12)^{-1} mod 97. Inverse of 12 mod97 is 89 (since 12·89 ≡ 1). λ = 29·89 mod97 = 59. x3 = 59^2 - 2·3 = 3481 - 6 = 3475 mod97 = 80. y3 = 59·(3 - 80) - 6 ≡ 59·(-77) - 6 ≡ 10. So 2P = (80,10).

Check arithmetic twice in exams; show intermediate modular reductions.

Common mistakes

  • Reusing random k in ElGamal encryption makes c1 identical and leaks relations between plaintexts; if one plaintext becomes known the others can be recovered, but it does not by itself reveal the secret key x. Always choose a fresh random k.
  • In knapsack, students forget to ensure w is superincreasing or to compute modular inverse m^{-1} correctly. Also remember the original Merkle–Hellman scheme is broken (Shamir, 1982) and is not used in practice; it is taught as a historical construction.
  • ECC: mixing multiplicative notation and additive notation incorrectly (write scalar multiplication as k·P, not g^k). Also forgetting to check discriminant 4a^3 + 27b^2 ≠ 0 (mod p).
  • Arithmetic mistakes in modular inverses and reductions; always reduce intermediate results modulo p.

For your exam

Question types: definitions (DLP, superincreasing sequence, ECDLP), short notes (compare DH and ECC), derivations (show DH shared secret equality), numericals (compute DH/ElGamal/ECC examples), algorithms (double-and-add trace), diagrams (DH exchange, ECC chord/tangent). Full-marks answers include clear assumptions (prime p, generator g, size choices), stepwise computation with modular reductions shown, and explicit inverses computed. For proofs show algebraic equalities (e.g., ElGamal decryption proof).

Quick check

  1. In DH, if Alice sends A = g^a and Bob sends B = g^b, why do they get the same secret? Short answer.
  2. In ElGamal, why is it fatal to reuse the same random k for two different messages with same public key?
  3. State the elliptic-curve equation and the formula for λ when adding two distinct points P and Q. Answers: 1) Because Alice computes B^a = g^{ba} and Bob computes A^b = g^{ab}, and ab = ba so both equal g^{ab}. 2) Reusing k makes c1 identical and h^k the same; an attacker can divide c2 values to cancel h^k and get the ratio of plaintexts, and if one plaintext is known they can recover the other (but this alone does not reveal the secret key x). 3) y^2 ≡ x^3 + a x + b (mod p); λ = (y2 - y1)·(x2 - x1)^{-1} (mod p).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?