Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constructor with externally built matrix #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,14 @@ impl<F: Field> ReedSolomon<F> {
vandermonde.multiply(&top.invert().unwrap())
}

/// Creates a new instance of Reed-Solomon erasure code encoder/decoder.
/// Cheks the validity of data and parity shard counts.
///
/// Returns `Error::TooFewDataShards` if `data_shards == 0`.
///
/// Returns `Error::TooFewParityShards` if `parity_shards == 0`.
///
/// Returns `Error::TooManyShards` if `data_shards + parity_shards > F::ORDER`.
pub fn new(data_shards: usize, parity_shards: usize) -> Result<ReedSolomon<F>, Error> {
fn check_shard_count(data_shards: usize, parity_shards: usize) -> Result<(), Error> {
if data_shards == 0 {
return Err(Error::TooFewDataShards);
}
Expand All @@ -452,6 +452,18 @@ impl<F: Field> ReedSolomon<F> {
if data_shards + parity_shards > F::ORDER {
return Err(Error::TooManyShards);
}
Ok(())
}

/// Creates a new instance of Reed-Solomon erasure code encoder/decoder.
///
/// Returns `Error::TooFewDataShards` if `data_shards == 0`.
///
/// Returns `Error::TooFewParityShards` if `parity_shards == 0`.
///
/// Returns `Error::TooManyShards` if `data_shards + parity_shards > F::ORDER`.
pub fn new(data_shards: usize, parity_shards: usize) -> Result<ReedSolomon<F>, Error> {
Self::check_shard_count(data_shards, parity_shards)?;

let total_shards = data_shards + parity_shards;

Expand All @@ -466,6 +478,42 @@ impl<F: Field> ReedSolomon<F> {
})
}

/// Creates a new instance of Reed-Solomon erasure code encoder/decoder with an externally built matrix.
///
/// Returns `Error::TooFewDataShards` if `data_shards == 0`.
///
/// Returns `Error::TooFewParityShards` if `parity_shards == 0`.
///
/// Returns `Error::TooManyShards` if `data_shards + parity_shards > F::ORDER`.
///
/// Returns `Error::IncorrectShardSize` if `data_shards != matrix.col_count()`.
///
/// Returns `Error::IncorrectShardSize` if `data_shards + parity_shards != matrix.row_count()`.
pub fn new_with_matrix(
data_shards: usize,
parity_shards: usize,
matrix: Matrix<F>,
) -> Result<ReedSolomon<F>, Error> {
Self::check_shard_count(data_shards, parity_shards)?;

let total_shards = data_shards + parity_shards;

if data_shards != matrix.col_count() {
return Err(Error::IncorrectShardSize);
}
if total_shards != matrix.row_count() {
return Err(Error::IncorrectShardSize);
}

Ok(ReedSolomon {
data_shard_count: data_shards,
parity_shard_count: parity_shards,
total_shard_count: total_shards,
matrix,
data_decode_matrix_cache: Mutex::new(LruCache::new(DATA_DECODE_MATRIX_CACHE_CAPACITY)),
})
}

pub fn data_shard_count(&self) -> usize {
self.data_shard_count
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ mod macros;

mod core;
mod errors;
mod matrix;

#[cfg(test)]
mod tests;

pub mod galois_16;
pub mod galois_8;
pub mod matrix;

pub use crate::errors::Error;
pub use crate::errors::SBSError;
Expand Down
47 changes: 46 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;

use super::{galois_8, Error, SBSError};
use super::{galois_8, matrix, Error, SBSError};
use rand::{self, thread_rng, Rng};

mod galois_16;
Expand Down Expand Up @@ -2617,3 +2617,48 @@ fn test_encode_single_error_handling() {
);
}
}

#[test]
fn test_new_with_matrix_invalid_data_shards() {
let matrix = matrix::Matrix::new_with_data(vec![
vec![1, 0, 0],
vec![0, 1, 0],
vec![0, 0, 1],
vec![1, 1, 1],
vec![4, 2, 1],
]);

assert_eq!(
Error::IncorrectShardSize,
ReedSolomon::new_with_matrix(10, 2, matrix).unwrap_err()
);
}

#[test]
fn test_new_with_matrix_invalid_parity_shards() {
let matrix = matrix::Matrix::new_with_data(vec![
vec![1, 0, 0],
vec![0, 1, 0],
vec![0, 0, 1],
vec![1, 1, 1],
vec![4, 2, 1],
]);

assert_eq!(
Error::IncorrectShardSize,
ReedSolomon::new_with_matrix(3, 1, matrix).unwrap_err()
);
}

#[test]
fn test_new_with_matrix_ok() {
let matrix = matrix::Matrix::new_with_data(vec![
vec![1, 0, 0],
vec![0, 1, 0],
vec![0, 0, 1],
vec![1, 1, 1],
vec![4, 2, 1],
]);

assert!(ReedSolomon::new_with_matrix(3, 2, matrix).is_ok());
}