code wiki / _hdl_build / nx_mirror_orchestrator_gate.nx

nx_mirror_orchestrator_gate.nx source

↩ module page · 127 lines · 8616 B

1// nx_mirror_orchestrator_gate.nx -- GATE for nx_mirror_orchestrator (rung-1: the deterministic, NEVER-BRICK core 2// of "mirror a published open artifact -> NAS + receipt", composing the shipped sovereign primitives). Proves, 3// with NO live network and NO NAS write: 4// T1 SCHEME RESOLVE -- http/https->HTTP, torrent/magnet->TORRENT, unknown->REJECT (fail-closed transport). 5// T2 BYTE-EXACT VERIFY-- sha256("abc") == the FIPS-180-4/NIST vector; mo_verify PASSES the true digest and 6// FAILS a flipped one (never-store-corrupt: a byte-flipped/truncated/MITM'd file is rejected). 7// T3 IDEMPOTENT KEY -- content-address key = sha|dest, deterministic (re-run identical) -> re-mirror = no-op. 8// T4 RECEIPT SHAPE -- mo_receipt_bind emits EXACTLY status|sha|dest|verify|ts (the nx_pub_receipt binding rung-2 signs). 9// T5 MANIFEST PARSE -- a TAB-delimited manifest row splits into name/scheme/source/sha/dest; missing field -> empty. 10// T6 DRY PLAN / NEVER-BRICK -- an integrated dry plan (parse->resolve->verify->key->receipt) completes with ZERO 11// live side effects, deterministically. Then prints the Apertus-shaped MIRROR PLAN (dry). 12// GREEN iff T1-T6 pass. Sovereign nx_cc->nxasm (no gcc). expect_exit: 0 license_tier: ORIGINAL 13import "nx_mirror_orchestrator.nx" 14import "nx_syscalls.nx" 15 16func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func wn(v: i64) -> i64 { var m: i64=v; if m<0{w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 18 19// build a TAB-delimited row from 5 fields (explicit byte 9 -> no reliance on "\t" literal support). 20func build_row(out: *u8, a: *u8, b: *u8, c: *u8, d: *u8, e: *u8) -> i64 { 21 var o: i64 = mo_cat(out,0,a); out[o]=9 as u8; o=o+1 22 o = mo_cat(out,o,b); out[o]=9 as u8; o=o+1 23 o = mo_cat(out,o,c); out[o]=9 as u8; o=o+1 24 o = mo_cat(out,o,d); out[o]=9 as u8; o=o+1 25 o = mo_cat(out,o,e); out[o]=0 as u8; return o 26} 27 28func main() -> i64 { 29 w("=== nx_mirror_orchestrator_gate: rung-1 deterministic core (compose shipped primitives, never-brick) ===\n" as *u8) 30 var pass: i64=0; var total: i64=0 31 32 // the canonical FIPS 180-4 vector: sha256("abc") 33 let abc: *u8 = "abc" as *u8 34 let abc_sha: *u8 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" as *u8 35 36 // ---------- T1: SCHEME RESOLVE (fail-closed) ---------- 37 var t1: i64=1 38 if mo_scheme_code("http" as *u8) != MO_HTTP { t1=0 } 39 if mo_scheme_code("https" as *u8) != MO_HTTP { t1=0 } 40 if mo_scheme_code("magnet" as *u8)!= MO_TORRENT { t1=0 } 41 if mo_scheme_code("torrent" as *u8)!=MO_TORRENT { t1=0 } 42 if mo_scheme_code("ftp" as *u8) != MO_REJECT { t1=0 } 43 if mo_scheme_code("gopher" as *u8)!= MO_REJECT { t1=0 } 44 total=total+1; if t1==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 45 w("T1 scheme resolve: http/https->HTTP, torrent/magnet->TORRENT, unknown->REJECT (fail-closed)\n" as *u8) 46 47 // ---------- T2: BYTE-EXACT VERIFY vs NIST vector + fail-closed ---------- 48 let got: *u8 = sys_mmap(80); mo_sha_hex(abc, 3, got) 49 let vec_ok: i64 = mo_streq(got, abc_sha) 50 let v_true: i64 = mo_verify(abc, 3, abc_sha) // must PASS 51 let bad: *u8 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ae" as *u8 // last nibble flipped 52 let v_flip: i64 = mo_verify(abc, 3, bad) // must FAIL (never-store-corrupt) 53 var t2: i64=1 54 if vec_ok != 1 { t2=0 } 55 if v_true != 1 { t2=0 } 56 if v_flip != 0 { t2=0 } 57 total=total+1; if t2==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 58 w("T2 byte-exact verify: sha256(\"abc\")=" as *u8); w(got); w(" match-vec=" as *u8); wn(vec_ok); w(" pass-true=" as *u8); wn(v_true); w(" reject-flip=" as *u8); wn(v_flip); w("\n" as *u8) 59 60 // ---------- T3: IDEMPOTENT CONTENT-ADDRESS KEY (deterministic) ---------- 61 let dest: *u8 = "nas:/volume1/ai/apertus/apertus-8b.safetensors" as *u8 62 let k1: *u8 = sys_mmap(256); mo_cas_key(abc_sha, dest, k1) 63 let k2: *u8 = sys_mmap(256); mo_cas_key(abc_sha, dest, k2) 64 let kexp: *u8 = sys_mmap(256); var ko: i64=mo_cat(kexp,0,abc_sha); kexp[ko]=124 as u8; ko=ko+1; ko=mo_cat(kexp,ko,dest); kexp[ko]=0 as u8 65 var t3: i64=1 66 if mo_streq(k1,k2)!=1 { t3=0 } // deterministic 67 if mo_streq(k1,kexp)!=1 { t3=0 } // = sha|dest 68 total=total+1; if t3==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 69 w("T3 idempotent CAS key: sha|dest deterministic (re-mirror = no-op)\n" as *u8) 70 71 // ---------- T4: RECEIPT BINDING SHAPE (== nx_pub_receipt) ---------- 72 let rb: *u8 = sys_mmap(512); mo_receipt_bind(rb, "PUBLISHED" as *u8, abc_sha, dest, "ok" as *u8, "1730000000" as *u8) 73 let rexp: *u8 = sys_mmap(512) 74 var ro: i64=mo_cat(rexp,0,"PUBLISHED" as *u8); rexp[ro]=124 as u8; ro=ro+1 75 ro=mo_cat(rexp,ro,abc_sha); rexp[ro]=124 as u8; ro=ro+1 76 ro=mo_cat(rexp,ro,dest); rexp[ro]=124 as u8; ro=ro+1 77 ro=mo_cat(rexp,ro,"ok" as *u8); rexp[ro]=124 as u8; ro=ro+1 78 ro=mo_cat(rexp,ro,"1730000000" as *u8); rexp[ro]=0 as u8 79 var t4: i64=1; if mo_streq(rb,rexp)!=1 { t4=0 } 80 total=total+1; if t4==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 81 w("T4 receipt binding: status|sha|dest|verify|ts (the exact bytes rung-2's rcpt_emit signs)\n" as *u8) 82 83 // ---------- T5: MANIFEST PARSE ---------- 84 let row: *u8 = sys_mmap(512) 85 build_row(row, "apertus-8b" as *u8, "http" as *u8, "https://example/model.safetensors" as *u8, abc_sha, dest) 86 let f0: *u8=sys_mmap(128); mo_parse_field(row,0,f0) 87 let f1: *u8=sys_mmap(128); mo_parse_field(row,1,f1) 88 let f3: *u8=sys_mmap(128); mo_parse_field(row,3,f3) 89 let f4: *u8=sys_mmap(128); mo_parse_field(row,4,f4) 90 let f9: *u8=sys_mmap(128); let f9n: i64=mo_parse_field(row,9,f9) // missing -> empty 91 var t5: i64=1 92 if mo_streq(f0,"apertus-8b" as *u8)!=1 { t5=0 } 93 if mo_streq(f1,"http" as *u8)!=1 { t5=0 } 94 if mo_streq(f3,abc_sha)!=1 { t5=0 } 95 if mo_streq(f4,dest)!=1 { t5=0 } 96 if f9n != 0 { t5=0 } 97 total=total+1; if t5==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 98 w("T5 manifest parse: name/scheme/source/sha/dest split; missing field -> empty\n" as *u8) 99 100 // ---------- T6: DRY INTEGRATED PLAN (never-brick: parse->resolve->verify->key->receipt, ZERO live side effects) ---------- 101 // content is a fixture ("abc") standing in for the fetched bytes; NO fetch, NO NAS write occurs. 102 let sc: i64 = mo_scheme_code(f1) 103 let vok: i64 = mo_verify(abc, 3, f3) // verify fixture vs the row's expected digest 104 let pk: *u8 = sys_mmap(256); mo_cas_key(f3, f4, pk) 105 let pr: *u8 = sys_mmap(512); mo_receipt_bind(pr, "PUBLISHED" as *u8, f3, f4, "sha-ok" as *u8, "1730000000" as *u8) 106 var t6: i64=1 107 if sc != MO_HTTP { t6=0 } 108 if vok != 1 { t6=0 } 109 if mo_slen(pk) <= 0 { t6=0 } 110 if mo_slen(pr) <= 0 { t6=0 } 111 total=total+1; if t6==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) } 112 w("T6 dry plan / never-brick: resolve+verify+key+receipt, ZERO live fetch/NAS-write, deterministic\n" as *u8) 113 114 // ---------- DEMO: the Apertus-shaped MIRROR PLAN (dry) ---------- 115 w("\n --- MIRROR PLAN (dry; live fetch+NAS+signed-receipt = rung-2, behind operator confirm) ---\n" as *u8) 116 w(" name = " as *u8); w(f0); w("\n" as *u8) 117 w(" method = " as *u8); if sc==MO_HTTP { w("HTTP (nx_https_fetch_follow, own TLS-1.3)\n" as *u8) } else { if sc==MO_TORRENT { w("TORRENT (nx_torrent_get + nx_dht_get_peers swarm)\n" as *u8) } else { w("REJECT\n" as *u8) } } 118 w(" source = " as *u8); let f2: *u8=sys_mmap(256); mo_parse_field(row,2,f2); w(f2); w("\n" as *u8) 119 w(" expect = sha256:" as *u8); w(f3); w("\n" as *u8) 120 w(" dest = " as *u8); w(f4); w("\n" as *u8) 121 w(" verify = " as *u8); if vok==1 { w("WOULD-STORE (digest matches)\n" as *u8) } else { w("WOULD-REJECT (fail-closed)\n" as *u8) } 122 w(" key = " as *u8); w(pk); w("\n" as *u8) 123 124 w("\n=== nx_mirror_orchestrator_gate " as *u8); wn(pass); w("/" as *u8); wn(total) 125 if pass==total { w(" GREEN (rung-1: deterministic mirror core -- integrity fail-closed, idempotent, never-brick; live dispatch = rung-2)\n" as *u8); sys_exit(0); return 0 } 126 w(" RED\n" as *u8); sys_exit(1); return 1 127}