code wiki / _hdl_build / nx_store_ingest_cid_gate.nx

nx_store_ingest_cid_gate.nx source

↩ module page · 127 lines · 6649 B

1// nx_store_ingest_cid_gate.nx -- R1 gate: proves the `prompt` factor is now in the generation-CID, 2// fixing the latent collision where two DIFFERENT prompts at the same seed/model/sampler/steps/ 3// generator_host mapped to ONE CID -> the gallery would silently drop the second as a dup. 4// Composes the GREEN organ nx_store_ingest_compute_cid (computes a CID with NO store side-effect -> 5// a pure CID comparison). Builds tiny chunk-walkable PNGs in-memory: sig+IHDR copied verbatim from 6// the known-good KAT fixture (nx_store_ingest_test.nx) + real tEXt chunks (CRC32) + IEND. 7// NO fake green: if `prompt` were NOT harvested, P_cat and P_dog would collide -> the gate goes RED. 8// The cg_text tEXt-chunk writer here is the prototype for the R2 sovereign PNG tEXt writer. 9// license_tier: ORIGINAL 10import "nx_store_ingest.nx" 11import "nx_syscalls.nx" 12 13func cg(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func cg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 15 16// sig(8) + IHDR(declared len 14, chunk-walkable) copied verbatim from the known-good KAT fixture. 34 bytes. 17func cg_prefix(b: *u8) -> i64 { 18 b[0]=137 as u8; b[1]=80 as u8; b[2]=78 as u8; b[3]=71 as u8 19 b[4]=13 as u8; b[5]=10 as u8; b[6]=26 as u8; b[7]=10 as u8 20 b[8]=0 as u8; b[9]=0 as u8; b[10]=0 as u8; b[11]=14 as u8 21 b[12]=73 as u8; b[13]=72 as u8; b[14]=68 as u8; b[15]=82 as u8 22 b[16]=0 as u8; b[17]=0 as u8; b[18]=0 as u8; b[19]=0 as u8; b[20]=1 as u8 23 b[21]=0 as u8; b[22]=0 as u8; b[23]=0 as u8; b[24]=1 as u8; b[25]=8 as u8 24 b[26]=6 as u8; b[27]=0 as u8; b[28]=0 as u8; b[29]=0 as u8 25 b[30]=115 as u8; b[31]=22 as u8; b[32]=206 as u8; b[33]=242 as u8 26 return 34 27} 28 29// write a tEXt chunk at b[off]: len(4 BE) "tEXt" keyword \0 value, then CRC32(type+data). returns new off. 30func cg_text(b: *u8, off: i64, kw: *u8, val: *u8) -> i64 { 31 let kwlen: i64 = cg_slen(kw) 32 let vallen: i64 = cg_slen(val) 33 let dlen: i64 = kwlen + 1 + vallen 34 b[off] = ((dlen >> 24) & 0xff) as u8 35 b[off+1] = ((dlen >> 16) & 0xff) as u8 36 b[off+2] = ((dlen >> 8) & 0xff) as u8 37 b[off+3] = (dlen & 0xff) as u8 38 b[off+4]=116 as u8; b[off+5]=69 as u8; b[off+6]=88 as u8; b[off+7]=116 as u8 // "tEXt" 39 var i: i64 = 0 40 while i < kwlen { b[off+8+i] = kw[i]; i = i + 1 } 41 b[off+8+kwlen] = 0 as u8 42 var j: i64 = 0 43 while j < vallen { b[off+8+kwlen+1+j] = val[j]; j = j + 1 } 44 var cc: i64 = 0xffffffff 45 var ci: i64 = off + 4 46 let ce: i64 = off + 8 + dlen 47 while ci < ce { 48 cc = cc ^ (b[ci] & 0xff) 49 var cb: i64 = 0 50 while cb < 8 { let cm: i64 = 0 - (cc & 1); cc = (cc >> 1) ^ (0xedb88320 & cm); cb = cb + 1 } 51 ci = ci + 1 52 } 53 let cv: i64 = (cc ^ 0xffffffff) & 0xffffffff 54 b[ce] = ((cv >> 24) & 0xff) as u8 55 b[ce+1] = ((cv >> 16) & 0xff) as u8 56 b[ce+2] = ((cv >> 8) & 0xff) as u8 57 b[ce+3] = (cv & 0xff) as u8 58 return ce + 4 59} 60 61// IEND chunk (len 0, "IEND", const CRC AE426082 = 174 66 96 130). returns new off. 62func cg_iend(b: *u8, off: i64) -> i64 { 63 b[off]=0 as u8; b[off+1]=0 as u8; b[off+2]=0 as u8; b[off+3]=0 as u8 64 b[off+4]=73 as u8; b[off+5]=69 as u8; b[off+6]=78 as u8; b[off+7]=68 as u8 65 b[off+8]=174 as u8; b[off+9]=66 as u8; b[off+10]=96 as u8; b[off+11]=130 as u8 66 return off + 12 67} 68 69// build a PNG with the 5 base GENREC tEXt chunks; if withprompt==1 also a `prompt` chunk = pval. 70func cg_build(b: *u8, withprompt: i64, pval: *u8) -> i64 { 71 var off: i64 = cg_prefix(b) 72 off = cg_text(b, off, "seed" as *u8, "100" as *u8) 73 off = cg_text(b, off, "model" as *u8, "zit" as *u8) 74 off = cg_text(b, off, "sampler" as *u8, "euler" as *u8) 75 off = cg_text(b, off, "steps" as *u8, "8" as *u8) 76 off = cg_text(b, off, "generator_host" as *u8, "laptop" as *u8) 77 if withprompt == 1 { off = cg_text(b, off, "prompt" as *u8, pval) } 78 off = cg_iend(b, off) 79 return off 80} 81 82// 1 iff the two 69-byte CIDs are equal 83func cg_cideq(a: *u8, b: *u8) -> i64 { var i: i64=0; while i<69 { if (a[i]&0xff)!=(b[i]&0xff){return 0} i=i+1 } return 1 } 84 85func main() -> i64 { 86 let canon: *u8 = sys_mmap(8192) 87 let rk: *u8 = sys_mmap(96) 88 89 // P_cat: 5 base GENREC + prompt="a red fox" 90 let bcat: *u8 = sys_mmap(8192) 91 let lcat: i64 = cg_build(bcat, 1, "a red fox" as *u8) 92 let cidcat: *u8 = sys_mmap(80) 93 let rc1: i64 = nx_store_ingest_compute_cid(bcat, lcat, cidcat, canon, rk) 94 if rc1 < 0 { cg("CIDGATE FAIL: P_cat compute_cid <0 (no GENREC harvested)\n" as *u8); sys_exit(1); return 1 } 95 96 // P_dog: identical EXCEPT prompt="a blue dog" 97 let bdog: *u8 = sys_mmap(8192) 98 let ldog: i64 = cg_build(bdog, 1, "a blue dog" as *u8) 99 let ciddog: *u8 = sys_mmap(80) 100 let rc2: i64 = nx_store_ingest_compute_cid(bdog, ldog, ciddog, canon, rk) 101 if rc2 < 0 { cg("CIDGATE FAIL: P_dog compute_cid <0\n" as *u8); sys_exit(1); return 1 } 102 103 // THE FIX: different prompt -> different CID (pre-fix these collided to one CID) 104 if cg_cideq(cidcat, ciddog) == 1 { cg("CIDGATE FAIL: cat==dog -- prompt NOT in the CID (collision bug present)\n" as *u8); sys_exit(1); return 1 } 105 cg("GATE ROW prompt-distinguishes: GREEN -- 2 PNGs identical except prompt -> DIFFERENT CIDs (collision fixed)\n" as *u8) 106 107 // determinism: P_cat2 == P_cat -> same CID (idempotent ingest preserved) 108 let bcat2: *u8 = sys_mmap(8192) 109 let lcat2: i64 = cg_build(bcat2, 1, "a red fox" as *u8) 110 let cidcat2: *u8 = sys_mmap(80) 111 nx_store_ingest_compute_cid(bcat2, lcat2, cidcat2, canon, rk) 112 if cg_cideq(cidcat, cidcat2) != 1 { cg("CIDGATE FAIL: same prompt -> different CID (non-deterministic)\n" as *u8); sys_exit(1); return 1 } 113 cg("GATE ROW deterministic: GREEN -- identical inputs (incl prompt) -> identical CID\n" as *u8) 114 115 // backward-compat: the 5-key (no-prompt) PNG STILL harvests + yields a CID DISTINCT from prompt-bearing 116 let bnp: *u8 = sys_mmap(8192) 117 let lnp: i64 = cg_build(bnp, 0, "x" as *u8) 118 let cidnp: *u8 = sys_mmap(80) 119 let rc3: i64 = nx_store_ingest_compute_cid(bnp, lnp, cidnp, canon, rk) 120 if rc3 < 0 { cg("CIDGATE FAIL: no-prompt PNG compute_cid <0 (5-key harvest regressed)\n" as *u8); sys_exit(1); return 1 } 121 if cg_cideq(cidcat, cidnp) == 1 { cg("CIDGATE FAIL: prompt-bearing CID == no-prompt CID (prompt ignored)\n" as *u8); sys_exit(1); return 1 } 122 cg("GATE ROW backward-compat: GREEN -- 5-key (no-prompt) PNG still harvests + yields a DISTINCT CID\n" as *u8) 123 124 cg("STORE-INGEST-CID-GATE 3/3 GREEN -- prompt is a CID factor: cat!=dog + deterministic + no-prompt-compat\n" as *u8) 125 sys_exit(0) 126 return 0 127}