How a QR code survives a torn corner Image: Donald Trung Quoc Don (CC BY-SA 4.0)

How a QR code survives a torn corner

A QR code on a poster gets rained on, scratched, and half covered by a sticker, and a phone camera reads it anyway. Companies print their logo over the centre of their codes on purpose, and the codes still work. None of this is luck. Every QR code carries, alongside its message, a block of extra symbols computed by an algorithm that Irving Reed and Gustave Solomon published in 1960, and those symbols allow a reader to recover the message even when a sizeable part of the code is wrong. The arithmetic behind the algorithm takes place in a finite field with 256 elements, which is the field that Unit 12 of the masterclass constructs.

What is in a QR code

The code was designed in 1994 by Masahiro Hara at Denso Wave, a Toyota subsidiary, to track car parts; the name stands for Quick Response. The small squares are the cells of the code. Three large squares in the corners tell the camera where the code is and how it is tilted, and the rest of the grid holds data in blocks of 8 cells, each block a byte, read in a zigzag from the bottom right. A code of version 1 is 21 modules on a side and holds 26 bytes; version 40 is 177 on a side and holds 2956. The message takes some of the bytes. The remainder are error-correction bytes, and how many there are depends on a level chosen when the code is made: L, M, Q, or H, allowing about 7, 15, 25, or 30 percent of the bytes to be damaged.

Take version 1 at level M, a code of the kind you see on a business card. It has 26 bytes in all, 16 for the message and 10 for error correction. The reader can correct any 5 wrong bytes, wherever they are and whatever has happened to them. Since each byte is a patch of 8 cells, that is a good part of the code.

The idea in one sentence

Two points determine a line. If you want to send two numbers to a friend across a bad telephone line, you can treat them as the slope and intercept of a line, read off five points on that line, and send the five points. If the friend receives four of them correctly and one wrong, four of the five points lie on a common line and one does not, and the friend can find the line, discard the stray point, and recover your two numbers. Send more points and more errors can be tolerated. That is the whole idea of a Reed-Solomon code: the message is encoded as a polynomial, the polynomial is sampled at more points than its degree requires, and the extra samples make the polynomial recoverable when some samples are corrupted.

More exactly, a message of \(k\) bytes is turned into a polynomial of degree less than \(k\), and \(n > k\) values of it are transmitted. Any \(k\) correct values determine the polynomial, and the redundancy \(n - k\) allows the decoder to correct up to \((n - k)/2\) errors whose positions it does not know, or \(n - k\) errors whose positions it does, such as bytes that are missing because the corner is torn off. For version 1 at level M, \(n - k = 10\) and the code corrects 5 unknown errors or 10 erasures. The property that makes the whole thing work is the one proved in Unit 2: a non-zero polynomial of degree less than \(k\) has fewer than \(k\) roots, so two different polynomials of that degree cannot agree at \(k\) points.

Why a finite field

The line-through-points idea needs arithmetic in which one can add, subtract, multiply, and divide, so that lines through given points can be found: in other words, a field. Real numbers are a field, but a byte is not a real number, and rounding would destroy everything. What is needed is a field with exactly 256 elements, so that every element is a byte, every sum and product of bytes is a byte, and every non-zero byte has a reciprocal that is also a byte.

Such a field exists, and Unit 12 builds it. Start with the field \(\mathbb{F}_2\) of two elements, 0 and 1, with \(1 + 1 = 0\). A byte is a string of 8 bits, which can be read as a polynomial of degree at most 7 with coefficients in \(\mathbb{F}_2\): the byte 10001101 is \(x^7 + x^3 + x^2 + 1\). Adding two bytes means adding the polynomials, which is the bitwise exclusive or, and is done by a single machine instruction. Multiplying means multiplying the polynomials and then taking the remainder on division by a fixed polynomial of degree 8, which for QR codes is

\[ x^8 + x^4 + x^3 + x^2 + 1. \]

The point of the remainder is to keep the result within 8 bits, and the point of choosing this particular polynomial is that it is irreducible over \(\mathbb{F}_2\), meaning that it cannot be factored into polynomials of lower degree. The theorem of Unit 12 says that the remainders modulo an irreducible polynomial of degree 8 form a field with \(2^8 = 256\) elements, usually written \(\mathbb{F}_{256}\). Division works because of the Euclidean algorithm for polynomials, in the same way that division modulo a prime works for integers in Unit 7. There are 30 irreducible polynomials of degree 8 over \(\mathbb{F}_2\), and any of them would give the same field up to renaming; the QR standard picks this one, and the AES encryption standard picks a different one, \(x^8 + x^4 + x^3 + x + 1\).

Encoding and decoding

The encoder does not literally sample a polynomial. It multiplies the message polynomial by \(x^{n-k}\) and divides by a fixed generator polynomial whose roots are consecutive powers of a chosen element of \(\mathbb{F}_{256}\), and the remainder of that division is the block of error-correction bytes. The received codeword is then a polynomial that should be divisible by the generator, and the decoder checks by evaluating it at the generator's roots. If every value is zero, nothing is wrong. If not, the values form a set of numbers called syndromes, and the Berlekamp-Massey algorithm of 1968 turns the syndromes into a polynomial whose roots say which bytes are wrong, after which a second calculation says by how much. All of this is arithmetic in \(\mathbb{F}_{256}\) with tables of 256 entries, which is why a phone does it in a fraction of a millisecond. The page Creating a QR Code step by step by Project Nayuki shows every stage of the encoding for any text you type, with the error-correction bytes computed in front of you.

Where else the same code lives

Reed-Solomon codes are the most widely deployed piece of abstract algebra in existence. Compact discs use two of them, which is why a CD with a scratch plays without a click; DVDs and Blu-ray discs do the same. The Voyager probes carry a Reed-Solomon encoder, first used for the pictures of Uranus in 1986, and the code became part of the standard for deep-space communication for decades. Hard drives and the RAID systems of data centres use them to survive a failed disk, and so do the barcodes on parcels and the Data Matrix codes on circuit boards. Reed and Solomon's paper, Polynomial Codes over Certain Finite Fields, is four pages long, and for its first decade it had no efficient decoder and no application.

The algebra it needs is exactly the content of Units 7 and 12 of the masterclass: congruences and the ring \(\mathbb{Z}/n\mathbb{Z}\), the polynomial ring over a field, division with remainder, irreducible polynomials, and the construction of a field with \(p^n\) elements as a quotient. The units build \(\mathbb{F}_4\) by hand, then \(\mathbb{F}_{256}\), and Unit 12 closes with the Reed-Solomon encoding of a short message as its application. The curriculum has the details, and the next time a damaged code scans, it is worth remembering that a polynomial of degree 7 over a field of two elements did the work.

Study algebra properly

The Algebra Masterclass: weekly units, quizzes, a final exam, and a certificate.
About the program