code wiki / _hdl_build / nx_fed_registry.nx
nx_fed_registry.nx source
↩ module page · 192 lines · 10679 B
1// nx_fed_registry.nx -- FEDERATED DRIVER REGISTRY (X-DRV-F2).
2//
3// The main system aggregates the CONSENTING spores' signed telemetry (X-DRV-F1) and REFINES the
4// driver registry: for each device it picks the config that MEASURABLY WINS (fewest emu-steps per
5// I/O) across all reporting nodes, and writes that back as the recommended config -> every node
6// adopting the federated registry gets the best-known driver = the network effect.
7//
8// Each node report (the federated inbox, default knowledge/registry/federated_reports.tsv):
9// deviceid <TAB> config_spec <TAB> steps <TAB> node_pubkey_hex <TAB> sig_hex
10// where sig = ed25519 over the EXACT payload "fedreport deviceid=<id> config=<spec> steps=<n>".
11// A report whose signature does NOT verify is REJECTED (consent + authenticity enforced -- only
12// genuinely-signed contributions count; you cannot poison the registry with unsigned data).
13//
14// nx_fed_registry [reports_path]
15// -> knowledge/registry/driver_registry_federated.tsv (deviceid -> winning config_spec + steps)
16// -> knowledge/status/fed_registry.log (FEDREG: per-device winner, nodes-aggregated, rejected)
17// Sovereign (syscalls + ed25519), no gcc/.sh. license_tier: ORIGINAL
18import "nx_syscalls.nx"
19import "nx_ed25519_signature.nx"
20const FR_MAGIC_262144: i64 = 262144
21const FR_MAGIC_2048: i64 = 2048
22
23const FR_OUT: *u8 = "knowledge/registry/driver_registry_federated.tsv"
24const FR_LOG: *u8 = "knowledge/status/fed_registry.log"
25const FR_MAXDEV: i64 = 32
26
27func fr_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
28func fr_fp(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 }
29func fr_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;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 }
30func fr_n(v: i64) -> i64 { fr_fn(1, v); return 0 }
31
32func fr_read(path: *u8, buf: *u8, cap: i64) -> i64 {
33 let fd: i64 = sys_openat_rd(path)
34 if fd < 0 { return 0 - 1 }
35 var n: i64 = 0
36 var go: i64 = 1
37 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap - 1 { go = 0 } }
38 sys_close(fd)
39 return n
40}
41
42func fr_field(buf: *u8, ls: i64, le: i64, idx: i64) -> i64 {
43 if idx == 0 { return ls }
44 var f: i64 = 0; var p: i64 = ls
45 while p < le { if buf[p] == (9 as u8) { f = f + 1; if f == idx { return p + 1 } } p = p + 1 }
46 return 0 - 1
47}
48func fr_field_end(buf: *u8, s: i64, le: i64) -> i64 { var q: i64 = s; while q < le { if buf[q] == (9 as u8) { return q } q = q + 1 } return le }
49
50func fr_parse_num(buf: *u8, p: i64, le: i64) -> i64 {
51 var q: i64 = p; var val: i64 = 0
52 if q + 1 < le { if buf[q] == (48 as u8) { if buf[q+1] == (120 as u8) {
53 q = q + 2
54 var go: i64 = 1
55 while go == 1 { if q >= le { go = 0 } else { let c: i64 = buf[q] as i64; var d: i64 = 0-1; if c>=48 { if c<=57 { d=c-48 } } if c>=97 { if c<=102 { d=c-87 } } if c>=65 { if c<=70 { d=c-55 } } if d<0 { go=0 } else { val=val*16+d; q=q+1 } } }
56 return val
57 }}}
58 var go2: i64 = 1
59 while go2 == 1 { if q >= le { go2 = 0 } else { let c: i64 = buf[q] as i64; if c>=48 { if c<=57 { val=val*10+(c-48); q=q+1 } else { go2=0 } } else { go2=0 } } }
60 return val
61}
62
63func fr_hexdec(src: *u8, hexlen: i64, out: *u8) -> i64 {
64 if (hexlen % 2) != 0 { return 0 - 1 }
65 var i: i64 = 0
66 while i < hexlen / 2 {
67 let c0: i64 = src[i*2] as i64; let c1: i64 = src[i*2+1] as i64
68 var h0: i64 = 0-1; var h1: i64 = 0-1
69 if c0>=48 { if c0<=57 { h0=c0-48 } } if c0>=97 { if c0<=102 { h0=c0-87 } } if c0>=65 { if c0<=70 { h0=c0-55 } }
70 if c1>=48 { if c1<=57 { h1=c1-48 } } if c1>=97 { if c1<=102 { h1=c1-87 } } if c1>=65 { if c1<=70 { h1=c1-55 } }
71 if h0<0 { return 0-1 } if h1<0 { return 0-1 }
72 out[i] = ((h0<<4)|h1) as u8; i = i + 1
73 }
74 return hexlen / 2
75}
76
77func fr_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } return off + i }
78func fr_catn(dst: *u8, off: i64, v: i64) -> i64 { let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; var k: i64=0; if m==0{t[0]=48;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{dst[off+i]=t[k-1-i];i=i+1}; return off+k }
79func fr_catbytes(dst: *u8, off: i64, src: *u8, n: i64) -> i64 { var i: i64=0; while i<n { dst[off+i]=src[i]; i=i+1 } return off+n }
80
81func main(argc: i64, argv: *i64) -> i64 {
82 var reports_path: *u8 = "knowledge/registry/federated_reports.tsv" as *u8
83 if argc >= 2 { reports_path = argv[1] as *u8 }
84
85 fr_p("=== nx_fed_registry: aggregate consenting nodes' signed telemetry -> refine driver-specs ===\n" as *u8)
86
87 let rb: *u8 = sys_mmap(FR_MAGIC_262144)
88 let rn: i64 = fr_read(reports_path, rb, FR_MAGIC_262144)
89 if rn <= 0 { fr_p("FEDREG verdict=RED reason=no-reports\n" as *u8); sys_exit(1); return 1 }
90
91 // per-device aggregation: deviceid -> (best_steps, best_config_start/len in a config buffer)
92 let dev_id: *i64 = sys_mmap(8 * FR_MAXDEV) as *i64
93 let dev_steps: *i64 = sys_mmap(8 * FR_MAXDEV) as *i64
94 let dev_cfg: *u8 = sys_mmap(FR_MAXDEV * 512) // 512 bytes per winning config path
95 var ndev: i64 = 0
96 var accepted: i64 = 0
97 var rejected: i64 = 0
98
99 let payload: *u8 = sys_mmap(FR_MAGIC_2048)
100 let sig: *u8 = sys_mmap(64)
101 let pub: *u8 = sys_mmap(32)
102 let cfgbuf: *u8 = sys_mmap(512)
103
104 var ls: i64 = 0
105 while ls < rn {
106 var le: i64 = ls
107 var sc: i64 = 1
108 while sc == 1 { if le >= rn { sc = 0 } else { if rb[le] == (10 as u8) { sc = 0 } else { le = le + 1 } } }
109 if rb[ls] != (35 as u8) { if le > ls {
110 let f0: i64 = fr_field(rb, ls, le, 0) // deviceid
111 let f1: i64 = fr_field(rb, ls, le, 1) // config spec
112 let f2: i64 = fr_field(rb, ls, le, 2) // steps
113 let f3: i64 = fr_field(rb, ls, le, 3) // pubkey hex
114 let f4: i64 = fr_field(rb, ls, le, 4) // sig hex
115 if f0>=0 { if f1>=0 { if f2>=0 { if f3>=0 { if f4>=0 {
116 let did: i64 = fr_parse_num(rb, f0, le)
117 let f1e: i64 = fr_field_end(rb, f1, le)
118 let cfglen: i64 = f1e - f1
119 var ci: i64 = 0; while ci < cfglen { cfgbuf[ci] = rb[f1+ci]; ci = ci + 1 } cfgbuf[cfglen] = 0 as u8
120 let steps: i64 = fr_parse_num(rb, f2, le)
121 let f3e: i64 = fr_field_end(rb, f3, le)
122 let f4e: i64 = fr_field_end(rb, f4, le)
123 // rebuild the EXACT signed payload: "fedreport deviceid=<id> config=<spec> steps=<n>"
124 var po: i64 = 0
125 po = fr_cat(payload, po, "fedreport deviceid=" as *u8); po = fr_catn(payload, po, did)
126 po = fr_cat(payload, po, " config=" as *u8); po = fr_catbytes(payload, po, cfgbuf, cfglen)
127 po = fr_cat(payload, po, " steps=" as *u8); po = fr_catn(payload, po, steps)
128 // decode pub + sig, verify
129 var ok: i64 = 0
130 if (f3e - f3) == 64 { if (f4e - f4) == 128 {
131 fr_hexdec((rb as i64 + f3) as *u8, 64, pub)
132 fr_hexdec((rb as i64 + f4) as *u8, 128, sig)
133 if ed25519_verify_full(pub, payload, po, sig) == NX_ED25519_SIG_OK { ok = 1 }
134 }}
135 if ok == 0 {
136 rejected = rejected + 1
137 fr_p(" REJECTED (bad signature) deviceid=" as *u8); fr_n(did); fr_p(" config=" as *u8); fr_p(cfgbuf); fr_p("\n" as *u8)
138 } else {
139 accepted = accepted + 1
140 // aggregate: min-steps wins per device
141 var slot: i64 = 0 - 1
142 var di: i64 = 0
143 while di < ndev { if dev_id[di] == did { slot = di } di = di + 1 }
144 if slot < 0 { if ndev < FR_MAXDEV {
145 slot = ndev; dev_id[slot] = did; dev_steps[slot] = steps
146 var k: i64 = 0; while k <= cfglen { dev_cfg[slot*512 + k] = cfgbuf[k]; k = k + 1 }
147 ndev = ndev + 1
148 } } else {
149 if steps < dev_steps[slot] {
150 dev_steps[slot] = steps
151 var k2: i64 = 0; while k2 <= cfglen { dev_cfg[slot*512 + k2] = cfgbuf[k2]; k2 = k2 + 1 }
152 }
153 }
154 fr_p(" accepted deviceid=" as *u8); fr_n(did); fr_p(" config=" as *u8); fr_p(cfgbuf); fr_p(" steps=" as *u8); fr_n(steps); fr_p("\n" as *u8)
155 }
156 }}}}}
157 }}
158 ls = le + 1
159 }
160
161 // write the refined federated registry: deviceid -> winning config + steps
162 let ofd: i64 = sys_openat_wr(FR_OUT, 0x1a4)
163 if ofd >= 0 {
164 fr_fp(ofd, "# driver_registry_federated.tsv -- AUTHORED BY nx_fed_registry. Per device the config\n" as *u8)
165 fr_fp(ofd, "# with the fewest measured emu-steps across CONSENTING signed node reports (the winner\n" as *u8)
166 fr_fp(ofd, "# flows back to every node). columns: deviceid\twinning_config_spec\tbest_steps\tnodes\n" as *u8)
167 var di: i64 = 0
168 while di < ndev {
169 fr_fn(ofd, dev_id[di]); fr_fp(ofd, "\t" as *u8)
170 fr_fp(ofd, (dev_cfg as i64 + di*512) as *u8); fr_fp(ofd, "\t" as *u8)
171 fr_fn(ofd, dev_steps[di]); fr_fp(ofd, "\n" as *u8)
172 di = di + 1
173 }
174 sys_close(ofd)
175 }
176
177 let lf: i64 = sys_openat_append(FR_LOG, 0x1a4)
178 if lf >= 0 {
179 fr_fp(lf, "FEDREG verdict=GREEN devices=" as *u8); fr_fn(lf, ndev)
180 fr_fp(lf, " accepted=" as *u8); fr_fn(lf, accepted)
181 fr_fp(lf, " rejected=" as *u8); fr_fn(lf, rejected)
182 var di2: i64 = 0
183 while di2 < ndev { fr_fp(lf, " winner[dev" as *u8); fr_fn(lf, dev_id[di2]); fr_fp(lf, "]=steps" as *u8); fr_fn(lf, dev_steps[di2]); di2 = di2 + 1 }
184 fr_fp(lf, " epoch=" as *u8); fr_fn(lf, sys_now_realtime_sec()); fr_fp(lf, "\n" as *u8); sys_close(lf)
185 }
186
187 fr_p("FEDREG summary devices=" as *u8); fr_n(ndev); fr_p(" accepted=" as *u8); fr_n(accepted); fr_p(" rejected=" as *u8); fr_n(rejected); fr_p("\n" as *u8)
188 var di3: i64 = 0
189 while di3 < ndev { fr_p(" WINNER deviceid=" as *u8); fr_n(dev_id[di3]); fr_p(" config=" as *u8); fr_p((dev_cfg as i64 + di3*512) as *u8); fr_p(" steps=" as *u8); fr_n(dev_steps[di3]); fr_p("\n" as *u8); di3 = di3 + 1 }
190 sys_exit(0)
191 return 0
192}