pub struct ReedSolomon<H> { /* private fields */ }Expand description
A SIMD-optimized Reed-Solomon coder that emits chunks that can be proven against a bmt.
§Behavior
The encoder takes input data, splits it into k data shards, and generates m recovery
shards using Reed-Solomon encoding.
All n = k + m shards are then used to build a bmt, producing a single root hash. Each shard
is packaged as a chunk containing the shard data, its index, and a Merkle multi-proof against the bmt root.
§Encoding
+--------------------------------------+
| Original Data (Bytes) |
+--------------------------------------+
|
v
+--------------------------------------+
| [Length Prefix | Original Data...] |
+--------------------------------------+
|
v
+----------+ +----------+ +-----------+
| Shard 0 | | Shard 1 | .. | Shard k-1 | (Data Shards)
+----------+ +----------+ +-----------+
| | |
| | |
+------------+-------------+
|
v
+------------------+
| Reed-Solomon |
| Encoder (k, m) |
+------------------+
|
v
+----------+ +----------+ +-----------+
| Shard k | | Shard k+1| .. | Shard n-1 | (Recovery Shards)
+----------+ +----------+ +-----------+§Merkle Tree Construction
All n shards (data and recovery) are hashed and used as leaves to build a bmt.
Shards: [Shard 0, Shard 1, ..., Shard n-1]
| | |
v v v
Hashes: [H(S_0), H(S_1), ..., H(S_n-1)]
\ / \ /
\ / \ /
+---+ +---+
| |
\ /
\ /
+-----+
|
v
+----------+
| Root |
+----------+The final output is the bmt root and a set of n chunks.
(Root, [Chunk 0, Chunk 1, ..., Chunk n-1])
Each chunk contains:
shard: The shard data (original or recovery).index: The shard’s original index (0 to n-1).proof: A Merkle multi-proof of the shard’s inclusion in thebmt.
§Decoding and Verification
The decoder requires any k chunks to reconstruct the original data.
- Each chunk’s Merkle multi-proof is verified against the
bmtroot. - Exactly
kshards are fed to the Reed-Solomon decoder (any surplus chunks are redundant), which reconstructs the full codeword: the missing data shards and the missing recovery shards both come out of the same decode. When allkdata shards are already present there is nothing to decode, so the recovery shards are re-encoded instead. - To ensure consistency, a new
bmtroot is generated over the full reconstructed codeword. This new root MUST match the originalbmtroot. This prevents attacks where an adversary provides a valid set of chunks that decode to different data, and binds any surplus chunks (which are rebuilt from the reconstruction, not trusted). - If the roots match, the original data is extracted from the reconstructed data shards.
Decoding is all-or-nothing per commitment. A successful decode proves that the committed
codeword is the canonical encoding of the returned data under this Config, so every set
of at least k shards checked against that commitment decodes to the same data. A
commitment that is not the canonical encoding of any payload fails for every such set.
§Supported Configurations
With k = minimum_shards and m = extra_shards, the total k + m must fit in a u16, and
larger totals return Error::TooManyTotalShards. The coder works over GF(2^16) and also
requires min(k, m).next_power_of_two() + max(k, m) to be at most 65536. Totals that fit but
fail this condition return Error::ReedSolomon carrying
UnsupportedShardCount.
The canonical shard width is the payload length plus its four-byte prefix, divided by k
and rounded up to an even number of bytes. Encoding returns Error::InvalidDataLength when
the payload length or that width exceeds u32::MAX (with k of one, the five largest payload
lengths) or when k + m shards of that width exceed MAX_ALLOCATION_BYTES.
§Resource Usage
Encoding and decoding hold all k + m shards of the codeword in memory, along with the Merkle
tree over those shards and a bounded per-thread coding workspace. At the canonical width, the
codeword is about (k + m) / k times the payload. Encoding also returns a Merkle proof for
every shard.
Decoding reconstructs the m shards beyond the k it decodes from and rebuilds the
commitment over all of them, so even the minimum k shards cost the full codeword. With k
of one and m of 65,534, decoding a single shard reconstructs and hashes 65,534 shards of
its width.
Callers that accept a Config or shards from an untrusted source must bound the shard counts
and the maximum payload length before encoding or decoding. Reading a Scheme::Shard takes
the same maximum and rejects any shard wider than the canonical width of a maximum-length
payload.
Trait Implementations§
Source§impl<H> Clone for ReedSolomon<H>
impl<H> Clone for ReedSolomon<H>
impl<H> Copy for ReedSolomon<H>
Source§impl<H> Debug for ReedSolomon<H>
impl<H> Debug for ReedSolomon<H>
Source§impl<H: Hasher> Scheme for ReedSolomon<H>
impl<H: Hasher> Scheme for ReedSolomon<H>
Source§fn max_shard_size(config: &Config, data: usize) -> usize
fn max_shard_size(config: &Config, data: usize) -> usize
Returns an upper bound on the encoded size of any chunk Scheme::encode produces under
config from at most data bytes.
§Panics
Panics if config cannot encode data bytes: its total exceeds u16::MAX shards, the
backend does not support its shard counts, data or the shard width exceeds u32::MAX,
or the codeword exceeds MAX_ALLOCATION_BYTES.
Source§type Commitment = <H as Hasher>::Digest
type Commitment = <H as Hasher>::Digest
Source§type Shard = Chunk<<H as Hasher>::Digest>
type Shard = Chunk<<H as Hasher>::Digest>
Source§type CheckedShard = CheckedChunk<H>
type CheckedShard = CheckedChunk<H>
Source§fn encode(
config: &Config,
data: impl Buf,
strategy: &impl Strategy,
) -> Result<(Self::Commitment, Vec<Self::Shard>), Self::Error>
fn encode( config: &Config, data: impl Buf, strategy: &impl Strategy, ) -> Result<(Self::Commitment, Vec<Self::Shard>), Self::Error>
Source§fn check(
config: &Config,
commitment: &Self::Commitment,
index: u16,
shard: &Self::Shard,
strategy: &impl Strategy,
) -> Result<Self::CheckedShard, Self::Error>
fn check( config: &Config, commitment: &Self::Commitment, index: u16, shard: &Self::Shard, strategy: &impl Strategy, ) -> Result<Self::CheckedShard, Self::Error>
Source§fn check_many(
config: &Config,
commitment: &Self::Commitment,
shards: &[(u16, &Self::Shard)],
strategy: &impl Strategy,
) -> Vec<Result<Self::CheckedShard, Self::Error>>
fn check_many( config: &Config, commitment: &Self::Commitment, shards: &[(u16, &Self::Shard)], strategy: &impl Strategy, ) -> Vec<Result<Self::CheckedShard, Self::Error>>
Auto Trait Implementations§
impl<H> Freeze for ReedSolomon<H>where
PhantomData<H>: Freeze,
impl<H> RefUnwindSafe for ReedSolomon<H>where
PhantomData<H>: RefUnwindSafe,
impl<H> Send for ReedSolomon<H>where
PhantomData<H>: Send,
impl<H> Sync for ReedSolomon<H>where
PhantomData<H>: Sync,
impl<H> Unpin for ReedSolomon<H>where
PhantomData<H>: Unpin,
impl<H> UnsafeUnpin for ReedSolomon<H>where
PhantomData<H>: UnsafeUnpin,
impl<H> UnwindSafe for ReedSolomon<H>where
PhantomData<H>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more