Reed-Solomon codes
| Problem | Correct errors in whole bytes (symbols) rather than bits, so that bursts of corrupted bits cost no more than a few symbol errors |
| Devised by | Irving Reed and Gustave Solomon (1960); Elwyn Berlekamp (1968) and James Massey (1969), the decoding algorithm; Robert Chien (1964), the root search |
| Accuracy | Corrects any byte errors, the most possible for the redundancy (MDS); parity bytes identical to the reedsolo package (table below) |
| Cost | Encoding field multiplications; decoding for the syndromes and the Chien search plus for Berlekamp-Massey |
| Read first | Linear block codes and Hamming codes, Polynomials and decimal-string integers |
| API | ReedSolomon (new, encode, parity, decode, correct, error_capacity) |
Overview
Hamming codes work on bits and correct one of them. Reed-Solomon (RS) codes work on whole bytes and correct many, by a construction based on polynomials.
The idea is a familiar one. A polynomial of degree less than is determined by its values at any points. So let the message bytes be the coefficients of such a polynomial, and transmit its values at points. The extra values are redundant: any correct values would be enough to recover the polynomial. Two different polynomials of degree below can agree at no more than points (their difference would otherwise have too many roots), so two codewords differ in at least positions. That is the largest minimum distance any code with these parameters can have.
With that distance an RS code corrects byte errors, wherever they are and however many bits of each byte are wrong. This makes RS codes especially good against bursts, runs of consecutive bit errors: a burst of up to bits never touches more than bytes. The library uses the systematic form of the code, in which the codeword is the message itself followed by parity bytes.
For the arithmetic to work, the bytes must be elements of a number system in which one can add, subtract, multiply and divide, a field, with exactly 256 elements. That is the finite field GF(256).
Where it is used
Reed-Solomon codes correct whole bytes, so a scratch or a burst of interference costs no more than a few symbols.
- CDs, DVDs and Blu-ray. A scratched disc still plays because interleaved Reed-Solomon codes repair the burst of lost bytes.
- QR codes. A QR code that is torn, dirty or partly covered by a logo still scans, because up to 30% of its bytes can be rebuilt (level H).
- Space and broadcast. NASA missions concatenate a Reed-Solomon outer code with a convolutional inner code, and digital TV and DSL use the same combination.
- Storage systems. Data centres use Reed-Solomon-style erasure codes to survive failed disks without keeping full copies of the data.
History
Irving Reed and Gustave Solomon, at MIT Lincoln Laboratory, published their five-page paper in 1960. Decoding was impractical until Elwyn Berlekamp’s algorithm (1968), which James Massey (1969) recast as the synthesis of the shortest linear feedback shift register generating the syndromes. Robert Chien’s search for the error locations (1964) completed the decoder. RS codes were then used everywhere: Voyager’s images (concatenated with a convolutional code), compact discs (two cross-interleaved RS codes), DVDs, QR codes, DSL and RAID 6. RS(255, 223) became the CCSDS deep-space standard.
The idea
The field GF(256)
Ordinary integer arithmetic modulo 256 will not do, because division is not always possible in it (2 has no inverse). The field GF(256) is built differently. A byte is read as a polynomial of degree below 8 whose coefficients are its bits; for example is .
- Addition adds the polynomials, with coefficients modulo 2. That is the XOR of the bytes. Every element is its own negative, so subtraction is the same operation.
- Multiplication multiplies the polynomials and takes the remainder on division by a fixed polynomial of degree 8, here (0x11D), which keeps the result within eight bits. Because this polynomial cannot be factored, every nonzero element has an inverse, just as the integers modulo a prime do.
The polynomial is also primitive, which means that the powers of the element (the byte 2) run through all 255 nonzero elements before repeating. Every nonzero element is thus for some , its “logarithm”. Multiplication becomes addition of logarithms modulo 255, done with two small tables.
Systematic encoding
In the systematic form, the code is described through polynomial roots. Regard a word of bytes as the coefficients of a polynomial of degree below . The codewords are defined to be the polynomials that vanish at consecutive powers of , that is, the multiples of the generator polynomial
To encode a message polynomial so that its bytes appear unchanged in the codeword, shift it up by places, , leaving room for the parity bytes, and divide by . Subtracting the remainder makes the result exactly divisible:
The remainder has degree below , so it occupies only the low positions: those are the parity bytes, and the high positions still hold the message. The division is carried out by a shift register over GF(256). Codes may be shortened to any by fixing leading message bytes at zero and not sending them.
Decoding
The received polynomial is , where the error polynomial has a nonzero coefficient at each corrupted position. Suppose there are errors, at positions with values . The decoder must find both the positions and the values: unknowns. It has equations, which is why it can handle .
- Syndromes. Evaluate the received polynomial at the roots of : for . Since every codeword vanishes there, : as with Hamming codes, the syndromes depend only on the errors. If all are zero there is no detectable error. Otherwise, writing for the error locators,
These equations are nonlinear in the unknown , and solving them directly is hard. The classical method solves for the locations first, by a detour. 2. Error-locator polynomial. Define the polynomial whose roots are the inverses of the locators, . Each syndrome is a sum of powers , and such sums satisfy a linear recurrence whose coefficients are those of : . (This is the same fact that Prony’s method rests on.) The Berlekamp-Massey algorithm finds the shortest recurrence that generates the observed syndromes, and hence . If its length exceeds , the word is uncorrectable. 3. Chien search. Find the roots of by trying every candidate: evaluate it at for each position . A zero means position is in error. If fewer than roots are found within the codeword, the word is uncorrectable. 4. Error values. With the locators known, the syndrome equations are linear in the values . Solve them (here by Gauss-Jordan elimination; Forney’s formula is the usual faster alternative) and subtract the errors from the received word.
With more than errors the decoder usually reports failure. Rarely, the corrupted word lies within distance of a different codeword, and the decoder returns a wrong message.
Figure 19.3. RS failure probability.
The simulated failure rate follows the binomial probability of more than 16 byte errors. Of the 10667 simulated codewords that were not recovered, the decoder reported 10667 as uncorrectable and returned a wrong message for 0.
Table 19.2. Reed-Solomon parity compared with `reedsolo`, and correction of injected errors.
| code (n, k) | parity bytes | parity vs reedsolo | errors t = (n − k)/2 injected | decoding |
|---|---|---|---|---|
| (255, 223) | 32 | identical | 16 | all corrected |
| (255, 239) | 16 | identical | 8 | all corrected |
| (26, 16) | 10 | identical | 5 | all corrected |
| (15, 9) | 6 | identical | 3 | all corrected |
| (7, 3) | 4 | identical | 2 | all corrected |
Algorithm
Encoding a message of bytes into a codeword of bytes.
- Keep a register of bytes, initially zero.
- For each message byte: add (XOR) it to the top byte of the register to get a feedback value; shift the register up by one byte; add the feedback value times each coefficient of the generator polynomial to the corresponding byte of the register.
- After the last message byte, the register holds the remainder. Append it to the message.
This is long division of by , one message byte at a time, and costs field operations.
Decoding a received word.
- Syndromes. Evaluate the received polynomial at by Horner’s rule. If all are zero, return the message part unchanged.
- Locator. Run the Berlekamp-Massey algorithm on the syndromes. It processes them one at a time: it computes the discrepancy between the next syndrome and what the current recurrence predicts, and when the discrepancy is not zero it corrects the recurrence with a multiple of an earlier version of itself, lengthening it when necessary. The result is and its degree . If , report failure.
- Positions. Evaluate for every position of the codeword; the zeros are the error positions. If their number is not , report failure.
- Values. Solve the linear system given by the first syndrome equations for the error values.
- Correct. Add each error value to the byte at its position, and return the message part.
Decoding costs field operations for the syndromes and the search, plus for the linear system.
Implementation
// Berlekamp-Massey for the error locator polynomial (lowest degree first).
let mut c = vec![1u8];
let mut b = vec![1u8];
let (mut l, mut m, mut bb) = (0usize, 1usize, 1u8);
for i in 0..nsym {
let mut d = s[i];
for j in 1..=l.min(c.len() - 1) {
d ^= gmul(c[j], s[i - j]);
}
if d == 0 {
m += 1;
} else if 2 * l <= i {
let t = c.clone();
let coef = gdiv(d, bb);
if c.len() < b.len() + m {
c.resize(b.len() + m, 0);
}
for (j, &bj) in b.iter().enumerate() {
c[j + m] ^= gmul(coef, bj);
}
l = i + 1 - l;
b = t;
bb = d;
m = 1;
} else {
let coef = gdiv(d, bb);
if c.len() < b.len() + m {
c.resize(b.len() + m, 0);
}
for (j, &bj) in b.iter().enumerate() {
c[j + m] ^= gmul(coef, bj);
}
m += 1;
}
}
if l > nsym / 2 {
return Err(ErrorsJSL::RuntimeError("Reed-Solomon word has too many errors to correct."));
}
decodereturns the message and the number of corrected bytes;correctreturns the whole codeword.- The conventions (0x11D, , first root ) are those of QR codes and of the
reedsoloPython package. CCSDS uses a different field polynomial and first root, so its codewords differ.
In practice
//! Reed-Solomon RS(255, 223): correcting up to 16 byte errors, including long bursts.
use lib_jsl_information::forward_error_correction::ReedSolomon;
fn main() {
let rs = ReedSolomon::new(255, 223).unwrap();
let message: Vec<u8> = (0..223).map(|i| (i * 7 % 256) as u8).collect();
let cw = rs.encode(&message).unwrap();
println!("RS({}, {}): {} parity bytes, corrects {} byte errors", rs.n(), rs.k(), cw.len() - message.len(), rs.error_capacity());
// A burst wiping out 16 consecutive bytes (128 bits).
let mut rx = cw.clone();
for b in &mut rx[100..116] {
*b = !*b;
}
let (decoded, corrected) = rs.decode(&rx).unwrap();
println!("16-byte burst: {corrected} bytes corrected, message recovered: {}", decoded == message);
// One more error is beyond the code's capacity.
rx[200] ^= 0x55;
println!("17 errors: {:?}", rs.decode(&rx).map(|(_, c)| c));
// A shortened code for short messages.
let short = ReedSolomon::new(20, 12).unwrap();
let cw = short.encode(b"Hello, RS!!!").unwrap();
let mut rx = cw.clone();
rx[0] = b'J';
rx[15] ^= 0xFF;
let (m, c) = short.decode(&rx).unwrap();
println!("RS(20, 12): {} errors corrected -> {:?}", c, String::from_utf8(m).unwrap());
}
RS(255, 223): 32 parity bytes, corrects 16 byte errors
16-byte burst: 16 bytes corrected, message recovered: true
17 errors: Err(RuntimeError("Reed-Solomon word is uncorrectable."))
RS(20, 12): 2 errors corrected -> "Hello, RS!!!"
Strengths
- Maximum distance for the redundancy; corrects bursts; flexible lengths by shortening.
Weaknesses
- Symbol-oriented: scattered single-bit errors each cost a whole byte; hard-decision decoding uses no soft information.
- This decoder does not take erasure positions, which would double the number of correctable symbols.
Exercises
- Show that an code has (the Singleton bound), and that RS codes meet it.
- Compute for over GF(256) and encode a 3-byte message by hand.
- Show that the syndromes of an error pattern are a sum of geometric sequences, and why the locator polynomial generates them.
- Derive Forney’s formula for the error values from the error evaluator polynomial.
- Show that errors and erasures are correctable when .
Hints and answers
- Fixing coordinates of a codeword leaves at most one codeword (MDS-style argument), so , and with this gives . A nonzero polynomial of degree has at most roots, so an RS codeword (its evaluations) has weight at least : equality.
- For , over GF(256) with ; , so . Encode by dividing by and appending the remainder.
- For errors at locations with values , : sums of geometric sequences. The locator annihilates them: .
- With , the error value at location is (with first consecutive root ).
- Each erasure uses one unit of redundancy and each error two (locating plus correcting), so correction is possible when .
Further reading
- I. S. Reed and G. Solomon, “Polynomial codes over certain finite fields”, Journal of the Society for Industrial and Applied Mathematics 8 (1960) 300–304.
- J. L. Massey, “Shift-register synthesis and BCH decoding”, IEEE Transactions on Information Theory 15 (1969) 122–127.
- E. R. Berlekamp, Algebraic Coding Theory, McGraw-Hill, 1968.
- S. B. Wicker and V. K. Bhargava (eds.), Reed-Solomon Codes and Their Applications, IEEE Press, 1994.