Documentation

CslibFrontier.Crypto.Computational.SIS.MLKEM.Compress

ML-KEM coefficient compression #

FIPS 203 compresses a coefficient x ∈ ℤ_q to d bits and lifts it back by Compress_d(x) = ⌈(2^d / q) · x⌋ mod 2^d and Decompress_d(y) = ⌈(q / 2^d) · y⌋ (NIST, Section 4.2.1), where ⌈r⌋ rounds to the nearest integer with ties rounded up. Natural-number rounded division roundDiv a b = ⌊(2a + b) / (2b)⌋ implements ⌈a / b⌋, so compress and decompress below are these two maps for every modulus q and width d, written on canonical representatives.

The three facts used by K-PKE are proved for every positive modulus:

Main definitions #

References #

Rounded natural division ⌈a / b⌋ = ⌊(2a + b) / (2b)⌋, with ties rounded up.

def Cslib.Crypto.MLKEM.compress (q d : ℕ) (x : ZMod q) :
ZMod (2 ^ d)

FIPS 203 compression Compress_d(x) = ⌈(2^d / q) · x⌋ mod 2^d of a coefficient modulo q, computed on the canonical representative x.val ∈ [0, q).

def Cslib.Crypto.MLKEM.decompress (q d : ℕ) (y : ZMod (2 ^ d)) :

FIPS 203 decompression Decompress_d(y) = ⌈(q / 2^d) · y⌋ of a d-bit value, computed on its canonical representative y.val ∈ [0, 2^d).

def Cslib.Crypto.MLKEM.compressPoly {n : ℕ} (q d : ℕ) (f : Fin n → ZMod q) :
Fin n → ZMod (2 ^ d)

Coefficientwise compression of a polynomial f ∈ ℤ_q[X] / (X^n + 1) in coefficient form.

def Cslib.Crypto.MLKEM.decompressPoly {n : ℕ} (q d : ℕ) (f : Fin n → ZMod (2 ^ d)) :
Fin n → ZMod q

Coefficientwise decompression of a polynomial with d-bit coefficients.

@[simp]
theorem Cslib.Crypto.MLKEM.compressPoly_apply {n : ℕ} (q d : ℕ) (f : Fin n → ZMod q) (i : Fin n) :
compressPoly q d f i = compress q d (f i)
@[simp]
theorem Cslib.Crypto.MLKEM.decompressPoly_apply {n : ℕ} (q d : ℕ) (f : Fin n → ZMod (2 ^ d)) (i : Fin n) :
decompressPoly q d f i = decompress q d (f i)
theorem Cslib.Crypto.MLKEM.roundDiv_spec {a b : ℕ} (hb : 0 < b) :
2 * b * roundDiv a b ≤ 2 * a + b ∧ 2 * a + b < 2 * b * roundDiv a b + 2 * b

The defining inequalities of rounded division: b · ⌈a / b⌋ is within b / 2 of a.

theorem Cslib.Crypto.MLKEM.roundDiv_eq_of_le_of_lt {a b c : ℕ} (hle : 2 * b * c ≤ 2 * a + b) (hlt : 2 * a + b < 2 * b * c + 2 * b) :
roundDiv a b = c

Rounded division is characterized by its defining inequalities.

theorem Cslib.Crypto.MLKEM.compress_decompress {q d : ℕ} (hd : 2 ^ d < q) (y : ZMod (2 ^ d)) :
compress q d (decompress q d y) = y

Compression inverts decompression (NIST, Section 4.2.1): whenever 2^d < q, Compress_d(Decompress_d(y)) = y for every d-bit value y.

Rounding error of compression: for every positive modulus q, width d and coefficient x, the centered representative of Decompress_d(Compress_d(x)) - x has magnitude at most ⌈q / 2^(d + 1)⌋. This is the standard error bound used in the correctness analysis of Kyber; FIPS 203 itself only remarks that the error is small.

theorem Cslib.Crypto.MLKEM.decompress_one_eq (q : ℕ) (y : ZMod (2 ^ 1)) :
decompress q 1 y = ↑((q + 1) / 2 * y.val)

Decompress_1 sends the bit 0 to 0 and the bit 1 to ⌈q / 2⌋ = (q + 1) / 2.

theorem Cslib.Crypto.MLKEM.compress_one_decompress_one_add {q B : ℕ} (hB : 4 * B + 2 < q) (y : ZMod (2 ^ 1)) {w : ZMod q} (hw : w.valMinAbs.natAbs ≤ B) :
compress q 1 (decompress q 1 y + w) = y

One-bit decoding with bounded error: when 4 * B + 2 < q and the centered representative of w has magnitude at most B, Compress_1(Decompress_1(y) + w) = y for each bit y.