code wiki / (root) / nx_uxf_cid.nx

nx_uxf_cid.nx source

↩ module page · 68 lines · 2859 B

1// nx_uxf_cid.nx -- UXF arc R1c: the SELF-DESCRIBING multicodec/profile CID, built ON TOP 2// of nx_canon_cid (do NOT touch the foundation -- Rule 19 additive, Rule 3 no patch cascade). 3// 4// The substrate spec (knowledge/specs/2026-06-09-tutoring-storage-substrate-rung1.md) defines 5// CID = multihash + a multicodec(record-type) tag, but the shipped cid_of() omits the 6// multicodec. This adds it. The profiled CID carries a PROFILE/codec id that is: 7// (a) FOLDED INTO the hash -> same bytes under different profiles => different CID 8// (no cross-profile collision; still fully content-addressed within a profile), AND 9// (b) READABLE from the CID string -> an endpoint can pick the right view WITHOUT any 10// out-of-band schema = Law-1 endpoint-adaptive ; new profiles = new codec ids with 11// NO format change = Law-4 extensible (Rule 25: add an id, never a new format). 12// 13// Profiled CID: "nxc1-" <2hex codec> "-" <64hex sha256(codec_byte || canon_bytes)> NUL (len 72) 14// No hardware/persistent writes (Rule 26). license_tier: ORIGINAL 15import "nx_syscalls.nx" 16import "nx_sha256.nx" 17import "nx_canon_cid.nx" 18 19// UXF profile/codec ids (the multicodec tag) -- the data-driven extension point. 20const UXF_DATA: i64 = 1 21const UXF_DOC: i64 = 2 22const UXF_MEDIA: i64 = 3 23const UXF_CONFIG: i64 = 4 24const UXF_ARCHIVE: i64 = 5 25 26func uxf_nib_hex(v: i64) -> i64 { 27 if v > 9 { return 87 + v } // a-f 28 return 48 + v // 0-9 29} 30 31func uxf_hex_nib(b: i64) -> i64 { 32 if b >= 48 { if b <= 57 { return b - 48 } } // 0-9 33 if b >= 97 { if b <= 102 { return b - 87 } } // a-f 34 if b >= 65 { if b <= 70 { return b - 55 } } // A-F 35 return 0 - 1 36} 37 38// profiled CID = "nxc1-" + 2hex(codec) + "-" + 64hex(sha256(codec || canon)); writes NUL; returns 72. 39func uxf_cid_profiled(codec: i64, canon: *u8, n: i64, cid: *u8) -> i64 { 40 let tmp: *u8 = sys_mmap(n + 16) 41 tmp[0] = (codec & 0xff) as u8 42 var i: i64 = 0 43 while i < n { tmp[1 + i] = canon[i]; i = i + 1 } 44 let dg: *u8 = sys_mmap(40) 45 sha256_digest(tmp, n + 1, dg) 46 cid[0] = 110 as u8; cid[1] = 120 as u8; cid[2] = 99 as u8; cid[3] = 49 as u8; cid[4] = 45 as u8 47 cid[5] = uxf_nib_hex((codec >> 4) & 15) as u8 48 cid[6] = uxf_nib_hex(codec & 15) as u8 49 cid[7] = 45 as u8 50 i = 0 51 while i < 32 { 52 let b: i64 = dg[i] & 0xff 53 cid[8 + i * 2] = uxf_nib_hex((b >> 4) & 15) as u8 54 cid[9 + i * 2] = uxf_nib_hex(b & 15) as u8 55 i = i + 1 56 } 57 cid[72] = 0 as u8 58 return 72 59} 60 61// READ the profile/codec back out of a profiled CID (self-describing); (0-1) if malformed. 62func uxf_codec_of_cid(cid: *u8) -> i64 { 63 let hi: i64 = uxf_hex_nib(cid[5] as i64) 64 let lo: i64 = uxf_hex_nib(cid[6] as i64) 65 if hi < 0 { return 0 - 1 } 66 if lo < 0 { return 0 - 1 } 67 return (hi * 16) + lo 68}