code wiki / _hdl_build / nx_chaos_logistic_gate.nx
nx_chaos_logistic_gate.nx source
↩ module page · 100 lines · 4788 B
1// nx_chaos_logistic_gate.nx -- resumes the nishi-library SIM workstream's stated next step:
2// "no-float determinism UNDER CHAOS = live proof of the Reproducibility-EXCEEDS axis."
3//
4// Vehicle = the canonical discrete chaotic system, the LOGISTIC MAP x' = r*x*(1-x), in pure
5// integer/no-float fixed point (micro-scale S=1e6, r scaled by RD=1e3). Reordered to avoid i64
6// overflow: x*(S-x) <= 2.5e11, *R <= ~1e15, / (RD*S=1e9). Stays in [0,S] for r<=4.
7//
8// THREE properties, each MEASURED, with a liar-kill so GREEN means something:
9// (1) SENSITIVE DEPENDENCE: two seeds 1 micro-unit apart (0.200000 vs 0.200001) DIVERGE to O(S)
10// at r=3.9 (chaotic) -- the butterfly effect, measured as max separation after burn-in.
11// (2) DETERMINISM / REPRODUCIBILITY: the SAME integer trajectory recomputed gives a BIT-IDENTICAL
12// divergence number -- no-float => exactly reproducible even in the chaotic regime (the EXCEEDS
13// property an IEEE-754 sim cannot guarantee under reordering).
14// (3) NEGATIVE CONTROL (liar-kill): the SAME 1-unit-apart seeds at r=2.5 (non-chaotic, converges to
15// the fixed point 0.6) do NOT diverge -- so the gate cannot be fooled into calling order "chaos".
16// GREEN requires chaos-diverges AND order-does-not AND chaos-is-reproducible.
17//
18// Sovereign: imports only nx_syscalls. Additive. license_tier: ORIGINAL
19import "nx_syscalls.nx"
20
21const CL_S: i64 = 1000000 // micro-scale for x in [0,1]
22const CL_RD: i64 = 1000 // r scaled by 1000 (r=3.9 -> R=3900)
23const CL_BIG: i64 = 100000 // chaos threshold: max separation must EXCEED S/10
24const CL_SMALL: i64 = 10000 // order threshold: separation must stay BELOW S/100
25
26func cl_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
27func cl_p(s: *u8) -> i64 { let n: i64 = cl_len(s); sys_write(1, s, n); return 0 }
28func cl_pn(v: i64) -> i64 {
29 let bb: *u8 = sys_mmap(28); var m: i64 = v
30 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
31 let t: *u8 = sys_mmap(28); var k: i64 = 0
32 if m == 0 { t[0] = 48 as u8; k = 1 }
33 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
34 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
35 sys_write(1, bb, k); return 0
36}
37
38// one logistic step in fixed point: x' = R*x*(S-x)/(RD*S). bounded, no overflow for R<=4000.
39func cl_iter(X: i64, R: i64) -> i64 {
40 let p: i64 = X * (CL_S - X)
41 let q: i64 = R * p
42 return q / (CL_RD * CL_S)
43}
44
45// step two trajectories in lockstep; return MAX |Xa-Xb| over steps [burn..n). This is robust:
46// once decorrelated, chaotic orbits are far apart at SOME late step; converging orbits never are.
47func cl_divmax(x0a: i64, x0b: i64, R: i64, n: i64, burn: i64) -> i64 {
48 var xa: i64 = x0a
49 var xb: i64 = x0b
50 var mx: i64 = 0
51 var i: i64 = 0
52 while i < n {
53 xa = cl_iter(xa, R)
54 xb = cl_iter(xb, R)
55 if i >= burn {
56 var d: i64 = xa - xb
57 if d < 0 { d = 0 - d }
58 if d > mx { mx = d }
59 }
60 i = i + 1
61 }
62 return mx
63}
64
65func main() -> i64 {
66 cl_p("=== nx_chaos_logistic_gate: no-float determinism UNDER CHAOS (logistic map) ===\n" as *u8)
67 let SEEDA: i64 = 200000 // 0.200000
68 let SEEDB: i64 = 200001 // 0.200001 (1 micro-unit apart)
69 let RCHAOS: i64 = 3900 // r = 3.9 (chaotic)
70 let RORDER: i64 = 2500 // r = 2.5 (converges to fixed point 0.6)
71 let N: i64 = 90
72 let BURN: i64 = 30
73
74 let chaos1: i64 = cl_divmax(SEEDA, SEEDB, RCHAOS, N, BURN)
75 let chaos2: i64 = cl_divmax(SEEDA, SEEDB, RCHAOS, N, BURN) // recompute -> must be identical
76 let order1: i64 = cl_divmax(SEEDA, SEEDB, RORDER, N, BURN)
77
78 cl_p(" seeds: 0.200000 vs 0.200001 (1e-6 apart); N=" as *u8); cl_pn(N)
79 cl_p(" burn=" as *u8); cl_pn(BURN); cl_p("\n" as *u8)
80 cl_p(" (1) sensitive-dependence r=3.9 max|dA-dB| = " as *u8); cl_pn(chaos1)
81 cl_p(" (need > " as *u8); cl_pn(CL_BIG); cl_p(")\n" as *u8)
82 cl_p(" (2) determinism re-run r=3.9 max|dA-dB| = " as *u8); cl_pn(chaos2)
83 cl_p(" (must == run 1)\n" as *u8)
84 cl_p(" (3) neg-control (order) r=2.5 max|dA-dB| = " as *u8); cl_pn(order1)
85 cl_p(" (must < " as *u8); cl_pn(CL_SMALL); cl_p(")\n" as *u8)
86
87 var pass: i64 = 0
88 var ok: i64 = 1
89 if chaos1 > CL_BIG { pass = pass + 1 } else { ok = 0 }
90 if chaos1 == chaos2 { pass = pass + 1 } else { ok = 0 }
91 if order1 < CL_SMALL { pass = pass + 1 } else { ok = 0 }
92
93 cl_p(" RESULT pass=" as *u8); cl_pn(pass); cl_p("/3 verdict=" as *u8)
94 if ok == 1 {
95 cl_p("GREEN (chaos diverges, no-float result is bit-reproducible, order does NOT diverge)\n" as *u8)
96 sys_exit(0); return 0
97 }
98 cl_p("RED\n" as *u8)
99 sys_exit(1); return 1
100}