Subroutine Logo
Subroutine
← Back to Articles Security Advanced 7 min read

Post-Quantum Cryptography: Lattice-Based Key Encapsulation with ML-KEM

Transitioning public-key infrastructure away from RSA and elliptic curves toward NIST FIPS 203 Module-LWE lattices to protect against future quantum adversaries.

Published: 2026-09-07
#Security#Cryptography#Post-Quantum#ML-KEM#Kyber#Lattices#C#Go

Modern secure internet communications rely on three public-key cryptographic primitives: RSA, Diffie-Hellman (DH), and Elliptic Curve Cryptography (ECDH / ECDSA).

These algorithms derive their security from two mathematical problems:

  1. Integer Factorization: Factoring the product of two large prime numbers (N=p×qN = p \times q).
  2. The Discrete Logarithm Problem: Finding kk given gg and gk(modp)g^k \pmod p, or finding scalar kk on elliptic curve point P=kGP = k \cdot G.

In 1994, mathematician Peter Shor published Shor’s Algorithm. When executed on a sufficiently large, fault-tolerant quantum computer (a Cryptographically Relevant Quantum Computer, or CRQC), Shor’s algorithm solves discrete logarithms and integer factorization in O((logN)3)O((\log N)^3) polynomial time.

This represents a complete break of RSA, ECDH, and Ed25519 signatures.

Even before large quantum computers are built, security architects face the “Harvest Now, Decrypt Later” threat: adversaries intercept and store encrypted internet traffic today, preparing to decrypt it retroactively once quantum hardware matures.


1. Summary & NIST Standardized PQC Algorithms

In August 2024, the National Institute of Standards and Technology (NIST) published its final post-quantum standards:

NIST StandardAlgorithm NameFormer NameCategoryPrimary Use Case
FIPS 203ML-KEMCRYSTALS-KyberModule Lattice Key EncapsulationTLS 1.3 Key Exchange, VPN handshakes
FIPS 204ML-DSACRYSTALS-DilithiumModule Lattice Digital SignaturesDigital certificates, code signing, PKI
FIPS 205SLH-DSASPHINCS+Stateless Hash-Based SignaturesFallback signature scheme (Zero lattice math)

2. Mathematical Foundation: Module Learning With Errors (M-LWE)

ML-KEM (Kyber) bases its hardness on the Module Learning With Errors (M-LWE) problem over polynomial rings.

Let the ring be:

Rq=Zq[X]/(X256+1)R_q = \mathbb{Z}_q[X] / (X^{256} + 1)

where q=3329q = 3329. Elements in RqR_q are polynomials of degree less than 256 with integer coefficients modulo 3329.

graph LR
    subgraph MathCore["Module-LWE System"]
    A["Public Matrix A in R_q^(k x k)"]
    S["Secret Vector s (Small coefficients)"]
    E["Error Vector e (Centered Binomial noise)"]
    T["Public Key t = A * s + e (mod q)"]
    A --> T
    S --> T
    E --> T
    end

The fundamental hardness property:

  • Forward Computation: Given matrix A\mathbf{A}, secret vector s\mathbf{s}, and error e\mathbf{e}, calculating t=As+e(modq)\mathbf{t} = \mathbf{A} \mathbf{s} + \mathbf{e} \pmod q is fast via the Number Theoretic Transform (NTT) (O(NlogN)O(N \log N) complexity).
  • Adversarial Inversion: Given A\mathbf{A} and t\mathbf{t}, finding s\mathbf{s} or e\mathbf{e} requires finding shortest vectors in high-dimensional Euclidean lattices, a problem where no polynomial-time quantum algorithm exists.

3. Key Encapsulation Mechanism (KEM) Protocol Flow

Unlike Diffie-Hellman where both parties compute key halves symmetrically, a KEM operates in three explicit phases:

sequenceDiagram
    autonumber
    actor Alice as Receiver (Server)
    actor Bob as Initiator (Client)
    
    Alice->>Alice: KeyGen() -> (pk, sk)
    Alice->>Bob: Transmit Public Key (pk)
    Bob->>Bob: Encaps(pk) -> (Ciphertext c, SharedSecret K)
    Bob->>Alice: Transmit Ciphertext (c)
    Alice->>Alice: Decaps(c, sk) -> SharedSecret K
  1. KeyGen(): Alice generates public key pk=(t,ρ)pk = (\mathbf{t}, \rho) and private key sk=ssk = \mathbf{s}.
  2. Encaps(pk): Bob generates a random 32-byte secret mm, encrypts it under Alice’s public key to create ciphertext cc, and hashes mm to derive symmetric key KK.
  3. Decaps(c, sk): Alice uses her secret key s\mathbf{s} to decrypt ciphertext cc, recovering secret mm and deriving the identical symmetric key KK.

4. Hybrid Key Exchange in TLS 1.3 (X25519Kyber768)

To guarantee that modern connections remain secure even if an unforeseen cryptanalytic breakthrough weakens lattices, the IETF and major browsers (Chrome, Firefox, Cloudflare) mandate Hybrid Key Exchange.

Clients negotiate both classical X25519 and post-quantum ML-KEM-768 in parallel:

Ksession=HKDF-Extract(ECDH_SecretMLKEM_Secret)K_{\text{session}} = \text{HKDF-Extract}(\text{ECDH\_Secret} \mathbin{\Vert} \text{MLKEM\_Secret})

An attacker must break both the Elliptic Curve Discrete Logarithm problem and the Module Lattice Shortest Vector problem to decipher the session.

Performance Overhead

Kyber-768 public keys are 1,184 bytes and ciphertexts are 1,088 bytes (compared to 32 bytes for X25519). However, polynomial arithmetic via NTT executes in less than 50 microseconds on modern CPUs, negligible compared to network round-trip times.


5. Dual-Language Implementation

NTT Polynomial Transform & Hybrid KEM
kyber_ntt.c
C99 (Number Theoretic Transform - NTT)
#include <stdint.h>

#define KYBER_N 256
#define KYBER_Q 3329
#define MONTGOMERY_FACTOR 2285 // 2^16 % 3329

int16_t montgomery_reduce(int32_t a) {
    int16_t t;
    t = (int16_t)a * 62209; // Q^{-1} mod 2^16
    t = (a - (int32_t)t * KYBER_Q) >> 16;
    return t;
}

// Forward NTT over R_q = Z_q[X]/(X^256 + 1)
void ntt(int16_t poly[KYBER_N], const int16_t zetas[128]) {
    unsigned int len, start, j, k = 1;
    int16_t zeta, t;

    for (len = 128; len >= 2; len >>= 1) {
        for (start = 0; start < KYBER_N; start += 2 * len) {
            zeta = zetas[k++];
            for (j = start; j < start + len; j++) {
                t = montgomery_reduce((int32_t)zeta * poly[j + len]);
                poly[j + len] = poly[j] - t;
                poly[j] = poly[j] + t;
            }
        }
    }
}

6. Architectural Guidance

  • Enable X25519MLKEM768 hybrid key exchange on TLS termination proxies (Envoy, Nginx, Cloudflare) immediately to mitigate “Harvest Now, Decrypt Later” exposure on sensitive data with long confidentiality horizons (financial, healthcare, defense).
  • Plan for MTU fragmentation: larger public keys (~1.2KB) may push TLS ClientHello packets across TCP packet boundaries, requiring validation of middlebox and firewall handling of multi-packet handshakes.