I ADDED REED-SOLOMON TO THE SELF-HEALING ARCHIVE?! lz_rs — RS(255,223), -r Levels, and 6,243 Checks With Zero Failures

I added Reed-Solomon to the self-healing archive — lz_rs, RS(255,223), and 6,243 checks with zero failures

lzply already heals itself: lzip compression, a BCH layer that fixes scattered bit errors, and a SHA-256 hash that guarantees you never get wrong data back silently. Now it has a third layer. lz_rs is everything lzply does, plus Reed-Solomon parity over the whole archive — the same tool, the same commands, one more layer of armor. This is the format the deep-repair work builds on, and it is in the same commit as the CRC16 migration: b27b345.

Why two layers — BCH is a bit code, RS is a byte code

BCH corrects scattered bit errors cheaply: for the same correction power it needs 39–42% less parity than Reed-Solomon. But BCH is a bit code — one bad byte with 8 flipped bits costs 8 of its t, and a burst that wipes a run of bytes costs far more. Reed-Solomon is a symbol code: one bad byte costs 1 of its t no matter how many bits in that byte flipped, so a burst of N bad bytes costs N. RS is the right tool for byte damage and bursts; BCH stays the right tool for scattered bit rot. Stacked, the archive survives both.

The pipeline, per entry:

write: raw bytes -> lzip compress (or store, level 0) -> BCH encode -> RS encode
read:  RS decode -> BCH decode (two-pass, hash-gated) -> lzip decompress -> SHA-256 check

The SHA-256 check at the end is the oracle for the whole stack: no data is ever returned unless it hashes correctly. A bounded-distance decoder can miscorrect when damage exceeds the level limit, and the hash is what catches it — the same guarantee lzply makes for BCH alone.

The code: RS(255, 255-p) over GF(2^8)

  • Code: RS(255, 255-p), systematic (data first, parity after), first byte = highest-degree coefficient.
  • Field: primitive polynomial 0x11D, primitive element alpha = 2, first consecutive root fcr = 0, so the generator is g(x) = prod (x - alpha^i) over the parity symbols.
  • Block: 255 symbols. A block corrects up to p/2 symbol (byte) errors.
  • Shortening: the last block of an entry holds fewer than 255-p data bytes. It is encoded as if zero-padded, but only the data bytes present plus the p parity bytes are stored. The decoder reconstructs the zero pad — those symbols were never stored, so they cannot be corrupt — and, after correction, checks the pad is still zero. A miscorrection that flips a pad symbol is rejected outright.
  • Decode: syndromes → Berlekamp-Massey → Chien search → Forney magnitudes → re-check the syndromes. Anything that does not come back clean is a loud failure, never a silent wrong answer at this layer.

The -r levels

LevelParity symbols per 255-byte blockCorrects per blockOverhead on incompressible data
-r 00 (no RS)00%
-r 184 byte errors~3.2%
-r 2 (default)168 byte errors~6.7%
-r 32412 byte errors~10.4%
-r 43216 byte errors~14.3%

Default is -r 2 (16 parity symbols, 8 correctable byte errors per block). -r 0 behaves like plain lzply: the stored bytes are byte-identical to what lzply -e 0 writes for the same input — verified by the test suite, only the headers differ.

Benchmarks — measured on hot_newyork, 8 cores

testdata/mixed: 2,387,757 bytes in, 13 entries, default -e 4 -6:

-rArchive bytesCompressExtract
01,065,039772 ms92 ms
11,099,551754 ms143 ms
21,136,367811 ms191 ms
31,175,727819 ms252 ms
41,217,903872 ms307 ms

random.bin: 1,048,576 bytes of incompressible data:

-rArchive bytesCompressTest
01,048,752515 ms49 ms
11,082,720555 ms106 ms
21,118,960512 ms158 ms
31,157,712565 ms218 ms
41,199,248545 ms271 ms

RS overhead on incompressible data is +33,968 / +36,240 / +38,752 / +41,536 bytes per level step — about 3.2–3.9% of the data per step. Compress time is flat across levels (lzip + BCH dominate); decode time grows about 50 ms per level per MiB (Berlekamp-Massey + Chien search per block). Small compressible files cost almost nothing: text.txt (218,800 bytes) archives to 644 bytes at -r 0 and 740 at -r 4 (+24 bytes per level).

What the tests prove

rs_test (unit): 6,243 checks, 0 failures. It covers the level-to-parity mapping, encoded lengths, clean round-trips at every parity for 15 buffer lengths (including shortened last blocks and empty input), determinism, an independent re-implementation cross-check of the encoder (own GF tables, own generator build, own polynomial division — 200 random blocks per parity), a syndrome-zero check on every encoded block, and corruption recovery: random symbol errors and burst errors up to t per block must decode back to the exact original, while t+1 errors in one block must never decode back to the original. Parity-byte-only errors and shortened-last-block errors are covered explicitly.

tests/run_rs_tests.sh (shell, wired into make test): 61 checks, 0 failures. Round-trips at every -r level, -e 0 / -e 7 / -0 combinations, determinism, -r2 and --rs-level forms, -r 5 rejection, -l / -tr / -i, selectors, empty and one-byte files, append + duplicate replacement, byte-identical stored data for -r 0 vs lzply -e 0, and the corruption matrix on a 1 MiB random file at every level: t scattered byte errors recovered, t errors in one block recovered, burst of t recovered, burst of 2t over a block boundary recovered, t+1 in one block fails with exit 3, burst of t+1 fails with exit 3, 8 scattered bit errors recovered (BCH layer under RS), -i still works on corrupted data. Also -e 0 -r 2 (RS alone) recovers 8 and fails 9, both formats reject each other, and a truncated archive fails.

CRC16, and deep repair on top

The LZRS v1 header always had a reserved crc16 field — it was reserved for exactly this. It is now filled: CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF) of the compressed blob, computed by writers and verified by readers after error correction, before decompression. The same migration covers the base lzply format (LZPL v4 → v5, header 78 → 80 bytes). Old archives without a valid CRC16 are rejected cleanly with a message telling you to re-create them — never misdecoded.

The CRC16 is also the fast oracle for lzply_deep_repair, the Chase/list decoder that recovers errors past the BCH limit t: every beyond-t candidate is CRC-checked before the expensive decompress + SHA-256 step. Measured across its corruption matrix: 1,162 candidates CRC-checked, 1,024 rejected by CRC, 0 false accepts. Deep repair works on lz_rs archives too — RS decodes first, so a 512-byte file with 9 errors recovers at L0 when -r 2 (RS fixed them) and at L1 when -r 0. The full story, with the per-level matrix, is in the updated two-pass BCH article.

Try it

lz_rs -c -r 2 photos/ photos.lzrs      # default RS level
lz_rs -c -r 4 -e 5 -9 docs/ backup.lzrs
lz_rs -c -r 0 docs/ no-rs.lzrs        # no RS parity, like plain lzply
lz_rs -d backup.lzrs out/              # extract (RS+BCH repair automatic)
lz_rs -t backup.lzrs                   # test (decode and verify)
lz_rs -i backup.lzrs                   # inspect: shows "BCH 001a r2"

On extract/test, a repaired entry prints what was fixed, e.g. file OK (RS corrected 8 symbols) or file OK (RS corrected 3 symbols, BCH corrected 5 bits). Exit codes are the same as lzply: 0 ok, 1 usage, 2 I/O error, 3 corrupt/unrecoverable.

The code is in lzply on GitHub (github.com/cnmcdee/lzply), commit b27b345. The format is documented in docs/lzrs-format.md. Cross-server verification on hot_chicago is in progress; the full numbers will appear in the hotconfig write-up.

Linux Rocks Every Day