Skip to main content

Module fec

Module fec 

Source
Expand description

Reed-Solomon erasure coding over GF(2^8) — the wire codec’s redundant knob.

Split a payload into k data shards plus n − k parity shards; a receiver reconstructs the EXACT payload from ANY k of the n shards. This is the reconstructable axis no general wire format ships: drop, duplicate, or reorder up to n − k shards on a lossy link (UDP / multicast / BLE / LoRa) and the message still arrives, with no retransmit and no coordination.

The code is systematic (the first k shards ARE the data chunks, so lossless delivery costs no decode) and MDS (any k shards suffice). The encoding matrix is a Vandermonde matrix made systematic by multiplying through the inverse of its own top k×k block: every k-row subset stays invertible, which is exactly the any-k-of-n guarantee.

Functions§

decode
Reconstruct the payload from ANY k (index, shard) pairs out of the n. None if fewer than k distinct valid shards are present (then the message is unrecoverable).
encode
Encode data into n shards (k data + n − k parity). Returns the original byte length (for un-padding on decode) and the shards. None for a degenerate (k, n) (k == 0, n < k, or n > 256 — GF(2^8) has only 256 distinct nodes).
frame_redundant
Split a message into n self-describing FEC shards (k data + n − k parity), each independently transmittable. A receiver reconstructs the exact message from ANY k via reconstruct_redundant. msg_id groups a message’s shards on the wire.
reconstruct_redundant
Reconstruct a message from a bag of received FEC shards. Groups by message id and reconstructs the first message that has at least k distinct shards present; returns its (msg_id, payload). Shards from other messages, malformed shards, header- inconsistent shards, and duplicate indices are ignored. None if no message has reached its k-shard threshold.
shard_header
Peek a framed shard’s identity without copying its payload: (msg_id, k, n). None if bytes is not a FEC shard (wrong magic / too short). Lets a receiver group shards by message and know how many it needs (k) before reconstructing.