code wiki / _hdl_build / nx_sfu_select_gate.nx
nx_sfu_select_gate.nx source
↩ module page · 200 lines · 11837 B
1import "nx_gate_base.nx"
2// nx_sfu_select_gate.nx -- adversarial gate for the sovereign selective-forwarding core. Proves the JVB
3// mechanics: default=today's behavior, switch ONLY at target-layer key, seq rewrite contiguous ACROSS the
4// switch, no starvation when LO never comes, no duplicate delivery, hostile inputs refused. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_sfu_select.nx"
7
8func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
9" as *u8); return ok }
10func gn(v: i64) -> i64 {
11 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
12 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}
13 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
14func ck(name: *u8, ok: i64, pass_p: *i64, tot_p: *i64) -> i64 {
15 tot_p[0]=tot_p[0]+1
16 gw(" " as *u8); gw(name)
17 if ok==1 { gw(" PASS\n" as *u8); pass_p[0]=pass_p[0]+1 } else { gw(" FAIL\n" as *u8) }
18 return 0 }
19
20func main() -> i64 {
21 gw("=== nx_sfu_select_gate: sovereign SFU forwarding (switch-at-key, seq-rewrite, no-starve) ===\n" as *u8)
22 let pass: *i64 = sys_mmap(16) as *i64; let tot: *i64 = sys_mmap(16) as *i64
23 let mem: *i64 = sys_mmap(SFU_REGION) as *i64
24 sfu_init(mem)
25
26 // T1 DEFAULT = today's behavior: HI frames forward with contiguous rewritten seq; LO ignored (unsubscribed).
27 // sender 3 -> receiver 7. in_seq deliberately weird (100,101,102) -- out must be 0,1,2.
28 var ok: i64 = 1
29 if sfu_on_frame(mem, 3, 7, 0, 1) != 0 { ok = 0 } // HI key -> out 0
30 if sfu_on_frame(mem, 3, 7, 0, 0) != 1 { ok = 0 } // HI P -> out 1
31 if sfu_on_frame(mem, 3, 7, 1, 1) != (0-1) { ok = 0 } // LO key -> NOT forwarded (want=HI)
32 if sfu_on_frame(mem, 3, 7, 0, 0) != 2 { ok = 0 } // HI P -> out 2 (contiguous DESPITE the dropped LO between)
33 ck("T1 default-HI contiguous rewrite, LO ignored \x00" as *u8, ok, pass, tot)
34
35 // T2 SWITCH TO LO only at LO KEY; HI keeps flowing until then; after switch HI dropped.
36 ok = 1
37 sfu_want(mem, 3, 7, 1) // subscribe LO
38 if sfu_on_frame(mem, 3, 7, 0, 0) != 3 { ok = 0 } // HI P still flows (no LO key yet)
39 if sfu_on_frame(mem, 3, 7, 1, 0) != (0-1) { ok = 0 } // LO P pre-key -> undecodable -> drop
40 if sfu_on_frame(mem, 3, 7, 1, 1) != 4 { ok = 0 } // LO KEY -> SWITCH + forward, seq contiguous (4)
41 if sfu_on_frame(mem, 3, 7, 0, 0) != (0-1) { ok = 0 } // HI P now dropped
42 if sfu_on_frame(mem, 3, 7, 0, 1) != (0-1) { ok = 0 } // even HI KEY dropped (want=LO holds)
43 if sfu_on_frame(mem, 3, 7, 1, 0) != 5 { ok = 0 } // LO P flows
44 ck("T2 switch-at-LO-key, HI dropped after, seq holds \x00" as *u8, ok, pass, tot)
45
46 // T3 SWITCH BACK to HI only at HI key.
47 ok = 1
48 sfu_want(mem, 3, 7, 0)
49 if sfu_on_frame(mem, 3, 7, 1, 0) != 6 { ok = 0 } // LO still flows
50 if sfu_on_frame(mem, 3, 7, 0, 0) != (0-1) { ok = 0 } // HI P pre-key -> drop
51 if sfu_on_frame(mem, 3, 7, 0, 1) != 7 { ok = 0 } // HI KEY -> switch back
52 if sfu_on_frame(mem, 3, 7, 1, 1) != (0-1) { ok = 0 } // LO key now dropped
53 ck("T3 switch-back at HI key only \x00" as *u8, ok, pass, tot)
54
55 // T4 NO STARVATION: fresh pair, want=LO but sender NEVER produces LO -> HI flows forever.
56 ok = 1
57 sfu_want(mem, 5, 9, 1)
58 if sfu_on_frame(mem, 5, 9, 0, 1) != 0 { ok = 0 }
59 var i: i64 = 0
60 while i < 50 { if sfu_on_frame(mem, 5, 9, 0, 0) != (i+1) { ok = 0; i = 50 } else { i = i + 1 } }
61 ck("T4 want-LO + no LO produced -> HI never starves \x00" as *u8, ok, pass, tot)
62
63 // T5 PAIR ISOLATION: receiver 7 on LO must not disturb receiver 9 on HI from the SAME sender.
64 ok = 1
65 sfu_init(mem)
66 sfu_want(mem, 2, 7, 1)
67 if sfu_on_frame(mem, 2, 7, 1, 1) != 0 { ok = 0 } // 7 switches to LO at its key
68 if sfu_on_frame(mem, 2, 9, 1, 1) != (0-1) { ok = 0 } // 9 (default HI) drops LO
69 if sfu_on_frame(mem, 2, 9, 0, 0) != 0 { ok = 0 } // 9 gets HI with its OWN seq space
70 if sfu_on_frame(mem, 2, 7, 0, 0) != (0-1) { ok = 0 } // 7 drops HI
71 ck("T5 per-pair isolation + independent seq spaces \x00" as *u8, ok, pass, tot)
72
73 // T6 CONN REUSE: reset clears both directions.
74 ok = 1
75 sfu_reset_conn(mem, 7)
76 if sfu_on_frame(mem, 2, 7, 1, 0) != (0-1) { ok = 0 } // 7's LO subscription gone (fresh member = HI default)
77 if sfu_on_frame(mem, 2, 7, 0, 1) != 0 { ok = 0 } // HI forwards from out_seq 0 again
78 ck("T6 conn-reuse reset -> defaults + seq restart \x00" as *u8, ok, pass, tot)
79
80 // T7 HOSTILE: bad indexes, self-forward, junk want -- all refused, then state still sane.
81 ok = 1
82 if sfu_on_frame(mem, 0-1, 7, 0, 1) != (0-1) { ok = 0 }
83 if sfu_on_frame(mem, 2, 128, 0, 1) != (0-1) { ok = 0 }
84 if sfu_on_frame(mem, 5, 5, 0, 1) != (0-1) { ok = 0 } // self
85 if sfu_want(mem, 2, 7, 9) != (0-1) { ok = 0 } // junk layer
86 if sfu_want(mem, 200, 7, 1) != (0-1) { ok = 0 }
87 if sfu_on_frame(mem, 2, 7, 0, 0) != 1 { ok = 0 } // pair 2->7 unharmed (continues at 1)
88 ck("T7 hostile args refused, state intact \x00" as *u8, ok, pass, tot)
89
90 // T8 WIRE HELPERS: frame-bit parse + LE seq rewrite + LSUB parse round-trip.
91 ok = 1
92 let fb: *u8 = sys_mmap(64)
93 fb[0] = 0x57 as u8
94 var j: i64 = 0; while j < 8 { fb[1+j] = (65+j) as u8; j = j + 1 }
95 fb[9]=9 as u8; fb[10]=0 as u8; fb[11]=0 as u8; fb[12]=0 as u8
96 fb[13] = 3 as u8 // key + LO
97 let inf: *i64 = sys_mmap(32) as *i64
98 if sfu_frame_bits(fb, 20, inf) != 0 { ok = 0 }
99 if inf[0] != 1 { ok = 0 }
100 if inf[1] != 1 { ok = 0 }
101 sfu_rewrite_seq(fb, 16909060) // 0x01020304
102 if (fb[9]&0xff) != 4 { ok = 0 }
103 if (fb[10]&0xff) != 3 { ok = 0 }
104 if (fb[11]&0xff) != 2 { ok = 0 }
105 if (fb[12]&0xff) != 1 { ok = 0 }
106 let lb: *u8 = sys_mmap(64)
107 lb[0] = 0x58 as u8
108 j = 0; while j < 8 { lb[1+j] = (97+j) as u8; j = j + 1 } // subscriber id (unused by parse)
109 lb[9]=0 as u8; lb[10]=0 as u8; lb[11]=0 as u8; lb[12]=0 as u8
110 j = 0; while j < 8 { lb[13+j] = (65+j) as u8; j = j + 1 } // target id "ABCDEFGH"
111 lb[21] = 1 as u8 // want LO
112 let tid: *u8 = sys_mmap(16)
113 if sfu_parse_lsub(lb, 22, tid) != 1 { ok = 0 }
114 if (tid[0]&0xff) != 65 { ok = 0 }
115 if (tid[7]&0xff) != 72 { ok = 0 }
116 if sfu_parse_lsub(lb, 21, tid) != (0-1) { ok = 0 } // short -> refused
117 lb[21] = 7 as u8
118 if sfu_parse_lsub(lb, 22, tid) != (0-1) { ok = 0 } // junk want -> refused
119 if sfu_frame_bits(lb, 22, inf) != (0-1) { ok = 0 } // 0x58 is not a video frame
120 fb[0] = 0x77 as u8 // E2EE video: SFU must treat it like 0x57 (flags @13)
121 if sfu_frame_bits(fb, 20, inf) != 0 { ok = 0 }
122 if inf[0] != 1 { ok = 0 }
123 if inf[1] != 1 { ok = 0 }
124 ck("T8 wire helpers: bits (0x57+0x77), rewrite, LSUB \x00" as *u8, ok, pass, tot)
125
126 // ---- ACTIVITY PLANE (R4: dominant-speaker + last-N) ----
127 // T9 bucket window: speech bytes score, roll to next bucket keeps prev, 2-bucket idle clears all.
128 ok = 1
129 sfu_init(mem)
130 let T0: i64 = 10 * SFU_ACT_BUCKET_US
131 sfu_audio_bytes(mem, 3, 900, T0)
132 if sfu_act_score(mem, 3, T0) != 900 { ok = 0 }
133 sfu_audio_bytes(mem, 3, 100, T0 + SFU_ACT_BUCKET_US) // next bucket: cur=100 prev=900
134 if sfu_act_score(mem, 3, T0 + SFU_ACT_BUCKET_US) != 1000 { ok = 0 }
135 if sfu_act_score(mem, 3, T0 + 3 * SFU_ACT_BUCKET_US) != 0 { ok = 0 } // idle 2 buckets -> silent
136 ck("T9 activity window: accumulate, roll, decay \x00" as *u8, ok, pass, tot)
137
138 // T10 dominant: argmax + 5/4 hysteresis (challenger at 1.2x does NOT flip; at 1.3x it does).
139 ok = 1
140 sfu_init(mem)
141 let cand: *i64 = sys_mmap(64) as *i64
142 cand[0] = 1; cand[1] = 2; cand[2] = 3
143 if sfu_dominant(mem, cand, 3, 0-1, T0) != (0-1) { ok = 0 } // silent room -> nobody
144 sfu_audio_bytes(mem, 1, 1000, T0)
145 if sfu_dominant(mem, cand, 3, 0-1, T0) != 1 { ok = 0 } // 1 speaks -> dominant
146 sfu_audio_bytes(mem, 2, 1200, T0) // 1.2x -> hysteresis holds 1
147 if sfu_dominant(mem, cand, 3, 1, T0) != 1 { ok = 0 }
148 sfu_audio_bytes(mem, 2, 100, T0) // now 1300 = 1.3x -> flips to 2
149 if sfu_dominant(mem, cand, 3, 1, T0) != 2 { ok = 0 }
150 ck("T10 dominant argmax + 5/4 anti-flap hysteresis \x00" as *u8, ok, pass, tot)
151
152 // T11 last-N: receiver 9 caps N=1 -> only the TOP-active sender passes; lastn=0 stays unlimited.
153 ok = 1
154 sfu_init(mem)
155 cand[0] = 1; cand[1] = 2; cand[2] = 9
156 sfu_audio_bytes(mem, 1, 5000, T0) // 1 = loud
157 sfu_audio_bytes(mem, 2, 50, T0) // 2 = quiet
158 if sfu_video_rank_ok(mem, 2, 9, cand, 3, T0) != 1 { ok = 0 } // no cap yet -> passes
159 sfu_set_lastn(mem, 9, 1)
160 if sfu_video_rank_ok(mem, 1, 9, cand, 3, T0) != 1 { ok = 0 } // top-1 -> passes
161 if sfu_video_rank_ok(mem, 2, 9, cand, 3, T0) != 0 { ok = 0 } // rank 2 -> blocked
162 sfu_set_lastn(mem, 9, 2)
163 if sfu_video_rank_ok(mem, 2, 9, cand, 3, T0) != 1 { ok = 0 } // N=2 -> passes again
164 ck("T11 last-N rank filter per receiver \x00" as *u8, ok, pass, tot)
165
166 // T12 silent tie -> most recent VIDEO wins the rank; hostile midx refused.
167 ok = 1
168 sfu_init(mem)
169 cand[0] = 1; cand[1] = 2; cand[2] = 9
170 sfu_video_seen(mem, 1, T0 - 500)
171 sfu_video_seen(mem, 2, T0 - 100) // 2 = fresher video, equal (0) audio
172 sfu_set_lastn(mem, 9, 1)
173 if sfu_video_rank_ok(mem, 2, 9, cand, 3, T0) != 1 { ok = 0 }
174 if sfu_video_rank_ok(mem, 1, 9, cand, 3, T0) != 0 { ok = 0 }
175 if sfu_audio_bytes(mem, 0-1, 10, T0) != (0-1) { ok = 0 }
176 if sfu_set_lastn(mem, 5, 0-2) != (0-1) { ok = 0 }
177 ck("T12 tie-break by video recency + hostile refusal \x00" as *u8, ok, pass, tot)
178
179 // T13 LAYER SUSPENSION (R5): lo_wanted truth-table + lprod change-detection (announce only on flips).
180 ok = 1
181 sfu_init(mem)
182 if sfu_lo_wanted(mem, 4) != 0 { ok = 0 } // nobody subscribed -> no LO needed
183 if sfu_lprod_check(mem, 4) != 1 { ok = 0 } // first eval announces (unannounced -1 -> 0)
184 if sfu_lprod_check(mem, 4) != 0 { ok = 0 } // steady state -> silent
185 sfu_want(mem, 4, 9, 1) // receiver 9 subscribes LO
186 if sfu_lo_wanted(mem, 4) != 1 { ok = 0 }
187 if sfu_lprod_check(mem, 4) != 1 { ok = 0 } // flip 0->1 announced
188 if sfu_lprod_last(mem, 4) != 1 { ok = 0 }
189 sfu_on_frame(mem, 4, 9, 1, 1) // LO key -> pair now FLOWING LO (cur=LO)
190 sfu_want(mem, 4, 9, 0) // unsubscribe...
191 if sfu_lo_wanted(mem, 4) != 1 { ok = 0 } // ...but still flowing -> still needed (no starve)
192 sfu_on_frame(mem, 4, 9, 0, 1) // HI key -> switch back completes
193 if sfu_lo_wanted(mem, 4) != 0 { ok = 0 }
194 if sfu_lprod_check(mem, 4) != 1 { ok = 0 } // flip 1->0 announced -> sender suspends
195 ck("T13 layer suspension: want/flow truth + flip-only \x00" as *u8, ok, pass, tot)
196
197 gw("SFU-SELECT: " as *u8); gn(pass[0]); gw("/" as *u8); gn(tot[0])
198 if pass[0]==tot[0] { gw(" ALL verdict=GREEN\n" as *u8); return 0 }
199 gw(" RED\n" as *u8)
200 return 1 }