code wiki / _hdl_build / _bf_erfc_oracle_gate.nx
_bf_erfc_oracle_gate.nx source
↩ module page · 102 lines · 4614 B
1// _bf_erfc_oracle_gate.nx -- ALGEBRAIC self-gate for the sovereign 120-bit erfc CF
2// oracle (nx_bigfloat120_erfc, ME2-ERF-002 foundation). No external reference: the
3// oracle proves ITSELF by the identity erf(x)+erfc(x)=1, checked at FULL bigfloat
4// precision in the overlap window x in [1.5,1.75] where BOTH the 48-term erf Taylor AND
5// the depth-240 erfc CF are accurate. Two INDEPENDENT methods agreeing to >2^-80 on the
6// identity is conclusive proof both are correct (a correlated 80-bit error is nil).
7// Plus a structural check over the TARGET domain x in [1.75,6]: erfc strictly decreasing
8// and strictly positive (the property the f64 kernel will rely on).
9// Durable: ERFCORACLE row -> knowledge/status/math_engine.log. Exit 0 iff both held.
10// license_tier: ORIGINAL
11import "nx_syscalls.nx"
12import "nx_bigfloat120.nx"
13import "nx_bigfloat120_div.nx"
14import "nx_bigfloat120_trig.nx"
15import "nx_bigfloat120_erfc.nx"
16
17func eg_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func eg_f(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 }
19func eg_n(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 }
20
21func main() -> i64 {
22 eg_p("=== BF-ERFC ORACLE GATE: erf+erfc=1 (algebraic, no external reference) ===\n" as *u8)
23 let one: *i64 = bf_new()
24 bf_set_int(one, 1)
25 let kb: *i64 = bf_new()
26 let xb: *i64 = bf_new()
27 let ev: *i64 = bf_new()
28 let ec: *i64 = bf_new()
29 let s: *i64 = bf_new()
30 let d: *i64 = bf_new()
31
32 // ---- identity window: x = k/64, k = 96..112 (1.5 .. 1.75) ----
33 var maxdb: i64 = 0
34 var worst: i64 = 0
35 var ipts: i64 = 0
36 var k: i64 = 96
37 while k <= 112 {
38 bf_set_int(kb, k)
39 bf_div_small(xb, kb, 64)
40 let xbits: i64 = bf_to_f64(xb, 0, 0)
41 bf_erf(ev, xb)
42 bf_erfc(ec, xb)
43 bf_add(s, ev, ec)
44 if bf_cmp(s, one) >= 0 { bf_sub(d, s, one) } else { bf_sub(d, one, s) }
45 var db: i64 = bf_to_f64(d, 0, 0)
46 db = db & 0x7FFFFFFFFFFFFFFF
47 if db > maxdb { maxdb = db; worst = xbits }
48 ipts = ipts + 1
49 k = k + 1
50 }
51 // bar: |erf+erfc-1| < 2^-80 <=> abs-f64-bits < (943<<52)
52 let thresh: i64 = 943 << 52
53 var id_ok: i64 = 0
54 if maxdb < thresh { id_ok = 1 }
55 // agreement bits = 1023 - unbiased-exp-of-worst-dev (bigger = better; >80 passes)
56 var agree: i64 = 1023
57 if maxdb != 0 { agree = 1023 - (maxdb >> 52) }
58
59 // ---- structural check over the TARGET domain: x = k/4, k = 7..24 (1.75 .. 6.0) ----
60 // erfc strictly decreasing and strictly positive.
61 let pc: *i64 = bf_new()
62 var have_prev: i64 = 0
63 var mono_ok: i64 = 1
64 var mpts: i64 = 0
65 k = 7
66 while k <= 24 {
67 bf_set_int(kb, k)
68 bf_div_small(xb, kb, 4)
69 bf_erfc(ec, xb)
70 if bf_is_zero(ec) == 1 { mono_ok = 0 } // must be strictly positive
71 if have_prev == 1 {
72 if bf_cmp(ec, pc) >= 0 { mono_ok = 0 } // must be strictly < previous
73 }
74 bf_copy(pc, ec)
75 have_prev = 1
76 mpts = mpts + 1
77 k = k + 1
78 }
79
80 var ok: i64 = 0
81 if id_ok == 1 { if mono_ok == 1 { ok = 1 } }
82
83 let lfd: i64 = sys_openat_append("knowledge/status/math_engine.log" as *u8, 0x1a4)
84 if lfd >= 0 {
85 eg_f(lfd, "ERFCORACLE epoch=" as *u8); eg_n(lfd, sys_now_realtime_sec())
86 eg_f(lfd, " identity_pts=" as *u8); eg_n(lfd, ipts)
87 eg_f(lfd, " min_agree_bits=" as *u8); eg_n(lfd, agree)
88 eg_f(lfd, " worst_x_bits=" as *u8); eg_n(lfd, worst)
89 eg_f(lfd, " mono_pts=" as *u8); eg_n(lfd, mpts)
90 eg_f(lfd, " mono_pos=" as *u8); eg_n(lfd, mono_ok)
91 eg_f(lfd, " method=laplace-CF-depth240/exp-taylor160/erf+erfc=1" as *u8)
92 if ok == 1 { eg_f(lfd, " verdict=GREEN\n" as *u8) } else { eg_f(lfd, " verdict=RED\n" as *u8) }
93 sys_close(lfd)
94 }
95 eg_p(" identity_pts=" as *u8); eg_n(1, ipts)
96 eg_p(" min_agree_bits=" as *u8); eg_n(1, agree)
97 eg_p(" mono_pts=" as *u8); eg_n(1, mpts); eg_p(" mono_pos=" as *u8); eg_n(1, mono_ok)
98 if ok == 1 { eg_p(" ERFCORACLE: GREEN (erf+erfc=1 to >2^-80 + monotone-positive on 1.75..6)\n" as *u8); sys_exit(0); return 0 }
99 eg_p(" ERFCORACLE: RED (identity or monotonicity broke -- see min_agree_bits)\n" as *u8)
100 sys_exit(1)
101 return 1
102}