code wiki / _hdl_build / nx_spore_min_test.nx

nx_spore_min_test.nx source

↩ module page · 68 lines · 3598 B

1// nx_spore_min_test.nx -- the minimal SPORE, and it MEASURES ITS OWN footprint. 2// The spore is the tiniest viable bootstrap: it carries just the trust core it 3// needs to safely pull + verify the first seed pieces (the FNV-1a piece hash = 4// germination integrity), and nothing else. Because a NishiLang binary is a 5// STATIC, libc-FREE, OS-runtime-FREE ELF -- just machine code + raw syscalls -- 6// the spore is KILOBYTES, not the megabytes a runtime-dependent stack needs. 7// THIS is what makes "a USB drive as a proper computer" real: the bootstrap owes 8// nothing to a host OS/runtime. 9// 10// The spore reads its OWN ELF size (/proc/self/exe, lseek SEEK_END) -- sovereign 11// self-awareness of its footprint -- and gates that it stays under budget. 12// Known answer: prints "spore=<bytes>" and exit 0 iff the trust core works AND 13// the footprint is tiny (< 16 KB). 14 15import "nx_spore_syscalls.nx" // minimal surface, not the full ~40-wrapper module 16 17// Budget from MEASURED reality (not a guess): with the full nx_syscalls module 18// linked (no DCE), the spore was 18904 B unstripped / 16968 B stripped, of which 19// ~6 KB was dead syscall wrappers. On the minimal surface it lands ~8 KB. 12 KB 20// gives honest headroom and still proves the point: kilobytes, not the MB-GB a 21// libc/runtime stack needs. Tighten this as nxasm/DCE shrink it further. 22const SPORE_BUDGET: i64 = 12288 // 12 KB ceiling on the bootstrap 23const FNV_OFFSET: i64 = 0 - 3750763034362895579 // 0xcbf29ce484222325 24const FNV_PRIME: i64 = 1099511628211 // 0x100000001b3 25const SEEK_END: i64 = 2 26 27func sp_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 28func sp_hash(s: *u8, len: i64) -> i64 { 29 var h: i64 = FNV_OFFSET 30 var i: i64 = 0 31 while i < len { h = h ^ (s[i] as i64); h = h * FNV_PRIME; i = i + 1 } 32 return h 33} 34func _emit(name: *u8, v: i64) -> i64 { 35 var n: i64 = 0; while name[n] != (0 as u8) { n = n + 1 } sys_write(1, name, n) 36 let b: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m } 37 let t: *u8 = sys_mmap(28); var k: i64 = 0 38 if m == 0 { t[0] = 48; k = 1 } 39 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 } 40 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 41 b[k] = 10; sys_write(1, b, k + 1); return 0 42} 43 44func main() -> i64 { 45 // 1. the spore's TRUST CORE works: it can hash + verify a piece (deterministic; 46 // a corrupted copy would hash differently -> the spore rejects it). 47 let good: *u8 = "first-seed-capability-piece" as *u8 48 let hg: i64 = sp_hash(good, sp_slen(good)) 49 let again: i64 = sp_hash(good, sp_slen(good)) 50 if hg != again { sys_exit(1); return 1 } 51 let bad: *u8 = "first-seed-capability-Piece" as *u8 // one byte differs 52 if sp_hash(bad, sp_slen(bad)) == hg { sys_exit(2); return 2 } // must detect corruption 53 54 // 2. SELF-MEASURE the spore footprint (read its own ELF size, sovereign). 55 let fd: i64 = sys_openat_rd("/proc/self/exe" as *u8) 56 if fd < 0 { sys_exit(3); return 3 } 57 let size: i64 = sys_lseek(fd, 0, SEEK_END) 58 sys_close(fd) 59 60 _emit("spore=" as *u8, size) 61 sys_write(1, " bytes -- static, no libc, no runtime, no OS dependency.\n" as *u8, 56) 62 sys_write(1, " a proper computer's bootstrap on a USB stick (a runtime stack is MB-GB).\n" as *u8, 73) 63 64 // 3. GATE: the trust core proved out AND the footprint is tiny. 65 if size <= 0 { sys_exit(4); return 4 } 66 if size > SPORE_BUDGET { sys_exit(5); return 5 } 67 sys_exit(0); return 0 68}