code wiki / _hdl_build / nx_hash_opt_register.nx

nx_hash_opt_register.nx source

↩ module page · 47 lines · 4129 B

1// nx_hash_opt_register.nx -- registers the SHA-1 + SHA-256 throughput optimization (the #1 2// torrent callout) via Librarian alloc + dual-write, gated by the RACI ingest path. Engineer 3// evidence = the byte-identical DIFFERENTIAL gates (golden roll over lengths 0..256) + the NIST 4// KAT + the piece-verify KAT all exit 0. This is a HEAL (the "slow is a bug" defect, fixed + 5// proven identical), not a new feature. license_tier: ORIGINAL 6import "nx_cap_register.nx" 7import "nx_capreg_librarian.nx" 8import "nx_syscalls.nx" 9 10func hor_run(path: *u8) -> i64 { 11 let pid: i64 = sys_fork() 12 if pid == 0 { 13 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 14 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 15 let argv: *i64 = sys_mmap(32) as *i64; argv[0] = path as i64; argv[1] = 0 16 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = 0 17 sys_execve(path, argv, envp); sys_exit(127) 18 } 19 let st: *i64 = sys_mmap(16) as *i64 20 sys_wait4(pid, st, 0) 21 return st[0] 22} 23 24func main() -> i64 { 25 var allok: i64 = 1 26 if hor_run("/tmp/nx_sha256_diff_probe.sov.elf" as *u8) != 0 { allok = 0 } 27 if hor_run("/tmp/nx_sha1_diff_probe.sov.elf" as *u8) != 0 { allok = 0 } 28 if hor_run("/tmp/nx_sha256_kat_test.sov.elf" as *u8) != 0 { allok = 0 } 29 if hor_run("/tmp/nx_piece_manager_test.sov.elf" as *u8) != 0 { allok = 0 } 30 cr_w(1, "ENGINEER: sha256-diff + sha1-diff + NIST-KAT + piece-verify all byte-identical exit-0 = " as *u8); cr_wn(1, allok); cr_w(1, "\n" as *u8) 31 32 let eng: i64 = ig_engineer(1, 1, 1, allok) 33 let dec: i64 = ig_decision(eng, ig_council(eng, 1, 1, 2), 1) 34 if dec != IG_INGEST { cr_w(1, "HELD\n" as *u8); sys_exit(1) } 35 if cr_can_register(4, 2, 6, dec) != 1 { cr_w(1, "REFUSED\n" as *u8); sys_exit(1) } 36 37 let lp: *u8 = "/tmp/nishi_cap_registry.log" as *u8 38 let jp: *u8 = "knowledge/status/cap_registry_durable.log" as *u8 39 40 let idx1: i64 = cl_next_idx(lp, jp) 41 let ok1: i64 = cl_register_dual(lp, jp, idx1, 4, 2, 42 "HEAL SHA-1 + SHA-256 THROUGHPUT ~4.4-5x, BYTE-IDENTICAL (arc=TORRENT; addresses the #1 measured callout from nx_hash_bench -- 'slow is a bug'). ROOT CAUSE localized by READING the source (not guessed) = O(n) SOURCE patterns, NOT a compiler mystery: sha256_k was a 64-way linear if-chain (~2080 cmp/block just for K); sha256 block bytes lived in 8 byte-packed fields read via linear scan; BOTH hashes did a per-BLOCK mmap SYSCALL for the message schedule (~16k syscalls/MB); rotr/rotl were function calls. FIX in place on the canonical modules (rule 3: rewrite w/ root cause, not patch): K materialized ONCE per ctx into an array (sha256_k stays the DRY constant source; if-chain runs 64x per HASH not per block) indexed only by PUBLIC round; contiguous O(1) block buffer; W schedule allocated ONCE per ctx + reused (zero per-block syscalls); rotr/rotl inlined. MEASURED sovereign nx_cc->nxasm: SHA-1 ~13->~57 MB/s (~4.4x), SHA-256 ~6->~30 MB/s (~5x). CORRECTNESS PROVEN AIRTIGHT (priority for SIL3 crypto): golden DIFFERENTIAL captured from the PRE-opt production hash over EVERY length 0..256 (all padding boundaries) -- SHA-256 roll=680824791, SHA-1 roll=196783082 -- reproduced IDENTICALLY post-opt = byte-for-byte identical; NIST 'abc' KAT + sha256/sha384 coexist + piece_manager SHA-1 verify all GREEN; diff probes are now PERMANENT regression gates asserting the golden. SAFETY: SIL3 constant_time_by_construction PRESERVED (all indices public, never secret); no_table_lookup note now has a public-indexed K array -> FLAGGED for operator/safety review (trivially revertible). HONEST REMAINING GAP (leaderboard col7 D->C): SHA-256 ~30 still ~2-3x below naive C, ~50x below SHA-NI -> next tier = sovereign CODEGEN (register alloc/spills, same lever as the LICM/codegen arc) or a SHA-NI nxasm path. Composes nx_sha1+nx_sha256; feeds nx_torrent_bench(334) col7 + nx_hash_bench(350)" as *u8) 43 if ok1 != 1 { cr_w(1, "DUAL-WRITE FAILED\n" as *u8); sys_exit(1) } 44 cr_w(1, "CAPREG idx=" as *u8); cr_wn(1, idx1); cr_w(1, " REGISTERED -- hashing ~4.4-5x byte-identical\n" as *u8) 45 sys_exit(0) 46 return 0 47}