code wiki / (root) / nx_revimg_discover_gate.nx

nx_revimg_discover_gate.nx source

↩ module page · 139 lines · 8467 B

1// nx_revimg_discover_gate.nx -- referee for the reverse-image cross-site discovery engine. 2// Synthetic multi-site corpus (perceptual fingerprints with planted relationships -- no decode needed): 3// site0 A (official): F1,F2 = the entity's seed media; F3,F4 = A's OTHER media. 4// site1 B (fan repost): F1^3bits = a RECOMPRESSED copy of F1 (Hamming 3); F5 = B's other media; N1 = noise. 5// site2 C (unrelated): U1,U2 -- nothing of the entity. 6// site3 D: F5^3bits = a copy of F5 (reachable ONLY after F5 is harvested) ; F6. 7// seeds R1 = {F1,F2}. copy_thresh = 6. 8// row1 CROSS-SITE MATCH -- seed F1 is found on BOTH site0 (exact) and site1 (recompressed) = "what other 9// sites have the media", robust to recompression a sha256 content-hash would miss. 10// row2 HOST RANKING -- site A (hosts 2 seeds) outranks site B (1); unrelated C is never a host. 11// row3 HARVEST -- site A's other media {F3,F4} and site B's {F5,N1} are harvested as NEW results. 12// row4 GROW -- folding the harvest into the seeds discovers site D (via F5's copy): the host 13// set grows 2 -> 3. The search results grew. 14// row5 PRECISION -- unrelated site C is NEVER a host, before or after growth (no false growth). 15// GREEN iff 5/5. Durable verdict -> knowledge/status/revimg_discover_gate.log. license_tier: ORIGINAL 16import "nx_revimg_discover.nx" 17 18func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 19func g_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); 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; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 } 20func g_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 21func g_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); 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; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 22 23func g_has_site(arr: *i64, n: i64, sid: i64) -> i64 { var i: i64=0; while i<n { if arr[i]==sid { return 1 } i=i+1 } return 0 } 24func g_hits_of(sites: *i64, hits: *i64, n: i64, sid: i64) -> i64 { var i: i64=0; while i<n { if sites[i]==sid { return hits[i] } i=i+1 } return 0-1 } 25func g_arr_has(arr: *i64, n: i64, val: i64) -> i64 { var i: i64=0; while i<n { if arr[i]==val { return 1 } i=i+1 } return 0 } 26 27func main() -> i64 { 28 g_puts("=== REVIMG DISCOVER GATE (reverse-image cross-site: find host sites -> harvest -> grow) ===\n" as *u8) 29 let THRESH: i64 = 6 30 31 let F1: i64 = 0x1111111111111111 32 let F2: i64 = 0x2222222222222222 33 let F3: i64 = 0x3333333333333333 34 let F4: i64 = 0x4444444444444444 35 let F5: i64 = 0x5555555555555555 36 let F6: i64 = 0x6666666666666666 37 let N1: i64 = 0x7777777777777777 38 let U1: i64 = 0x0808080808080808 39 let U2: i64 = 0x1818181818181818 40 41 let NC: i64 = 11 42 let cfp: *i64 = sys_mmap(8*NC) as *i64 43 let csite: *i64 = sys_mmap(8*NC) as *i64 44 // site0 A 45 cfp[0]=F1; csite[0]=0 46 cfp[1]=F2; csite[1]=0 47 cfp[2]=F3; csite[2]=0 48 cfp[3]=F4; csite[3]=0 49 // site1 B 50 cfp[4]=F1 ^ 0x7; csite[4]=1 // recompressed copy of F1 (Hamming 3) 51 cfp[5]=F5; csite[5]=1 52 cfp[6]=N1; csite[6]=1 53 // site2 C 54 cfp[7]=U1; csite[7]=2 55 cfp[8]=U2; csite[8]=2 56 // site3 D 57 cfp[9]=F5 ^ 0x7; csite[9]=3 // copy of F5 (Hamming 3) 58 cfp[10]=F6; csite[10]=3 59 60 // seeds R1 61 let s1: *i64 = sys_mmap(8*2) as *i64 62 s1[0]=F1; s1[1]=F2 63 64 let oci: *i64 = sys_mmap(8*64) as *i64 65 let osi: *i64 = sys_mmap(8*64) as *i64 66 let oham: *i64 = sys_mmap(8*64) as *i64 67 let hsite: *i64 = sys_mmap(8*16) as *i64 68 let hhits: *i64 = sys_mmap(8*16) as *i64 69 let hvfp: *i64 = sys_mmap(8*32) as *i64 70 let hvci: *i64 = sys_mmap(8*32) as *i64 71 72 var pass: i64 = 0 73 let rows: i64 = 5 74 75 // ---- row1: cross-site match -- F1 found on site0 AND site1 (recompressed) ---- 76 let nm: i64 = nx_revimg_match(s1, 2, cfp, NC, THRESH, oci, osi, oham, 64) 77 var f1_on0: i64 = 0 78 var f1_on1: i64 = 0 79 var mi: i64 = 0 80 while mi < nm { 81 if osi[mi]==0 { 82 let st: i64 = csite[oci[mi]] 83 if st==0 { f1_on0=1 } 84 if st==1 { f1_on1=1 } 85 } 86 mi = mi + 1 87 } 88 g_puts(" row1 cross-site match: seed F1 on site0? " as *u8); g_num(f1_on0); g_puts(" on site1(recompressed)? " as *u8); g_num(f1_on1); g_puts(" -> " as *u8) 89 if f1_on0==1 { if f1_on1==1 { pass=pass+1; g_puts("PASS (found the repost a byte-hash would miss)\n" as *u8) } else { g_puts("FAIL\n" as *u8) } } else { g_puts("FAIL\n" as *u8) } 90 91 // ---- row2: host ranking -- A hits=2 > B hits=1, C absent ---- 92 let nh1: i64 = nx_revimg_host_sites(s1, 2, cfp, csite, NC, THRESH, hsite, hhits, 16) 93 let hA: i64 = g_hits_of(hsite, hhits, nh1, 0) 94 let hB: i64 = g_hits_of(hsite, hhits, nh1, 1) 95 let hasC1: i64 = g_has_site(hsite, nh1, 2) 96 g_puts(" row2 host ranking: A=" as *u8); g_num(hA); g_puts(" B=" as *u8); g_num(hB); g_puts(" C present? " as *u8); g_num(hasC1); g_puts(" -> " as *u8) 97 if hA==2 { if hB==1 { if hasC1==0 { pass=pass+1; g_puts("PASS (more entity media = more relevant)\n" as *u8) } else { g_puts("FAIL\n" as *u8) } } else { g_puts("FAIL\n" as *u8) } } else { g_puts("FAIL\n" as *u8) } 98 99 // ---- row3: harvest host sites' OTHER media ---- 100 let naA: i64 = nx_revimg_harvest(0, s1, 2, cfp, csite, NC, THRESH, hvfp, hvci, 32) 101 let gotF3: i64 = g_arr_has(hvfp, naA, F3) 102 let hvfpB: *i64 = sys_mmap(8*32) as *i64 103 let hvciB: *i64 = sys_mmap(8*32) as *i64 104 let naB: i64 = nx_revimg_harvest(1, s1, 2, cfp, csite, NC, THRESH, hvfpB, hvciB, 32) 105 let gotF5: i64 = g_arr_has(hvfpB, naB, F5) 106 g_puts(" row3 harvest: A-other=" as *u8); g_num(naA); g_puts("(F3? " as *u8); g_num(gotF3); g_puts(") B-other=" as *u8); g_num(naB); g_puts("(F5? " as *u8); g_num(gotF5); g_puts(") -> " as *u8) 107 if naA==2 { if gotF3==1 { if naB==2 { if gotF5==1 { pass=pass+1; g_puts("PASS (their other media surfaced)\n" as *u8) } else { g_puts("FAIL\n" as *u8) } } else { g_puts("FAIL\n" as *u8) } } else { g_puts("FAIL\n" as *u8) } } else { g_puts("FAIL\n" as *u8) } 108 109 // ---- row4: GROW -- seeds += harvested(A)+harvested(B), re-discover -> site D appears ---- 110 let s2: *i64 = sys_mmap(8*16) as *i64 111 var ns2: i64 = 0 112 s2[0]=F1; s2[1]=F2; ns2=2 113 var hi: i64 = 0 114 while hi < naA { s2[ns2]=hvfp[hi]; ns2=ns2+1; hi=hi+1 } 115 hi = 0 116 while hi < naB { s2[ns2]=hvfpB[hi]; ns2=ns2+1; hi=hi+1 } 117 let hsite2: *i64 = sys_mmap(8*16) as *i64 118 let hhits2: *i64 = sys_mmap(8*16) as *i64 119 let nh2: i64 = nx_revimg_host_sites(s2, ns2, cfp, csite, NC, THRESH, hsite2, hhits2, 16) 120 let hasD: i64 = g_has_site(hsite2, nh2, 3) 121 g_puts(" row4 grow: seeds " as *u8); g_num(2); g_puts("->" as *u8); g_num(ns2); g_puts(", host sites " as *u8); g_num(nh1); g_puts("->" as *u8); g_num(nh2); g_puts(" (site D found? " as *u8); g_num(hasD); g_puts(") -> " as *u8) 122 if hasD==1 { if nh2 > nh1 { pass=pass+1; g_puts("PASS (results grew via reverse-image discovery)\n" as *u8) } else { g_puts("FAIL\n" as *u8) } } else { g_puts("FAIL\n" as *u8) } 123 124 // ---- row5: precision -- unrelated site C never a host, even after growth ---- 125 let hasC2: i64 = g_has_site(hsite2, nh2, 2) 126 g_puts(" row5 precision: unrelated site C a host after grow? " as *u8); g_num(hasC2); g_puts(" -> " as *u8) 127 if hasC2==0 { pass=pass+1; g_puts("PASS (no false growth)\n" as *u8) } else { g_puts("FAIL\n" as *u8) } 128 129 g_puts("----\nREVIMG-DISCOVER-GATE rows=" as *u8); g_num(rows); g_puts(" pass=" as *u8); g_num(pass); g_puts("\n" as *u8) 130 let lg: i64 = sys_openat_append("knowledge/status/revimg_discover_gate.log" as *u8, 0x1a4) 131 if lg>=0 { 132 g_w(lg,"REVIMG-DISCOVER-GATE rows=" as *u8); g_wn(lg,rows); g_w(lg," pass=" as *u8); g_wn(lg,pass) 133 g_w(lg," hostsR1=" as *u8); g_wn(lg,nh1); g_w(lg," hostsR2=" as *u8); g_wn(lg,nh2) 134 if pass==rows { g_w(lg," verdict=GREEN\n" as *u8) } else { g_w(lg," verdict=RED\n" as *u8) } 135 sys_close(lg) 136 } 137 if pass==rows { g_puts("REVIMG-DISCOVER-GATE GREEN\n" as *u8); sys_exit(0); return 0 } 138 g_puts("REVIMG-DISCOVER-GATE RED\n" as *u8); sys_exit(1); return 1 139}