code wiki / _hdl_build / nx_quic_fec_test.nx

nx_quic_fec_test.nx source

↩ module page · 65 lines · 3527 B

1// nx_quic_fec_test.nx -- RUNG 8a gate: FEC over QUIC DATAGRAM frames. Proves a lost source packet is 2// reconstructed byte-exact with ZERO retransmit (the resilience exceed), the payload rides on R1's 3// DATAGRAM frame, and the single-erasure limit is honest. Native, sovereign. license_tier: ORIGINAL 4import "nx_quic_fec.nx" 5import "nx_g_check_lib.nx" 6import "nx_g_puts_lib.nx" 7 8func g_pn(v: i64) -> i64 { 9 if v==0 { sys_write(1,"0" as *u8,1); return 0 } 10 let b: *u8 = sys_mmap(28); var x: i64 = v 11 var d: i64=0; var y: i64=x 12 while y>0 { d=d+1; y=y/10 } 13 var i: i64=d-1; y=x 14 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 15 sys_write(1,b,d); return 0 16} 17func main() -> i64 { 18 g_puts("nx_quic_fec gate -- FEC over DATAGRAM frames (resilience exceed)\n" as *u8) 19 var pass: i64=0; var total: i64=0 20 let N: i64 = 4; let L: i64 = 100 21 22 // 4 distinct source packets, flat in source[N*L] 23 let source: *u8 = sys_mmap(N*L) 24 var i: i64=0 25 while i<N { var j: i64=0; while j<L { source[i*L+j]=((i*37+j*7+11)&255) as u8; j=j+1 } i=i+1 } 26 let parity: *u8 = sys_mmap(L) 27 quic_fec_encode(source, N, L, parity) 28 29 // ---- a source packet rides in a DATAGRAM frame (R1) byte-exact ---- 30 let frame: *u8 = sys_mmap(256) 31 let flen: i64 = quic_datagram_encode(frame, (source as i64 + 2*L) as *u8, L) 32 let poff: *i64 = sys_mmap(8) as *i64 33 let pp: i64 = quic_datagram_parse(frame, flen, poff) 34 var dg_ok: i64=0 35 if pp==L { dg_ok=1; var k: i64=0; while k<L { if frame[poff[0]+k]!=source[2*L+k] { dg_ok=0; k=L } else { k=k+1 } } } 36 pass = pass + g_check("source packet rides in a DATAGRAM frame (R1) byte-exact" as *u8, dg_ok); total=total+1 37 38 // ---- lose source[2], recover via FEC, zero retransmit ---- 39 let present: *i64 = sys_mmap(8*N) as *i64 40 present[0]=1; present[1]=1; present[2]=0; present[3]=1 41 let out: *u8 = sys_mmap(L) 42 let rec_idx: i64 = quic_fec_recover_one(source, present, N, L, parity, 1, out) 43 var rec_ok: i64=0 44 if rec_idx==2 { rec_ok=1; var k: i64=0; while k<L { if out[k]!=source[2*L+k] { rec_ok=0; k=L } else { k=k+1 } } } 45 pass = pass + g_check("FEC recovers lost source[2] byte-exact, ZERO retransmit (beats ARQ stall)" as *u8, rec_ok); total=total+1 46 47 // ---- honest limit: 2 erasures in a block -> cannot recover ---- 48 present[1]=0 49 let r2: i64 = quic_fec_recover_one(source, present, N, L, parity, 1, out) 50 pass = pass + g_check("2 erasures/block -> -1 (honest single-erasure XOR limit; interleave for bursts)" as *u8, (r2 == 0-1) as i64); total=total+1 51 52 // ---- parity itself lost, all source present -> nothing to recover (sources intact) ---- 53 present[1]=1; present[2]=1 54 let r3: i64 = quic_fec_recover_one(source, present, N, L, parity, 0, out) 55 pass = pass + g_check("parity lost + all source present -> -1 (no recovery needed)" as *u8, (r3 == 0-1) as i64); total=total+1 56 57 // ---- non-vacuous: parity is the XOR of all sources (not zero) ---- 58 var nz: i64=0; var k: i64=0; while k<L { if parity[k]!=(0 as u8) { nz=1; k=L } else { k=k+1 } } 59 pass = pass + g_check("parity is non-trivial (XOR of distinct sources != 0)" as *u8, nz); total=total+1 60 61 g_puts("---- quic_fec gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 62 if pass == total { g_puts("VERDICT: GREEN (FEC over DATAGRAM frames: lost packets recovered zero-retransmit -- the resilience exceed)\n" as *u8); return 0 } 63 g_puts("VERDICT: RED\n" as *u8) 64 return 1 65}