nx_native_contract_gate.nx source
↩ module page · 239 lines · 8985 B
1// nx_native_contract_gate.nx -- F103a R-N0: THE NATIVE CONTRACT v1 GATE (on nx_gate_verdict, D001 migrate-on-touch).
2// Reads the contract card knowledge/registry/native_contract_v1.txt (pipe rows:
3// clause_id|axis|law|rejects|status|witness_path|needle) and GROUNDS it in live organ bytes:
4// PROVEN/PARTIAL -> witness file must EXIST and carry the needle (no clause without a live organ)
5// NEGCTL -> fabricated needle must be ABSENT from the witness (scanner non-vacuity tooth)
6// NEGCTL_ABSENT -> phantom witness file must be ABSENT (existence-check tooth)
7// DESIGN -> counted only (honest declared gap; a DESIGN row never fakes a witness)
8// Structural teeth: >=12 rows, >=6 PROVEN, >=1 DESIGN, >=2 negctl, all 6 doctrine axes, 0 malformed.
9// argv[1] optionally overrides the card path (pinned at tool registration). Exit 0 GREEN / 1 RED.
10// license_tier: ORIGINAL No hw writes (Rule 26): reads card + sources, writes stdout only.
11import "nx_syscalls.nx"
12import "nx_gate_verdict.nx"
13
14const NCG_CARD_PATH: *u8 = "knowledge/registry/native_contract_v1.txt" as *u8
15const NCG_CARD_CAP: i64 = 65536
16const NCG_WIT_CAP: i64 = 1048576
17const NCG_SCR: i64 = 512
18const NCG_PIPE: i64 = 124
19const NCG_NL: i64 = 10
20const NCG_HASH: i64 = 35
21
22func ncg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
23
24func ncg_eq(a: *u8, b: *u8) -> i64 {
25 var i: i64 = 0
26 var r: i64 = 0 - 1
27 while r < 0 {
28 let ca: i64 = a[i] as i64
29 let cb: i64 = b[i] as i64
30 if ca != cb { r = 0 }
31 if r < 0 { if ca == 0 { r = 1 } }
32 if r < 0 { i = i + 1 }
33 }
34 return r
35}
36
37func ncg_read(path: *u8, buf: *u8, cap: i64) -> i64 {
38 let fd: i64 = sys_openat_rd(path)
39 if fd < 0 { return 0 - 1 }
40 var o: i64 = 0
41 var go: i64 = 1
42 while go == 1 {
43 let rem: i64 = cap - o
44 if rem <= 0 { go = 0 }
45 if go == 1 {
46 let q: *u8 = buf + o
47 let r: i64 = sys_read(fd, q, rem)
48 if r <= 0 { go = 0 }
49 if r > 0 { o = o + r }
50 }
51 }
52 sys_close(fd)
53 return o
54}
55
56func ncg_find(hay: *u8, hn: i64, nd: *u8) -> i64 {
57 let nn: i64 = ncg_slen(nd)
58 if nn <= 0 { return 0 }
59 var found: i64 = 0
60 let lim: i64 = hn - nn
61 var i: i64 = 0
62 while i <= lim {
63 if found == 0 {
64 var j: i64 = 0
65 var ok: i64 = 1
66 while j < nn {
67 if hay[i + j] != nd[j] { ok = 0; j = nn } else { j = j + 1 }
68 }
69 if ok == 1 { found = 1 }
70 }
71 i = i + 1
72 }
73 return found
74}
75
76func ncg_cp(src: *u8, o: i64, n: i64, dst: *u8) -> i64 {
77 var m: i64 = n
78 if m > 500 { m = 500 }
79 if m < 0 { m = 0 }
80 var i: i64 = 0
81 while i < m { dst[i] = src[o + i]; i = i + 1 }
82 dst[i] = 0 as u8
83 return m
84}
85
86func ncg_name(dst: *u8, id: *u8, suf: *u8) -> i64 {
87 var o: i64 = 0
88 var i: i64 = 0
89 while id[i] != (0 as u8) { dst[o] = id[i]; o = o + 1; i = i + 1 }
90 dst[o] = 32 as u8
91 o = o + 1
92 var j: i64 = 0
93 while suf[j] != (0 as u8) { dst[o] = suf[j]; o = o + 1; j = j + 1 }
94 dst[o] = 0 as u8
95 return o
96}
97
98func ncg_axis(ax: *i64, s: *u8) -> i64 {
99 let e0: i64 = ncg_eq(s, "exec" as *u8)
100 if e0 == 1 { ax[0] = 1 }
101 let e1: i64 = ncg_eq(s, "authority" as *u8)
102 if e1 == 1 { ax[1] = 1 }
103 let e2: i64 = ncg_eq(s, "data" as *u8)
104 if e2 == 1 { ax[2] = 1 }
105 let e3: i64 = ncg_eq(s, "process" as *u8)
106 if e3 == 1 { ax[3] = 1 }
107 let e4: i64 = ncg_eq(s, "syscall-shape" as *u8)
108 if e4 == 1 { ax[4] = 1 }
109 let e5: i64 = ncg_eq(s, "determinism" as *u8)
110 if e5 == 1 { ax[5] = 1 }
111 return 0
112}
113
114func main(argc: i64, argv: *i64) -> i64 {
115 let ctr: *i64 = gv_ctr()
116 gv_head("NX-NATIVE-CONTRACT-V1 -- F103a R-N0: the native identity card, every clause grounded in live organ bytes" as *u8)
117 let card: *u8 = sys_mmap(NCG_CARD_CAP)
118 let wbuf: *u8 = sys_mmap(NCG_WIT_CAP)
119 let zid: *u8 = sys_mmap(NCG_SCR)
120 let zax: *u8 = sys_mmap(NCG_SCR)
121 let zst: *u8 = sys_mmap(NCG_SCR)
122 let zwit: *u8 = sys_mmap(NCG_SCR)
123 let znd: *u8 = sys_mmap(NCG_SCR)
124 let zname: *u8 = sys_mmap(1024)
125 let fo: *i64 = sys_mmap(64) as *i64
126 let fl: *i64 = sys_mmap(64) as *i64
127 let cnt: *i64 = sys_mmap(64) as *i64
128 let ax: *i64 = sys_mmap(64) as *i64
129 var cpath: *u8 = NCG_CARD_PATH
130 if argc >= 2 { let a1: i64 = argv[1]; cpath = a1 as *u8 }
131 let cn: i64 = ncg_read(cpath, card, NCG_CARD_CAP)
132 var t1: i64 = 0
133 if cn > 100 { t1 = 1 }
134 gv_check("T1 contract card readable (>100 bytes)" as *u8, t1, ctr)
135 if t1 == 0 { let rc0: i64 = gv_verdict("NATIVE-CONTRACT-V1" as *u8, ctr, "card missing" as *u8); sys_exit(rc0) }
136 var pos: i64 = 0
137 while pos < cn {
138 var e: i64 = pos
139 var sc: i64 = 1
140 while sc == 1 {
141 if e >= cn { sc = 0 } else { if card[e] == (NCG_NL as u8) { sc = 0 } else { e = e + 1 } }
142 }
143 let ll: i64 = e - pos
144 var skip: i64 = 0
145 if ll < 3 { skip = 1 }
146 if skip == 0 { if card[pos] == (NCG_HASH as u8) { skip = 1 } }
147 if skip == 0 {
148 var fi: i64 = 0
149 fo[0] = pos
150 var i: i64 = pos
151 while i < e {
152 if card[i] == (NCG_PIPE as u8) {
153 if fi < 6 { fl[fi] = i - fo[fi]; fi = fi + 1; fo[fi] = i + 1 }
154 }
155 i = i + 1
156 }
157 fl[fi] = e - fo[fi]
158 fi = fi + 1
159 if fi != 7 { cnt[5] = cnt[5] + 1 }
160 if fi == 7 {
161 cnt[0] = cnt[0] + 1
162 ncg_cp(card, fo[0], fl[0], zid)
163 ncg_cp(card, fo[1], fl[1], zax)
164 ncg_cp(card, fo[4], fl[4], zst)
165 ncg_cp(card, fo[5], fl[5], zwit)
166 ncg_cp(card, fo[6], fl[6], znd)
167 ncg_axis(ax, zax)
168 let is_pr: i64 = ncg_eq(zst, "PROVEN" as *u8)
169 let is_pa: i64 = ncg_eq(zst, "PARTIAL" as *u8)
170 let is_de: i64 = ncg_eq(zst, "DESIGN" as *u8)
171 let is_nc: i64 = ncg_eq(zst, "NEGCTL" as *u8)
172 let is_na: i64 = ncg_eq(zst, "NEGCTL_ABSENT" as *u8)
173 if is_pr == 1 { cnt[1] = cnt[1] + 1 }
174 if is_pa == 1 { cnt[2] = cnt[2] + 1 }
175 if is_de == 1 { cnt[3] = cnt[3] + 1 }
176 if is_nc == 1 { cnt[4] = cnt[4] + 1 }
177 if is_na == 1 { cnt[4] = cnt[4] + 1 }
178 var wneed: i64 = 0
179 if is_pr == 1 { wneed = 1 }
180 if is_pa == 1 { wneed = 1 }
181 if wneed == 1 {
182 let wn: i64 = ncg_read(zwit, wbuf, NCG_WIT_CAP)
183 var hit: i64 = 0
184 if wn > 0 { hit = ncg_find(wbuf, wn, znd) }
185 ncg_name(zname, zid, "grounded: witness organ exists + needle found" as *u8)
186 gv_check(zname, hit, ctr)
187 }
188 if is_nc == 1 {
189 let wn2: i64 = ncg_read(zwit, wbuf, NCG_WIT_CAP)
190 var pass2: i64 = 0
191 if wn2 > 0 { let f2: i64 = ncg_find(wbuf, wn2, znd); if f2 == 0 { pass2 = 1 } }
192 ncg_name(zname, zid, "negctl: fabricated needle ABSENT from witness" as *u8)
193 gv_check(zname, pass2, ctr)
194 }
195 if is_na == 1 {
196 let wn3: i64 = ncg_read(zwit, wbuf, NCG_WIT_CAP)
197 var pass3: i64 = 0
198 if wn3 < 0 { pass3 = 1 }
199 ncg_name(zname, zid, "negctl: phantom witness file ABSENT" as *u8)
200 gv_check(zname, pass3, ctr)
201 }
202 }
203 }
204 pos = e + 1
205 }
206 var s1: i64 = 0
207 if cnt[0] >= 12 { s1 = 1 }
208 gv_check("T-struct rows >= 12" as *u8, s1, ctr)
209 var s2: i64 = 0
210 if cnt[1] >= 6 { s2 = 1 }
211 gv_check("T-struct PROVEN clauses >= 6" as *u8, s2, ctr)
212 var s3: i64 = 0
213 if cnt[3] >= 1 { s3 = 1 }
214 gv_check("T-struct honesty: >=1 DESIGN gap declared" as *u8, s3, ctr)
215 var s4: i64 = 0
216 if cnt[4] >= 2 { s4 = 1 }
217 gv_check("T-struct >=2 negative-control teeth" as *u8, s4, ctr)
218 var s5: i64 = 0
219 let axsum: i64 = ax[0] + ax[1] + ax[2] + ax[3] + ax[4] + ax[5]
220 if axsum == 6 { s5 = 1 }
221 gv_check("T-struct all 6 doctrine axes present (exec authority data process syscall-shape determinism)" as *u8, s5, ctr)
222 var s6: i64 = 0
223 if cnt[5] == 0 { s6 = 1 }
224 gv_check("T-struct zero malformed rows" as *u8, s6, ctr)
225 gv_puts(" counts: rows=" as *u8)
226 gv_num(cnt[0])
227 gv_puts(" proven=" as *u8)
228 gv_num(cnt[1])
229 gv_puts(" partial=" as *u8)
230 gv_num(cnt[2])
231 gv_puts(" design=" as *u8)
232 gv_num(cnt[3])
233 gv_puts(" negctl=" as *u8)
234 gv_num(cnt[4])
235 gv_puts("\n" as *u8)
236 let rc: i64 = gv_verdict("NATIVE-CONTRACT-V1" as *u8, ctr, "R-N0 card grounded in live organs; gaps DECLARED + filed" as *u8)
237 sys_exit(rc)
238 return 0
239}