code wiki / _hdl_build / nx_sovgit_obj_gate.nx

nx_sovgit_obj_gate.nx source

↩ module page · 116 lines · 7229 B

1// nx_sovgit_obj_gate.nx -- ★SOVGIT roadmap rung X0: the sovereign git OBJECT STORE, proven byte-exact against 2// STOCK git 2.39 (object-format=sha256). Gold oracle generated by real git (independent): 3// empty-blob 473a0f4c.. blob "hello" 8aec4e48.. tree 06d48a30.. commit(fixed-date) 02acc3e8.. 4// T1-T4 = SHA-256 object-id parity; T5 = write->read loose round-trip byte-exact; T6 = zlib(frame) inflate 5// round-trip. GREEN => X0 is BUILT (the census's named first build) and a stock `git cat-file` can read our 6// loose objects (proven out-of-gate). license_tier: ORIGINAL expect_exit: 0 7import "nx_sovgit_obj.nx" 8 9func hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func pn(v: i64) -> i64 { let b: *u8 = sys_mmap(32) as *u8; var x: i64 = v; var ng: i64 = 0; if x < 0 { ng = 1; x = 0 - x } var i: i64 = 31; if x == 0 { b[i] = 48 as u8; i = i - 1 } while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } if ng == 1 { b[i] = 45 as u8; i = i - 1 } sys_write(1, (b as i64 + i + 1) as *u8, 31 - i); return 0 } 11 12// 64 hex chars at `got` equal the NUL-terminated expected string? 13func hexeq(got: *u8, exp: *u8) -> i64 { 14 var i: i64 = 0 15 while i < 64 { 16 if exp[i] == (0 as u8) { return 0 } 17 if got[i] != exp[i] { return 0 } 18 i = i + 1 19 } 20 return 1 21} 22 23func show64(got: *u8) -> i64 { var i: i64 = 0; while i < 64 { sys_write(1, (got as i64 + i) as *u8, 1); i = i + 1 } return 0 } 24 25func check(name: *u8, got: *u8, exp: *u8, fails: *i64) -> i64 { 26 hw(name); hw(" = " as *u8); show64(got) 27 if hexeq(got, exp) == 1 { hw(" OK\n" as *u8); return 0 } 28 hw(" FAIL want " as *u8); hw(exp); hw("\n" as *u8) 29 fails[0] = fails[0] + 1 30 return 1 31} 32 33// bytes at a[..n] equal bytes at b[..n]? 34func beq(a: *u8, b: *u8, n: i64) -> i64 { 35 var i: i64 = 0 36 while i < n { if a[i] != b[i] { return 0 } i = i + 1 } 37 return 1 38} 39 40func main() -> i64 { 41 hw("=== nx_sovgit_obj_gate -- rung X0 object store vs STOCK git 2.39 (object-format=sha256) ===\n" as *u8) 42 let fails: *i64 = sys_mmap(16) as *i64 43 fails[0] = 0 44 let hx: *u8 = sys_mmap(80) 45 46 // ---- T1 empty blob : SHA-256("blob 0\0") == git's documented sha256 empty-blob constant ---- 47 sg_oid_hex("blob" as *u8, "" as *u8, 0, hx) 48 check("T1 empty-blob" as *u8, hx, "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813" as *u8, fails) 49 50 // ---- T2 blob "hello" ---- 51 sg_oid_hex("blob" as *u8, "hello" as *u8, 5, hx) 52 check("T2 blob hello" as *u8, hx, "8aec4e4876f854f688d0ebfc8f37598f38e5fd6903cccc850ca36591175aeb60" as *u8, fails) 53 54 // ---- T3 tree : one entry "100644 hello.txt\0" + raw(blob hello) ---- 55 let raw: *u8 = sys_mmap(32) 56 sg_oid_raw("blob" as *u8, "hello" as *u8, 5, raw) 57 let tree: *u8 = sys_mmap(128) 58 let tlen: i64 = sg_tree_entry(tree, 0, "100644" as *u8, "hello.txt" as *u8, raw) 59 sg_oid_hex("tree" as *u8, tree, tlen, hx) 60 check("T3 tree " as *u8, hx, "06d48a30caf6b4263876dbc71cec3bedfa08df79a4a59d55fbb9ea106a1ecc9c" as *u8, fails) 61 62 // ---- T4 commit (fixed author/committer dates -> deterministic hash) ---- 63 let cbody: *u8 = "tree 06d48a30caf6b4263876dbc71cec3bedfa08df79a4a59d55fbb9ea106a1ecc9c\nauthor t <t@t> 1700000000 +0000\ncommitter t <t@t> 1700000000 +0000\n\nmsg\n" as *u8 64 let clen: i64 = sg_slen(cbody) 65 hw(" (commit body len = " as *u8); pn(clen); hw(", expect 142)\n" as *u8) 66 sg_oid_hex("commit" as *u8, cbody, clen, hx) 67 check("T4 commit " as *u8, hx, "02acc3e80a9f6c427e5013d38a788218a59adaba62eaa221ee570a4df963407e" as *u8, fails) 68 69 // ---- T5 write a blob loose, read it back byte-exact ---- 70 sys_mkdir("knowledge/_sovgit_x0" as *u8, 0x1ed) 71 let wr: i64 = sg_write_loose("knowledge/_sovgit_x0/objects" as *u8, "blob" as *u8, "hello" as *u8, 5, hx) 72 let rout: *u8 = sys_mmap(64) 73 let rtyp: *u8 = sys_mmap(16) 74 let rlen: i64 = sg_read_loose("knowledge/_sovgit_x0/objects" as *u8, hx, rout, rtyp) 75 hw("T5 round-trip write->read: id " as *u8); show64(hx); hw("\n" as *u8) 76 var t5ok: i64 = 1 77 if wr != 0 { t5ok = 0 } 78 if rlen != 5 { t5ok = 0 } 79 if beq(rout, "hello" as *u8, 5) != 1 { t5ok = 0 } 80 if beq(rtyp, "blob" as *u8, 4) != 1 { t5ok = 0 } 81 if rtyp[4] != (0 as u8) { t5ok = 0 } 82 if t5ok == 1 { hw(" type=" as *u8); hw(rtyp); hw(" len=" as *u8); pn(rlen); hw(" content=" as *u8); sys_write(1, rout, rlen); hw(" OK\n" as *u8) } 83 if t5ok != 1 { hw(" FAIL (wr=" as *u8); pn(wr); hw(" rlen=" as *u8); pn(rlen); hw(")\n" as *u8); fails[0] = fails[0] + 1 } 84 85 // ---- T6 zlib(frame) inflates back to the exact frame ---- 86 let fbuf: *u8 = sys_mmap(64) 87 let flen: i64 = sg_frame("blob" as *u8, "hello" as *u8, 5, fbuf) 88 let zbuf: *u8 = sys_mmap(256) 89 let zlen: i64 = sg_zwrap(fbuf, flen, zbuf) 90 let zr: *NxZlibResult = nx_zlib_inflate(zbuf, zlen, 1 << 20) 91 var t6ok: i64 = 1 92 if zr.error_code != 0 { t6ok = 0 } 93 if zr.output_size != flen { t6ok = 0 } 94 if t6ok == 1 { if beq(zr.output_data, fbuf, flen) != 1 { t6ok = 0 } } 95 hw("T6 zlib round-trip: frame " as *u8); pn(flen); hw("B -> deflate " as *u8); pn(zlen); hw("B -> inflate " as *u8); pn(zr.output_size); hw("B" as *u8) 96 if t6ok == 1 { hw(" OK\n" as *u8) } 97 if t6ok != 1 { hw(" FAIL (err=" as *u8); pn(zr.error_code); hw(")\n" as *u8); fails[0] = fails[0] + 1 } 98 99 // ---- report ---- 100 let rep: *u8 = sys_mmap(4096) 101 var o: i64 = 0 102 o = sg_cpy(rep, o, "SOVGIT X0 -- SOVEREIGN GIT OBJECT STORE, proven byte-exact vs STOCK git 2.39 (object-format=sha256).\n" as *u8, 0, sg_slen("SOVGIT X0 -- SOVEREIGN GIT OBJECT STORE, proven byte-exact vs STOCK git 2.39 (object-format=sha256).\n" as *u8)) 103 o = sg_cpy(rep, o, "Frame = \"<type> <len>\\0<content>\"; id = SHA-256(frame); loose = zlib(frame) at objects/<2>/<62>.\n" as *u8, 0, sg_slen("Frame = \"<type> <len>\\0<content>\"; id = SHA-256(frame); loose = zlib(frame) at objects/<2>/<62>.\n" as *u8)) 104 o = sg_cpy(rep, o, "T1 empty-blob 473a0f4c.. | T2 blob 8aec4e48.. | T3 tree 06d48a30.. | T4 commit 02acc3e8.. | T5 write->read byte-exact | T6 zlib round-trip.\n" as *u8, 0, sg_slen("T1 empty-blob 473a0f4c.. | T2 blob 8aec4e48.. | T3 tree 06d48a30.. | T4 commit 02acc3e8.. | T5 write->read byte-exact | T6 zlib round-trip.\n" as *u8)) 105 o = sg_cpy(rep, o, "Reuses sovereign sha256_digest + dfe_compress + nx_zlib_inflate + adler32 -- no reinvention. NEXT rung X1 = packfiles + smart-HTTP git-upload-pack/receive-pack (a stock `git clone` round-trips).\n" as *u8, 0, sg_slen("Reuses sovereign sha256_digest + dfe_compress + nx_zlib_inflate + adler32 -- no reinvention. NEXT rung X1 = packfiles + smart-HTTP git-upload-pack/receive-pack (a stock `git clone` round-trips).\n" as *u8)) 106 rep[o] = 0 as u8 107 let fd: i64 = sys_openat_wr("knowledge/sovgit_x0.txt\x00" as *u8, 0x1a4) 108 sys_write(fd, rep, o) 109 sys_close(fd) 110 111 hw("\n" as *u8) 112 if fails[0] == 0 { hw("SOVGIT-X0 GREEN -- object store byte-exact vs stock git (blob/tree/commit ids + loose round-trip + zlib). rung X0 BUILT.\n" as *u8); sys_exit(0); return 0 } 113 hw("SOVGIT-X0 RED fails=" as *u8); pn(fails[0]); hw("\n" as *u8) 114 sys_exit(1) 115 return 1 116}