code wiki / _hdl_build / nx_base32_extvec_gate.nx

nx_base32_extvec_gate.nx source

↩ module page · 179 lines · 7778 B

1// nx_base32_extvec_gate.nx -- TWELFTH provably third-party-validated claim, from the same pinned document as the eighth. Base32 is a DIFFERENT alphabet and a DIFFERENT pad arithmetic than Base64 -- sharing a source does not make it the same claim. First OUTSIDE 2// cryptography: Base64 vs RFC 4648 section 10. 3// 4// ★A FIFTH VECTOR DIALECT. The previous seven gates read hex in four different layouts; RFC 4648 states 5// its vectors as QUOTED ASCII: 6// BASE32("") = "" 7// BASE32("f") = "Zg==" 8// BASE32("foobar") = "Zm9vYmFy" 9// No hex reader touches this. ★The recurring lesson of this session, once more: there is no general 10// parser, only one proven against the section in front of you. 11// 12// Construction unchanged from the other seven: nothing expected appears in this source, the document is 13// pinned to a digest nx_vecfetch computed IN-PROCESS AT THE SOCKET, and every input AND output below is 14// read out of that pinned document at run time. 15// ★THE EMPTY-STRING VECTOR IS KEPT DELIBERATELY. BASE32("") = "" is the case an encoder is most likely to 16// get wrong (a stray pad, a spurious byte) and the one a hand-written test is most likely to omit. The 17// authority published it; we do not get to skip it. 18// license_tier: ORIGINAL expect_exit: 0 19import "nx_syscalls.nx" 20import "nx_sha256_wasm.nx" 21import "nx_base32.nx" 22import "nx_gate_verdict.nx" 23 24func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 25func wb(b: *u8, n: i64) -> i64 { sys_write(1, b, n); return 0 } 26 27func nn(v: i64) -> i64 { 28 var m: i64 = v 29 if m < 0 { w("-" as *u8); m = 0 - m } 30 let t: *u8 = sys_mmap(32) 31 var k: i64 = 0 32 if m == 0 { t[0] = 48 as u8; k = 1 } 33 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 34 let b: *u8 = sys_mmap(32) 35 var j: i64 = 0 36 while j < k { b[j] = t[k - 1 - j]; j = j + 1 } 37 sys_write(1, b, k) 38 return 0 39} 40 41func hexnib(v: i64) -> i64 { if v < 10 { return 48 + v } return 87 + v } 42 43func starts(b: *u8, n: i64, at: i64, s: *u8) -> i64 { 44 var i: i64 = 0 45 while s[i] != (0 as u8) { 46 if at + i >= n { return 0 } 47 if b[at + i] != s[i] { return 0 } 48 i = i + 1 49 } 50 return 1 51} 52 53func findfrom(b: *u8, n: i64, s: *u8, from: i64) -> i64 { 54 var p: i64 = from 55 while p < n { 56 if starts(b, n, p, s) == 1 { return p } 57 p = p + 1 58 } 59 return 0 - 1 60} 61 62// Copy the run of bytes from `from` up to (not including) the next double-quote. Returns the length, or -1 63// if no closing quote appears. Empty is a VALID result -- BASE32("") = "" is a real published vector, so a 64// zero length must never be treated as a parse failure. 65func parseq(b: *u8, n: i64, from: i64, out: *u8, cap: i64, endout: *i64) -> i64 { 66 var p: i64 = from 67 var k: i64 = 0 68 while p < n { 69 if b[p] == (34 as u8) { endout[0] = p; return k } 70 if k >= cap { return 0 - 1 } 71 out[k] = b[p] 72 k = k + 1 73 p = p + 1 74 } 75 return 0 - 1 76} 77 78func main() -> i64 { 79 w("nx_base32_extvec_gate -- Base32 vs RFC 4648 section 10, READ FROM THE FETCHED DOCUMENT\n" as *u8) 80 81 let lp: *i64 = sys_mmap(16) as *i64 82 lp[0] = 0 83 let b: *u8 = sys_read_file("knowledge/extvec/rfc4648.txt\x00" as *u8, lp) 84 if lp[0] <= 0 { w("RED: fetched vector file absent -- run nx_vecfetch.\n" as *u8); return 1 } 85 86 let ctx: *u8 = sys_mmap(1024) 87 let dg: *u8 = sys_mmap(64) 88 nx_sha256_one_shot(b, lp[0], ctx, dg) 89 let hx: *u8 = sys_mmap(80) 90 var i: i64 = 0 91 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 } 92 let wnt: *u8 = "84e14418f795d503be5f34bf23ce4ebaa119e9ec7c9f667d8caeb111385b178f\x00" as *u8 93 var pin: i64 = 1 94 i = 0 95 while i < 64 { if hx[i] != wnt[i] { pin = 0 } i = i + 1 } 96 w(" acquisition digest: " as *u8); wb(hx, 64); w("\n" as *u8) 97 if pin == 0 { w("RED: PIN FAILED -- not the file nx_vecfetch acquired.\n" as *u8); return 1 } 98 w(" PIN OK -- bytes match the digest computed in-process at the socket\n" as *u8) 99 100 // Anchor on the SECTION BODY, not the contents line: "10. Test Vectors" (two spaces) appears in the 101 // body; the TOC line reads "10. Test Vectors ....". Take the last occurrence to be safe. 102 var at: i64 = 0 103 var scan: i64 = 0 104 var dsec: i64 = 0 105 while dsec == 0 { 106 let h: i64 = findfrom(b, lp[0], "BASE32(\"" as *u8, scan) 107 if h < 0 { dsec = 1 } else { if at == 0 { at = h } scan = h + 8 } 108 } 109 if at == 0 { w("RED: no BASE32(\") vectors found\n" as *u8); return 1 } 110 111 let inb: *u8 = sys_mmap(64) 112 let exp: *u8 = sys_mmap(64) 113 let got: *u8 = sys_mmap(128) 114 let ep: *i64 = sys_mmap(16) as *i64 115 116 var pass: i64 = 0 117 var fail: i64 = 0 118 var seen: i64 = 0 119 var cur: i64 = at 120 var done: i64 = 0 121 while done == 0 { 122 let h: i64 = findfrom(b, lp[0], "BASE32(\"" as *u8, cur) 123 if h < 0 { done = 1 } 124 else { 125 ep[0] = 0 126 let inlen: i64 = parseq(b, lp[0], h + 8, inb, 60, ep) 127 if inlen < 0 { done = 1 } 128 else { 129 let q: i64 = findfrom(b, lp[0], "= \"" as *u8, ep[0]) 130 if q < 0 { done = 1 } 131 else { 132 ep[0] = 0 133 let elen: i64 = parseq(b, lp[0], q + 3, exp, 60, ep) 134 if elen < 0 { done = 1 } 135 else { 136 let glen: i64 = base32_encode(inb, inlen, got) 137 var same: i64 = 1 138 if glen != elen { same = 0 } 139 else { i = 0; while i < elen { if got[i] != exp[i] { same = 0 } i = i + 1 } } 140 seen = seen + 1 141 if same == 1 { 142 pass = pass + 1 143 w(" PASS BASE32(\"" as *u8); wb(inb, inlen); w("\") == \"" as *u8); wb(exp, elen); w("\"\n" as *u8) 144 } else { 145 fail = fail + 1 146 w(" FAIL BASE32(\"" as *u8); wb(inb, inlen); w("\") expected \"" as *u8); wb(exp, elen) 147 w("\" got \"" as *u8); wb(got, glen); w("\"\n" as *u8) 148 } 149 cur = ep[0] 150 } 151 } 152 } 153 } 154 } 155 156 // The authority publishes SEVEN Base64 vectors in section 10 (empty through "foobar"). Finding fewer 157 // means the reader lost some -- and a gate that silently grades 3 of 7 while printing GREEN is exactly 158 // the coverage-gaming this workstream exists to stop. 159 if seen < 7 { 160 w(" RED: only " as *u8); nn(seen); w(" vectors parsed; RFC 4648 section 10 publishes 7.\n" as *u8) 161 w(" Refusing to report GREEN on a partial read of the authority.\n" as *u8) 162 fail = fail + 1 163 } 164 165 w("\n refsrc=https://www.rfc-editor.org/rfc/rfc4648.txt\n" as *u8) 166 w(" refsrcdig=" as *u8); wb(hx, 64); w("\n" as *u8) 167 w(" ref=RFC4648-10-base32 gate=nx_base32_extvec_gate\n" as *u8) 168 w("nx_base32_extvec_gate: vectors=" as *u8); nn(seen) 169 w(" pass=" as *u8); nn(pass); w(" fail=" as *u8); nn(fail) 170 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 171 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 172 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 173 let ctr__dry: *i64 = gv_ctr() 174 ctr__dry[0] = pass 175 ctr__dry[1] = pass + fail 176 let rc__dry: i64 = gv_verdict("BASE32-EXTVEC-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 177 sys_exit(rc__dry) 178 return rc__dry 179}