Chapter 5
Reed-Solomon codes
On this page
Chapter 4 ended with a Reed-Solomon encoder in miniature: two data bytes, divided by a generator polynomial, gave two error correction bytes. This chapter does the same with real data, then turns to the harder half: how a scanner uses the error correction bytes to tell whether any bytes were misread, find them, and work out what they should have been.
Reed-Solomon codes are named after Irving Reed and Gustave Solomon, who described them in 1960. The same codes protect the data on CDs, DVDs and Blu-ray discs, and data sent back by space probes such as Voyager.
EncodingLink to this section
A QR code’s data bytes are split into one or more blocks, and each block gets its own error correction bytes. The last section of this chapter explains the splitting; until then, take a single block, the 16 data bytes of a version 1-M code. As in chapter 4, the bytes of a block are the coefficients of a polynomial, with the first byte the highest power (7.5.2).
The generator polynomial for a block with error correction bytes has a factor for each of the first powers of (Annex A). As in chapter 4, is the byte 2, so the factors are , , , and so on:
Version 1-M has 10 error correction bytes, so its generator has degree 10. Multiplied out, it is
Table A.1 lists it with each coefficient as a power of , such as for 216.
Encoding works as in chapter 2: append zero bytes to the data, which multiplies its polynomial by , divide by , and put the remainder in place of the zeros. Taking away the remainder leaves a multiple of , and taking away is the same as adding, so the finished block is a multiple of . Each step of the division clears one data byte, so there are as many steps as data bytes.
The widget below steps through the standard’s own example, 01234567 in version 1-M (Annex I), which chapter 3 turned
into data bytes. Try your own text too.
Numeric mode: 16 data bytes (5⅛ encoding the text, 10⅞ of padding), then 10 error correction bytes.
The leading byte, the coefficient of x²⁵, is 16. Subtract 16x¹⁵·g(x): each coefficient of g(x) times 16, lined up under it. That clears it to 0.
After this step: 0 cleared · 1 the 10 bytes after the leading byte, which the next step works on · 0 the appended zeros, which end up as the remainder
The result
After the last step, the remainder is the error correction bytes: 165, 36, 212, 193, 237, 54, 199, 135, 44, 85. Put after the data, they make a block of 26 bytes that is a multiple of g(x):
Each step only involves the leading byte and the 10 after it. The standard describes the encoder that way, as a circuit with 10 one-byte registers that holds those 10 bytes and takes in one data byte at a time (Figure 14).
DecodingLink to this section
A scanner reads the 26 bytes of the block, some of which may be wrong, without knowing which. From those bytes alone it has to answer three questions: are any of them wrong, which ones, and what should they have been? Annex B sets out the steps, and the sections below follow them.
SyndromesLink to this section
is 0 at each of , since one of its factors is 0 there. A valid block is times something, so it is 0 there too. A scanner takes the block it read as a polynomial , and works out its value at each of those points:
These are the syndromes, a medical word for a set of symptoms. If they are all 0, the block is valid, and the scanner accepts it.
If some bytes were misread, is the block that was sent, , plus an error polynomial . is 0 except at the misread bytes, where its coefficient is the difference between what was read and what was sent: as bytes add by XOR, that’s the bits that were flipped. Since ,
so the syndromes depend only on the errors, not on the data. Everything that follows works from the syndromes alone.
One errorLink to this section
Suppose a single byte was misread: the coefficient of , with error value . Then , and writing for the error’s position,
The syndromes are , then , then , and so on: each is the one before times . So the first syndrome is the error value, and dividing the second by the first gives , which says where the error is.
For example, take the block of Annex I, and suppose its sixth byte, 128, is misread as 255. The syndromes are 127, 104, 204, 41, 126, 220, 166, 144, 131 and 205. They aren’t all 0, so something is wrong. Their logs, the powers of they are equal to, show the pattern: 87, 107, 127, 147, 167, 187, 207, 227, 247 and 12. Each is 20 more than the one before; the last wraps around, as .
So , and the error is in the coefficient of . The first byte is the coefficient of , so
that is the sixth byte. The error value is , and adding it to the byte that was read gives back what was
sent: , as 11111111 ⊕ 01111111 = 10000000. The other eight syndromes check the answer: with more
than one error, their logs wouldn’t keep going up by the same step.
ErasuresLink to this section
Sometimes a scanner knows which bytes are damaged: their modules may be covered, torn away, or impossible to make out. The standard calls a damaged byte in a known place an erasure, and one in an unknown place an error (7.5.1).
For an erasure only the value is unknown, and each syndrome gives an equation for the values. With two erasures, at positions and with values and , the first two syndromes are
and are known, so these are two linear equations in two unknowns, solved as in school algebra. Multiplying the first by and adding it to the second cancels :
isn’t 0, because the positions are different, so dividing by it gives , and then .
The same works for any number of erasures: erasures need the first syndromes, and the equations always have exactly one solution when the positions are all different. So a block can fill in as many erasures as it has error correction bytes, wherever they are: any 10 of the 26 bytes of version 1-M.
ErrorsLink to this section
An error has two unknowns: where it is, and its value. Two unknowns need two syndromes, so the 10 syndromes of version 1-M can find and fix at most 5 errors. That’s why an erasure is worth half an error, as chapter 2 put it: one error correction byte to find where a damaged byte is, and one to work out its value. The standard’s formula (7.5.1) counts both kinds:
for erasures and errors in a block with error correction bytes. is the number of error correction bytes kept back to detect errors rather than correct them. It’s 0 except in the smallest codes, for reasons explained below.
The values are found as for erasures, once the positions are known. But the positions are harder: they are in the exponents, so the equations for them aren’t linear. A scanner could try every possible set of positions, solving for the values each time and keeping the set that fits all the syndromes. For 4 errors among 26 bytes, there are 14,950 sets to try. But blocks in larger codes are up to 153 bytes long and can correct up to 15 errors, and there are about ways to choose 15 positions out of 153. Instead, a decoder finds the positions with a polynomial.
The error locatorLink to this section
Take two errors, at positions and with values and , so that . The error locator polynomial is the polynomial whose roots are the error positions:
where and , remembering that subtracting is adding. Since , . Multiplying both sides by gives
The same holds for and , and adding the two gives
The same two numbers, and , turn each pair of syndromes into the next. This time the unknowns are not in exponents. Taking and ,
are two linear equations for and , solved as for erasures. That gives , and its roots are the error positions. To find them, the scanner simply tries each byte of the block: if , the coefficient of is wrong. Once the positions are known, the errors can be treated as erasures, and their values come from the syndromes as before.
The same works for any number of errors. With errors, has degree , and its unknown coefficients come from equations, which use the syndromes to : two syndromes per error again. Annex B writes out the four equations for the four errors that version 1-M can correct. For a single error, , and the equations say , the pattern in the example above.
A scanner doesn’t know how many errors there are. It tries 0, 1, 2 and so on, and takes the fewest whose fits every syndrome, not just the used to find it. The syndromes left over check the answer.
So a scanner decodes a block in four steps (Annex B):
- Work out the syndromes. If they are all 0, stop: there are no errors.
- Find from the syndromes, and try each byte of the block to find its roots, the error positions.
- Find the error values from the syndromes.
- Add each error value to the byte that was read.
A slip in Annex B. Step (iii) of Annex B gives the equations for the error values with , , … in the first equation and on the right. But by its own step (i), , which is the plain sum of the error values. Its equations hold for to , not to . (The step also lists the error positions as , and , missing out .)
Erasures and errors togetherLink to this section
When a block has both, the erasures are cancelled out of the syndromes first. An erasure at position with value adds to each , so has no trace of it: . Doing this along the whole list of syndromes gives a list one shorter with the erasure gone. Each error is still there, as a sequence that grows by the same at each step, only with a different value. So each erasure uses up one syndrome, and finding the errors in what’s left takes two syndromes per error: again.
Try itLink to this section
The widget below decodes the block from the encoder above. Select bytes to damage them: Change gives a byte a different value, as a misread byte would have, and Erase marks it as unreadable. The bar counts what the damage uses up: two for each error, one for each erasure.
The block encoded above, “01234567”: 16 data bytes, then 10 error correction bytes.
16 data · 165 error correction · 255 changed: an error · ? erased
0 × 2 for errors + 0 for erasures = 0 of the 8 this block can use: 10 error correction bytes, 2 kept back.
1. Syndromes
S0 = 0S1 = 0S2 = 0S3 = 0S4 = 0S5 = 0S6 = 0S7 = 0S8 = 0S9 = 0 All 0: nothing to correct.
No errors: every syndrome is 0.
Too many errorsLink to this section
With more damage than that, the decoder may find no that fits, or one without enough roots among the block’s bytes. Then it knows the block is too badly damaged, and the scanner reports that it can’t read the code. Or it may find a that fits, which turns the block into a different valid block, and the scanner returns the wrong data without noticing.
It’s the same as with the format information in chapter 2: two valid blocks always differ in at least bytes. If two differed in bytes or fewer, subtracting one from the other would give a valid block with at most bytes that aren’t 0, and its syndromes would all be 0. But think of those bytes as erasures in a block of zeros: the erasure equations have exactly one solution, and with every syndrome 0, it is all zeros. So the two blocks can’t differ at all. With at most errors, then, the block that was read is closer to the block that was sent than to any other. With more, it may land closer to a different one. The last example in the widget is damage of that kind.
That’s rare when the damage is random, but less rare in small codes. Suppose a block is misread so badly that its bytes are effectively random. The decoder accepts it if it happens to be within 5 bytes of a valid block, which for version 1-M happens to about 1 random block in 17 million. With , it corrects at most 4 errors, the last two syndromes only check the answer, and the odds drop to about 1 in 20 billion. The values of in 7.5.1 bring the smallest codes into line with the rest: with them, the odds for every version and level are about 1 in 2 billion or better.
Erasures need the same care. A decoder using most of its syndromes on erasures has few left to check with, so the standard sets whenever more than half of a block’s error correction bytes are erased, and says not to use erasures at all in codes with fewer than 8 error correction bytes (7.5.1).
Faster methodsLink to this section
Annex B’s method works for any QR code, but most decoders use faster methods that give the same answers. These are the names you may have heard:
- Berlekamp–Massey algorithm (Elwyn Berlekamp, 1968, and James Massey, 1969). It builds one syndrome at a time, adjusting it whenever the next syndrome doesn’t fit, instead of solving the equations. It finds the number of errors along the way, and for errors takes about steps instead of the of solving equations. The Euclidean algorithm, which finds the greatest common divisor of two numbers, is another way to find .
- Chien search (Robert Chien, 1964). The same trial of every position, arranged so that moving from one position to the next multiplies each term of by a fixed power of , instead of working out afresh each time. It suits hardware, which can do all the multiplications at once.
- Forney’s algorithm (David Forney, 1965). A formula that gives each error value directly from and the syndromes, instead of solving equations.
Annex B’s method itself is known as Peterson’s method, after W. Wesley Peterson, who worked it out in 1960 for BCH codes like those in chapter 2; Daniel Gorenstein and Neal Zierler extended it to Reed-Solomon codes in 1961. Many descriptions also write the locator the other way round, as , whose roots are the inverses of the error positions: it’s the same polynomial with its coefficients in reverse order.
A QR code block never needs more than 15 errors found, so solving the equations is quick enough for a scanner, and the widget above does it that way.
Splitting into blocks and interleavingLink to this section
Larger codes split their data bytes into several blocks, each with its own error correction bytes (Table 9). One reason is that a Reed-Solomon block can’t be longer than 255 bytes: each byte needs its own position , and there are only 255 non-zero bytes. A version 40 code has 3,706 bytes. Another is speed, as finding more errors in a longer block takes more work. QR codes keep their blocks to at most 153 bytes, with at most 30 error correction bytes each.
Every block in a code has the same number of error correction bytes. When the data bytes don’t divide evenly, some blocks get one more data byte than the others, and the shorter blocks come first. Version 5-H, for example, has two blocks with 11 data bytes and two with 12, each with 22 error correction bytes (7.6).
The blocks are then interleaved (7.6): the final sequence takes the first data byte of each block in turn, then the second byte of each, and so on, then the error correction bytes in the same way. The longer blocks’ extra data bytes come last among the data. Numbering version 5-H’s data bytes D1 to D46 and its error correction bytes E1 to E88, block by block, the sequence starts D1, D12, D23, D35, D2, D13, D24, D36, and so on (Figure 15).
Interleaving is there for damage. Chapter 6 places the bytes in the code in sequence, each next to the one before, so a patch of damage usually hits a run of consecutive bytes. Interleaving shares the run out among the blocks, so each gets only a few of the damaged bytes. Try it below: damage a run of bytes, and compare the standard’s interleaved order with the blocks placed one after another.
Version 5-H: 134 bytes in 2 blocks of 33 bytes (11 data + 22 error correction) and 2 blocks of 34 bytes (12 data + 22 error correction). Each block can correct 11 errors.
The sequence of bytes
Where the damage lands
data · error correction · damaged · the number on the right counts each block's damaged bytes
Point at a byte to see where it is in the sequence and in its block.
No block has more than 11 damaged bytes, and each can correct 11, so the code can still be read.
The interleaved sequence is what fills the data area of the code. Chapter 6 shows where each of its bits goes.