nx_fec_stripe_test.nx source
↩ module page · 95 lines · 4035 B
1// nx_fec_stripe_test.nx -- 1:1 KAT for FEC-complementary striping
2// (nx_fec_stripe.nx). Proves the exceed over plain striping: a source
3// packet lost on its path with NO duplicate is reconstructed by repair
4// parity on the complementary path -- i.e. CORRELATED loss recovered.
5// EXHAUSTIVE: sweeps EVERY single-source-loss position (row FEC must
6// recover any 1-per-row loss). Plus a both-paths-lossy case, the honest
7// over-budget case, and the bandwidth-vs-duplication point.
8//
9// expect_exit: 0
10// license_tier: ORIGINAL
11
12import "nx_fec_stripe.nx"
13
14const SL: i64 = 4 // columns
15const SD: i64 = 3 // rows
16const SS: i64 = 4 // bytes/pkt (K = 12 source)
17
18func main() -> i64 {
19 let K: i64 = SL * SD
20 let golden: *u8 = sys_mmap(K * SS)
21 let src: *u8 = sys_mmap(K * SS)
22 let rows: *u8 = sys_mmap(SD * SS)
23 let cols: *u8 = sys_mmap(SL * SS)
24 let present: *i64 = sys_mmap(K * 8) as *i64
25 let rp: *i64 = sys_mmap(SD * 8) as *i64
26 let cp: *i64 = sys_mmap(SL * 8) as *i64
27
28 var i: i64 = 0
29 while i < K * SS { golden[i] = ((i * 11 + 3) & 0xff) as u8; i = i + 1 }
30 i = 0
31 while i < K * SS { src[i] = golden[i]; i = i + 1 }
32 fec_encode_rows(src, SL, SD, SS, rows)
33 fec_encode_cols(src, SL, SD, SS, cols)
34
35 // ---- T1 EXHAUSTIVE: every single source loss (no duplicate, source on
36 // path0 only) is reconstructed by the repair parity on path1 ----
37 var lost: i64 = 0
38 while lost < K {
39 // reset clean grid + all repairs present (path1 intact)
40 var j: i64 = 0
41 while j < K * SS { src[j] = golden[j]; j = j + 1 }
42 j = 0
43 while j < K { present[j] = 1; j = j + 1 }
44 j = 0
45 while j < SD { rp[j] = 1; j = j + 1 }
46 j = 0
47 while j < SL { cp[j] = 1; j = j + 1 }
48 // simulate: source `lost` dropped on path0 (zeroed, no copy anywhere)
49 fec_zero(fec_pkt(src, lost, SS), SS)
50 present[lost] = 0
51 // verify path-plan: source on path0, repairs on path1
52 if fec_stripe_path_of(lost, K) != 0 { return 1 }
53 if fec_stripe_path_of(K + 1, K) != 1 { return 2 }
54 // recover from the complementary repair
55 if fec_stripe_recover(src, SL, SD, SS, present, rows, rp, cols, cp) != 0 { return 3 }
56 // bytes exact?
57 j = 0
58 while j < K * SS { if (src[j] & 0xff) != (golden[j] & 0xff) { return 4 } j = j + 1 }
59 lost = lost + 1
60 }
61
62 // ---- T2: BOTH paths lossy within budget -- source loss + a lost col
63 // repair, recovered via the surviving row repair (correlated loss) ----
64 i = 0
65 while i < K * SS { src[i] = golden[i]; i = i + 1 }
66 i = 0
67 while i < K { present[i] = 1; i = i + 1 }
68 i = 0
69 while i < SD { rp[i] = 1; i = i + 1 }
70 i = 0
71 while i < SL { cp[i] = 1; i = i + 1 }
72 fec_zero(fec_pkt(src, 6, SS), SS); present[6] = 0 // source lost on path0 (row1)
73 cp[2] = 0 // a col repair lost on path1
74 if fec_stripe_recover(src, SL, SD, SS, present, rows, rp, cols, cp) != 0 { return 5 }
75 i = 0
76 while i < K * SS { if (src[i] & 0xff) != (golden[i] & 0xff) { return 6 } i = i + 1 }
77
78 // ---- T3: over-budget (2x2 block, both row+col parity insufficient) ->
79 // honestly unrecoverable, no corruption ----
80 i = 0
81 while i < K { present[i] = 1; i = i + 1 }
82 i = 0
83 while i < SD { rp[i] = 1; i = i + 1 }
84 i = 0
85 while i < SL { cp[i] = 1; i = i + 1 }
86 present[5] = 0; present[6] = 0; present[9] = 0; present[10] = 0
87 if fec_stripe_recover(src, SL, SD, SS, present, rows, rp, cols, cp) != 4 { return 7 }
88
89 // ---- T4: bandwidth -- FEC stripe overhead < 100% (1+1 duplication) ----
90 if fec_stripe_overhead_pct(FEC_ROW, SL, SD) != 25 { return 8 } // 3/12 = 25% vs 100% dup
91 if fec_stripe_overhead_pct(FEC_2D, SL, SD) != 58 { return 9 } // 7/12 = 58% vs 100% dup
92
93 sys_write(1, "FEC-STRIPE KAT PASS (every single source loss recovered by complementary parity; correlated-loss recovery at <100% overhead)\n", 124)
94 return 0
95}