Lattice Side-Channel Attacks: KyberSlash and ML-DSA
Recover secret coefficients from variable-time division and deterministic signing faults in Python, then apply the exact constant-time fixes.
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 class | Affected step | Root cause | Constant-time fix |
|---|---|---|---|
| KyberSlash (2023) | ML-KEM (Decrypt / Compress_1) | Hardware integer division / modulo on secret residual (v − sᵀu) / 3329 leaks timing | Replace `/` and `%` with constant-time Barrett multiplication + bit-shifts |
| Early-exit ciphertext check | ML-KEM (Decaps FO check) | Short-circuit `memcmp(c, c′)` leaks first mismatched byte index via response time | XOR-accumulate all 1,088 bytes and select K′ vs K̄ with constant-time bitwise mask |
| Deterministic nonce fault | ML-DSA (Sign without `rnd`) | Clock/voltage glitch or Rowhammer flip during hash c′ ≠ c reuses exact mask y | Use FIPS 204 default hedged mode (`rnd ← 32 random bytes`) + verify-after-sign |
| Early rejection break | ML-DSA (Norm check `‖z‖∞`) | `if abs(z[i]) >= gamma1 - beta: break` reveals which polynomial coordinate overflowed | OR-accumulate overflow flags across all 256ℓ coefficients before branching on retry |
1. KyberSlash: why dividing by a public prime leaks secrets
In ML-KEM bit recovery, Compress_1(w) maps a secret-dependent decryption residual to a message bit via ((w << 1) + 1664) / 3329. Because 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 (or check the exact range 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
Recall from ML-DSA Rejection Sampling that signing produces over . If a device uses deterministic ML-DSA (rnd = 0^32) and signs the same message twice — once normally, and once while an attacker injects a voltage/laser glitch or Rowhammer bit-flip after mask is expanded so that the challenge hash changes from to — both signatures share the exact same secret mask :
Whenever is invertible in (overwhelmingly likely modulo ), a single subtraction and polynomial inversion immediately hands the attacker the entire secret signing key ! 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 across linear NTT operations and using masked boolean-to-arithmetic conversions during binomial sampling and Compress_1 bit decoding.