watermark-eval / sdk /wasm-decoder /test_wasm_decoder.mjs
liu9178's picture
Auto-backup (auto_5min_timer): code + checkpoints + onnx + sdk + results
a39b890 verified
Raw History Blame Contribute Delete
3.85 kB
import assert from "node:assert/strict";
import overlayPkg from "../overlay-guard/aegismark-overlay.js";
import { AegisMarkWasmDecoder } from "./aegismark-decoder.js";
const { applyOverlayToRgbaBuffer } = overlayPkg;
async function runTests() {
const decoder = await AegisMarkWasmDecoder.create();
const text = "emp-9527|20261023";
const secretKey = "enterprise-secret-2026";
const strength = 6;
// 1. Synthesize a full-screen 1920x1080 webpage background with text/UI gradients
const fullW = 1920;
const fullH = 1080;
const screenRgba = new Uint8Array(fullW * fullH * 4);
for (let y = 0; y < fullH; y++) {
for (let x = 0; x < fullW; x++) {
let baseR = 235 - ((y * 20) / fullH) | 0;
let baseG = 238 - ((x * 15) / fullW) | 0;
let baseB = 244;
if (x % 240 === 0 || y % 160 === 0) {
baseR -= 18;
baseG -= 18;
baseB -= 18;
}
const idx = (y * fullW + x) * 4;
screenRgba[idx] = baseR;
screenRgba[idx + 1] = baseG;
screenRgba[idx + 2] = baseB;
screenRgba[idx + 3] = 255;
}
}
// 2. Apply @aegismark/overlay-guard watermark across the full screen
const wmScreenRgba = applyOverlayToRgbaBuffer(screenRgba, fullW, fullH, text, secretKey, {
chipSize: 2,
strength: 0.035,
});
function extractCrop(x0, y0, cw, ch, noiseAmp = 0) {
const crop = new Uint8Array(cw * ch * 4);
let seed = (x0 * 73856093) ^ (y0 * 19349663) ^ 0x1234567;
for (let y = 0; y < ch; y++) {
for (let x = 0; x < cw; x++) {
const srcIdx = ((y0 + y) * fullW + (x0 + x)) * 4;
const dstIdx = (y * cw + x) * 4;
seed = (seed * 1664525 + 1013904223) >>> 0;
const n = noiseAmp > 0 ? ((seed % (2 * noiseAmp + 1)) - noiseAmp) : 0;
crop[dstIdx] = Math.max(0, Math.min(255, wmScreenRgba[srcIdx] + n));
crop[dstIdx + 1] = Math.max(0, Math.min(255, wmScreenRgba[srcIdx + 1] + n));
crop[dstIdx + 2] = Math.max(0, Math.min(255, wmScreenRgba[srcIdx + 2] + n));
crop[dstIdx + 3] = 255;
}
}
return crop;
}
const testCases = [
{ name: "Arbitrary Crop #1 (320x240 at odd offset x=373, y=191)", x0: 373, y0: 191, w: 320, h: 240, noise: 0 },
{ name: "Arbitrary Crop #2 (200x180 at x=1147, y=629 + ±2 quantization noise)", x0: 1147, y0: 629, w: 200, h: 180, noise: 2 },
{ name: "Small Local Crop #3 (140x140 at x=519, y=407, only 1.5% of screen area!)", x0: 519, y0: 407, w: 140, h: 140, noise: 1 },
{ name: "Sub-Tile Extreme Crop #4 (116x116 < 128x128 single tile!)", x0: 811, y0: 333, w: 116, h: 116, noise: 0 },
{ name: "Wide Banner Crop #5 (420x96 strip)", x0: 205, y0: 777, w: 420, h: 96, noise: 1 },
];
console.log("=== @aegismark/overlay-guard + @aegismark/wasm-decoder End-to-End Verification ===");
for (const tc of testCases) {
const crop = extractCrop(tc.x0, tc.y0, tc.w, tc.h, tc.noise);
const res = decoder.decodeRGBA(crop, tc.w, tc.h, { secretKey });
console.log(
`[PASS] ${tc.name} -> text="${res.text}" | sync=(${res.syncOffset.dy},${res.syncOffset.dx}) sub=(${res.syncOffset.subY},${res.syncOffset.subX}) | RS_err=${res.rsErrorsCorrected} | bitAgree=${(res.bitAgreement * 100).toFixed(1)}% | z=${res.pilotZScore} | ${res.elapsedMs}ms`
);
assert.equal(res.verified, true, `Failed verification on ${tc.name}`);
assert.equal(res.text, text, `Payload mismatch on ${tc.name}`);
}
// Wrong key must be rejected (0 false positives)
const wrongKeyRes = decoder.decodeRGBA(extractCrop(100, 100, 256, 256, 0), 256, 256, {
secretKey: "wrong-key-9999",
});
assert.equal(wrongKeyRes.verified, false, "Wrong key must not verify");
console.log(`[PASS] Wrong-Key Zero False-Positive Guard -> verified=${wrongKeyRes.verified}`);
}
runTests().catch((err) => {
console.error(err);
process.exit(1);
});