Erasure correcting codes (CCSDS 131.5-B)
README.md

erasure #

Erasure correcting codes (CCSDS 131.5-B) in pure OCaml.

Overview #

Links to a spacecraft drop whole packets. Asking for one again costs a round trip, and in deep space that round trip can take minutes. An erasure code sends extra symbols instead, so the receiver can rebuild the data from whatever gets through. This library implements CCSDS 131.5-B. It cuts the data into k source symbols and encodes them as n symbols, and any k of those n give the data back. The code is systematic: the first k symbols it emits are the data itself. When nothing is lost the decoding matrix is the identity and there is nothing to eliminate.

The parity symbols come from a Cauchy matrix over GF(2^8), the field of the 256 byte values, where addition is XOR. The matrix has m = n - k rows and k columns, and its entry at row i, column j is 1 / (x_i + y_j) with x_i = i for i < m and y_j = m + j for j < k. The n values x_i and y_j are all distinct. Any square submatrix of a Cauchy matrix can be inverted. If some source symbols go missing, take the rows of the parity symbols that did arrive and keep only the columns of the missing source symbols: that is such a square submatrix, so any k of the n symbols give a system the decoder can solve. The construction needs n distinct field elements and GF(2^8) only has 256, so Erasure.config refuses an n above 255.

To encode, the library computes n - k parity symbols. Each one mixes the k source symbols over s bytes, for (n - k) k s multiplications in GF(2^8) in all. To decode, it takes the first k symbols it is given and writes one row for each: a unit row for a source symbol, the Cauchy row for a parity symbol. It then runs Gauss-Jordan elimination on that k x k system and carries the s bytes of each symbol along with its row. If e source symbols are missing, e of the rows are dense. Each of the k pivots scales its own row and updates at most those e rows, over at most k + s entries each, so decoding costs at most (e + 1) k (k + s) multiplications. With no loss (e = 0) the decoder only scales the pivot rows, which is k (k + s) multiplications by one. The example below uses k = 128, n = 192 and s = 64: encoding costs 64 x 128 x 64 = 524,288 multiplications, and decoding after the loss of 64 source symbols costs at most 65 x 128 x 192 = 1,597,440.

Installation #

Install with opam:

$ opam install erasure

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install erasure

Usage #

(* Configure: 128 source symbols, 192 total, 64 bytes each. *)
let cfg = Erasure.config ~k:128 ~n:192 ~symbol_size:64

(* Encode: produces 192 symbols (128 data + 64 parity). *)
let data = Bytes.make (128 * 64) '\x42'
let symbols = Erasure.encode cfg data

(* Simulate 33% loss: keep only 128 of 192 symbols. *)
let received =
  Array.to_list symbols
  |> List.mapi (fun i s -> (i, s))
  |> List.filteri (fun i _ -> i < 128)

(* Decode: any 128 symbols recover the original data. *)
let () =
  match Erasure.decode cfg received with
  | Ok recovered -> assert (recovered = data)
  | Error msg -> Fmt.failwith "decode failed: %s" msg

Losing 64 of 192 symbols is losing a third of them, which is the most this code can recover from. Here the 64 symbols dropped are exactly the parity symbols, so the decoder finds every source symbol and has nothing to eliminate. Keep another 128 instead (the last 128, say) to make it run the elimination.

API #

  • config builds a code with k source symbols, n symbols in all and a symbol size in bytes.
  • encode returns the n symbols for some data.
  • decode gets the data back from any k of the n symbols.
  • ccsds is the CCSDS parameter set (k = 128, n = 192, 64-byte symbols).

References #

  • CCSDS 131.5-B-1, Erasure Correcting Codes for Use in Near-Earth and Deep-Space Communications.

Licence #

ISC License. See LICENSE.md for details.