rustfs-erasure-codec 7.0.0

Rust implementation of Reed-Solomon erasure coding
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
use super::ops::{
    add_mod16, fft_dit2_16, gf16_mul, ifft_dit2_16, mul_log16, mulgf16, slice_xor_u16, sub_mod16,
};
use super::tables::build_tables16;
use super::*;
use alloc::vec;

#[test]
fn test_leopard_gf16_tables_shapes() {
    let tables = build_tables16();
    assert_eq!(tables.log_lut.len(), ORDER16);
    assert_eq!(tables.exp_lut.len(), ORDER16 * 2);
    assert_eq!(tables.fft_skew.len(), MODULUS16);
    assert_eq!(tables.log_walsh.len(), ORDER16);
}

#[test]
fn test_leopard_gf16_log_exp_roundtrip() {
    let tables = build_tables16();
    for x in 1..ORDER16 {
        let log_x = tables.log_lut[x];
        let recovered = tables.exp_lut[log_x as usize];
        assert_eq!(recovered, x as u16, "x={x}");
    }
}

#[test]
fn test_leopard_gf16_check_luts() {
    let _tables = build_tables16();
    // Check the LFSR cycle directly: x = 2*x mod polynomial
    let mut x: u32 = 1;
    let mut count = 0usize;
    loop {
        count += 1;
        x <<= 1;
        if x >= ORDER16 as u32 {
            x ^= super::POLYNOMIAL16;
        }
        if x == 1 {
            break;
        }
        if count > MODULUS16 {
            break;
        }
    }
    assert_eq!(
        count, MODULUS16,
        "LFSR should cycle through all 65535 nonzero elements"
    );
}

#[test]
fn test_leopard_gf16_mul_identity() {
    let tables = build_tables16();
    for x in 0..ORDER16 {
        let result = gf16_mul(x as u16, 1, &tables.log_lut, &tables.exp_lut);
        assert_eq!(result, x as u16, "x={x}");
    }
}

#[test]
fn test_leopard_gf16_mul_zero() {
    let tables = build_tables16();
    for x in 0..ORDER16 {
        assert_eq!(gf16_mul(0, x as u16, &tables.log_lut, &tables.exp_lut), 0);
        assert_eq!(gf16_mul(x as u16, 0, &tables.log_lut, &tables.exp_lut), 0);
    }
}

#[test]
fn test_leopard_gf16_fft_ifft_roundtrip() {
    let tables = build_tables16();
    let n = 16;
    let original: Vec<u16> = (0..n).map(|i| (i as u16 * 137 + 42) % 65535).collect();
    let mut data = original.clone();

    // Apply FFT butterflies using split_at_mut.
    for i in 0..n / 2 {
        let (left, right) = data.split_at_mut(n / 2);
        fft_dit2_16(
            &mut left[i..i + 1],
            &mut right[i..i + 1],
            (i + 1) as u16,
            &tables,
        );
    }

    // Apply inverse FFT butterflies (reverse order).
    for i in (0..n / 2).rev() {
        let (left, right) = data.split_at_mut(n / 2);
        ifft_dit2_16(
            &mut left[i..i + 1],
            &mut right[i..i + 1],
            (i + 1) as u16,
            &tables,
        );
    }

    assert_eq!(data, original, "FFT -> IFFT should recover original data");
}

#[test]
fn test_leopard_gf16_mulgf16_basic() {
    let tables = build_tables16();
    let input = vec![1u16, 2, 3, 4, 5];
    let mut output = vec![0u16; 5];

    // Multiply by g^0 = 1 (identity).
    mulgf16(&mut output, &input, 0, &tables);
    assert_eq!(output, input, "mul by g^0 should be identity");

    // Multiply by g^MODULUS16 = g^65535 = 1 (wraparound).
    output.fill(0);
    mulgf16(&mut output, &input, super::MODULUS16 as u16, &tables);
    assert_eq!(output, input, "mul by g^65535 should be identity");
}

#[test]
fn test_leopard_gf16_slice_xor_u16() {
    let a = vec![0x1234u16, 0x5678, 0x9ABC, 0xDEF0];
    let mut b = vec![0xFFFFu16, 0x0000, 0x1234, 0x5678];
    slice_xor_u16(&mut b, &a);
    assert_eq!(
        b,
        vec![0x1234 ^ 0xFFFF, 0x5678, 0x9ABC ^ 0x1234, 0xDEF0 ^ 0x5678]
    );
}

#[test]
fn test_leopard_gf16_encode_basic() {
    let data_shards = 4;
    let parity_shards = 2;
    let shard_size = 128;

    let data: Vec<Vec<u8>> = (0..data_shards)
        .map(|i| {
            (0..shard_size)
                .map(|j| ((i * 137 + j * 31) & 0xFF) as u8)
                .collect()
        })
        .collect();
    let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

    let driver =
        encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

    assert_eq!(driver.shard_size, shard_size);
    assert_eq!(driver.m, 2);

    let all_zeros = parity.iter().all(|p| p.iter().all(|&b| b == 0));
    assert!(!all_zeros, "parity should be non-zero after encoding");
}

#[test]
fn test_leopard_gf16_encode_verify_roundtrip() {
    let data_shards = 3;
    let parity_shards = 2;
    let shard_size = 64;

    let data: Vec<Vec<u8>> = (0..data_shards)
        .map(|i| vec![i as u8 + 1; shard_size])
        .collect();
    let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

    encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

    let mut parity2: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];
    encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity2).unwrap();

    assert_eq!(parity, parity2, "re-encoding should produce same parity");
}

/// Test FWHT self-inverse property: fwht(fwht(x)) = x.
#[test]
fn test_leopard_gf16_fwht_self_inverse() {
    // Build a small test vector.
    let mut data = [0u16; super::ORDER16];
    data[0] = 42;
    data[1] = 1000;
    data[2] = 65534;
    data[3] = 12345;
    data[4] = 7;

    let original = data;

    // Apply FWHT twice — should recover original.
    super::ops::fwht16_mtrunc(&mut data, super::ORDER16, super::ORDER16);
    super::ops::fwht16_variable(&mut data[..super::ORDER16]);

    for i in 0..16 {
        assert_eq!(
            data[i], original[i],
            "position {i}: got {:#06x}, expected {:#06x}",
            data[i], original[i]
        );
    }
}

/// Test encode-decode with non-uniform per-position data.
#[test]
fn test_leopard_gf16_reconstruct_nonuniform() {
    let data_shards = 3;
    let parity_shards = 2;
    let total_shards = data_shards + parity_shards;
    let shard_size = 64;

    // Non-uniform data: each byte position has different values across shards.
    let data: Vec<Vec<u8>> = (0..data_shards)
        .map(|i| {
            (0..shard_size)
                .map(|j| ((i * shard_size + j) & 0xFF) as u8)
                .collect()
        })
        .collect();
    let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

    encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

    let mut shards: Vec<Vec<u8>> = Vec::new();
    for d in &data {
        shards.push(d.clone());
    }
    for p in &parity {
        shards.push(p.clone());
    }

    let original_0 = shards[0].clone();
    shards[0] = vec![0u8; shard_size];

    let present: Vec<bool> = (0..total_shards).map(|i| i != 0).collect();
    let input_snapshots: Vec<Option<Vec<u8>>> = shards
        .iter()
        .enumerate()
        .map(|(i, s)| if i != 0 { Some(s.clone()) } else { None })
        .collect();
    let input_data: Vec<Option<&[u8]>> = input_snapshots.iter().map(|o| o.as_deref()).collect();
    let mut outputs: Vec<&mut [u8]> = shards.iter_mut().map(|s| s.as_mut_slice()).collect();

    decode::reconstruct_with_tables16(
        &present,
        &mut outputs,
        &input_data,
        data_shards,
        parity_shards,
        init_leopard_gf16_tables(),
    )
    .unwrap();

    assert_eq!(
        outputs[0],
        original_0.as_slice(),
        "recovered shard 0 should match original"
    );
}

/// Verify basic GF16 arithmetic properties.
#[test]
fn test_leopard_gf16_arithmetic() {
    let tables = build_tables16();

    // Verify log/exp consistency: exp_lut[log_lut[x]] = x for all nonzero x.
    for x in 1u16..65535 {
        let log_x = tables.log_lut[x as usize];
        let recovered = tables.exp_lut[log_x as usize];
        assert_eq!(recovered, x, "exp(log({x})) != {x}, got {recovered}");
    }

    // Verify exp_lut[0] = 1 (multiplicative identity).
    assert_eq!(tables.exp_lut[0], 1);

    // Verify log_lut[1] = 0 (log of identity).
    assert_eq!(tables.log_lut[1], 0);

    // Verify gf16_mul(a, 1) = a for all a.
    for a in 0u16..65535 {
        let result = gf16_mul(a, 1, &tables.log_lut, &tables.exp_lut);
        assert_eq!(result, a, "gf16_mul({a}, 1) = {result}");
    }

    // Verify gf16_mul(0, x) = 0 for all x.
    for x in 0u16..65535 {
        assert_eq!(gf16_mul(0, x, &tables.log_lut, &tables.exp_lut), 0);
        assert_eq!(gf16_mul(x, 0, &tables.log_lut, &tables.exp_lut), 0);
    }

    // Verify mul_log16(a, log_lut[b]) = gf16_mul(a, b).
    let test_vals = [1u16, 2, 3, 100, 1000, 32768, 65534];
    for &a in &test_vals {
        for &b in &test_vals {
            let r1 = gf16_mul(a, b, &tables.log_lut, &tables.exp_lut);
            let r2 = mul_log16(
                a,
                tables.log_lut[b as usize],
                &tables.log_lut,
                &tables.exp_lut,
            );
            assert_eq!(
                r1, r2,
                "gf16_mul({a},{b})={r1} != mul_log16({a},log({b}))={r2}"
            );
        }
    }

    // Verify add_mod16/sub_mod16 roundtrip.
    for a in 0u16..100 {
        for b in 0u16..100 {
            let sum = add_mod16(a, b);
            let diff = sub_mod16(sum, b);
            assert_eq!(
                diff, a,
                "add_mod16/sub_mod16 roundtrip failed for ({a}, {b})"
            );
        }
    }
}

/// Verify fft_skew values match Go's klauspost/reedsolomon library.
#[test]
fn test_leopard_gf16_fft_skew_values() {
    let tables = init_leopard_gf16_tables();

    // Expected values from Go (verified via dump test).
    let expected_skew: [u16; 8] = [
        0xFFFF, 0xFFFF, 0x5555, 0xFFFF, 0x4444, 0x5555, 0x8888, 0xFFFF,
    ];
    for (i, &exp) in expected_skew.iter().enumerate() {
        assert_eq!(
            tables.fft_skew[i], exp,
            "fft_skew[{i}] = {:#06x}, expected {:#06x}",
            tables.fft_skew[i], exp
        );
    }

    // Verify plans build without panic for 3+2 config.
    let data_shards = 3usize;
    let parity_shards = 2usize;
    let m = parity_shards.max(1).next_power_of_two();
    let work_size = m + data_shards;
    let n = work_size.next_power_of_two();
    let input_count = 5usize;

    let _ifft_plan = super::build_ifft_decode_dit16_plan(input_count, n, &tables.fft_skew);
    let _fft_plan = super::build_fft_dit16_plan(work_size, n, &tables.fft_skew);
}

/// Test encode+decode roundtrip directly using encode_with_tables16 and reconstruct_with_tables16.
#[test]
#[allow(clippy::needless_range_loop)]
fn test_leopard_gf16_encode_decode_roundtrip() {
    let data_shards = 3;
    let parity_shards = 2;
    let total_shards = data_shards + parity_shards;
    let shard_size = 64;

    let data: Vec<Vec<u8>> = (0..data_shards)
        .map(|i| {
            (0..shard_size)
                .map(|j| ((i * shard_size + j) & 0xFF) as u8)
                .collect()
        })
        .collect();
    let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

    encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

    // Lose shard 0 (data shard).
    let original_0 = data[0].clone();
    let mut shards: Vec<Vec<u8>> = Vec::new();
    for i in 0..data_shards {
        if i == 0 {
            shards.push(vec![0u8; shard_size]);
        } else {
            shards.push(data[i].clone());
        }
    }
    for p in &parity {
        shards.push(p.clone());
    }

    let present: Vec<bool> = (0..total_shards).map(|i| i != 0).collect();
    let input_snapshots: Vec<Option<Vec<u8>>> = shards
        .iter()
        .enumerate()
        .map(|(i, s)| if i != 0 { Some(s.clone()) } else { None })
        .collect();
    let input_data: Vec<Option<&[u8]>> = input_snapshots.iter().map(|o| o.as_deref()).collect();
    let mut outputs: Vec<&mut [u8]> = shards.iter_mut().map(|s| s.as_mut_slice()).collect();

    decode::reconstruct_with_tables16(
        &present,
        &mut outputs,
        &input_data,
        data_shards,
        parity_shards,
        init_leopard_gf16_tables(),
    )
    .unwrap();

    assert_eq!(
        outputs[0],
        original_0.as_slice(),
        "recovered shard 0 should match original"
    );
}

#[test]
fn test_leopard_gf16_reconstruct_single_missing() {
    let data_shards = 3;
    let parity_shards = 2;
    let total_shards = data_shards + parity_shards;
    let shard_size = 64;

    let mut shards: Vec<Vec<u8>> = (0..total_shards)
        .map(|i| vec![(i + 1) as u8; shard_size])
        .collect();

    // Encode parity.
    {
        let (data_part, parity_part) = shards.split_at_mut(data_shards);
        let data_refs: Vec<&[u8]> = data_part.iter().map(|d| d.as_slice()).collect();
        let mut parity_refs: Vec<&mut [u8]> =
            parity_part.iter_mut().map(|p| p.as_mut_slice()).collect();
        encode::encode_with_tables16(data_shards, parity_shards, &data_refs, &mut parity_refs)
            .unwrap();
    }

    let original_0 = shards[0].clone();

    shards[0] = vec![0u8; shard_size];

    let present: Vec<bool> = (0..total_shards).map(|i| i != 0).collect();
    let input_snapshots: Vec<Option<Vec<u8>>> = shards
        .iter()
        .enumerate()
        .map(|(i, s)| if i != 0 { Some(s.clone()) } else { None })
        .collect();
    let input_data: Vec<Option<&[u8]>> = input_snapshots.iter().map(|o| o.as_deref()).collect();
    let mut outputs: Vec<&mut [u8]> = shards.iter_mut().map(|s| s.as_mut_slice()).collect();

    decode::reconstruct_with_tables16(
        &present,
        &mut outputs,
        &input_data,
        data_shards,
        parity_shards,
        init_leopard_gf16_tables(),
    )
    .unwrap();

    assert_eq!(
        outputs[0],
        original_0.as_slice(),
        "recovered shard 0 should match original"
    );
}

#[test]
fn test_leopard_gf16_reconstruct_multiple_missing() {
    let data_shards = 4;
    let parity_shards = 3;
    let total_shards = data_shards + parity_shards;
    let shard_size = 128;

    let mut shards: Vec<Vec<u8>> = (0..total_shards)
        .map(|i| {
            (0..shard_size)
                .map(|j| ((i * 137 + j * 31) & 0xFF) as u8)
                .collect()
        })
        .collect();

    {
        let (data_part, parity_part) = shards.split_at_mut(data_shards);
        let data_refs: Vec<&[u8]> = data_part.iter().map(|d| d.as_slice()).collect();
        let mut parity_refs: Vec<&mut [u8]> =
            parity_part.iter_mut().map(|p| p.as_mut_slice()).collect();
        encode::encode_with_tables16(data_shards, parity_shards, &data_refs, &mut parity_refs)
            .unwrap();
    }

    let erased = [0, 2, 5];
    let originals: Vec<(usize, Vec<u8>)> = erased.iter().map(|&i| (i, shards[i].clone())).collect();
    for &i in &erased {
        shards[i] = vec![0u8; shard_size];
    }

    let present: Vec<bool> = (0..total_shards).map(|i| !erased.contains(&i)).collect();
    let input_snapshots: Vec<Option<Vec<u8>>> = shards
        .iter()
        .enumerate()
        .map(|(i, s)| {
            if !erased.contains(&i) {
                Some(s.clone())
            } else {
                None
            }
        })
        .collect();
    let input_data: Vec<Option<&[u8]>> = input_snapshots.iter().map(|o| o.as_deref()).collect();
    let mut outputs: Vec<&mut [u8]> = shards.iter_mut().map(|s| s.as_mut_slice()).collect();

    decode::reconstruct_with_tables16(
        &present,
        &mut outputs,
        &input_data,
        data_shards,
        parity_shards,
        init_leopard_gf16_tables(),
    )
    .unwrap();

    for (i, original) in &originals {
        assert_eq!(
            outputs[*i],
            original.as_slice(),
            "recovered shard {i} should match original"
        );
    }
}

/// Verify the full decode pipeline with manual step-by-step computation.
#[test]
#[allow(clippy::needless_range_loop, clippy::vec_init_then_push)]
fn test_leopard_gf16_decode_pipeline_manual() {
    let data_shards = 3;
    let parity_shards = 2;
    let total_shards = data_shards + parity_shards;
    let shard_size = 64;

    // Use uniform data for easier debugging.
    let data: Vec<Vec<u8>> = (0..data_shards)
        .map(|i| vec![(i + 1) as u8; shard_size])
        .collect();
    let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

    encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

    // Verify: encode then decode roundtrip for shard 0.
    let original_0 = data[0].clone();
    let mut shards: Vec<Vec<u8>> = Vec::new();
    for i in 0..data_shards {
        if i == 0 {
            shards.push(vec![0u8; shard_size]);
        } else {
            shards.push(data[i].clone());
        }
    }
    for p in &parity {
        shards.push(p.clone());
    }

    let present: Vec<bool> = (0..total_shards).map(|i| i != 0).collect();
    let input_snapshots: Vec<Option<Vec<u8>>> = shards
        .iter()
        .enumerate()
        .map(|(i, s)| if i != 0 { Some(s.clone()) } else { None })
        .collect();
    let input_data: Vec<Option<&[u8]>> = input_snapshots.iter().map(|o| o.as_deref()).collect();
    let mut outputs: Vec<&mut [u8]> = shards.iter_mut().map(|s| s.as_mut_slice()).collect();

    decode::reconstruct_with_tables16(
        &present,
        &mut outputs,
        &input_data,
        data_shards,
        parity_shards,
        init_leopard_gf16_tables(),
    )
    .unwrap();

    assert_eq!(
        outputs[0],
        original_0.as_slice(),
        "recovered shard 0 should match original"
    );
}

#[test]
fn test_leopard_gf16_reconstruct_simple_1plus1() {
    let data_shards = 1;
    let parity_shards = 1;
    let shard_size = 64;

    let data: Vec<Vec<u8>> = vec![(0..shard_size).map(|j| ((j + 1) & 0xFF) as u8).collect()];
    let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

    encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

    let mut shards: Vec<Vec<u8>> = vec![
        vec![0u8; shard_size], // missing data shard 0
        parity[0].clone(),
    ];

    let original_0 = data[0].clone();

    let present: Vec<bool> = vec![false, true];
    let input_snapshots: Vec<Option<Vec<u8>>> = shards
        .iter()
        .enumerate()
        .map(|(i, s)| if i != 0 { Some(s.clone()) } else { None })
        .collect();
    let input_data: Vec<Option<&[u8]>> = input_snapshots.iter().map(|o| o.as_deref()).collect();
    let mut outputs: Vec<&mut [u8]> = shards.iter_mut().map(|s| s.as_mut_slice()).collect();

    decode::reconstruct_with_tables16(
        &present,
        &mut outputs,
        &input_data,
        data_shards,
        parity_shards,
        init_leopard_gf16_tables(),
    )
    .unwrap();

    assert_eq!(
        outputs[0],
        original_0.as_slice(),
        "recovered shard 0 should match original"
    );
}

#[test]
fn test_leopard_gf16_encode_parity_values() {
    // Compare parity output with Go's klauspost/reedsolomon for 3+2 config.
    let data_shards = 3;
    let parity_shards = 2;
    let shard_size = 64;

    #[cfg(feature = "std")]
    {
        let tables = init_leopard_gf16_tables();

        // Print the fft_skew values used for encoding
        let m = 2usize;
        let skew = &tables.fft_skew[m - 1..]; // fft_skew[1..]
        println!("skew (fft_skew[1..]): {:?}", &skew[..8]);
        println!(
            "skew[1] = 0x{:04x} (used by first IFFT final_stage)",
            skew[1]
        );
        println!(
            "skew[3] = 0x{:04x} (used by second IFFT final_stage)",
            skew[3]
        );
    }

    let mut data: Vec<Vec<u8>> = Vec::new();
    for i in 0..data_shards {
        let shard: Vec<u8> = (0..shard_size)
            .map(|j| ((i * 64 + j) & 0xFF) as u8)
            .collect();
        data.push(shard);
    }
    let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

    encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

    #[cfg(feature = "std")]
    println!("parity[0][:16] = {:?}", &parity[0][..16]);
    #[cfg(feature = "std")]
    println!("parity[1][:16] = {:?}", &parity[1][..16]);

    // Go reference parity values for the same input:
    let go_parity0: [u8; 16] = [
        59, 148, 199, 104, 147, 60, 111, 192, 227, 76, 31, 176, 75, 228, 183, 24,
    ];
    let go_parity1: [u8; 16] = [
        251, 85, 5, 171, 87, 249, 169, 7, 43, 133, 213, 123, 135, 41, 121, 215,
    ];

    assert_eq!(&parity[0][..16], &go_parity0, "parity[0] mismatch with Go");
    assert_eq!(&parity[1][..16], &go_parity1, "parity[1] mismatch with Go");
}

#[test]
fn test_leopard_gf16_encode_coefficients() {
    // Verify encode coefficients match Go's klauspost/reedsolomon.
    // For 3+2, the encoding matrix should be:
    //   parity[0] = data[0]*0x0003 + data[1]*0x0002 + data[2]*0x0005
    //   parity[1] = data[0]*0x0002 + data[1]*0x0003 + data[2]*0x0004
    // where + is XOR and * is GF(2^16) multiplication.
    let data_shards = 3usize;
    let parity_shards = 2usize;
    let shard_size = 64usize;

    // Expected coefficients from Go
    let expected: [[u16; 3]; 2] = [
        [0x0003, 0x0002, 0x0005], // parity[0]
        [0x0002, 0x0003, 0x0004], // parity[1]
    ];

    for data_idx in 0..data_shards {
        let mut data: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; data_shards];
        data[data_idx][0] = 1; // u16 LE = 0x0001
        let mut parity: Vec<Vec<u8>> = vec![vec![0u8; shard_size]; parity_shards];

        encode::encode_with_tables16(data_shards, parity_shards, &data, &mut parity).unwrap();

        for p in 0..parity_shards {
            let got = parity[p][0] as u16 | ((parity[p][1] as u16) << 8);
            let exp = expected[p][data_idx];
            assert_eq!(
                got, exp,
                "coeff[data{data_idx}][parity{p}] = 0x{got:04x}, expected 0x{exp:04x}"
            );
        }
    }
}