Skip to content

Commit

Permalink
add reconstruction from systematic chunks (#36)
Browse files Browse the repository at this point in the history
* add recovery from systematic chunks

* fix clippy

* add EmptyShard error
  • Loading branch information
alindima authored Dec 20, 2023
1 parent 1012b44 commit be37510
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
3 changes: 3 additions & 0 deletions reed-solomon-novelpoly/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum Error {

#[error("Shards do have inconsistent lengths: first = {first}, other = {other})")]
InconsistentShardLengths { first: usize, other: usize },

#[error("Shard is empty")]
EmptyShard,
}

/// Result alias to simplify API.
Expand Down
54 changes: 53 additions & 1 deletion reed-solomon-novelpoly/src/novel_poly_basis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ impl ReedSolomon {
Ok(shards)
}

/// each shard contains one symbol of one run of erasure coding
/// Reconstruct from chunks.
///
/// The result may be padded with zeros. Truncate the output to the expected byte length.
pub fn reconstruct<S: Shard>(&self, received_shards: Vec<Option<S>>) -> Result<Vec<u8>> {
let gap = self.n.saturating_sub(received_shards.len());

Expand Down Expand Up @@ -190,6 +192,10 @@ impl ReedSolomon {
})
.expect("Existential shard count is at least k shards. qed");

if first_shard_len == 0 {
return Err(Error::EmptyShard);
}

// make sure all shards have the same length as the first one
if let Some(other_shard_len) = received_shards[(first_shard_idx + 1)..].iter().find_map(|shard| {
shard.as_ref().and_then(|shard| {
Expand Down Expand Up @@ -231,6 +237,52 @@ impl ReedSolomon {

Ok(acc)
}

/// Reconstruct from the set of systematic chunks.
/// Systematic chunks are the first `k` chunks, which contain the initial data.
///
/// Provide a vector containing chunk data. If too few chunks are provided, recovery is not
/// possible.
/// The result may be padded with zeros. Truncate the output to the expected byte length.
pub fn reconstruct_from_systematic<S: Shard>(&self, chunks: Vec<S>) -> Result<Vec<u8>> {
let Some(first_shard) = chunks.first() else {
return Err(Error::NeedMoreShards { have: 0, min: self.k, all: self.n });
};
if chunks.len() < self.k {
return Err(Error::NeedMoreShards { have: chunks.len(), min: self.k, all: self.n });
}

let shard_len = AsRef::<[[u8; 2]]>::as_ref(first_shard).len();

if shard_len == 0 {
return Err(Error::EmptyShard);
}

if let Some(length) = chunks.iter().find_map(|c| {
let length = AsRef::<[[u8; 2]]>::as_ref(c).len();
if length != shard_len {
Some(length)
} else {
None
}
}) {
return Err(Error::InconsistentShardLengths { first: shard_len, other: length });
}

let mut systematic_bytes = Vec::with_capacity(shard_len * 2 * self.k);

for i in 0..shard_len {
for chunk in chunks.iter().take(self.k) {
// No need to check for index out of bounds because i goes up to shard_len and
// we return an error for non uniform chunks.
let chunk = AsRef::<[[u8; 2]]>::as_ref(chunk)[i];
systematic_bytes.push(chunk[0]);
systematic_bytes.push(chunk[1]);
}
}

Ok(systematic_bytes)
}
}

#[cfg(test)]
Expand Down
17 changes: 17 additions & 0 deletions reed-solomon-novelpoly/src/novel_poly_basis/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,23 @@ impl Arbitrary for ArbitraryData {
}
}

#[test]
fn round_trip_systematic_quickcheck() {
fn property(available_data: ArbitraryData, n_validators: u16) {
let n_validators = n_validators.max(2);
let rs = CodeParams::derive_parameters(n_validators as usize, (n_validators as usize - 1) / 3 + 1)
.unwrap()
.make_encoder();
let kpow2 = rs.k;
let chunks = rs.encode::<WrappedShard>(&available_data.0).unwrap();
let mut res = rs.reconstruct_from_systematic(chunks.into_iter().take(kpow2).collect()).unwrap();
res.truncate(available_data.0.len());
assert_eq!(res, available_data.0);
}

QuickCheck::new().quickcheck(property as fn(ArbitraryData, u16))
}

#[test]
fn round_trip_quickcheck() {
fn property(available_data: ArbitraryData, n_validators: u16) {
Expand Down

0 comments on commit be37510

Please sign in to comment.