code wiki / _hdl_build / nx_chaos_gate.nx

nx_chaos_gate.nx source

↩ module page · 65 lines · 5395 B

1// nx_chaos_gate.nx -- NATIVE verify of the browser chaos sim (base = mmap; SAME code runs in sovereign WASM). The 2// logistic map x'=r x(1-x) across regimes: (a) FIXED POINT at r=2.5 -> x*=1-1/r=0.6, (b) PERIOD-2 at r=3.2 (orbit 3// alternates between two values), (c) CHAOS at r=3.9 (wide aperiodic spread), (d) SENSITIVE DEPENDENCE -- two starts 4// one unit apart diverge hugely at r=3.9. The headline: (e) DETERMINISM -- the SAME (x0,r) run twice gives the 5// BIT-EXACT same chaotic sequence; integer/no-float chaos is reproducible exactly where IEEE-754 float chaos 6// diverges machine-to-machine = the live face of the Reproducibility EXCEEDS. Liar-kill: at r=2.5 the two nearby 7// starts CONVERGE (no divergence) -- chaos is genuinely r-dependent, not faked. license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_chaos.nx" 10import "nx_png.nx" 11 12func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); 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; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 } 14func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 } 15func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x } 16 17func iterate_n(x0: i64, R: i64, n: i64) -> i64 { var x: i64=x0; var i: i64=0; while i<n { x=logistic(x,R); i=i+1 } return x } 18func diff_after(R: i64, a0: i64, b0: i64, n: i64) -> i64 { var a: i64=a0; var b: i64=b0; var i: i64=0; while i<n { a=logistic(a,R); b=logistic(b,R); i=i+1 } return iabs(a-b) } 19func spread(R: i64, trans: i64, samp: i64) -> i64 { 20 var x: i64=500000; var i: i64=0; while i<trans { x=logistic(x,R); i=i+1 } 21 var mn: i64=x; var mx: i64=x; i=0; while i<samp { x=logistic(x,R); if x<mn { mn=x } if x>mx { mx=x } i=i+1 } 22 return mx-mn 23} 24 25func main() -> i64 { 26 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 27 g_w("=== NX-CHAOS native verify (logistic-map deterministic chaos; SAME code runs in sovereign wasm) ===\n") 28 let base: i64 = sys_mmap(2*1048576) as i64 29 30 let fp: i64 = iterate_n(500000, 2500, 800) // r=2.5 fixed point -> 600000 31 // r=3.2 period-2 32 var x: i64=500000; var i: i64=0; while i<600 { x=logistic(x,3200); i=i+1 } 33 let a: i64=x; let b: i64=logistic(a,3200); let c: i64=logistic(b,3200) 34 var p2: i64=0; if iabs(a-c)<300 { if iabs(a-b)>50000 { p2=1 } } 35 let sp_chaos: i64 = spread(3900, 200, 200) // r=3.9 wide spread 36 let sp_fixed: i64 = spread(2500, 200, 200) // r=2.5 ~0 spread 37 let div39: i64 = diff_after(3900, 500000, 500001, 60) // sensitive dependence 38 let div25: i64 = diff_after(2500, 500000, 500001, 200)// converges (liar-kill) 39 let run1: i64 = iterate_n(500000, 3900, 120); let run2: i64 = iterate_n(500000, 3900, 120) 40 41 g_w(" fixed-pt(r2.5)="); g_n(fp); g_w(" (x*=600000) period2(r3.2): a="); g_n(a); g_w(" b="); g_n(b); g_w(" c="); g_n(c); g_w("\n") 42 g_w(" spread chaos(r3.9)="); g_n(sp_chaos); g_w(" vs fixed(r2.5)="); g_n(sp_fixed); g_w(" sensitive div(r3.9,60it)="); g_n(div39); g_w(" vs div(r2.5)="); g_n(div25); g_w("\n") 43 g_w(" determinism: run1="); g_n(run1); g_w(" run2="); g_n(run2); g_w(" (must be identical)\n") 44 45 init_impl(base); var t: i64=0; while t<HISTN { tick_impl(base, 0); t=t+1 } 46 render_impl(base) 47 write_png((base + O_FB) as *i64, W, H, "knowledge/nx_chaos.png" as *u8) 48 let m: *u8=(base+O_FB) as *u8; var nb: i64=0; i=0 49 while i<W*H { let bb: i64=i*8; if (m[bb] as i64)!=10 { nb=nb+1 } else { if (m[bb+1] as i64)!=10 { nb=nb+1 } else { if (m[bb+2] as i64)!=18 { nb=nb+1 } } } i=i+1 } 50 51 g_row("RUNS: the logistic map iterated across regimes (fixed point / period-2 / chaos)" as *u8, (fp>0) as i64, pass) 52 g_row("FIXED POINT (r=2.5): converges to x* = 1-1/r = 0.6 within 0.2%" as *u8, (iabs(fp-600000)<2000) as i64, pass) 53 g_row("PERIOD-2 (r=3.2): the orbit alternates between two distinct values (x_n = x_n+2 != x_n+1)" as *u8, p2, pass) 54 g_row("CHAOS (r=3.9): aperiodic wide spread (>0.4) vs the fixed point's ~0 spread" as *u8, ((sp_chaos>400000) as i64)*((sp_fixed<2000) as i64), pass) 55 g_row("SENSITIVE DEPENDENCE: two starts one unit apart diverge hugely at r=3.9 (>0.1) -- the butterfly effect" as *u8, (div39>100000) as i64, pass) 56 g_row("DETERMINISM=EXCEEDS: the SAME (x0,r) run twice gives the BIT-EXACT same chaotic value (integer reproducibility where float diverges)" as *u8, (run1==run2) as i64, pass) 57 g_row("LIAR-KILL: at r=2.5 the two nearby starts CONVERGE (<0.01 apart) -- divergence is genuinely r-dependent, not faked" as *u8, (div25<10000) as i64, pass) 58 g_row("RASTER: framebuffer drawn (two diverging trajectories) -> knowledge/nx_chaos.png" as *u8, (nb>100) as i64, pass) 59 var iface: i64=0; if ww()==W { if hh()==H { if fb_off()==O_FB { iface=1 } } } 60 g_row("WASM-READY: init/tick/render/ww/hh/fb_off interface intact" as *u8, iface, pass) 61 62 g_w("NX-CHAOS-GATE rows=9 pass="); g_n(pass[0]) 63 if pass[0]==9 { g_w(" verdict=GREEN (deterministic chaos; sensitive-dependence + BIT-EXACT reproducibility, browser-ready)\n"); sys_exit(0); return 0 } 64 g_w(" verdict=RED\n"); sys_exit(1); return 1 65}