code wiki / _hdl_build / nx_base16alt_extvec_gate.nx

nx_base16alt_extvec_gate.nx source

↩ module page · 202 lines · 10262 B

1// nx_base16alt_extvec_gate.nx -- ADJUDICATES the hex.nx FORK (4 live importers) against RFC 4648 sec 10. 2// 3// hex.nx has NO base16_encode (that uppercase entry point was added to nx_hex_codec.nx today), so this 4// gate grades hex.nx's hex_encode CASE-INSENSITIVELY. * THE QUESTION HERE IS "IS THIS FORK'S NIBBLE 5// MAPPING CORRECT", NOT "IS IT BASE16-CONFORMANT" -- the case difference is a known, already-localised 6// non-defect, and folding it in would convict the fork for something its twin does too. 7// * GRADE A FORK ON WHAT DISTINGUISHES IT FROM ITS TWIN, NOT ON WHAT THEY SHARE. 8//// nx_base16alt_extvec_gate.nx -- validated against RFC4648-10-BASE16, read from the pinned+corroborated RFC4648 document. 9// 10// RFC 4648 section 10 publishes SEVEN BASE16 vectors; section 8 specifies the Base16 alphabet as 11// "0123456789ABCDEF" -- UPPERCASE, normatively. runtime/nx_hex_codec.nx's hex_encode emits LOWERCASE. 12// 13// ★★★★★ THE TEMPTING MOVE WAS THE GAMING MOVE, AGAIN: lowercase the authority's expected string before 14// comparing, and this gate goes GREEN in one line. That is WEAKENING THE AUTHORITY TO FIT THE 15// IMPLEMENTATION -- the same substitution as writing an Argon2 gate at p=1 because our Argon2 only does 16// p=1. The published answer does not get edited to match us. 17// 18// SO THE GATE LOCALISES INSTEAD OF MERELY FAILING. Two tests per vector: 19// T-EXACT byte-for-byte vs the document -> answers "are we RFC 4648 Base16 conformant?" 20// T-NOCASE ASCII-case-insensitive comparison -> answers "is the NIBBLE MAPPING right, and only the 21// alphabet case wrong?" 22// ★A FAILING TEST THAT CANNOT SAY *WHICH PART* FAILED SENDS SOMEONE TO REWRITE THE WHOLE ENCODER. Splitting 23// the comparison turns "our hex is broken" into "our hex is correct but is not Base16", which is a 24// one-line remedy (a second, uppercase-alphabet entry point) rather than a rewrite. 25// 26// ⚠SCOPE: this gate binds `nx_hex_codec.nx`. A SECOND `hex_encode` with the same name exists in `hex.nx` 27// -- the THIRD duplicate-library pair found in this subtree today (after sha1/nx_sha1 and 28// hkdf_sha1/nx_hkdf_sha1). ★NAME THE FILE YOU BOUND, NOT THE FUNCTION YOU CALLED. 29// 30// Document pinned to a CORROBORATED digest: the sovereign fetcher and .NET WebClient independently agree 31// (knowledge/extvec/CORROBORATION.tsv), so the expected values are the IETF's bytes, not ours. 32// license_tier: ORIGINAL expect_exit: 0 33import "nx_syscalls.nx" 34import "nx_sha256_wasm.nx" 35import "hex.nx" 36import "nx_gate_verdict.nx" // D001 base class: gv_need/gv_check/gv_verdict -- canonical verdict LAST + journal frame 37 38func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 39func wb(b: *u8, n: i64) -> i64 { sys_write(1, b, n); return 0 } 40 41func nn(v: i64) -> i64 { 42 var m: i64 = v 43 if m < 0 { w("-" as *u8); m = 0 - m } 44 let t: *u8 = sys_mmap(32) 45 var k: i64 = 0 46 if m == 0 { t[0] = 48 as u8; k = 1 } 47 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 48 let b: *u8 = sys_mmap(32) 49 var j: i64 = 0 50 while j < k { b[j] = t[k - 1 - j]; j = j + 1 } 51 sys_write(1, b, k) 52 return 0 53} 54 55func hexnib(v: i64) -> i64 { if v < 10 { return 48 + v } return 87 + v } 56func lc(c: i64) -> i64 { if c >= 65 { if c <= 90 { return c + 32 } } return c } 57 58func starts(b: *u8, n: i64, at: i64, s: *u8) -> i64 { 59 var i: i64 = 0 60 while s[i] != (0 as u8) { 61 if at + i >= n { return 0 } 62 if b[at + i] != s[i] { return 0 } 63 i = i + 1 64 } 65 return 1 66} 67func findfrom(b: *u8, n: i64, s: *u8, from: i64) -> i64 { 68 var p: i64 = from 69 while p < n { if starts(b, n, p, s) == 1 { return p } p = p + 1 } 70 return 0 - 1 71} 72 73// Copy up to the next double-quote. EMPTY IS VALID -- BASE16("") = "" is a published vector and the one an 74// encoder is most likely to get wrong, so a zero length must never be treated as a parse failure. 75func parseq(b: *u8, n: i64, from: i64, out: *u8, cap: i64, endout: *i64) -> i64 { 76 var p: i64 = from 77 var k: i64 = 0 78 while p < n { 79 if b[p] == (34 as u8) { endout[0] = p; return k } 80 if k >= cap { return 0 - 1 } 81 out[k] = b[p] 82 k = k + 1 83 p = p + 1 84 } 85 return 0 - 1 86} 87 88func main() -> i64 { 89 w("nx_base16alt_extvec_gate -- Base16 vs RFC 4648 section 10, READ FROM THE FETCHED DOCUMENT\n" as *u8) 90 91 let ctr: *i64 = gv_ctr() 92 let lp: *i64 = sys_mmap(16) as *i64 93 lp[0] = 0 94 let b: *u8 = sys_read_file("knowledge/extvec/rfc4648.txt\x00" as *u8, lp) 95 var have: i64 = 0 96 if lp[0] > 0 { have = 1 } 97 // ABSENT DOC IS THE THIRD STATE, not RED: no evidence about the encoder either way (gv_need -> SKIP). 98 if gv_need("knowledge/extvec/rfc4648.txt (fetch with nx_vecfetch)" as *u8, have, ctr) == 0 { 99 return gv_verdict("BASE16ALT-EXTVEC-GATE" as *u8, ctr, "" as *u8) 100 } 101 102 let ctx: *u8 = sys_mmap(1024) 103 let dg: *u8 = sys_mmap(64) 104 nx_sha256_one_shot(b, lp[0], ctx, dg) 105 let hx: *u8 = sys_mmap(80) 106 var i: i64 = 0 107 while i < 32 { hx[i*2] = hexnib(((dg[i] as i64)/16)&15) as u8; hx[i*2+1] = hexnib((dg[i] as i64)&15) as u8; i = i + 1 } 108 let wnt: *u8 = "84e14418f795d503be5f34bf23ce4ebaa119e9ec7c9f667d8caeb111385b178f\x00" as *u8 109 var pin: i64 = 1 110 i = 0 111 while i < 64 { if hx[i] != wnt[i] { pin = 0 } i = i + 1 } 112 w(" acquisition digest: " as *u8); wb(hx, 64); w("\n" as *u8) 113 if gv_check("T-PIN document digest matches the corroborated pin" as *u8, pin, ctr) == 0 { 114 w(" not the corroborated document; refusing to parse an unpinned doc.\n" as *u8) 115 return gv_verdict("BASE16ALT-EXTVEC-GATE" as *u8, ctr, "" as *u8) 116 } 117 w(" PIN OK -- CORROBORATED (sovereign fetch and .NET WebClient agree)\n\n" as *u8) 118 119 let inb: *u8 = sys_mmap(64) 120 let exp: *u8 = sys_mmap(64) 121 let got: *u8 = sys_mmap(128) 122 let got2: *u8 = sys_mmap(128) 123 let ep: *i64 = sys_mmap(16) as *i64 124 125 var seen: i64 = 0 126 var exact: i64 = 0 127 var nocase: i64 = 0 128 var cur: i64 = 0 129 var done: i64 = 0 130 while done == 0 { 131 let h: i64 = findfrom(b, lp[0], "BASE16(\"" as *u8, cur) 132 if h < 0 { done = 1 } 133 else { 134 ep[0] = 0 135 let inlen: i64 = parseq(b, lp[0], h + 8, inb, 60, ep) 136 if inlen < 0 { done = 1 } 137 else { 138 let q: i64 = findfrom(b, lp[0], "= \"" as *u8, ep[0]) 139 if q < 0 { done = 1 } 140 else { 141 ep[0] = 0 142 let elen: i64 = parseq(b, lp[0], q + 3, exp, 60, ep) 143 if elen < 0 { done = 1 } 144 else { 145 // TWO ARMS, ONE DOCUMENT. base16_encode is the RFC-conformant entry point added 146 // after this gate localised the gap; hex_encode is the pre-existing lowercase 147 // encoder, kept and still graded so the distinction stays VISIBLE rather than 148 // being quietly absorbed by the fix. 149 // ★A GATE THAT ONLY TESTS THE FIXED PATH FORGETS WHY THE FIX WAS NEEDED, AND THE 150 // NEXT PERSON RE-MERGES THE TWO CONTRACTS. 151 let glen: i64 = hex_encode(inb, inlen, got) 152 let hlen: i64 = hex_encode(inb, inlen, got2) 153 var same_exact: i64 = 1 154 var same_nocase: i64 = 1 155 if glen != elen { same_exact = 0 } 156 else { 157 i = 0 158 while i < elen { if got[i] != exp[i] { same_exact = 0 } i = i + 1 } 159 } 160 if hlen != elen { same_nocase = 0 } 161 else { 162 i = 0 163 while i < elen { if lc(got2[i] as i64) != lc(exp[i] as i64) { same_nocase = 0 } i = i + 1 } 164 } 165 seen = seen + 1 166 if same_exact == 1 { exact = exact + 1 } 167 if same_nocase == 1 { nocase = nocase + 1 } 168 w(" BASE16(\"" as *u8); wb(inb, inlen); w("\") doc=\"" as *u8); wb(exp, elen) 169 w("\" ours=\"" as *u8); wb(got, glen); w("\" base16_exact=" as *u8) 170 if same_exact == 1 { w("PASS" as *u8) } else { w("FAIL" as *u8) } 171 w(" hex_nocase=" as *u8) 172 if same_nocase == 1 { w("PASS" as *u8) } else { w("FAIL" as *u8) } 173 w("\n" as *u8) 174 cur = ep[0] 175 } 176 } 177 } 178 } 179 } 180 181 if gv_check("T-PARSE all 7 published BASE16 vectors parsed from the pinned doc" as *u8, seen == 7, ctr) == 0 { 182 w(" only " as *u8); nn(seen); w(" of 7 parsed -- the doc is pin-verified, so a partial read is OUR parser's defect; refusing further teeth.\n" as *u8) 183 return gv_verdict("BASE16ALT-EXTVEC-GATE" as *u8, ctr, "" as *u8) 184 } 185 186 w("\n refsrc=https://www.rfc-editor.org/rfc/rfc4648.txt\n" as *u8) 187 w(" refsrcdig=" as *u8); wb(hx, 64); w("\n" as *u8) 188 w(" ref=RFC4648-10-BASE16 gate=nx_base16alt_extvec_gate\n" as *u8) 189 w(" BOUND: hex.nx (the FORK under adjudication; its twin nx_hex_codec.nx carries the uppercase base16_encode)\n" as *u8) 190 w("nx_base16alt_extvec_gate: vectors=" as *u8); nn(seen) 191 w(" exact=" as *u8); nn(exact); w(" nocase=" as *u8); nn(nocase); w("\n" as *u8) 192 193 gv_check("T-NOCASE hex.nx hex_encode nibble mapping matches all 7 vectors case-insensitively" as *u8, nocase == 7, ctr) 194 // exact-case is deliberately NOT a tooth here: the fork is graded on what DISTINGUISHES it from its 195 // twin (nibble mapping); the case divergence is the twin's already-localised non-defect (see 196 // nx_base16_extvec_gate). exact= is still printed above as data, never as a verdict input. 197 if nocase < 7 { 198 w(" differs beyond what the localised case non-defect explains -- the fork's encoding itself\n" as *u8) 199 w(" disagrees with the authority.\n" as *u8) 200 } 201 return gv_verdict("BASE16ALT-EXTVEC-GATE" as *u8, ctr, "" as *u8) 202}