code wiki / (root) / nx_optimizer_milestone_attest.nx

nx_optimizer_milestone_attest.nx source

↩ module page · 130 lines · 6519 B

1// nx_optimizer_milestone_attest.nx -- substrate-native attribution 2// for the alloca-load-fold peephole shipped 2026-05-20. 3// 4// REPLACES "the right commit message" with a hash-chained attestation 5// that cannot be silently overwritten by parallel-agent `git add -A`. 6// 7// Per [[NISHI_STORAGE_SUBSTRATE_ROADMAP]] vs Git row of the EXCEED 8// matrix: native SHA-256 + L1 append-only journal + tamper-detect 9// chain. Git only has refs.log; we ship cryptographic linkage. 10// 11// What this program emits: 12// 13// 1. nx_blob_store_put(patched_codegen_source) -> hash A 14// content-address of nx_x86_64_ctx.nx after the peephole edit 15// 2. nx_blob_store_put(emitted_asm) -> hash B 16// content-address of peephole_tightloop.s the self-host produced 17// 3. nx_blob_store_put(attestation_payload) -> hash C 18// content-address of the metrics description 19// 4. nx_journal_log_append three entries (schema 20260520) -> seq 0,1,2 20// 5. nx_journal_log_verify_chain -> must return -1 (chain intact) 21// 6. nx_journal_log_latest_hash -> the chain head; the canonical 22// identity of this optimizer milestone, independent of git 23// 24// To re-verify any time later: rerun this program against the same 25// inputs; identical hashes mean nothing was tampered. 26 27import "nx_syscalls.nx" 28import "nx_blob_store.nx" 29import "nx_journal_log.nx" 30import "nx_hex.nx" 31const K_MAGIC_20260520: i64 = 20260520 32 33func _puts(fd: i64, s: *u8) -> i64 { 34 var n: i64 = 0 35 while s[n] != 0 { n = n + 1 } 36 sys_write(fd, s, n) 37 return 0 38} 39 40func _put_hash(fd: i64, label: *u8, w0: i64, w1: i64, w2: i64, w3: i64) -> i64 { 41 _puts(fd, label) 42 nx_hex_i64(fd, w0) 43 nx_hex_i64(fd, w1) 44 nx_hex_i64(fd, w2) 45 nx_hex_i64(fd, w3) 46 _puts(fd, "\n" as *u8) 47 return 0 48} 49 50func main() -> i64 { 51 let stdout: i64 = 1 52 53 let store: *NxBlobStore = nx_blob_store_new() 54 let j: *NxJournalLog = nx_journal_log_new(store) 55 if nx_journal_log_is_valid(j) != 1 { return 1 } 56 57 // ---- Step 1: blob the patched codegen source ---- 58 let len_src: *i64 = (sys_mmap(8)) as *i64 59 let src: *u8 = sys_read_file("nxc2/runtime/nx_x86_64_ctx.nx" as *u8, len_src) 60 if *len_src <= 0 { return 2 } 61 let hash_src: *NxBlobHash = nx_blob_hash_new() 62 let rc1: i64 = nx_blob_store_put(store, src, *len_src, hash_src) 63 if rc1 != NX_BLOB_OK { return 3 } 64 65 // ---- Step 2: blob the emitted asm ---- 66 let len_asm: *i64 = (sys_mmap(8)) as *i64 67 let asm_bytes: *u8 = sys_read_file("nxc2/_offc/peephole_tightloop.s" as *u8, len_asm) 68 if *len_asm <= 0 { return 4 } 69 let hash_asm: *NxBlobHash = nx_blob_hash_new() 70 let rc2: i64 = nx_blob_store_put(store, asm_bytes, *len_asm, hash_asm) 71 if rc2 != NX_BLOB_OK { return 5 } 72 73 // ---- Step 3: blob the attestation payload (machine-readable) ---- 74 // Format is line-oriented k=v plain text -- canonical bytes hash 75 // identically across machines / shells / locales. 76 let attest_src: *u8 = "schema=substrate_optimizer_milestone\nversion=1\ndate=2026-05-20\noptimizer=alloca_load_fold_peephole\nfile=nxc2/runtime/nx_x86_64_ctx.nx\nsites=x86ctx_load_value_v,x86ctx_emit_load,x86ctx_emit_store\ntransform=leaq_disp_rbp_reg_movq_paren_reg_reg2=>movq_disp_rbp_reg2\nasm_inner_loop_insns_before=24\nasm_inner_loop_insns_after=19\nasm_inner_loop_reduction_pct=21\nstabilizer_variants=6\nstabilizer_reps=4\nstabilizer_pre_mean_s=0.312\nstabilizer_post_mean_s=0.271\nstabilizer_mean_delta_pct=-13\npaired_variants_favoring_peephole=5_of_6\ndual_target_gate_pass_total=25_of_25\ndual_target_gate_regression=0\nbootstrap_path=off_c_self_host\nc_bootstrap_touched=0\ndecision_log_section=D014\ncomposes=feedback-never-touch-c-bootstrap-substrate-is-nishilang,project-off-c-step-1-self-host-compiles-overflow-target-2026-05-20,project-s-class-measurement-arc-2026-05-20,reference-berger-performance-matters-talk-archived\n" as *u8 77 var attest_len: i64 = 0 78 while attest_src[attest_len] != 0 { attest_len = attest_len + 1 } 79 let hash_attest: *NxBlobHash = nx_blob_hash_new() 80 let rc3: i64 = nx_blob_store_put(store, attest_src, attest_len, hash_attest) 81 if rc3 != NX_BLOB_OK { return 6 } 82 83 // ---- Step 4: chain via journal_log appends ---- 84 let SCHEMA_OPTIMIZER_MILESTONE: i64 = K_MAGIC_20260520 85 let seq_a: i64 = nx_journal_log_append(j, src, *len_src, SCHEMA_OPTIMIZER_MILESTONE) 86 if seq_a < 0 { return 7 } 87 let seq_b: i64 = nx_journal_log_append(j, asm_bytes, *len_asm, SCHEMA_OPTIMIZER_MILESTONE) 88 if seq_b < 0 { return 8 } 89 let seq_c: i64 = nx_journal_log_append(j, attest_src, attest_len, SCHEMA_OPTIMIZER_MILESTONE) 90 if seq_c < 0 { return 9 } 91 92 // ---- Step 5: verify the chain ---- 93 let v: i64 = nx_journal_log_verify_chain(j) 94 if v != -1 { return 10 } 95 96 // ---- Step 6: read the chain head (canonical milestone id) ---- 97 let lw0: *i64 = (sys_mmap(8)) as *i64 98 let lw1: *i64 = (sys_mmap(8)) as *i64 99 let lw2: *i64 = (sys_mmap(8)) as *i64 100 let lw3: *i64 = (sys_mmap(8)) as *i64 101 let rc_head: i64 = nx_journal_log_latest_hash(j, lw0, lw1, lw2, lw3) 102 if rc_head != NX_JOURNAL_OK { return 11 } 103 104 // ---- Step 7: print the attestation to stdout ---- 105 _puts(stdout, "=== Substrate-native optimizer-milestone attestation ===\n" as *u8) 106 _puts(stdout, "schema_id=20260520 (substrate_optimizer_milestone)\n" as *u8) 107 _puts(stdout, "milestone=alloca_load_fold_peephole\n" as *u8) 108 _puts(stdout, "date=2026-05-20\n" as *u8) 109 _puts(stdout, "n_entries=3 (codegen_source, emitted_asm, attestation_payload)\n" as *u8) 110 _puts(stdout, "blob_count=" as *u8) 111 nx_hex_i64(stdout, nx_blob_store_count(store)) 112 _puts(stdout, "\n" as *u8) 113 114 _put_hash(stdout, "blob_hash_codegen_source : 0x" as *u8, 115 hash_src.w0, hash_src.w1, hash_src.w2, hash_src.w3) 116 _put_hash(stdout, "blob_hash_emitted_asm : 0x" as *u8, 117 hash_asm.w0, hash_asm.w1, hash_asm.w2, hash_asm.w3) 118 _put_hash(stdout, "blob_hash_attestation : 0x" as *u8, 119 hash_attest.w0, hash_attest.w1, hash_attest.w2, hash_attest.w3) 120 _put_hash(stdout, "chain_head_hash : 0x" as *u8, 121 *lw0, *lw1, *lw2, *lw3) 122 123 _puts(stdout, "chain_verify=INTACT\n" as *u8) 124 _puts(stdout, "gate=PASS\n" as *u8) 125 _puts(stdout, "\nThe chain_head_hash above is the canonical milestone identity.\n" as *u8) 126 _puts(stdout, "Any future tamper of any entry breaks chain_verify.\n" as *u8) 127 _puts(stdout, "Re-run this program on the same inputs -> identical hashes.\n" as *u8) 128 129 return 0 130}