code wiki / _hdl_build / nx_ingest_foundation.nx
nx_ingest_foundation.nx source
↩ module page · 142 lines · 7223 B
1// nx_ingest_foundation.nx -- REUSABLE foundationing library (rule-of-three extraction: nx_codata_foundation
2// and nx_isotope_foundation were near-identical clones). One function FOUNDATIONS any ingested external
3// dataset per the operator law "if we consume we need to trace and build its lineage properly back to god":
4// (1) CONTENT-ADDRESS -> nx_canon_cid cid_of (nxc1-... sha256 identity, tamper-evident).
5// (2) SIGNED PROVENANCE -> nx_asset_provenance: ed25519 "downloaded" credential binding entity=CID + agent
6// + source-URL + fetch-date; deny-by-default verify; 3 liar-kills (flip / wrong-key / re-bind).
7// (3) LINEAGE TO GOD -> external data enters ONLY via the sovereign researcher (UNI-WIRE); walk
8// genesis_lineage from UNI-WIRE to ORIGIN(god) and assert it reaches god.
9// GREEN (returns 1) iff verify ALLOWs, all 3 tampers REJECT, and the transport lineage reaches god. Writes
10// the sovereign NXPC1 credential blob + a log line tagged by the caller. No main -- pure library. license_tier: ORIGINAL
11import "nx_syscalls.nx"
12import "nx_canon_cid.nx"
13import "nx_asset_provenance.nx"
14import "nx_ed25519_signature.nx"
15const IF_MAGIC_262144: i64 = 262144
16const IF_MAGIC_8192: i64 = 8192
17const IF_MAGIC_131072: i64 = 131072
18
19const IF_LINEAGE: *u8 = "knowledge/registry/genesis_lineage.tsv"
20
21func if_w(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 }
22func if_wn(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(16); var m: i64=v; let t: *u8=sys_mmap(16); 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{b[i]=t[k-1-i];i=i+1}; sys_write(fd,b,k); return 0 }
23
24func if_read(path: *u8, buf: *u8, cap: i64) -> i64 {
25 let fd: i64 = sys_openat_rd(path)
26 if fd < 0 { return 0 }
27 var n: i64 = 0
28 var r: i64 = sys_read(fd, buf, cap - 1)
29 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } }
30 sys_close(fd); buf[n] = 0 as u8
31 return n
32}
33func if_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 }
34func if_field(buf: *u8, ls: i64, le: i64, col: i64, dst: *u8) -> i64 {
35 var c: i64=0; var p: i64=ls; var k: i64=0
36 while p < le {
37 if buf[p]==(9 as u8) { if c==col { dst[k]=0 as u8; return k } c=c+1; if c==col { k=0 } }
38 else { if c==col { if buf[p]!=(13 as u8) { dst[k]=buf[p]; k=k+1 } } }
39 p = p + 1
40 }
41 dst[k] = 0 as u8
42 return k
43}
44func if_parent(buf: *u8, n: i64, id: *u8, par: *u8) -> i64 {
45 let f0: *u8 = sys_mmap(128)
46 var ls: i64=0; var i: i64=0
47 while i <= n {
48 var eol: i64=0
49 if i==n { eol=1 } else { if buf[i]==(10 as u8) { eol=1 } }
50 if eol==1 {
51 if i>ls { if buf[ls]!=(35 as u8) {
52 if_field(buf, ls, i, 0, f0)
53 if if_streq(f0, id)==1 { if_field(buf, ls, i, 2, par); return 1 }
54 } }
55 ls = i+1
56 }
57 i = i+1
58 }
59 return 0
60}
61func if_to_god(buf: *u8, n: i64, start: *u8) -> i64 {
62 let cur: *u8 = sys_mmap(128); let par: *u8 = sys_mmap(128)
63 var z: i64=0; while start[z]!=(0 as u8){cur[z]=start[z];z=z+1} cur[z]=0 as u8
64 var hops: i64=0
65 while hops < 64 {
66 if if_streq(cur, "ORIGIN" as *u8)==1 { return hops }
67 if if_parent(buf, n, cur, par) != 1 { return 0 - 1 }
68 z=0; while par[z]!=(0 as u8){cur[z]=par[z];z=z+1} cur[z]=0 as u8
69 hops = hops + 1
70 }
71 return 0 - 1
72}
73
74// FOUNDATION an ingested dataset. Returns 1 GREEN / 0 RED. cid_out (>=128B) is filled with the data's CID.
75func ingest_foundation(raw_path: *u8, source: *u8, date: *u8, agent: *u8, cred_path: *u8, log_path: *u8, logtag: *u8, cid_out: *u8) -> i64 {
76 let raw: *u8 = sys_mmap(IF_MAGIC_262144)
77 let rn: i64 = if_read(raw_path, raw, IF_MAGIC_262144)
78 if rn <= 0 { if_w(2, logtag); if_w(2, " verdict=RED reason=no-raw-data\n" as *u8); return 0 }
79
80 cid_of(raw, rn, cid_out)
81
82 let assertion: *u8 = sys_mmap(IF_MAGIC_8192)
83 let alen: i64 = prov_make(cid_out, prov_class_downloaded(), agent, "" as *u8, "" as *u8, source, date, assertion)
84 let priv: *u8 = sys_mmap(32); var i: i64=0; while i<32 { priv[i]=((0x4E + i*7) & 0xff) as u8; i=i+1 }
85 let pub: *u8 = sys_mmap(32); ed25519_pub_from_priv(priv, pub)
86 let cred: *u8 = sys_mmap(IF_MAGIC_8192)
87 let clen: i64 = prov_sign(assertion, alen, priv, cred)
88
89 let v_ok: i64 = prov_verify(cred, clen, pub, cid_out)
90 let saved: u8 = cred[PROV_HDR + 3]; cred[PROV_HDR + 3] = (saved ^ (1 as u8)) as u8
91 let t_flip: i64 = prov_verify(cred, clen, pub, cid_out)
92 cred[PROV_HDR + 3] = saved
93 let pub2: *u8 = sys_mmap(32); i=0; while i<32 { pub2[i]=pub[i]; i=i+1 } pub2[0] = (pub2[0] ^ (1 as u8)) as u8
94 let t_key: i64 = prov_verify(cred, clen, pub2, cid_out)
95 let badcid: *u8 = sys_mmap(128); i=0; while cid_out[i]!=(0 as u8){badcid[i]=cid_out[i];i=i+1} badcid[i]=0 as u8
96 if badcid[8]==(97 as u8) { badcid[8]=98 as u8 } else { badcid[8]=97 as u8 }
97 let t_bind: i64 = prov_verify(cred, clen, pub, badcid)
98
99 let lin: *u8 = sys_mmap(IF_MAGIC_131072); let ln: i64 = if_read(IF_LINEAGE, lin, IF_MAGIC_131072)
100 let hops: i64 = if_to_god(lin, ln, "UNI-WIRE" as *u8)
101
102 var ok: i64 = 0
103 if v_ok==1 { if t_flip==0 { if t_key==0 { if t_bind==0 { if hops>0 { ok=1 } } } } }
104
105 // THE CREDENTIAL IS THE DELIVERABLE, AND THIS WRITE'S RESULT USED TO BE DISCARDED.
106 // `ok` above is computed purely from an IN-MEMORY sign+verify, so a doc whose credential never
107 // reached disk still returned GREEN. Every consumer keys off the FILE -- the librarian reviewer
108 // tests `<creddir>/<name>.nxpc1` exists, and lf_one's resume path opens exactly that -- so a
109 // signature nobody can read is not a foundation. Measured 2026-08-01: 146 docs GREEN, 0 credentials.
110 // A WRITER IS ONLY TESTED BY A READ-BACK: write ALL of it, close, then RE-OPEN to confirm.
111 var wrote: i64 = 0
112 let cfd: i64 = sys_openat_wr(cred_path, 0x1a4)
113 if cfd >= 0 {
114 let wn: i64 = sys_write(cfd, cred, clen)
115 sys_close(cfd)
116 if wn == clen {
117 let rfd: i64 = sys_openat_rd(cred_path)
118 if rfd >= 0 { sys_close(rfd); wrote = 1 }
119 }
120 }
121 if wrote == 0 { ok = 0 }
122
123 var p2: i64=0
124 while p2 < 2 {
125 var fd: i64 = 1
126 if p2==1 { fd = sys_openat_append(log_path, 0x1a4) }
127 if fd >= 0 {
128 if_w(fd, logtag); if_w(fd, " cid=" as *u8); if_w(fd, cid_out)
129 if_w(fd, " bytes=" as *u8); if_wn(fd, rn)
130 if_w(fd, " verify=" as *u8); if_wn(fd, v_ok)
131 if_w(fd, " reject_flip=" as *u8); if_wn(fd, t_flip)
132 if_w(fd, " reject_wrongkey=" as *u8); if_wn(fd, t_key)
133 if_w(fd, " reject_rebind=" as *u8); if_wn(fd, t_bind)
134 if_w(fd, " lineage_hops_to_god=" as *u8); if_wn(fd, hops)
135 if_w(fd, " (UNI-WIRE->...->ORIGIN) cred_written=" as *u8); if_wn(fd, wrote)
136 if ok==1 { if_w(fd, " verdict=GREEN\n" as *u8) } else { if_w(fd, " verdict=RED\n" as *u8) }
137 if p2==1 { sys_close(fd) }
138 }
139 p2 = p2 + 1
140 }
141 return ok
142}