code wiki / _hdl_build / nx_room_fec_wire.nx
nx_room_fec_wire.nx source
↩ module page · 158 lines · 8887 B
1// nx_room_fec_wire.nx -- R1: the FRAME-LEVEL FEC wire protocol that makes the measured
2// loss-resilience (nx_room_resilience) REAL on the relay. A media frame is sharded into
3// k data + m parity packets (each with a 16-byte wire header); the receiver reconstructs
4// the ENTIRE frame byte-exact from any k surviving shards (<=m losses). This is the
5// shippable bridge: the abstract block accounting in nx_room_resilience is here proven on
6// real frame BYTES through encode -> packetize -> lose -> recover -> byte-exact compare.
7// Reuses the proven RS GF(256) codec from nx_room_fec.nx (no reimpl).
8//
9// WIRE HEADER (16B): [block_id u32][shard_idx u8][is_parity u8][k u8][m u8][frame_len u32][rsv u32]
10// The relay forwards shards opaquely (content-blind); only the endpoints encode/decode.
11// HONEST SCOPE: this proves the protocol + recovery. Wiring it into nx_vroom_daemon's
12// vd_stream + the browser client, and DEPLOYING to the live family link, is the
13// operator-gated follow-on (touches live family video -- coordinate, never hot-patch).
14//
15// main() is the SELF-VALIDATING GATE. Evidence -> knowledge/status/room_fec_wire.log.
16// license_tier: ORIGINAL
17import "nx_syscalls.nx"
18import "nx_room_fec.nx"
19const FW_MAGIC_12345: i64 = 12345
20
21const FW_LOG: *u8 = "knowledge/status/room_fec_wire.log"
22
23func fw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
24func fwn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(fd,bb,k); return 0 }
25
26// ---- wire header pack/unpack (16 bytes) ----
27func fwh_pack(buf: *u8, block_id: i64, idx: i64, is_parity: i64, k: i64, m: i64, fl: i64) -> i64 {
28 var b: i64=0; while b<4 { buf[b]=((block_id>>(b*8))&0xff) as u8; b=b+1 }
29 buf[4]=idx as u8; buf[5]=is_parity as u8; buf[6]=k as u8; buf[7]=m as u8
30 b=0; while b<4 { buf[8+b]=((fl>>(b*8))&0xff) as u8; b=b+1 }
31 buf[12]=0 as u8; buf[13]=0 as u8; buf[14]=0 as u8; buf[15]=0 as u8
32 return 16
33}
34func fwh_idx(buf: *u8) -> i64 { return buf[4] as i64 }
35func fwh_parity(buf: *u8) -> i64 { return buf[5] as i64 }
36func fwh_fl(buf: *u8) -> i64 { var v: i64=0; var b: i64=0; while b<4 { v = v | ((buf[8+b] as i64)<<(b*8)); b=b+1 } return v }
37
38// ---- encode a frame into n=k+m shards (each S symbols) ----
39func fwire_encode(exp: *i64, log: *i64, G: *i64, frame: *u8, fl: i64, k: i64, m: i64, S: i64, data: *i64, shards: *i64) -> i64 {
40 var p: i64=0; var i: i64=0
41 while i < k { var c: i64=0; while c < S { if p < fl { data[i*S+c] = frame[p] as i64; p=p+1 } else { data[i*S+c]=0 } c=c+1 } i=i+1 }
42 fec_encode(exp, log, G, data, shards, k, m, S)
43 return k + m
44}
45// ---- decode: present[] (1=shard arrived) -> recover frame_out (fl bytes). 0 ok / -1 unrecoverable ----
46func fwire_decode(exp: *i64, log: *i64, G: *i64, shards: *i64, present: *i64, k: i64, m: i64, S: i64, fl: i64, frame_out: *u8, erased: *i64, out: *i64) -> i64 {
47 let n: i64 = k + m
48 var z: i64=0; while z < n { if present[z]==1 { erased[z]=0 } else { erased[z]=1 } z=z+1 }
49 let rc: i64 = fec_decode(exp, log, G, shards, erased, out, k, m, S)
50 if rc != 0 { return 0 - 1 }
51 var p: i64=0; var i: i64=0
52 while i < k { var c: i64=0; while c < S { if p < fl { frame_out[p] = out[i*S+c] as u8; p=p+1 } c=c+1 } i=i+1 }
53 return 0
54}
55func frame_eq(a: *u8, b: *u8, fl: i64) -> i64 { var i: i64=0; while i < fl { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
56
57// exhaustive: every 2-shard-loss pattern recovers the frame byte-exact -> count
58func fwire_exhaustive2(exp: *i64, log: *i64, G: *i64, shards: *i64, frame: *u8, k: i64, m: i64, S: i64, fl: i64, present: *i64, erased: *i64, out: *i64, fout: *u8) -> i64 {
59 let n: i64 = k + m
60 var recovered: i64=0
61 var a: i64=0
62 while a < n {
63 var b: i64=a+1
64 while b < n {
65 var z: i64=0; while z < n { present[z]=1; z=z+1 }
66 present[a]=0; present[b]=0
67 let rc: i64 = fwire_decode(exp, log, G, shards, present, k, m, S, fl, fout, erased, out)
68 if rc==0 { if frame_eq(fout, frame, fl)==1 { recovered=recovered+1 } }
69 b=b+1
70 }
71 a=a+1
72 }
73 return recovered
74}
75
76func main() -> i64 {
77 let exp: *i64 = sys_mmap(8*512) as *i64
78 let log: *i64 = sys_mmap(8*256) as *i64
79 gf_init(exp, log)
80
81 let k: i64=8
82 let m: i64=2
83 let n: i64=k+m
84 let fl: i64=1000 // a media frame of 1000 bytes
85 let S: i64=(fl + k - 1) / k // 125 symbols/shard
86 let G: *i64 = sys_mmap(8*n*k) as *i64
87 fec_build_G(exp, log, G, k, m)
88
89 let frame: *u8 = sys_mmap(fl)
90 var i: i64=0; while i < fl { frame[i]=((i*131 + 7) & 255) as u8; i=i+1 }
91
92 let data: *i64 = sys_mmap(8*k*S) as *i64
93 let shards: *i64 = sys_mmap(8*n*S) as *i64
94 let present:*i64 = sys_mmap(8*n) as *i64
95 let erased: *i64 = sys_mmap(8*n) as *i64
96 let out: *i64 = sys_mmap(8*k*S) as *i64
97 let fout: *u8 = sys_mmap(fl)
98
99 fwire_encode(exp, log, G, frame, fl, k, m, S, data, shards)
100
101 var ok: i64=1
102
103 // T1: drop 2 shards (<=m) -> recover the WHOLE frame byte-exact
104 var z: i64=0; while z < n { present[z]=1; z=z+1 }
105 present[2]=0; present[7]=0
106 let rc1: i64 = fwire_decode(exp, log, G, shards, present, k, m, S, fl, fout, erased, out)
107 var t1: i64=0; if rc1==0 { if frame_eq(fout, frame, fl)==1 { t1=1 } }
108 if t1==0 { ok=0 }
109
110 // T2 (honest bound): drop 3 shards (>m) -> unrecoverable (only k-1 survive)
111 z=0; while z < n { present[z]=1; z=z+1 }
112 present[1]=0; present[4]=0; present[9]=0
113 let rc2: i64 = fwire_decode(exp, log, G, shards, present, k, m, S, fl, fout, erased, out)
114 var t2: i64=0; if rc2==(0-1) { t2=1 }
115 if t2==0 { ok=0 }
116
117 // T3: EXHAUSTIVE -- every 2-shard-loss pattern (C(10,2)=45) recovers byte-exact
118 let exh: i64 = fwire_exhaustive2(exp, log, G, shards, frame, k, m, S, fl, present, erased, out, fout)
119 var t3: i64=0; if exh==45 { t3=1 }
120 if t3==0 { ok=0 }
121
122 // T4 load-bearing: zero the parity rows -> a data-shard loss can no longer be recovered
123 let Gt: *i64 = sys_mmap(8*n*k) as *i64
124 var gi: i64=0; while gi < n*k { Gt[gi]=G[gi]; gi=gi+1 }
125 var pr: i64=k; while pr < n { var pc: i64=0; while pc < k { Gt[pr*k+pc]=0; pc=pc+1 } pr=pr+1 }
126 z=0; while z < n { present[z]=1; z=z+1 }
127 present[0]=0 // drop ONE data shard; needs parity to recover
128 let rc4: i64 = fwire_decode(exp, log, Gt, shards, present, k, m, S, fl, fout, erased, out)
129 var t4: i64=0
130 if rc4==(0-1) { t4=1 } else { if frame_eq(fout, frame, fl)==0 { t4=1 } } // singular OR wrong bytes
131 if t4==0 { ok=0 }
132
133 // T5: wire header pack/unpack round-trip byte-exact
134 let hdr: *u8 = sys_mmap(16)
135 fwh_pack(hdr, FW_MAGIC_12345, 9, 1, k, m, fl)
136 var t5: i64=0
137 if fwh_idx(hdr)==9 { if fwh_parity(hdr)==1 { if fwh_fl(hdr)==fl { t5=1 } } }
138 if t5==0 { ok=0 }
139
140 fw(1, "=== nx_room_fec_wire -- frame-level FEC recovery on the relay wire ===\n" as *u8)
141 fw(1, " frame_len=" as *u8); fwn(1, fl); fw(1, "B k=" as *u8); fwn(1, k); fw(1, " m=" as *u8); fwn(1, m); fw(1, " shard_symbols=" as *u8); fwn(1, S); fw(1, " n_shards=" as *u8); fwn(1, n); fw(1, "\n" as *u8)
142 fw(1, " T1 drop 2 (<=m) -> whole frame byte-exact: " as *u8); if t1==1 { fw(1,"PASS" as *u8) } else { fw(1,"FAIL" as *u8) }
143 fw(1, "\n T2 drop 3 (>m) -> unrecoverable (honest bound): " as *u8); if t2==1 { fw(1,"PASS" as *u8) } else { fw(1,"FAIL" as *u8) }
144 fw(1, "\n T3 exhaustive C(10,2)=45 patterns recovered=" as *u8); fwn(1, exh); fw(1, "/45: " as *u8); if t3==1 { fw(1,"PASS" as *u8) } else { fw(1,"FAIL" as *u8) }
145 fw(1, "\n T4 zeroed parity -> data-loss unrecoverable (parity load-bearing): " as *u8); if t4==1 { fw(1,"PASS" as *u8) } else { fw(1,"FAIL" as *u8) }
146 fw(1, "\n T5 wire-header pack/unpack round-trip: " as *u8); if t5==1 { fw(1,"PASS" as *u8) } else { fw(1,"FAIL" as *u8) }
147 fw(1, "\n SCOPE: protocol+recovery proven on real frame bytes; daemon/client wire-in + live deploy = operator-gated next step\n" as *u8)
148 if ok==1 { fw(1, "VERDICT: GREEN (frame-level FEC wire protocol recovers byte-exact under <=m loss)\n" as *u8) } else { fw(1, "VERDICT: RED\n" as *u8) }
149
150 let lfd: i64 = sys_openat_append(FW_LOG, 420)
151 if lfd >= 0 {
152 fw(lfd, "ROOMFECWIRE fl=1000 k=8 m=2 exhaustive=" as *u8); fwn(lfd, exh); fw(lfd, "/45 t1=" as *u8); fwn(lfd, t1); fw(lfd, " t2=" as *u8); fwn(lfd, t2); fw(lfd, " t4=" as *u8); fwn(lfd, t4); fw(lfd, " t5=" as *u8); fwn(lfd, t5)
153 if ok==1 { fw(lfd, " verdict=GREEN\n" as *u8) } else { fw(lfd, " verdict=RED\n" as *u8) }
154 sys_close(lfd)
155 }
156 if ok==1 { sys_exit(0) } else { sys_exit(1) }
157 return 0
158}