Chapter 4

Arithmetic on bytes

On this page

Chapter 2 protected the format and version information by dividing polynomials whose coefficients were bits. The data is protected the same way, with one change: the coefficients are bytes. Damage to a QR code usually comes in patches, such as a smudge or a tear across a cluster of neighbouring modules, and the 8 bits of each byte usually sit together in a block 2 modules wide and 4 tall (chapter 6 shows the layout). So the data’s error correction works with whole bytes: a damaged byte counts as one error, however many of its bits are wrong. These are Reed-Solomon codes, the subject of chapter 5.

Dividing polynomials whose coefficients are bytes takes a way to add, subtract, multiply and divide the bytes themselves. This chapter builds one.

From bits to bytesLink to this section

Chapter 2’s arithmetic on bits was ordinary arithmetic with one twist, 1+1=01 + 1 = 0, so that adding was XOR and subtracting was the same as adding. It followed all the usual rules: the order of a sum or product doesn’t matter, brackets multiply out, and anything except 0 can be divided by, though with only 0 and 1 there’s little to divide. A set of values with arithmetic like this is called a field, and a finite one is also called a Galois field, after the mathematician Évariste Galois.

Reed-Solomon codes need a field whose values are the 256 bytes. Encoding needs only adding and multiplying, but decoding solves equations to find the errors and repair them, and that takes division too.

Why not ordinary arithmetic?Link to this section

The obvious choice is ordinary arithmetic, keeping the remainder after dividing by 256, as computers do with 8-bit numbers. Adding and multiplying work, but dividing doesn’t:

The trouble is that 256 splits into factors, 2×1282 \times 128. Remainders after dividing by a prime number would work, but the nearest prime, 257, gives 257 different remainders, one too many for a byte.

Bytes as polynomialsLink to this section

The way out is chapter 2’s idea: read a byte’s 8 bits as the coefficients of a polynomial, the first bit for x7x^7 and the last for x0=1x^0 = 1:

00011101=x4+x3+x2+1\texttt{00011101} = x^4 + x^3 + x^2 + 1

Every byte is a polynomial of degree 7 or less with bit coefficients, and every such polynomial is a byte. Adding and multiplying the polynomials gives a way to add and multiply bytes. There is a catch, and its fix is the one that failed above, done with a polynomial that can’t be split into factors.

Adding bytesLink to this section

Adding polynomials adds the coefficients of each power separately. For example, take the bytes 82 and 25:

82=01010010=x6+x4+x25=00011001=x4+x3+1\begin{aligned} 82 &= \texttt{01010010} = x^6 + x^4 + x \\ 25 &= \texttt{00011001} = x^4 + x^3 + 1 \end{aligned}

Both have an x4x^4 term. In ordinary algebra their sum would have 2x42x^4, but the coefficients are bits, and as in chapter 2, 1+1=01 + 1 = 0. So the two x4x^4 terms cancel out:

(x6+x4+x)+(x4+x3+1)=x6+(1+1)x4+x3+x+1=x6+x3+x+1\begin{aligned} &(x^6 + x^4 + x) + (x^4 + x^3 + 1) \\ &\quad = x^6 + (1 + 1)x^4 + x^3 + x + 1 \\ &\quad = x^6 + x^3 + x + 1 \end{aligned}

Each power is added on its own, and nothing is carried to the next, so lining the bits up under their powers shows that adding is the XOR of the two bytes:

x7x6x5x4x3x2x1820101001025000110017501001011\begin{array}{r|cccccccc} & x^7 & x^6 & x^5 & x^4 & x^3 & x^2 & x & 1 \\ \hline 82 & 0 & 1 & 0 & 1 & 0 & 0 & 1 & 0 \\ 25 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 1 \\ \hline 75 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 1 \end{array}

Written as numbers, 82+25=7582 + 25 = 75. The sum of two bytes is always a byte. Every byte added to itself gives 0, so each byte is its own negative, and subtracting is the same as adding.

Multiplying bytesLink to this section

Multiplying polynomials multiplies every term of one by every term of the other, and adds up the results. Multiplying by xix^i moves every bit ii places to the left, so the product of two bytes aa and bb is the sum of copies of aa, one for each 1 bit in bb, shifted left by that bit’s position. It’s long multiplication in binary, but with XOR instead of carrying. For example, multiplying 3 by itself gives two xx terms, which cancel out, as 1+1=01 + 1 = 0:

3×3=(x+1)(x+1)=x2+x+x+1=x2+1=5\begin{aligned} 3 \times 3 &= (x + 1)(x + 1) \\ &= x^2 + x + x + 1 \\ &= x^2 + 1 = 5 \end{aligned}

The product can have up to 15 bits, from x14x^{14} down to x0x^0, too many for a byte. The fix is the one that failed for numbers: divide, and keep the remainder. Dividing by a polynomial of degree 8 leaves a remainder of degree 7 or less, which is a byte. The standard divides by

p(x)=x8+x4+x3+x2+1p(x) = x^8 + x^4 + x^3 + x^2 + 1

or 100011101 in bits (ISO/IEC 18004, 7.5.2). The division is chapter 2’s long division: XOR p(x)p(x) in under the leading 1, again and again, until only 8 bits are left.

Pick two bytes below, by their bits or as numbers, to multiply them.

a = x⁶ + x⁴ + x² + x + 1
b = x⁷ + x + 1
Try:

1. Multiply the polynomials

One copy of a for each 1 in b, shifted left by that bit's position, all added together with XOR:

01010111 01010111 01010111 010101101111001

2. Divide by p(x) and keep the remainder

The product has 14 bits, too many for a byte. Divide by p(x) = 100011101, XORing it in under each leading 1 until 8 bits are left:

010101101111001 100011101 000100011011001 100011101 000000000110001

0 zeros left of the leading 1 · 1 p(x) · 1 the answer · the dashed line marks the right-hand 8 bits, a byte

87 × 131 = 49 (31 in hexadecimal, 00110001)

Check: add the logs

Using the log table from the next section: 87 = 2189 and 131 = 2247, so their product is 2189 + 247 = 2436 = 2181, since 2255 = 1. That's 49.

Why this polynomial?Link to this section

Dividing by 256 failed because 256=2×128256 = 2 \times 128, so that 2 and 128 multiply to 0. Polynomials can split into factors too, with the same result. For example, x8+1=(x+1)8x^8 + 1 = (x + 1)^8, so dividing by x8+1x^8 + 1 instead, the bytes 3 (x+1x + 1) and 255 (x7+x6+⋯+1x^7 + x^6 + \dots + 1), which multiply to x8+1x^8 + 1, would give 0.

p(x)p(x) can’t be split into factors: it is irreducible, the polynomial version of a prime number, which is why the standard calls it the field’s “prime modulus polynomial”. With it, no two bytes multiply to 0 unless one of them is 0. There are 30 irreducible polynomials of degree 8, and any of them would give a field of 256 bytes. The next section shows what makes this one a good choice.

Powers of 2 and log tablesLink to this section

Multiplying by 2 is the simplest case. 2 is 00000010, or xx, so multiplying by 2 shifts the bits one place left. If a 1 moves out of the byte, the result has degree 8, and one step of division brings it back: XOR with 100011101. So the powers of 2 begin as usual, 1, 2, 4, 8, 16, 32, 64, 128, but then

28=100000000⊕100011101=00011101=29\begin{aligned} 2^8 &= \texttt{100000000} \oplus \texttt{100011101} \\ &= \texttt{00011101} = 29 \end{aligned}

and go on 58, 116, 232, 205, 135 and so on. Step through them below, or press Play. The powers of 2 visit every byte except 0, each exactly once, and arrive back at 1 at 22552^{255}.

28 = 29

28 = 2 × 27: shift 10000000 left to get 100000000. That's 9 bits, so XOR with 100011101 to get 00011101 = 29.

29 = 28, so its log is 8. Its inverse is 2247 = 131, because 28 × 2247 = 2255 = 1.

Each square is a byte, placed by its two hexadecimal digits: row 1, column D is 1D, or 29. The number in it is its log. the latest power · the inverse of the latest power · 0, which is never reached

So every byte except 0 is a power of 2, and has a log: the power kk, from 0 to 254, for which 2k2^k is that byte.

That isn’t so for every irreducible polynomial. The powers of 2 always come back to 1, but they can do it before reaching every byte. The AES encryption standard uses the irreducible polynomial x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1, and there the powers of 2 come back to 1 after only 51 steps. A polynomial for which the powers of 2 reach every byte except 0 is called primitive. 16 of the 30 irreducible polynomials of degree 8 are, including the standard’s, which is why it can call 2 the field’s primitive element (Annex A).

Finding all 16. A polynomial of degree 8 is x8x^8 plus one of 256 bytes, few enough to try every one. For each, start at 1 and keep multiplying by 2, shifting left and XORing with that polynomial, until the powers come back to 1. Exactly 16 polynomials take 255 steps, and the standard’s is the smallest of them. This finds them without checking first which polynomials are irreducible. Under one that splits into factors, some bytes multiply to 0, and no power of 2 can be one of them: if 2k×b2^k \times b were 0, multiplying on by 2 until the powers return to 1 would turn it into bb, but multiplying 0 always gives 0. So the powers can’t visit all 255 non-zero bytes.

Multiplying with logsLink to this section

Logs turn multiplying into adding, as they do for ordinary numbers:

2j×2k=2j+k2^j \times 2^k = 2^{j + k}

The powers repeat every 255 steps, as 2255=12^{255} = 1, so if j+kj + k is 255 or more, subtract 255. For example, 3=2253 = 2^{25}, so 3×3=2503 \times 3 = 2^{50}, which is 5, as before.

Software that makes and reads QR codes multiplies this way. A table of the 255 powers and a table of the logs turn every multiplication into three lookups and an addition, instead of shifting and dividing; the byte multiplier above checks its answer this way. 0 is the exception: it isn’t a power of 2, so it has no log, but anything times 0 is 0 anyway.

Dividing bytesLink to this section

The powers of 2 also give every byte except 0 an inverse: a byte to multiply it by to get 1. If a byte is 2k2^k, its inverse is 2255−k2^{255 - k}, because

2k×2255−k=2255=12^k \times 2^{255 - k} = 2^{255} = 1

For example, 2=212 = 2^1, so its inverse is 22542^{254}, which is 142. Multiplying 142, 10001110, by 2 shifts it left to 100011100, and XOR with 100011101 leaves 00000001, or 1. With remainders after dividing by 256, 2 had no inverse at all. The table of powers above shows the inverse of each byte: point at one to see it.

Dividing by a byte is multiplying by its inverse, which is the same as subtracting logs:

2j÷2k=2j−k2^j \div 2^k = 2^{j - k}

adding 255 if j−kj - k is negative. Every byte except 0 can be divided by, with exactly one answer.

So bytes can now be added, subtracted, multiplied and divided, following the same rules as ordinary arithmetic. This is the field the standard calls GF(2⁸): the Galois field with 28=2562^8 = 256 values (7.5.2).

Polynomials with byte coefficientsLink to this section

Reed-Solomon codes work with polynomials whose coefficients are bytes. Here xx is a new variable, and it no longer matters that each byte is itself a polynomial: bytes are just values to add and multiply as in this chapter. To keep the two apart, the standard writes the byte 2 as α\alpha, so its powers are α0=1\alpha^0 = 1, α1=2\alpha^1 = 2, α2=4\alpha^2 = 4, up to α254\alpha^{254} (Annex A).

Adding two polynomials adds the coefficients of each power, with byte addition, XOR. Here 3+1=23 + 1 = 2, 5+5=05 + 5 = 0 and 7+2=57 + 2 = 5:

(3x2+5x+7)+(x2+5x+2)=2x2+5\begin{aligned} (3x^2 + 5x + 7) &+ (x^2 + 5x + 2) \\ &= 2x^2 + 5 \end{aligned}

Multiplying multiplies every term by every term and adds the results, with byte multiplication. Reed-Solomon codes use products like this one, where subtracting is adding:

(x−1)(x−2)=x2+(1+2)x+1×2=x2+3x+2\begin{aligned} (x - 1)(x - 2) &= x^2 + (1 + 2)x + 1 \times 2 \\ &= x^2 + 3x + 2 \end{aligned}

Since 3=α253 = \alpha^{25}, the standard writes this as x2+α25x+αx^2 + \alpha^{25}x + \alpha, the first of its generator polynomials (Table A.1). Chapter 5 shows what they are for.

Dividing is long division again. Each step divides the leading coefficient of what’s left by the leading coefficient of the divisor, and subtracts that multiple of the divisor, which clears the leading term. For example, to divide 7x3+x27x^3 + x^2 by x2+3x+2x^2 + 3x + 2:

  1. The leading term is 7x37x^3, so subtract 7x7x times the divisor, 7x3+9x2+14x7x^3 + 9x^2 + 14x. Here 7×3=97 \times 3 = 9: 111 times 11 is 111 ⊕ 1110 = 1001. That leaves 8x2+14x8x^2 + 14x, as 1+9=81 + 9 = 8.
  2. The leading term is now 8x28x^2, so subtract 8 times the divisor, 8x2+24x+168x^2 + 24x + 16. That leaves 22x+1622x + 16, as 14+24=2214 + 24 = 22.

22x+1622x + 16 has a lower degree than the divisor, so it is the remainder, and the quotient is 7x+87x + 8. This divisor’s leading coefficient is 1, so each step only needed the leading coefficient of what was left; the divisors are always like this when encoding, but decoding needs division by other bytes too.

This is a Reed-Solomon encoder in miniature. The dividend is two data bytes, 7 and 1, followed by two zero bytes, and the divisor is the generator polynomial from above. Putting the remainder in place of the zeros gives 7x3+x2+22x+167x^3 + x^2 + 22x + 16: the data, then two error correction bytes, 22 and 16, which together make a multiple of x2+3x+2x^2 + 3x + 2. Chapter 5 does the same with real data and longer generators, and shows how a scanner uses the error correction bytes to find and repair damaged bytes.