WIP ring code: qr-anchored prototype, encoder, bench, lab pages

This commit is contained in:
2026-11-16 18:00:24 +01:00
parent 766426b328
commit 4784aaa67d
12 changed files with 811 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
// cross check against vectors produced by the python encoder: node tests/ringcode_cross.mjs vectors.json
import { readFileSync } from "node:fs";
import { dataBits, rsEncode, rsDecode, bytesFromBits, tokenFromPayload, SYNC } from "../static/js/ringcode.js";
const vectors = JSON.parse(readFileSync(process.argv[2], "utf8"));
let fails = 0;
const fail = (m) => { fails++; console.log("FAIL", m); };
for (const v of vectors) {
const payload = [...Buffer.from(v.payload, "hex")];
const cw = rsEncode(payload);
if (Buffer.from(cw).toString("hex") !== v.codeword) fail(`rs encode ${v.codeword}`);
if (SYNC + dataBits(cw) !== v.bits) fail(`bits ${v.token}`);
const back = bytesFromBits([...v.bits.slice(SYNC.length)].map(Number));
const msg = rsDecode(back);
if (!msg || tokenFromPayload(msg) !== v.token) fail(`roundtrip ${v.token}`);
for (const c of v.corrupted) {
const m = rsDecode([...Buffer.from(c.cw, "hex")]);
const ok = m && tokenFromPayload(m) === v.token;
if (c.errors <= 3 && !ok) fail(`fix ${c.errors} errors ${v.token}`);
if (c.errors > 3 && ok) console.log("note: decoded beyond capacity", c.errors);
}
}
console.log(fails ? `${fails} failures` : `ok ${vectors.length} vectors`);
process.exit(fails ? 1 : 0);