simd-rs63 0.1.1

SIMD-accelerated Reed-Solomon RS(9,6) erasure coding over GF(2^8), with no unsafe code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#![feature(portable_simd)]
#![deny(missing_docs)]

//! Reed-Solomon erasure coding over GF(2⁸).
//!
//! This crate implements a systematic RS(9, 6) code: stripes of [`N`] = 9 equal-sized blocks,
//! of which [`K`] = 6 carry data and [`M`] = 3 are parity. Any combination of up to [`M`]
//! lost blocks can be recovered from the remaining [`K`] blocks.
//!
//! # Quick start
//!
//! ```
//! use reed_solomon::{encode, recover, K, M, BLOCK_ALIGNMENT};
//!
//! let block_size = 4 * BLOCK_ALIGNMENT;
//!
//! // Build K data blocks.
//! let data: Vec<Vec<u8>> = (0..K).map(|i| vec![i as u8; block_size]).collect();
//!
//! // Encode: compute M parity blocks.
//! let mut parity: Vec<Vec<u8>> = vec![vec![0u8; block_size]; M];
//! encode(
//!     std::array::from_fn(|i| data[i].as_slice()),
//!     std::array::from_fn(|i| parity[i].as_mut_slice()),
//! ).unwrap();
//!
//! // Simulate losing data blocks 3, 4 and 5.
//! let mut r3 = vec![0u8; block_size];
//! let mut r4 = vec![0u8; block_size];
//! let mut r5 = vec![0u8; block_size];
//!
//! // Recover them from the surviving blocks.
//! recover(
//!     [(0, data[0].as_slice()), (1, data[1].as_slice()), (2, data[2].as_slice()),
//!      (6, parity[0].as_slice()), (7, parity[1].as_slice()), (8, parity[2].as_slice())],
//!     [(3, &mut r3), (4, &mut r4), (5, &mut r5)],
//! ).unwrap();
//!
//! assert_eq!(r3, data[3]);
//! assert_eq!(r4, data[4]);
//! assert_eq!(r5, data[5]);
//! ```
//!
//! # Block sizes
//!
//! All blocks in a call must be the same size, and that size must be a positive multiple of
//! [`BLOCK_ALIGNMENT`]. On this platform [`BLOCK_ALIGNMENT`] is chosen to match the widest
//! SIMD shuffle instruction available, so it varies by CPU (16 on NEON/SSSE3, 32 on AVX2,
//! 64 on AVX-512 VBMI).

mod gf8;
mod reed_solomon;
mod error;

use reed_solomon::LANES;

/// Total number of blocks per stripe (data + parity).
pub const N: usize = 9;

/// Number of data blocks per stripe.
pub const K: usize = 6;

/// Number of parity blocks per stripe.
///
/// Any combination of up to `M` lost blocks can be recovered from the remaining [`K`].
pub const M: usize = 3;

/// Required alignment for block sizes.
///
/// Every block passed to [`encode`] or [`recover`] must have a length that is a positive
/// multiple of this value. On this platform it equals the width of the widest SIMD shuffle
/// instruction available (16, 32, or 64 bytes).
pub const BLOCK_ALIGNMENT: usize = LANES;

pub use error::Error;

/// Computes the [`M`] parity blocks from [`K`] data blocks.
///
/// Data blocks are assigned indices `0..K` and parity blocks indices `K..N`. All blocks
/// must have the same length, which must be a positive multiple of [`BLOCK_ALIGNMENT`].
///
/// # Errors
///
/// - [`Error::InvalidBlockSize`] — block length is zero or not a multiple of
///   [`BLOCK_ALIGNMENT`].
/// - [`Error::BlockSizeMismatch`] — not all blocks have the same length.
///
/// # Example
///
/// ```
/// use reed_solomon::{encode, BLOCK_ALIGNMENT};
///
/// let block_size = 4 * BLOCK_ALIGNMENT;
///
/// let data = [
///     vec![0; block_size],
///     vec![1; block_size],
///     vec![2; block_size],
///     vec![3; block_size],
///     vec![4; block_size],
///     vec![5; block_size],
/// ];
///
/// let mut parity = [
///     vec![0; block_size],
///     vec![0; block_size],
///     vec![0; block_size],
/// ];
///
/// let data_shards = [
///     data[0].as_slice(),
///     data[1].as_slice(),
///     data[2].as_slice(),
///     data[3].as_slice(),
///     data[4].as_slice(),
///     data[5].as_slice(),
/// ];
///
/// let parity_shards = [
///     parity[0].as_mut_slice(),
///     parity[1].as_mut_slice(),
///     parity[2].as_mut_slice(),
/// ];
///
/// encode(data_shards, parity_shards).unwrap();
/// ```
pub fn encode(data: [&[u8]; K], parity: [&mut [u8]; M]) -> Result<(), Error> {
    let block_size = data[0].len();
    validate_block_size(block_size)?;
    for slice in data.iter().skip(1) {
        if slice.len() != block_size {
            return Err(Error::BlockSizeMismatch { expected: block_size, got: slice.len() });
        }
    }
    for slice in &parity {
        if slice.len() != block_size {
            return Err(Error::BlockSizeMismatch { expected: block_size, got: slice.len() });
        }
    }

    let survivors: [(usize, &[u8]); K] = std::array::from_fn(|i| (i, data[i]));
    let mut j = K;
    let to_fix: [(usize, &mut [u8]); M] = parity.map(|s| {
        let idx = j;
        j += 1;
        (idx, s)
    });

    reed_solomon::fix_errors(survivors, to_fix);
    Ok(())
}

/// Recovers up to [`M`] missing blocks from any [`K`] known blocks.
///
/// `known` contains exactly [`K`] blocks, each paired with its stripe index in `0..N`.
/// `missing` pairs output buffers with the indices of the blocks to recover. All blocks
/// must have the same length, which must be a positive multiple of [`BLOCK_ALIGNMENT`].
/// Indices across `known` and `missing` must be distinct.
///
/// The number of missing blocks `MISSING` must be at most [`M`]; this is enforced at
/// compile time.
///
/// # Errors
///
/// - [`Error::InvalidBlockSize`] — block length is zero or not a multiple of
///   [`BLOCK_ALIGNMENT`].
/// - [`Error::BlockSizeMismatch`] — not all blocks have the same length.
/// - [`Error::IndexOutOfRange`] — an index is ≥ [`N`].
/// - [`Error::DuplicateIndex`] — an index appears more than once.
///
/// # Example
///
/// ```
/// use reed_solomon::{encode, recover, K, M, BLOCK_ALIGNMENT};
///
/// let block_size = 4 * BLOCK_ALIGNMENT;
///
/// let data: Vec<Vec<u8>> = (0..K)
///     .map(|i| vec![i as u8; block_size])
///     .collect();
///
/// let mut parity: Vec<Vec<u8>> = vec![vec![0; block_size]; M];
///
/// let data_refs = [
///     data[0].as_slice(),
///     data[1].as_slice(),
///     data[2].as_slice(),
///     data[3].as_slice(),
///     data[4].as_slice(),
///     data[5].as_slice(),
/// ];
///
/// let parity_refs = [
///     parity[0].as_mut_slice(),
///     parity[1].as_mut_slice(),
///     parity[2].as_mut_slice(),
/// ];
///
/// encode(data_refs, parity_refs).unwrap();
///
/// // Recover data shard 0 using data shards 1..=5 and parity shard 0.
/// // Shard indexes 0..K are data shards, and K..K+M are parity shards.
/// let available = [
///     (1, data[1].as_slice()),
///     (2, data[2].as_slice()),
///     (3, data[3].as_slice()),
///     (4, data[4].as_slice()),
///     (5, data[5].as_slice()),
///     (K, parity[0].as_slice()),
/// ];
///
/// let mut recovered = vec![0; block_size];
/// let missing = [(0, recovered.as_mut_slice())];
///
/// recover(available, missing).unwrap();
///
/// assert_eq!(recovered, data[0]);
/// ```
pub fn recover<const MISSING: usize>(
    known: [(usize, &[u8]); K],
    missing: [(usize, &mut [u8]); MISSING],
) -> Result<(), Error> {
    const { assert!(MISSING <= M, "cannot recover more than M blocks at once") };

    if MISSING == 0 {
        return Ok(());
    }

    let block_size = known[0].1.len();
    validate_block_size(block_size)?;

    let mut seen = 0u16;
    for &(idx, slice) in &known {
        if idx >= N {
            return Err(Error::IndexOutOfRange(idx));
        }
        let bit = 1u16 << idx;
        if seen & bit != 0 {
            return Err(Error::DuplicateIndex(idx));
        }
        seen |= bit;
        if slice.len() != block_size {
            return Err(Error::BlockSizeMismatch { expected: block_size, got: slice.len() });
        }
    }
    for (idx, slice) in &missing {
        if *idx >= N {
            return Err(Error::IndexOutOfRange(*idx));
        }
        let bit = 1u16 << *idx;
        if seen & bit != 0 {
            return Err(Error::DuplicateIndex(*idx));
        }
        seen |= bit;
        if slice.len() != block_size {
            return Err(Error::BlockSizeMismatch { expected: block_size, got: slice.len() });
        }
    }

    reed_solomon::fix_errors(known, missing);
    Ok(())
}

fn validate_block_size(size: usize) -> Result<(), Error> {
    if size == 0 || size % BLOCK_ALIGNMENT != 0 {
        return Err(Error::InvalidBlockSize(size));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    const SIZE: usize = 4 * BLOCK_ALIGNMENT;

    fn make_data() -> [Vec<u8>; K] {
        std::array::from_fn(|i| vec![i as u8; SIZE])
    }

    fn encode_stripe(data: &[Vec<u8>; K]) -> [Vec<u8>; M] {
        let mut parity: [Vec<u8>; M] = std::array::from_fn(|_| vec![0u8; SIZE]);
        let [p0, p1, p2] = &mut parity;
        encode(
            std::array::from_fn(|i| data[i].as_slice()),
            [p0.as_mut_slice(), p1.as_mut_slice(), p2.as_mut_slice()],
        )
        .unwrap();
        parity
    }

    #[test]
    fn test_encode_recover_all_parity() {
        let data = make_data();
        let parity = encode_stripe(&data);

        let mut r0 = vec![0u8; SIZE];
        let mut r1 = vec![0u8; SIZE];
        let mut r2 = vec![0u8; SIZE];
        recover(
            std::array::from_fn(|i| (i, data[i].as_slice())),
            [(K, &mut r0), (K + 1, &mut r1), (K + 2, &mut r2)],
        )
        .unwrap();

        assert_eq!(r0, parity[0]);
        assert_eq!(r1, parity[1]);
        assert_eq!(r2, parity[2]);
    }

    #[test]
    fn test_recover_3_data_blocks() {
        let data = make_data();
        let [p0, p1, p2] = encode_stripe(&data);

        let mut r3 = vec![0u8; SIZE];
        let mut r4 = vec![0u8; SIZE];
        let mut r5 = vec![0u8; SIZE];
        recover(
            [
                (0, data[0].as_slice()),
                (1, data[1].as_slice()),
                (2, data[2].as_slice()),
                (6, p0.as_slice()),
                (7, p1.as_slice()),
                (8, p2.as_slice()),
            ],
            [(3, &mut r3), (4, &mut r4), (5, &mut r5)],
        )
        .unwrap();

        assert_eq!(r3, data[3]);
        assert_eq!(r4, data[4]);
        assert_eq!(r5, data[5]);
    }

    #[test]
    fn test_recover_mixed_data_and_parity() {
        let data = make_data();
        let [p0, p1, p2] = encode_stripe(&data);

        let mut r0 = vec![0u8; SIZE];
        let mut r6 = vec![0u8; SIZE];
        let mut r8 = vec![0u8; SIZE];
        recover(
            [
                (1, data[1].as_slice()),
                (2, data[2].as_slice()),
                (3, data[3].as_slice()),
                (4, data[4].as_slice()),
                (5, data[5].as_slice()),
                (7, p1.as_slice()),
            ],
            [(0, &mut r0), (6, &mut r6), (8, &mut r8)],
        )
        .unwrap();

        assert_eq!(r0, data[0]);
        assert_eq!(r6, p0);
        assert_eq!(r8, p2);
    }

    #[test]
    fn test_error_invalid_block_size_zero() {
        let data: [Vec<u8>; K] = std::array::from_fn(|_| vec![]);
        let mut p0 = vec![];
        let mut p1 = vec![];
        let mut p2 = vec![];
        assert_eq!(
            encode(
                std::array::from_fn(|i| data[i].as_slice()),
                [p0.as_mut_slice(), p1.as_mut_slice(), p2.as_mut_slice()],
            ),
            Err(Error::InvalidBlockSize(0))
        );
    }

    #[test]
    fn test_error_invalid_block_size_unaligned() {
        let sz = BLOCK_ALIGNMENT + 1;
        let data: [Vec<u8>; K] = std::array::from_fn(|_| vec![0u8; sz]);
        let mut p0 = vec![0u8; sz];
        let mut p1 = vec![0u8; sz];
        let mut p2 = vec![0u8; sz];
        assert_eq!(
            encode(
                std::array::from_fn(|i| data[i].as_slice()),
                [p0.as_mut_slice(), p1.as_mut_slice(), p2.as_mut_slice()],
            ),
            Err(Error::InvalidBlockSize(sz))
        );
    }

    #[test]
    fn test_error_block_size_mismatch() {
        let data: [Vec<u8>; K] =
            std::array::from_fn(|i| vec![0u8; if i == 3 { 2 * BLOCK_ALIGNMENT } else { SIZE }]);
        let mut p0 = vec![0u8; SIZE];
        let mut p1 = vec![0u8; SIZE];
        let mut p2 = vec![0u8; SIZE];
        assert_eq!(
            encode(
                std::array::from_fn(|i| data[i].as_slice()),
                [p0.as_mut_slice(), p1.as_mut_slice(), p2.as_mut_slice()],
            ),
            Err(Error::BlockSizeMismatch { expected: SIZE, got: 2 * BLOCK_ALIGNMENT })
        );
    }

    #[test]
    fn test_error_index_out_of_range() {
        let data = make_data();
        let [p0, _, _] = encode_stripe(&data);
        let mut r = vec![0u8; SIZE];
        assert_eq!(
            recover(
                [
                    (0, data[0].as_slice()),
                    (1, data[1].as_slice()),
                    (2, data[2].as_slice()),
                    (3, data[3].as_slice()),
                    (4, data[4].as_slice()),
                    (9, p0.as_slice()),
                ],
                [(5, &mut r)],
            ),
            Err(Error::IndexOutOfRange(9))
        );
    }

    #[test]
    fn test_error_duplicate_index() {
        let data = make_data();
        let [p0, _, _] = encode_stripe(&data);
        let mut r = vec![0u8; SIZE];
        assert_eq!(
            recover(
                [
                    (0, data[0].as_slice()),
                    (1, data[1].as_slice()),
                    (2, data[2].as_slice()),
                    (3, data[3].as_slice()),
                    (4, data[4].as_slice()),
                    (4, p0.as_slice()),
                ],
                [(5, &mut r)],
            ),
            Err(Error::DuplicateIndex(4))
        );
    }
}