ElGamal Encryption

lean4-proofcryptographyvisualization
c2/c1a=mc_2 / c_1^a = m

Statement

Let G=(Z/pZ)G = (\mathbb{Z}/p\mathbb{Z})^* with generator gg. Alice's key pair: private aa, public y=gay = g^a. To encrypt message mGm \in G, Bob picks random rr and computes:

c1=gr,c2=myr=mgarc_1 = g^r, \quad c_2 = m \cdot y^r = m \cdot g^{ar}

Decryption: Alice computes:

c2/c1a=mgar/gra=mc_2 / c_1^a = m \cdot g^{ar} / g^{ra} = m

The ciphertext (c1,c2)(c_1, c_2) hides mm under the DH shared secret garg^{ar}.

Visualization

Parameters: g=5g = 5, p=23p = 23, a=6a = 6 (Alice's private key), y=ga=8y = g^a = 8 (Alice's public key). Encrypt m=10m = 10 with Bob's random r=3r = 3.

ComputationValue
c1=gr=53mod23c_1 = g^r = 5^3 \bmod 23c1=125mod23=10c_1 = 125 \bmod 23 = 10
yr=83mod23y^r = 8^3 \bmod 23yr=512mod23=6y^r = 512 \bmod 23 = 6
c2=myr=106mod23c_2 = m \cdot y^r = 10 \cdot 6 \bmod 23c2=60mod23=14c_2 = 60 \bmod 23 = 14

Decryption:

ComputationValue
c1a=106mod23c_1^a = 10^6 \bmod 23c1a=106mod23=6c_1^a = 10^6 \bmod 23 = 6
c1ac_1^a inverse in Z/23Z\mathbb{Z}/23\mathbb{Z}61mod23=46^{-1} \bmod 23 = 4
m=c2(c1a)1=144mod23m' = c_2 \cdot (c_1^a)^{-1} = 14 \cdot 4 \bmod 23m=56mod23=10m' = 56 \bmod 23 = 10

Recovered m=10=mm' = 10 = m. Correctness holds: c1a=gra=gar=yrc_1^a = g^{ra} = g^{ar} = y^r, so the shared secret cancels.

Proof Sketch

  1. Expand c1ac_1^a. Since c1=grc_1 = g^r, we have c1a=grac_1^a = g^{ra}.

  2. Use commutativity. In an abelian group gra=gar=(ga)r=yrg^{ra} = g^{ar} = (g^a)^r = y^r.

  3. Cancellation. c2/c1a=(myr)/yr=mc_2 / c_1^a = (m \cdot y^r) / y^r = m. In group notation, c2(c1a)1=myr(yr)1=mc_2 \cdot (c_1^a)^{-1} = m \cdot y^r \cdot (y^r)^{-1} = m.

  4. Security. Without aa, an eavesdropper sees c1=grc_1 = g^r and c2=mgarc_2 = m \cdot g^{ar}; recovering mm requires the DH shared secret garg^{ar}, which is computationally hard.

Connections

ElGamal is a direct application of Diffie–HellmanDiffie–Hellman(ga)b=(gb)a(g^a)^b = (g^b)^aAlice and Bob derive the same shared secret g^(ab) from public keys g^a and g^b in any commutative group.Read more → — the shared secret garg^{ar} is precisely the DH shared key. The group law relies on Fermat's Little TheoremFermat's Little Theoremapa(modp)a^p \equiv a \pmod{p}For prime p, a^p is congruent to a mod pRead more → for the order of elements in (Z/pZ)(\mathbb{Z}/p\mathbb{Z})^*. The Chinese Remainder TheoremChinese Remainder TheoremZ/mnZZ/mZ×Z/nZ\mathbb{Z}/mn\mathbb{Z} \cong \mathbb{Z}/m\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z}Simultaneous congruences with coprime moduli have a unique solution mod their productRead more → underlies efficient multi-prime implementations.

Lean4 Proof

/-- ElGamal decryption identity in a commutative group:
    (m * y^r) * (g^r)^(-a_inv) = m  when  y = g^a  and
    we demonstrate the cancellation algebraically. -/

-- Core algebraic fact: in a CommGroup, (g^a)^r = (g^r)^a
theorem elgamal_shared_secret_eq {G : Type*} [CommGroup G] (g : G) (a r : ) :
    (g ^ a) ^ r = (g ^ r) ^ a := by
  rw [ pow_mul,  pow_mul, Nat.mul_comm]

/-- Cancellation: m * k * k⁻¹ = m in any group. -/
theorem elgamal_cancel {G : Type*} [Group G] (m k : G) :
    m * k * k⁻¹ = m := by
  rw [mul_assoc, mul_inv_cancel, mul_one]

/-- Concrete numeric check: decrypt(encrypt(10)) = 10.
    c1 = 5^3 mod 23 = 125 mod 23 = 10
    y^r = 8^3 mod 23 = 512 mod 23 = 6
    c2 = 10 * 6 mod 23 = 60 mod 23 = 14
    c1^a = 10^6 mod 23 = 6   (= g^(r*a) = g^(a*r) = y^r)
    inv(6) mod 23 = 4  (since 6*4=24≡1 mod 23)
    m' = c2 * inv(c1^a) = 14 * 4 mod 23 = 56 mod 23 = 10  -/
example : 5 ^ 3 % 23 = 10 := by decide
example : (8 : ) ^ 3 % 23 = 6 := by decide
example : 10 * 6 % 23 = 14 := by decide
example : (10 : ) ^ 6 % 23 = 6 := by decide   -- c1^a = y^r confirmed
example : 56 % 23 = 10 := by decide              -- 14 * 4 = 56, recover m