RsSimd

High-performance Reed-Solomon erasure coding in Elixir using SIMD-accelerated native code via reed-solomon-simd Rust crate.

This library provides a simple Elixir interface for encoding and recovering data across multiple shards, useful for distributed storage, forward error correction, or lossy channel protection.

Features

Installation

Add to mix.exs:

def deps do
  [
    {:reed_solomon_simd, "~> 0.1"}
  ]
end

API

encode(original_shards, recovery_shards, data)

Encodes data shards into parity shards.

Parameters:

Returns: {:ok, [recovery_shard1, ...]} or {:error, reason}

decode(original_shards, recovery_shards, provided_original, provided_recovery)

Recovers missing original shards from recovery shards.

Note: This library does NOT check for corruption in the provided original/recovery shards, so if there is a possibility of corruption in these, then there is a requirement to implement a checksum system to exclude corrupted shards

Parameters:

Returns

, ..} or {:error, reason}

correct(original_shards, recovery_shards, provided_original, provided_recovery)

Similar to decode/4, but return value is the entire original data, not just the recovered shards

See the note for decode/4 that this library does not check for corrupted shards.

Parameters:

Returns

or {:error, reason}

Example

# Encode
iex> original_data = [
    <<1,2,3,4>>,
    <<5,6,7,8>>,
    <<9,10,11,12>>,
    <<13,14,15,16>>
  ]

iex> {:ok, ecc} = RsSimd.encode(4, 2, original_data)
{:ok, [<<148, 141, 5, 70>>, <<148, 141, 5, 86>>]}

# Erase some shards
iex> erased = original_data
            |> Enum.with_index(fn e, idx -> {idx, e} end)
            |> Enum.drop(-1)
[
    {0, <<1, 2, 3, 4>>},
    {1, <<5, 6, 7, 8>>},
    {2, "\t\n\v\f"}
]

# Decode
iex> ecc_indexed = ecc |> Enum.with_index(fn e, idx -> {idx, e} end)

iex> {:ok, decoded} = RsSimd.decode(4, 2, erased, ecc_indexed)
{:ok, %{3 => <<13, 14, 15, 16>>}}

iex> {:ok, decoded} = RsSimd.correct(4, 2, erased, ecc_indexed)
{:ok, [<<1, 2, 3, 4>>, <<5, 6, 7, 8>>, "\t\n\v\f", <<13, 14, 15, 16>>]}