Lattice-based cryptography

Basic Lattice Cryptography (opens new window) by Vadim Lyubashevsky is an excellent introduction to lattice-based cryptography. Below are the notes I took while reading the first part.

Some notation first: x\lceil x \rceil means rounding up, x\lfloor x \rfloor means rounding down, x\lfloor x \rceil means the nearest integer,

All operations in what follows are performed in the ring Zq\mathbb{Z}_q, with the usual addition and multiplication of integers modulo qq. For a set SS, we write aSa \leftarrow S to mean that aa is chosen uniformly at random from the set SS. For any positive integer β\beta, we will define the set

[β]={β,...,1,0,1,...,β}[\beta] = \{-\beta, ..., -1, 0, 1,...,\beta \}

A motivating example

Let’s pretend that for positive integers qq, n>>mn >> m, and β<<q\beta << q, the following two distributions are computationally indistinguishable:

(A,As),AZqn×m,s[β]m(\mathbf{A}, \mathbf{A}\mathbf{s}), \mathbf{A} \leftarrow \mathbb{Z}_q^{n \times m}, \mathbf{s} \leftarrow [\beta]^m

(A,u),AZqn×m,uZqn(\mathbf{A}, \mathbf{u}), \mathbf{A} \leftarrow \mathbb{Z}_q^{n \times m}, \mathbf{u} \leftarrow \mathbb{Z}_q^n

This indistinguishability assumption is clearly false because one can use Gauss elimination to invert A\mathbf{A} (or an m×mm \times m submatrix of A\mathbf{A}) and check whether there is indeed an s[β]m\mathbf{s} \in [\beta]^m such that As=u\mathbf{A}\mathbf{s} = \mathbf{u}.

We will see later that a slightly modified version of this assumption forms the foundation of lattice-based cryptography.

We will now use this (false) assumption to construct a simple CPA-secure public key encryption scheme that is very similar to the discrete logarithm-based El Gamal encryption scheme:

sk:s[β]m,pk:(AZqn×m,t=As)sk: \mathbf{s} \leftarrow [\beta]^m, pk: (\mathbf{A} \leftarrow \mathbb{Z}_q^{n \times m}, \mathbf{t} = \mathbf{A} \mathbf{s})

To encrypt a message μZq\mu \in \mathbb{Z}_q, the encryptor picks a random r[β]m\mathbf{r} \leftarrow [\beta]^m and outputs:

(uT=rTA,v=rTt+μ)(\mathbf{u}^T = \mathbf{r}^T \mathbf{A}, v = \mathbf{r}^T \mathbf{t} + \mu)

To decrypt, one simply computes

μ=vuTs\mu = v - \mathbf{u}^T \mathbf{s}

The LWE problem

The following adjustment to the assumption from the previous section is needed to make it plausibly valid and still allow us to construct a cryptosystem following the same outline. A simple version of the Learning with Errors Problem (LWE), on which a lot of lattice-based cryptography rests is given next.

Definition: For positive integers mm, nn, qq, and β<q\beta < q, the LWEn,m,q,βLWE_{n,m,q,\beta} problem asks to distinguish between the following two distributions:

(A,As+e),AZqn×m,s[β]m,e[β]n(\mathbf{A}, \mathbf{A}\mathbf{s} + \mathbf{e}), \mathbf{A} \leftarrow \mathbb{Z}_q^{n \times m}, \mathbf{s} \leftarrow [\beta]^m, \mathbf{e} \leftarrow [\beta]^n

(A,u),AZqn×m,uZqn(\mathbf{A}, \mathbf{u}), \mathbf{A} \leftarrow \mathbb{Z}_q^{n \times m}, \mathbf{u} \leftarrow \mathbb{Z}_q^n

The crucial part that makes LWEn,m,q,βLWE_{n,m,q,\beta} hard is the presence of the additional “error” vector e\mathbf{e} which makes the Gaussian elimination attack inapplicable. The problem becomes harder as mm and β/q\beta / q grow. The parameter nn is not known to have a large impact on the hardness of the problem except in extreme cases.

In the original definition of LWE, a rounded Gaussian distribution was used, but it was later shown that other distributions, such as uniform or binomial (used in Kyber), can also be used.

To account for the different distributions that one could use, we can define the LWE problem relative to the distribution of the secrets ψ\psi as:

Definition: For positive integers mm, nn, qq, and β<q\beta < q, the LWEn,m,q,βLWE_{n,m,q,\beta} problem asks to distinguish between the following two distributions:

(A,As+e),AZqn×m,sψm,eψn(\mathbf{A}, \mathbf{A}\mathbf{s} + \mathbf{e}), \mathbf{A} \leftarrow \mathbb{Z}_q^{n \times m}, \mathbf{s} \leftarrow \psi^m, \mathbf{e} \leftarrow \psi^n

(A,u),AZqn×m,uZqn(\mathbf{A}, \mathbf{u}), \mathbf{A} \leftarrow \mathbb{Z}_q^{n \times m}, \mathbf{u} \leftarrow \mathbb{Z}_q^n

The hardness of the problem depends on the parameters mm, nn, qq, and β\beta. What’s interesting is that you can make it even too hard - having for example n<mn < m and β\beta large enough, then the distribution (A,As+e)(\mathbf{A}, \mathbf{A} \mathbf{s} + \mathbf{e}) could be statistically close to (A,u)(\mathbf{A}, \mathbf{u}), which would make it impossible to build an encryption scheme based on just this assumption. On the other hand, if we set m=1m = 1, then the problem becomes too easy because it’s not difficult to figure out whether As+e\mathbf{A} \mathbf{s} + \mathbf{e} is close to a multiple of A\mathbf{A}.

Also, s\mathbf{s} can be chosen from Zqm\mathbb{Z}_q^m, but it can be shown the LWE problem would be equally hard. For efficiency, s\mathbf{s} is usually choosen from a smaller set.

An LWE-Based Encryption Scheme

Finally, let’s fix the (broken) scheme from above. This scheme is due to Oded Regev and his famous paper from 2009.

The key generation is as follows:

sk:s[β]m,pk:(AZqm×m,t=As+e1),e1[β]msk: \mathbf{s} \leftarrow [\beta]^m, pk: (\mathbf{A} \leftarrow \mathbb{Z}_q^{m \times m}, \mathbf{t} = \mathbf{A} \mathbf{s} + \mathbf{e_1}), \mathbf{e_1} \in [\beta]^m

The scheme is able to encrypt only one bit at a time, so μ{0,1}\mu \in \{0, 1\}. To encrypt a message μZq\mu \in \mathbb{Z}_q, the encryptor picks a random r,e2[β]m,e3[β]\mathbf{r}, \mathbf{e_2} \leftarrow [\beta]^m, e_3 \leftarrow [\beta] and outputs:

(uT=rTA+e2T,v=rTt+e3+q2μ)(\mathbf{u}^T = \mathbf{r}^T \mathbf{A} + \mathbf{e_2}^T, v = \mathbf{r}^T \mathbf{t} + e_3 + \lceil \frac{q}{2} \rfloor \mu)

Note that q2\lceil \frac{q}{2} \rfloor is the closest integer to q2\frac{q}{2} (there is no computation in Zq\mathbb{Z}_q here).

To decrypt:

vuTs=rTe1+e3+q2μe2Tsv - \mathbf{u}^T \mathbf{s} = \mathbf{r}^T \mathbf{e_1} + e_3 + \lceil \frac{q}{2} \rfloor \mu - \mathbf{e_2}^T s

The decryption is then 00 if this value is closer to 00 and it is 11 if this value is closer to q2\frac{q}{2}.

Note that this would work as long as rTe1+e3e2Ts<q4\mathbf{r}^T \mathbf{e_1} + e_3 - \mathbf{e_2}^T s < \frac{q}{4}. That means we need to choose parameters such that 2mβ2+β2m\beta^2 + \beta is smaller than q4\frac{q}{4}.

Public key and ciphertext size trade-offs

Instead of storing the entire A\mathbf{A} (which would require m2log(q)m^2 log(q) bits), we can use a cryptographic PRF and 256-bit seed ρ\rho. The other part of the public key, t\mathbf{t}, depends on the secret key and cannot be compressed in a similar way. Thus, the total size of the public key is 256+mlog(q)256 + m \cdot log(q) bits.

The ciphertext can be represented using (m+1)log(q)(m + 1) log(q) bits. Concrete secure parameter settings will require taking m700m \approx 700 and q213q \approx 2^{13}, making it somewhat inefficient for the encryption of just one plaintext bit to be so large.

There is a generalization of the LWE encryption scheme that allows for various trade-offs between the public key and ciphertext sizes. We modify the sceme above as follows.

sk:S[β]m×l,pk:(AZqm×m,T=AS+E1),E1[β]msk: \mathbf{S} \leftarrow [\beta]^{m \times l}, pk: (\mathbf{A} \leftarrow \mathbb{Z}_q^{m \times m}, \mathbf{T} = \mathbf{A} \mathbf{S} + \mathbf{E_1}), \mathbf{E_1} \in [\beta]^m

Now we can encrypt N=klN = kl bits, which we arrange in the matrix M{0,1}k×l\mathbf{M} \in \{0, 1\}^{k \times l}. To encrypt M\mathbf{M}, the encryptor picks a random R,E2[β]m×k,E3[β]k×l\mathbf{R}, \mathbf{E_2} \leftarrow [\beta]^{m \times k}, \mathbf{E_3} \leftarrow [\beta]^{k \times l} and outputs:

(UT=RTA+E2T,V=RTT+E3+q2M)(\mathbf{U}^T = \mathbf{R}^T \mathbf{A} + \mathbf{E_2}^T, \mathbf{V} = \mathbf{R}^T \mathbf{T} + \mathbf{E_3} + \lceil \frac{q}{2} \rfloor \mathbf{M})

The decryption works as in the previous scheme, but we need to round up NN elements. We can see that by using different k,lk, l, we obtain a trade-off between the public key and ciphertext sizes.

Optimizations

The ciphertext part V\mathbf{V} contributes Nlog(q)N \cdot log(q) bits to the total ciphertext size. Instead of the encryptor publishing all log(q)log(q) bits of each coefficient of VV as part of the ciphertext, the encryptor can transmit only κ\kappa bits per coefficient. This is possible but introduces a further error to the decryption equation.

We choose a set SZqS \subset \mathbb{Z}_q with 2κ2^{\kappa} elements. We want for SS elements to be "nicely" distributed in Zq\mathbb{Z}_q. For example:

S={iq2κ;0i<2κ}S = \{\lceil i \frac{q}{2^{\kappa}} \rfloor; 0 \leq i < 2^{\kappa} \}

Then instead of using an element vv from Zq\mathbb{Z}_q, we use its closest element from SS, denoting it by HIGHS(v)HIGH_S(v). We denote LOWS(v)=vHIGHS(v)LOW_S(v) = v - HIGH_S(v). Instead of transmitting VZqk×l\mathbf{V} \in \mathbb{Z}_q^{k \times l} as part of the ciphertext, one could transmit V=HIGHS(V)\mathbf{V}' = HIGH_S(\mathbf{V}).

Note that there exists E\mathbf{E}' such that V=V+E\mathbf{V} = \mathbf{V}' + \mathbf{E}'.

If the ciphertext (U,V)(\mathbf{U}, \mathbf{V}') is created in this fashion, then the decryption VUS\mathbf{V}' - \mathbf{U} \mathbf{S} produces VUS=RE1+E3E+q2ME2S\mathbf{V}' - \mathbf{U}\mathbf{S} = \mathbf{R}\mathbf{E_1} + \mathbf{E_3} - \mathbf{E}' + \frac{q}{2} \mathbf{M} - \mathbf{E_2} \mathbf{S} where the only difference from the above is the presence of the E\mathbf{E}' term. However, if this term is small (and we chose SS such that the elements of E\mathbf{E}' are small), it can be considered just as an additional error, and the decryption process still works.

Modulus switching

Definition: For an element xZqx \in \mathbb{Z}_q and some positive integer pp, we define a mapping from Zq\mathbb{Z}_q to Zp\mathbb{Z}_p as

[x]qp=xpqZp[x]_{q \rightarrow p} = \lceil \frac{x p}{q} \rfloor \in \mathbb{Z}_p

We can relate the modulus switching and the compression to the set SS as described above as:

  1. p=2κp = 2^{\kappa}
  2. S={xpq;xZp}S = \{ \lceil x \rfloor_{p \rightarrow q}; x \in \mathbb{Z}_p \}
  3. HIGHS(x)=xqppqHIGH_S(x) = \lceil \lceil x \rfloor_{q \rightarrow p} \rfloor_{p \rightarrow q}
  4. LOWS(x)=xHIGHS(x)LOW_S(x) = x - HIGH_S(x)

It can be shown that if one first compresses an element in Zq\mathbb{Z}_q into an element in Zp\mathbb{Z}_p (where p<qp < q) and then decompresses back to Zq\mathbb{Z}_q, the result won’t be too far from the original element, so LOWS(x)LOW_S(x) is small. To reduced the size of the ciphertext, we use xqp\lceil x \rfloor_{q \rightarrow p} instead of xx.

Learning with rounding

It turns out that when the compression is used, we can omit adding the error because it’s already added by the compression/decompression step. Therefore, instead of using t=As+et = \mathbf{A}\mathbf{s} + \mathbf{e}, we can simply use t=Ast = \mathbf{A}\mathbf{s} because the probability that HIGHS(As+e)=AsHIGH_S(\mathbf{A}\mathbf{s} + \mathbf{e}) = \mathbf{A}\mathbf{s} is high.

The advantage of making the LWR assumption is that it allows for smaller parameters because it does not add the error e\mathbf{e}.

Encrypting more bits per slot

The encryption scheme above packed the NN-bit message into a matrix M{0,1}k×lM \in \{0,1\}^{k \times l}. However, it could also be packed into a matrix M{0,1,...,2b1}(kb)×(kb)M \in \{ 0,1,...,2b-1 \}^{(\frac{k}{\sqrt b})\times(\frac{k}{\sqrt b})}.

For decryption to work, the encryption needs to be modified to use q2bM\frac{q}{2^b} M instead of q2M\frac{q}{2} M. The parameters need to be adjusted such that the error is smaller than q2b+1\frac{q}{2^{b+1}} rather than q4\frac{q}{4}.

Non-interactive key exchange

What follows is a protocol for two parties to agree on a single bit b1b_1. To agree on a shared key of more bits, the techniques described above can be employed.

There is a public random matrix AZm×mA \in \mathbb{Z}^{m\times m} that is trusted by everyone to have been honestly generated. The first party chooses s1,e1[β]ms_1, e_1 \leftarrow [\beta]^m, and the second party chooses s2,e2[β]ms_2, e_2 \leftarrow [\beta]^m. The first party computes

u1T=s1TA+e1Tu_1^T = s_1^T A + e_1^T

and sends it to the second party. The second party computes

u2=As2+e2u_2 = A s_2 + e_2

and sends it to the first party.

The first party computes

s1Tu2=s1TAs2+s1Te2s_1^T u_2 = s_1^T A s_2 + s_1^T e_2

If this value is closer to q2\frac{q}{2}, it sets b1=1b_1 = 1. If it is closer to 00, it sets b1=0b_1 = 0.

The second party computes

u1Ts2=s1TAs2+e1Ts2u_1^T s_2 = s_1^T A s_2 + e_1^T s_2

If this value is closer to q2\frac{q}{2}, it sets b2=1b_2 = 1. If it is closer to 00, it sets b2=0b_2 = 0.

If the error terms are small enough, it holds b1=b2b_1 = b_2, and the two parties agreed on a shared bit.