Prerequisite: Kyber / ML-KEM

NIST's lattice standards avoid data-dependent S-box lookups and big-integer modular exponentiation, making them appear naturally constant-time at first glance. In practice, real-world vulnerabilities in post-quantum libraries over the last three years came not from BKZ or quantum computers, but from compiler-emitted division instructions, short-circuit loops, and deterministic fault injection.

Four implementation bugs every PQC code review checks

Vulnerability classAffected stepRoot causeConstant-time fix
KyberSlash (2023)ML-KEM (Decrypt / Compress_1)Hardware integer division / modulo on secret residual (v − sᵀu) / 3329 leaks timingReplace `/` and `%` with constant-time Barrett multiplication + bit-shifts
Early-exit ciphertext checkML-KEM (Decaps FO check)Short-circuit `memcmp(c, c′)` leaks first mismatched byte index via response timeXOR-accumulate all 1,088 bytes and select K′ vs K̄ with constant-time bitwise mask
Deterministic nonce faultML-DSA (Sign without `rnd`)Clock/voltage glitch or Rowhammer flip during hash c′ ≠ c reuses exact mask yUse FIPS 204 default hedged mode (`rnd ← 32 random bytes`) + verify-after-sign
Early rejection breakML-DSA (Norm check `‖z‖∞`)`if abs(z[i]) >= gamma1 - beta: break` reveals which polynomial coordinate overflowedOR-accumulate overflow flags across all 256ℓ coefficients before branching on retry

1. KyberSlash: why dividing by a public prime q=3329q = 3329 leaks secrets

In ML-KEM bit recovery, Compress_1(w) maps a secret-dependent decryption residual w=(v′−s⊤u′) mod 3329w = (v' - s^\top u') \bmod 3329 to a message bit via ((w << 1) + 1664) / 3329. Because 33293329 is a compile-time constant, developers assumed C, Rust, or Go compilers would always lower / 3329 into a constant-time multiplication. On several embedded ARM Cortex, RISC-V, and size-optimized (-Os) builds, compilers instead emitted raw udiv instructions whose cycle count varies with the dividend's bit-length!

Fix Compress_1 without division: multiply by the precomputed reciprocal ⌈228/3329⌉=80,635\lceil 2^{28} / 3329 \rceil = 80{,}635 (or check the exact range 833≤w≤2496833 \le w \le 2496 using constant-time unsigned subtraction borrows). Because integer multipliers on modern cores (excluding legacy Cortex-M3) run in fixed cycles, secret-dependent timing vanishes.

2. Glitching deterministic ML-DSA: one faulted signature recovers s1s_1

Recall from ML-DSA Rejection Sampling that signing produces z=y+cs1z = y + c s_1 over Rq=Z8380417[X]/(X256+1)R_q = \mathbb{Z}_{8380417}[X]/(X^{256}+1). If a device uses deterministic ML-DSA (rnd = 0^32) and signs the same message mm twice — once normally, and once while an attacker injects a voltage/laser glitch or Rowhammer bit-flip after mask yy is expanded so that the challenge hash changes from cc to c′≠cc' \neq c — both signatures share the exact same secret mask yy:

z=y+cs1,z′=y+c′s1⟹z−z′=(c−c′)s1(modq)z = y + c s_1, \qquad z' = y + c' s_1 \quad \Longrightarrow \quad z - z' = (c - c') s_1 \pmod q

Whenever c−c′c - c' is invertible in RqR_q (overwhelmingly likely modulo q=8380417q = 8380417), a single subtraction and polynomial inversion immediately hands the attacker the entire secret signing key s1s_1! That is why FIPS 204 made hedged signing (mixing 32 fresh random bytes rnd into mask derivation on every call) the default mode.

Verify constant-time Barrett compression and fault recovery in Python

q = 3329

def compress_1_barrett(w: int) -> int:
    """Division-free FIPS 203 Compress_1(w) exact for all 0 <= w < 3329."""
    # (((w << 1) + 1664) * 80635) >> 28  equals  ((2*w + 1664) // 3329) & 1
    t = ((w << 1) + 1664) * 80635
    return (t >> 28) & 1

for w in range(q):
    ref = (((w << 1) + q // 2) // q) & 1
    assert compress_1_barrett(w) == ref, f"Mismatch at w={w}"
print("Division-free Barrett Compress_1 verified across all 3,329 residues!")

def ct_bytes_eq(a: bytes, b: bytes) -> int:
    """Branchless equality returning 1 if equal, 0 otherwise (assuming equal length)."""
    diff = 0
    for x, y in zip(a, b):
        diff |= x ^ y
    return 1 ^ ((diff | -diff) >> 31 & 1)

# Deterministic fault recovery on scalar slice of ML-DSA:
s1_secret, y_mask = -2, 41205
c_valid, c_faulted = 1, -1
z_valid = y_mask + c_valid * s1_secret
z_faulted = y_mask + c_faulted * s1_secret
s1_recovered = (z_valid - z_faulted) // (c_valid - c_faulted)
assert s1_recovered == s1_secret
print("Recovered s1 from 1 faulted deterministic signature:", s1_recovered)

On smart cards, secure elements, or HSMs facing power/EM probing (DPA), even division-free code must also mask secret polynomials — splitting s=s(0)+s(1) mod qs = s^{(0)} + s^{(1)} \bmod q across linear NTT operations and using masked boolean-to-arithmetic conversions during binomial sampling and Compress_1 bit decoding.