Welcome to another blogpost in COSIC’s Guide to Crypto! In this series, researchers from COSIC will be informally writing about different concepts in cryptography. Our aim is to introduce the reader to these concepts with explanations, illustrations and examples; so let’s get started!
Introduction to Lattice-based Cryptography (Part 2)
It’s time for Lattice-based cryptography part 2, and this time we will discuss a real-life application: encryption. We will see how to make a simple encryption scheme based on the Learning with Errors (LWE) problem discussed in a previous blogpost. But don’t worry, you don’t need to understand anything about lattices! Just matrix/vector multiplication and modular arithmetic will get us far enough. With that said, are you ready to encrypt some bits?
Let’s first revisit the LWE problem (or at least a small secret variant of it). If someone generates a uniformly random matrix \(\pmb{A}\) with coefficients between \(0\) and \(q\) (i.e. \( \pmb{A} \leftarrow \mathcal{U}(\mathtt{Z}_q^{n\times n}) \)), and two secret vectors with coefficients drawn from some small distribution (i.e a distribution with small variance) \( \color{red}{\pmb{s}}, \color{red}{\pmb{e}} \leftarrow \text{small}(\mathtt{Z}_q^{n \times 1}) \), then an LWE sample can be calculated as:
\( (\pmb{A}, \pmb{b} = \pmb{A} \color{red}{\pmb{s}} + \color{red}{\pmb{e}} \bmod q) \).
The search LWE problem states that it is hard to recover the secret \( \color{red}{\pmb{s}} \) from such a sample, while the decision LWE problem states that it is hard to disinguish such an LWE sample from a uniformly random sample \( (\pmb{A}, \pmb{u} \leftarrow \mathcal{U}(\mathtt{Z}_q^{n\times 1})) \).
Of course the hardness of this problem depends on the parameter choices. To give an idea of the parameters, typically the secret vector length \(n\) is in between 512 and 1024, the modulus \(q\) is in the range \(2^{8}\) to \(2^{16}\) and the distribution \(\text{small}\) is a Gaussian or centered binomial distribution with variance 1 to 3.
Encryption with LWE
So how do we encrypt a bit using this LWE problem? Imagine Bob wants to send a one bit message to Alice.
key generation
First Alice needs to generate a public key that can be used by Bob to do the encryption. In our case this public key is simply an LWE sample \( (\pmb{A}, \pmb{b} = \pmb{A} \color{red}{\pmb{s}} + \color{red}{\pmb{e}} \bmod q) \), while the secret key is the secret vector \( \color{red}{\pmb{s}} \). Remember that due to the search LWE problem, \( \color{red}{\pmb{s}} \) is hard to recover from this sample, and due to the decisional LWE problem, both \(\pmb{A}\) and \(\pmb{b}\) look uniformly random to Bob or any possible attacker.
encryption
Bob can encrypt a bit \(m\) by calculating two LWE problems: one using \(\pmb{A}\) as random public element, and one using \(\pmb{b}\). To this end, Bob generates his own secret vectors \( \color{red}{\pmb{s’}}, \color{red}{\pmb{e’}} \leftarrow \text{small}(\mathtt{Z}_q^{n\times 1}) \) and \(\color{red}{\pmb{e}} \leftarrow \text{small}(\mathtt{Z}_q^{1 \times 1}) \) and uses these to make the following LWE samples:
\( (\pmb{A}, \pmb{b’} = \pmb{A}^T \color{red}{\pmb{s’}} + \color{red}{\pmb{e’}} \bmod q) \)
\( (\pmb{b}, v’ = \pmb{b}^T \color{red}{\pmb{s’}} + \color{red}{\pmb{e'{}’}} \bmod q) \)
As was the case with Alice’s public key, the LWE problem states that it is hard to recover \( \color{red}{\pmb{s’}} \) from these samples and that the samples look uniformly random. Now all Bob has to do is add the message to one of these samples. Notice that \(v’\) is a random integer between \(0\) and \(q\). Instead of releasing \(v’\), Bob adds the message to the most significant bit of \(v’\) by calculating \(v’ + \frac{q}{2} m \bmod q \). As \(v’\) seems random, \(v’ + \frac{q}{2} m \bmod q \) will also seem random and thus our bit is safely hidden. Bob’s ciphertext consists thus of the two samples:
\( (\pmb{A}, \pmb{b’} = \pmb{A}^T \color{red}{\pmb{s’}} + \color{red}{\pmb{e’}} \bmod q) \)
\( (\pmb{b}, v’ = \pmb{b}^T \color{red}{\pmb{s’}} + \color{red}{\pmb{e'{}’}} + \frac{q}{2} m \bmod q) \)
But in practice he does not have to send over \(\pmb{A}\) or \(\pmb{b}\) again.
decryption
Now it’s time for Alice to recover our message \(m\). She does this by first calculating \(\Delta v = v’ – \pmb{b’}^T \color{red}{\pmb{s}}\). To see why this might help, we can work out this equation, which gives (this should not be too hard to calculate yourself, but if you are lazy you can just believe me):
\(\Delta v = \frac{q}{2} m + \color{red}{\pmb{s’}}^T \pmb{A} \color{red}{\pmb{s}} + \color{red}{\pmb{e}}^T \color{red}{\pmb{s’}} + \color{red}{\pmb{e'{}’}} – \color{red}{\pmb{s’}}^T \pmb{A} \color{red}{\pmb{s}} – \color{red}{\pmb{s}}^T \color{red}{\pmb{e’}} \bmod q \)
The two terms involving \(\pmb{A}\) will have big coefficients, while the other terms are multiplications of small vectors and will thus also be relatively small. After having a closer look, we can see that the two big terms cancel each other out and we end up with:
\(\Delta v = \frac{q}{2} m + \color{red}{\pmb{e}}^T \color{red}{\pmb{s’}} + \color{red}{\pmb{e'{}’}} – \color{red}{\pmb{s}}^T \color{red}{\pmb{e’}} \bmod q \)
Now, as long as \( \color{red}{\pmb{e}}^T \color{red}{\pmb{s’}} + \color{red}{\pmb{e'{}’}} – \color{red}{\pmb{s}}^T \color{red}{\pmb{e’}} \) is small enough (i.e. smaller than \(\frac{q}{4}\)) we can recover the value of \(m\) as:
\(m = \lfloor \frac{2}{q} \Delta v \rceil \)
An overview of the steps taken by Alice and Bob can be found in this picture:

Conclusion
It was not so hard to build our own encryption scheme, was it? However, the devil is in the details. This scheme it is far from efficient. For example, we can only encrypt one bit, and the public key contains a full n by n matrix. Also, while this scheme seems secure, how do we actually prove it is secure (or at least as secure as the underlying LWE problem) and how do we keep it secure against active adversaries that submit their own malicious ciphertext. In the last years research into lattice-based encryption has greatly advanced, offering significant improvements in bandwidth and computational costs and a better understanding of the security of these encryption algorithms. But that is a story for another time.
Further reading:
- The original LWE encryption scheme is described in this paper:
O. Regev. On lattices, learning with errors, random linear codes, and cryptography. In STOC 2005. https://cims.nyu.edu/~regev/papers/qcrypto.pdf
However, recent algorithms that improve upon this paper might be more accessible to start your journey into practical LWE encryption schemes. Therefore, I would recommend you to start here:
- J. W. Bos, C. Costello, L. Ducas, I. Mironov, M. Naehrig, V. Nikolaenko, A. Raghunathan, and D. Stebila. Frodo: Take off the ring! Practical, quantum-secure key exchange from LWE. In CCS 2016 https://eprint.iacr.org/2016/659.pdf
- E. Alkim, L. Ducas, T. Pöppelmann, P. Schwabe. Post-quantum Key Exchange – A New Hope. USENIX Security Symposium 2016. https://eprint.iacr.org/2015/1092.pdf


