code wiki / _hdl_build / nx_cost_federation_test.nx

nx_cost_federation_test.nx source

↩ module page · 87 lines · 4791 B

1// nx_cost_federation_test.nx -- how the cost database GROWS AND GROWS, opt-in. 2// 3// The seed lands on a chip, measures it (nx_self_measure_cost), and -- ONLY IF the 4// user opts in -- contributes that profile back to the shared database, through the 5// SAME germination trust gate the seed uses for code: integrity hash + sanity proof 6// + crew-council governance. So: 7// * opted-in + sane -> ADMITTED, the DB gains a real profile for that chip 8// * poisoned (mul<add) -> REJECTED, the shared costs cannot be corrupted 9// * opted-OUT -> stays LOCAL, never leaves the node (consent honored) 10// Across a fleet, the DB grows to characterize every chip Nishi runs on -- our own 11// uops.info, federated, that uops.info itself can never have (sovereign cores, 12// sensors, damaged units). The richer it gets, the better every node optimizes. 13// 14// Known answer: opted-in sane profile admitted (DB grows), poisoned rejected, 15// opt-out kept local -> exit 0. 16 17import "nx_self_measure_cost.nx" 18import "nx_crew_council.nx" 19 20func cf_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 21func cf_num(v: i64) -> i64 { 22 let b: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m } 23 let t: *u8 = sys_mmap(28); var k: i64 = 0 24 if m == 0 { t[0] = 48; k = 1 } 25 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 } 26 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 27 sys_write(1, b, k); return 0 28} 29 30// contribute a measured profile to the shared DB. consent=0 -> stays local. Returns 31// 1 if ADMITTED (DB grew), 0 otherwise. Governed by hash + sanity + council. 32func cf_contribute(prof: *i64, consent: i64, db_size: *i64, a: *CrewAction, why: *i64) -> i64 { 33 if consent == 0 { return 0 } // opt-out: never leaves the node 34 let sane: i64 = sm_sane(prof) // the Engineer leg (measurement valid?) 35 let h: i64 = sm_hash(prof, 3) // integrity key (unused result, but computed) 36 if h == 0 { return 0 } 37 cc_set(a, "contribute measured cost profile" as *u8, sane, 1, 1, 1, 1) 38 let vd: i64 = cc_council(a, why) 39 if vd == CC_ACT { db_size[0] = db_size[0] + 1; return 1 } 40 return 0 41} 42 43func main() -> i64 { 44 cf_puts("=== federated, opt-in, self-growing cost database ===\n" as *u8) 45 let a: *CrewAction = sys_mmap(64) as *CrewAction 46 let why: *i64 = sys_mmap(8) as *i64 47 48 // 1. SELF-MEASURE this chip. 49 let prof: *i64 = sys_mmap(8 * 4) as *i64 50 sm_profile(prof) 51 cf_puts(" measured THIS chip (cycles/op, add=100): add=" as *u8); cf_num(prof[0]) 52 cf_puts(" mul=" as *u8); cf_num(prof[1]); cf_puts(" shl=" as *u8); cf_num(prof[2]); cf_puts("\n" as *u8) 53 54 // 2. shared DB seeded with the uops.info uarchs (Skylake + small core) = 2 chips. 55 let db: *i64 = sys_mmap(8) as *i64 56 db[0] = 2 57 cf_puts(" shared DB starts with " as *u8); cf_num(db[0]); cf_puts(" chips (seeded from uops.info)\n" as *u8) 58 59 // 3. user OPTS IN -> contribute this real measurement. 60 let r1: i64 = cf_contribute(prof, 1, db, a, why) 61 cf_puts(" opt-in contribute (measured chip) : " as *u8) 62 if r1 == 1 { cf_puts("ADMITTED -> DB now " as *u8); cf_num(db[0]); cf_puts(" chips\n" as *u8) } else { cf_puts("rejected\n" as *u8) } 63 64 // 4. a POISONED profile (claims mul cheaper than add -- impossible) is rejected. 65 let bad: *i64 = sys_mmap(8 * 4) as *i64 66 bad[0] = 100; bad[1] = 40; bad[2] = 70 67 let r2: i64 = cf_contribute(bad, 1, db, a, why) 68 cf_puts(" poisoned contribute (mul<add) : " as *u8) 69 if r2 == 1 { cf_puts("ADMITTED (BAD!)\n" as *u8) } else { cf_puts("REJECTED -> DB still " as *u8); cf_num(db[0]); cf_puts(" (uncorrupted)\n" as *u8) } 70 71 // 5. user OPTS OUT -> the measurement stays on the node, never contributed. 72 let r3: i64 = cf_contribute(prof, 0, db, a, why) 73 cf_puts(" opt-out (consent=0) : " as *u8) 74 if r3 == 0 { cf_puts("kept LOCAL -> DB still " as *u8); cf_num(db[0]); cf_puts(" (consent honored)\n" as *u8) } else { cf_puts("LEAKED (BAD!)\n" as *u8) } 75 76 cf_puts("----------------------------------------------------------------\n" as *u8) 77 cf_puts(" every opted-in node grows the DB with its REAL chip; poison can't enter;\n" as *u8) 78 cf_puts(" opt-out stays private. The more nodes, the better every node optimizes.\n" as *u8) 79 80 // GATE: opted-in sane admitted (DB 2->3); poison rejected; opt-out local. 81 if r1 != 1 { sys_exit(1); return 1 } 82 if db[0] != 3 { sys_exit(2); return 2 } // grew by exactly one (the real chip) 83 if r2 != 0 { sys_exit(3); return 3 } // poison must be rejected 84 if r3 != 0 { sys_exit(4); return 4 } // opt-out must not contribute 85 sys_exit(0) 86 return 0 87}