Crates.io | reed_solomon_rs |
lib.rs | reed_solomon_rs |
version | |
source | src |
created_at | 2024-11-01 20:14:55.650225 |
updated_at | 2024-11-20 15:18:10.776219 |
description | A Reed-Solomon Error Correction Code Library that uses the Berlekamp Welch Algorithm |
homepage | |
repository | https://github.com/SohamJog/reed_solomon_rs |
max_upload_size | |
id | 1432279 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This library provides a Rust implementation of Reed-Solomon Error Correction Codes (RS-ECC), allowing encoding and decoding of data for error correction and recovery. It uses the Berlekamp Welch Algorithm for error correction and decoding.
Reed-Solomon Error Correction Codes (RS-ECC) encode data into multiple shares, allowing recovery even if some shares are lost or corrupted. This library uses polynomial encoding and the Berlekamp-Welch algorithm to reconstruct and correct data efficiently. RS-ECC is widely applied in technologies such as CDs, DVDs, QR codes, satellite communications, DSL, WiMAX, and RAID 6 storage systems, where data integrity is essential.
Reed-Solomon encoding divides data into k original shares, generating n total shares by adding n - k redundant shares. This enables data recovery even if some shares are lost or corrupted. Each share represents a point on a polynomial of degree k - 1 over a finite field (GF(256) for this library ).
The encoding process works as follows:
Divide Data into Shares: The input data is split into k shares and used as coefficients of a polynomial $$P(x)$$ of degree k - 1.
Create Polynomial: This polynomial, $$P(x)$$, is evaluated at n distinct points, yielding n shares. Each point corresponds to a unique share, making it possible to reconstruct the original polynomial from any subset of k shares.
Redundant Data for Error Correction: Using Lagrange interpolation, any k shares can recover the polynomial $$P(x)$$, which encodes the original data. The algorithm’s error correction capability, t, is determined by the number of redundant shares, allowing correction of up to t errors, where $$t = \frac{n - k}{2}$$.
This approach ensures that the encoded data can withstand loss or corruption of up to n - k shares, making Reed-Solomon encoding ideal for scenarios requiring robust error correction.
The Berlekamp-Welch algorithm decodes Reed-Solomon codes by identifying both the original message polynomial and an error-locator polynomial, which helps determine the positions of corrupted symbols. Here’s a step-by-step summary of the algorithm:
Input Parameters:
Define Polynomials:
Construct Key Equations:
Solve the System:
Recover the Message Polynomial:
Error Correction:
This algorithm reconstructs the original data while correcting errors up to t corruptions in the shares.
This library operates over GF(256), supporting finite field operations for efficient error correction. The data structure for each share is defined as follows:
pub struct Share {
/// Number is essentially the X-coordinate on the encoding polynomial
pub number: usize,
/// Encoded data
pub data: Vec<u8>,
}
To set up a forward error correcting (FEC) object, you can initialize it by specifying the required number of shares and the total number of shares to be generated:
let required = 4;
let total = 8;
let f = FEC::new(required, total)?;
To encode data into shares, ensure that your data vector is divisible by the required number of shares. If not, it will be padded with underscores during encoding. Here’s how to encode:
let data: Vec<u8> = b"hello, world! __1234567".to_vec(); // Example data
let output = |s: Share| {
shares[s.number] = s.clone(); // Deep copy
};
f.encode(&data, output)?;
Decoding is straightforward. Assuming you have a vector of shares, you can decode them like this:
let data = f.decode([].to_vec(), shares)?;
The limits for encoding and decoding are governed by the parameters:
To enable error correction, it is necessary that $n > k$. The maximum number of correctable errors $t$ can be calculated as:
$$ t = \frac{n - k}{2} $$
To use this library in your project, add the following to your Cargo.toml
:
[dependencies]
reed_solomon = { git = "https://github.com/SohamJog/reed_solomon_rs" }
use reed_solomon_rs::fec::fec::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let required = 4; // Number of pieces required for reconstruction
let total = 8; // Total number of pieces to generate
// Initialize the FEC (Forward Error Correction) instance
let f = FEC::new(required, total)?;
// Create a vector to hold the shares
let mut shares: Vec<Share> = vec![Share { number: 0, data: vec![] }; total];
// Data to encode
let data = b"hello, world! __".to_vec();
// Define the output closure to store generated shares
let output = |s: Share| {
shares[s.number] = s.clone(); // Deep copy of the share
};
// Encode the data into shares
f.encode(&data, output)?;
// Maybe corrupt a share
shares[1].data[1] = b'?';
// Decode the shares
let result_data = f.decode([].to_vec(), shares)?;
// Verify the result
match String::from_utf8(result_data) {
Ok(decoded_string) => {
println!("Decoded data: {:?}", decoded_string);
}
Err(e) => {
println!("Invalid UTF-8 sequence: {}", e);
}
}
Ok(())
}
To run the tests, run
cargo test
To run the benchmarks, run
cargo bench
This project is licensed under the MIT license. See the LICENSE
file for more details
This library is based on the Go library Infectious.