PQCSA: A Gentle Introduction to Code Based PKE

Roughly 1.5 months ago, COSIC’s summer school on post-quantum cryptography and the standardization progress took place in Albena. Several candidates and their underlying cryptographic assumptions were discussed, but in this blog we will have a brief look at linear codes and how to build public key encryption schemes from them.

Linear codes

Let’s consider the following scenario: we aim to transmit some message \(\vec{m}\) over a lossy medium (e.g. straight over the wire), but, because of external influences, parts of the message may be corrupted.

The first goal in this case is to detect that something has gone wrong, since the receiver has no frame of reference. This is not a new problem, and it can be solved by using error detection mechanisms that add some data. A very simple way of performing error detection for a bit-string \(\vec{m} \in \{0, 1\}^k\) is to add a 1 at the end of the message if the sum of bits in \(\vec{m} \) is odd, and append 0 otherwise.

Picture illustrating the error detection mechanism based on parity. In the first row, a message with even number of ones, implying a zero is added at the end of the string. In the second row, the first bit of the string has flipped, rendering  the parity bit incorrect

The idea is illustrated in the picture above. In the first row, we have the message \( \vec{m}\) with even number of ones, implying that we add a zero end. In the second row, the message \( \vec{m}^*\) is what is received. There, the first bit has flipped, but we can detect it since the parity check bit is now incorrect. While we succeeded in our goal, this approach has an obvious limitation. Hint: what happens if more than one bit flips ? So there is a need for more sophisticated approaches, which do exist. E.g. there is the cyclic redundancy check (CRC) which is commonly deplyoed e.g. in the ethernet protocols.

So far we have discussed ways to detect codes and a natural question to ask is whether we can also correct them. This turns out to be the case, and in fact CRC can be used for error correction, although we won’t look further into this. In general, we will say that there is a message space \( M = \mathbb{F}_q^k\) and a code space \( C = \mathbb{F}_q^n, (n > k) \). Then, the purpose of an error correcting code, is to map a message \( \vec{m} \in M \) to a code word \( \vec{c} \in C \) such that it is possible to map \( \vec{c}\) back to \( \vec{m}\), even if the codeword has been corrupted. More concretely, this means that if we run the decoding algorithm on \( \tilde{c} = c + e \) instead of \( \vec{c} \), we should still recover \( \vec{m}\). There exists an entire zoo of different types of codes, but the one family we are interested in are linear codes.

Linear codes are characterized by the fact that the encoding procedure is linear, and can simply be described as \[ \mathsf{Encode}(\vec{m}) = G \vec{m} \] The matrix \( G \) is commonly referred to as the generator matrix. Linear codes are often preferred due to their simple description and the fact that it is often possible to implement them efficiently. For related reasons, they will be the primary candidates to build public key cryptography.

Code Based Cryptography

We just learned about linear codes, so the next question is how to build cryptographic primitives from them and what the underlying assumptions are. First, we recall the notion of hamming distance and hamming weight for vectors: \[ \mathsf{dist}(\vec{c}, \vec{c}’) := |\{ i \in [n] : \vec{c}_i \neq \vec{c}’_i \} | \] and \[ ||\vec{c}||_0 := \mathsf{dist}(\vec{c}, \vec{0}) \]

Then, we’ve mostly looked at how encoding works, but not really decoding. One the one hand, this is is an introduction, and the decoding methods will be highly dependent on the specific code used. On the other hand, it turns out that, in general, decoding can be made hard enough so that it’ll be possible to build encryption schemes from. Hence, we’ll describe next two of the popular cryptographic assumptions stemming from linear codes.

  • Our first problem is is the codeword-finding problem. Here, given a (noisy) vector \( \vec{c}’ = \vec{c} + \vec{e}\), with \( ||\vec{e}||_0 \leq w \), we are asked for a fixed \( w \) to recover \( \vec{c} \) knowing that in belongs to the code space of some known linear code.
  • Another common setting is the syndrome-decoding problem. In this case, we are given the parity check matrix \( H \) of a linear code with generator \( G \), which simply means that \( G \cdot H^\top = 0\). The parity check matrix leads to the notion of a syndrome which corresponds to \( \vec{s} = \vec{\tilde{c}}^\top H = (\vec{c} + \vec{e})^\top H = \vec{e}^\top H\). Then, the syndrome decoding problem, given \(\vec{s}, H, w\) asks us to find an \( \vec{e}\) with \( ||\vec{e}||_0 \leq w\) and \(\vec{s} = \vec{e}^\top H\).

From a theoretical view, variants of these problems have been shown to be NP complete and are therefore suited to build encryption schemes from. During NIST’s PQC competition, several code based scheme were considered that rely on variants or specific instantiations of the aforementioned problems, e.g. HQC, which was chosen for standardization, relies Quasi-Cyclic Moderate-Density Parity-Check codes. Describing HQC in detail is out of scope, so to close this blog post we’ll will have a look at another code based scheme that also participated in the NIST competition: classical McElliece.

Classical McEliece is a code-based scheme that is quite old compared to other NIST candidates, going back to the 70s. The scheme is a general blueprint that may use different linear codes internally, but, with that being said, the type of code will impact security. The go-to choice are binary Goppa codes, as that specific instantiation has resisted cryptanalysis for a long time. Nevertheless, we shall describe the general version below, starting from the case where we have a linear code with generator matrix \( G \), capable of correcting \( t \) errors. The system itself is simple and the sub-procedures for a public key scheme are described below:

  • Key Generation: Besides \( G \), the user also generates a binary, non-singular matrix \( S \) and a permutation matrix \( P \). Then, the public key corresponds to \( (\hat{G} = S G P, t) \) and the private key contains \( (S, P) \)
  • Encryption: To encrypt a vector \( \vec{m} \), one generates an error \( \vec{e}\) s.t. \( ||\vec{e}||_0 \leq t\) and returns \( \vec{\tilde{c}} = \hat{G} \vec{m} + \vec{e} \)
  • Decryption: First, the user computes \(\vec{\hat{c}} = \vec{\tilde{c}} P^{-1}\). Note that this has no effect on \( ||\vec{e}||_0\). Then, the user runs the normal decoding procedure of the code to obtain \( \vec{m} S\), from which they can easily obtain \( \vec{m} \), noting that \( S \) is non-singular.

It’s a nice exercise to show that this scheme is indeed correct and to think about how the security of the scheme is related to the hard problems we discussed earlier. As a final note, it’s important to point out that when we use McEliece, we need to make some additional tweaks and there are issues with the “textbook” version. Any interested reader can check out e.g. [Nojima et al.]