---
title: CRCs and error-correcting codes in hardware
heading: LFSR and parallel CRCs, Hamming and SECDED codes, syndromes and interleaving
eyebrow: S3 · Lesson 39 · CRCs and error-correcting codes in hardware
xp: 50
---

::: hook
Every Ethernet frame ends with a 32-bit CRC, and every word in a server's memory carries 8 extra bits that correct any single flipped bit. Both are linear codes over bits, and both turn into XOR networks in hardware: a CRC is polynomial division done by a shift register, or many bits at a time by a wide XOR matrix; an ECC is a set of parity checks whose failures, read as a binary number, point at the bit to flip. This lesson builds both and puts numbers on what they catch.
:::

::: play
- The line is the overhead of a SECDED code, the check bits as a fraction of the stored word, against the number of data bits m.
- Single-error correction needs r check bits with $2^r \ge m + r + 1$; double-error detection adds one more.
- Wide words amortize the checks: 5 bits protect 8 (38% overhead), 8 protect 64 (11%), 11 protect 512 (2%).
:::

```sim plot
view: graph
x: m = 4..512 log
y: o = 0..50 %
samples: 600
plot: (ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2)) + 1)/(m + ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2)) + 1)*100 | label: SECDED overhead
```

::: predict
## How many check bits does a SECDED code need for 128-bit words?

- [ ] 7
- [ ] 8
- [x] 9
- [ ] 16

::: reveal
Single-error correction needs the smallest r with $2^r \ge 128 + r + 1$: r = 7 gives 128 < 136, but r = 8 gives 256 ≥ 137. Detecting double errors as well takes one more overall parity bit: 9, a (137, 128) code with 6.6% overhead.
:::
:::

::: nameit
## CRCs

A CRC treats the message as a polynomial over GF(2) and appends the remainder of dividing it by a generator polynomial of degree n. The serial circuit is a linear-feedback shift register that takes one bit per clock; a **parallel CRC** precomputes how w bits at once change the n-bit state, an XOR matrix, for a throughput of $w f_\text{clk}$.

An n-bit CRC with a well-chosen generator detects:
- every single-bit error and every burst of length n or less;
- every odd number of errors, if $(x + 1)$ divides the generator;
- other random corruption with probability $1 - 2^{-n}$.

Standard check values pin down a CRC's exact parameters: CRC-16/CCITT-FALSE (generator 0x1021, initial value 0xFFFF) gives 0x29B1 for the ASCII string "123456789".

## Hamming codes

Put check bits at the power-of-two positions 1, 2, 4, …; check bit $2^j$ is the parity of every position whose index has bit j set. A single error at position k makes exactly the checks in k's binary expansion fail, so the **syndrome**, the failing checks read as a number, equals k. Correction is a decoder and an XOR.
$$2^r \ge m + r + 1$$
**SECDED** adds an overall parity bit: a nonzero syndrome with odd overall parity is a single error (correct it); a nonzero syndrome with even parity is a double error (report it).

## Interleaving

A particle strike can flip several adjacent cells. Interleaving the bits of k words across a row (k-way column multiplexing) puts adjacent cells in different words, so a burst of up to k bits causes at most one error per word.
:::

::: example
## Encode the data 1011 in Hamming(7,4) (positions 1 to 7 hold p1, p2, d1, p4, d2, d3, d4), flip the bit at position 6, and decode.

::: solution
1. **Parity bits.** With $d_1 d_2 d_3 d_4 = 1011$:
   $$\begin{aligned} p_1 &= d_1 \oplus d_2 \oplus d_4 \\ &= 0 \\ p_2 &= d_1 \oplus d_3 \oplus d_4 \\ &= 1 \\ p_4 &= d_2 \oplus d_3 \oplus d_4 \\ &= 0 \end{aligned}$$
   so the codeword is 0110011.
2. **Flip position 6.** That gives 0110001.
3. **Syndrome.** The checks over positions {1, 3, 5, 7}, {2, 3, 6, 7} and {4, 5, 6, 7} give 0, 1 and 1:
   $$110_2 = 6$$
4. **Correct.** Flipping bit 6 back restores 1011.

**Answer:** the codeword is 0110011; after the flip the syndrome is 6, and correcting bit 6 recovers 1011.
:::
:::

::: example
## A 32-bit CRC engine handles 64 bits per clock at 400 MHz. What throughput is that, and how wide must it be for 100 Gb/s at the same clock?

::: solution
1. **Throughput.**
   $$64 \times 400\ \text{MHz} = 25.6\ \text{Gb/s}$$
2. **Width for 100 Gb/s.** It needs 250 bits per clock, so a 256-bit datapath.
3. **The cost.** The XOR matrix grows with the width; very wide engines split the data into chunks and combine their CRCs.

**Answer:** 25.6 Gb/s, and a 256-bit datapath for 100 Gb/s.
:::
:::

::: example
## A network sees a million corrupted frames a day, each with random errors. How often does one slip past its CRC-32?

::: solution
1. **One frame's chance.** Each corrupted frame passes with probability
   $$2^{-32} = 2.3 \times 10^{-10}$$
2. **Per day.** So $2.3 \times 10^{-4}$ a day get through: about one every 4300 days, 12 years.
3. **A 16-bit CRC.** Under the same load it would let 15 through every day, which is why fast links use 32 bits.

**Answer:** about one every 4300 days, roughly 12 years.
:::
:::

::: check
kind: parts
given: m = 8 | 16 | 32 | 64 | 128

## A memory word holds {m} data bits protected by a Hamming code.

::: part
answer: ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2))
unit: bits
tolerance: 0

### How many check bits does single-error correction need?

::: misses
- [any] The smallest r with $2^r \ge m + r + 1$.
:::
:::

::: part
answer: ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2)) + 1
unit: bits
tolerance: 0

### And SECDED?

::: misses
- [any] One more for overall parity.
:::
:::

::: part
answer: (ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2)) + 1)/(m + ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2)) + 1)*100
unit: %
tolerance: 0.5%

### What fraction of the stored word is check bits?

::: misses
- [(ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2)) + 1)/m*100] Divide by the whole stored word, data plus checks.
- [any] Check bits over total bits.
:::
:::
:::

::: check
kind: parts
given: w = 8 | 32 | 64 | 128; f = 250 | 500; R = 40 | 100

## A parallel CRC engine takes {w} bits per clock at {f} MHz.

::: part
answer: w*f/1000
unit: Gb/s
tolerance: 0.5%

### What throughput does it reach?

::: misses
- [any] $wf_\text{clk}$.
:::
:::

::: part
answer: R*1000/f
unit: bits
tolerance: 0.5%

### How many bits per clock would {R} Gb/s need?

::: misses
- [any] Rate over clock.
:::
:::
:::

::: check
kind: parts
given: n = 16 | 32; E = 1000 | 1000000

## A link checks frames with an {n}-bit CRC and sees {E} randomly corrupted frames a day.

::: part
answer: 2^(-n)
tolerance: 0.5%

### What is the chance a corrupted frame passes?

::: misses
- [any] $2^{-n}$.
:::
:::

::: part
answer: E*365*2^(-n)
tolerance: 0.5%

### How many slip through in a year?

::: misses
- [any] Corrupted frames × probability.
:::
:::
:::

::: check
kind: parts
given: b = 2 | 3 | 4 | 6; k = 2 | 4 | 8

## A particle strike flips {b} adjacent cells in an SRAM row; the row interleaves {k} SECDED words bit by bit.

::: part
answer: ceil(b/k)
unit: errors
tolerance: 0

### How many errors land in the worst-hit word?

::: misses
- [any] $\lceil b/k\rceil$.
:::
:::

::: part
answer: b
tolerance: 0

### What is the least interleaving that keeps every word correctable?

::: misses
- [any] At least one word per flipped cell.
:::
:::
:::

::: check
kind: order

## Put a SECDED decoder's steps in order for a word read from memory.

1. Recompute the check bits from the data bits read
2. XOR them with the stored check bits to form the syndrome
3. Check the overall parity of the whole word
4. If the syndrome is nonzero and the parity is odd, flip the bit the syndrome names
5. If the syndrome is nonzero and the parity is even, report an uncorrectable double error

::: misses
- [any] Form the syndrome first, then use it together with the overall parity.
:::
:::

::: check
kind: choice

## Why does an n-bit CRC catch every burst error n bits long or shorter?

- [ ] Because it's longer than the burst
- [x] A burst is the error polynomial $x^i\,e(x)$ with e of degree below n; a generator of degree n with a nonzero constant term can't divide it, so the remainder changes
- [ ] Bursts always flip an odd number of bits
- [ ] CRCs compare every bit with a copy

::: misses
- [1] Length alone isn't the reason; the algebra is.
- [3] A burst can flip any number of bits.
- [4] A CRC keeps only n bits about the whole message.
:::
:::

::: check
kind: derivation
vars: m = 4..1024, r = 2..12

## A block code stores m data bits with r check bits.

::: step
answer: m/(m + r)

### What is its code rate?
:::

::: step
answer: r/(m + r)

### What fraction of the stored bits is overhead?
:::

::: step
answer: 2^r - r - 1

### What is the most data bits a single-error-correcting Hamming code with r checks can protect?
:::

::: right
Right. The checks must name every one of the $m + r$ positions, plus "no error".
:::
:::

::: check
kind: python

## Write `crc16(data)`: CRC-16/CCITT-FALSE of a bytes object (generator 0x1021, initial value 0xFFFF, most significant bit first, no final XOR), and `ham_fix(c)`: given a 7-bit Hamming codeword as a list of bits in positions 1 to 7 (p1, p2, d1, p4, d2, d3, d4), correct any single error and return (data bits [d1, d2, d3, d4], syndrome).

```python starter
def crc16(data):
    return 0

def ham_fix(c):
    return ([c[2], c[4], c[5], c[6]], 0)
```

```python solution
def crc16(data):
    c = 0xFFFF
    for b in data:
        c ^= b << 8
        for _ in range(8):
            c = ((c << 1) ^ 0x1021) & 0xFFFF if c & 0x8000 else (c << 1) & 0xFFFF
    return c

def ham_fix(c):
    s = 0
    for pos in range(1, 8):
        if c[pos - 1]:
            s ^= pos
    c = list(c)
    if s:
        c[s - 1] ^= 1
    return ([c[2], c[4], c[5], c[6]], s)
```

```python tests
assert crc16(b'123456789') == 0x29B1, "The standard check value: 0x29B1."
assert crc16(b'') == 0xFFFF, "Nothing in, the initial value out."
good = [0, 1, 1, 0, 0, 1, 1]
assert ham_fix(good) == ([1, 0, 1, 1], 0), "A clean codeword: syndrome 0."
for k in range(7):
    bad = list(good); bad[k] ^= 1
    assert ham_fix(bad) == ([1, 0, 1, 1], k + 1), "Each single error is named by its position and corrected."
```

::: right
Right. XORing the positions of all the 1 bits computes every parity check at once: that's the syndrome.
:::
:::

::: explain
## How does a Hamming code's syndrome point directly at the flipped bit?

::: model
The check bits sit at positions 1, 2, 4 and so on, and each covers exactly the positions whose binary index has its bit set: check 1 covers odd positions, check 2 covers positions with the 2's bit set, and so on. When the decoder recomputes the checks, a single error at position k disturbs precisely the checks corresponding to the 1 bits of k. Reading the failed checks as the bits of a binary number therefore reproduces k. A syndrome of zero means no single error, and any other value is the address of the bit to flip.
:::
:::

::: practice
::: check
given: m = 8 | 16 | 24 | 32 | 64 | 128 | 256
answer: ceil(log(m + 1 + ceil(log(m + 1)/log(2)))/log(2)) + 1
unit: bits
tolerance: 0

## SECDED check bits for {m} data bits?

::: misses
- [any] Smallest r with $2^r \ge m + r + 1$, plus one.
:::
:::

::: check
given: w = 16 | 256; f = 300 | 800
answer: w*f/1000
unit: Gb/s
tolerance: 0.5%

## Throughput of a {w}-bit-per-clock CRC at {f} MHz?

::: misses
- [any] $wf$.
:::
:::

::: check
given: n = 5 | 8 | 16 | 24 | 32
answer: 2^(-n)
tolerance: 0.5%

## Chance random corruption passes an {n}-bit CRC?

::: misses
- [any] $2^{-n}$.
:::
:::
:::
