25 lines
1.2 KiB
JavaScript
25 lines
1.2 KiB
JavaScript
// 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);
|