A from-scratch study of the Learning With Errors (LWE) problem in Python, seen from two sides:
- The designer: how to encrypt with LWE, and how to choose the parameters so that decryption works.
- The attacker: how to break LWE with lattice reduction, and when the attack fails.
The common thread is a trade-off: the error
Personal project, Anîsa Djedje, 3rd-year Double Degree in Mathematics & Computer Science, Université Paris-Saclay.
LWE was introduced by Oded Regev in 2005. Given a public matrix
LWE is believed to be hard even for quantum computers. It is the foundation of ML-KEM (Kyber), the post-quantum key encapsulation standard adopted by NIST in 2024 (FIPS 203).
| Part | Content | Status |
|---|---|---|
| 1 | LWE encryption and decryption reliability | ✅ Done |
| 2 | Breaking LWE with lattice reduction (LLL) | 🟡 In progress |
| 3 | From LWE to Module-LWE and Kyber | ⬜ Planned |
Implementation (no cryptographic library, NumPy only):
-
discrete_gaussian: sampler for the error$e$ . -
encrypt_LWE/decrypt_LWE: secret-key, bit-by-bit scheme. The bit$\text{msg} \in {0,1}$ is encoded by adding$\text{msg} \cdot \lfloor q/2 \rfloor$ , and decryption compares the result to the threshold$q/4$ . -
phase_transition_study: decryption success rate as a function of$\sigma$ ,$m$ ,$n$ and$q$ .
Key results:
-
Noise is necessary. With
$\sigma = 0$ , Gaussian elimination recovers$s$ immediately. - Phase transition. When the accumulated noise becomes too large, the success rate drops sharply to 50%, i.e. random guessing.
-
$m$ : above a threshold, more equations let the scheme tolerate more noise. -
$n$ : affects security, but barely changes decryption reliability. -
$q$ : a larger modulus leaves more room before the noise crosses$q/4$ .
Conclusion: decryption puts an upper bound on the noise
1. Lattices ✅
- Lattices, bases and determinant; good vs. bad bases, with a proof that two bases generate the same lattice and an illustration.
- LWE as a lattice problem: the vectors
$Az \bmod q$ form the lattice$\Lambda_q(A)$ , which contains$As$ . Recovering$s$ amounts to solving the Closest Vector Problem (CVP). - Toy example (
$q = 17$ ): a small error keeps$As$ as the closest lattice point to$b$ , a large one makes it jump to another point, i.e. a wrong secret.
2. The LLL algorithm ✅
- Built step by step, each ingredient motivated by a hand-computed counterexample: why Gram–Schmidt is not enough, size reduction, why swaps are needed, and the Lovász condition (
$\delta = 3/4$ ). - From-scratch implementation of
gram_schmidt,lllandis_lll_reduced. - Tests: hand-computed examples, determinant preservation, random tests in dimension 5.
3. The attack 🟡
- ✅ Kannan's embedding: turning CVP into SVP by adding
$b$ to the basis with an extra coordinate, so that$(e, 1)$ becomes a short lattice vector that LLL can find. - ✅ Recovering
$s$ : fix the sign, remove the noise ($As = b - e$ ), solve a linear system mod$q$ , check the result. - ✅ Full demo on the toy example: LLL finds
$e$ , then$s = 3$ , from$A$ and$b$ only. - ⬜ General construction of the basis of
$\Lambda_q(A)$ and of the Kannan matrix. - ⬜ The attack on real instances generated with the Part 1 code.
4. Experiments ⬜ Attack success rate as a function of
5. Conclusion ⬜ Decryption gives an upper bound on
- From LWE to Module-LWE, over the ring
$R_q = \mathbb{Z}_q[x]/(x^n + 1)$ . - Structure of Kyber and its parameter choices.
- The toy parameters (e.g.
$n = 2$ ,$q = 11$ ) are chosen for visualization and are trivially brute-forced. They are not secure settings. - The scheme in Part 1 is a secret-key version of LWE encryption, not Regev's public-key scheme.
Online: click the Open in Colab badge above.
Locally:
git clone https://github.com/anisadje/learning-with-errors.git
cd learning-with-errors
pip install -r requirements.txt
jupyter notebook LWE_EN.ipynbA French version is also available: LWE_FR.ipynb.
Stack: Python, NumPy, Matplotlib, Seaborn, Jupyter.
- O. Regev, On lattices, learning with errors, random linear codes, and cryptography, STOC 2005.
- A. K. Lenstra, H. W. Lenstra Jr., L. Lovász, Factoring polynomials with rational coefficients, Mathematische Annalen 261 (1982).
- R. Kannan, Minkowski's convex body theorem and integer programming, Mathematics of Operations Research 12(3) (1987).
- P. W. Shor, Algorithms for quantum computation: discrete logarithms and factoring, FOCS 1994.
- NIST, FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard, 2024.
- D. Micciancio, CSE 208: Advanced Cryptography (lattices), UC San Diego, lecture notes.
Anîsa Djedje · LinkedIn