import { after, test } from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import { groth16, wtns } from "snarkjs"; import { commitment } from "../lib/hidden-path/commit.ts"; import { validRoute } from "../lib/hidden-path/statement.ts"; import { parseArtifact, shareFragment, readFragment, tamperedCopy, scalarField, } from "../lib/hidden-path/artifact.ts"; import manifest from "../public/hidden-path/v1/manifest.json" with { type: "json" }; import vector from "../public/hidden-path/v1/example.json" with { type: "json" }; import vk from "../public/hidden-path/v1/verification-key.json" with { type: "json" }; const dir = "public/hidden-path/v1/"; const wasm = dir + "circuit.wasm"; const route = [0, 3, 4, 5, 8]; const input = (r, salt = 23n) => ({ route: r, salt: salt.toString(), commitment: commitment(r, salt), }); after(async () => { await globalThis.curve_bn128?.terminate(); }); test("the installed artifacts match their manifest and published proof", async () => { for (const [file, entry] of Object.entries(manifest.files)) { const bytes = await readFile(dir + file); assert.equal(bytes.length, entry.bytes); assert.equal( createHash("sha256").update(bytes).digest("hex"), entry.sha256, ); } assert.equal(vk.nPublic, 1); const artifact = readFragment( shareFragment(parseArtifact(JSON.stringify(vector))), ); assert.deepEqual(artifact, vector); assert.ok(await groth16.verify(vk, artifact.publicSignals, artifact.proof)); assert.equal( await groth16.verify( vk, tamperedCopy(artifact).publicSignals, artifact.proof, ), false, ); }); test("a fresh proof opens one commitment, with a different digest for a different salt", async () => { const witness = input(route); assert.notEqual(witness.commitment, input(route, 24n).commitment); const { proof, publicSignals } = await groth16.fullProve( witness, wasm, dir + "proving.zkey", ); assert.deepEqual(publicSignals, [witness.commitment]); assert.ok(await groth16.verify(vk, publicSignals, proof)); }); test("compiled witness rejects invalid edge, row wrap, endpoints, vertex, salt and opening", async () => { for (const invalid of [ [0, 2, 4, 5, 8], [0, 1, 2, 3, 8], [1, 2, 5, 7, 8], [0, 3, 4, 5, 7], [0, 3, 9, 5, 8], ]) { assert.equal(validRoute(invalid), false); await assert.rejects(wtns.calculate(input(invalid), wasm, { type: "mem" })); } for (const invalid of [ input(route, 1n << 128n), { ...input(route), commitment: "1" }, ]) await assert.rejects(wtns.calculate(invalid, wasm, { type: "mem" })); for (const good of [ [0, 1, 2, 5, 8], [0, 1, 4, 5, 8], [0, 1, 4, 7, 8], [0, 3, 4, 5, 8], [0, 3, 4, 7, 8], [0, 3, 6, 7, 8], ]) { assert.ok(validRoute(good)); const witness = { type: "mem" }; await wtns.calculate(input(good), wasm, witness, { memorySize: 64 }); assert.ok(await wtns.check(dir + "circuit.r1cs", witness)); } }); test("untrusted envelopes have bounded, canonical, versioned public inputs", () => { for (const change of [ { route }, { verificationKeyId: "foreign" }, { circuitVersion: "v2" }, { schemaVersion: 2 }, { publicSignals: [42] }, { publicSignals: ["01"] }, { publicSignals: [scalarField.toString()] }, { publicSignals: [] }, { proof: { ...vector.proof, pi_a: ["1", "-1", "1"] } }, { proof: { ...vector.proof, pi_b: [] } }, { proof: { ...vector.proof, extra: 1 } }, ]) assert.throws(() => parseArtifact(JSON.stringify({ ...vector, ...change })), ); for (const bad of ["{", "x".repeat(4097)]) assert.throws(() => parseArtifact(bad)); for (const bad of ["#x=y", "#p=!", "#p=" + "A".repeat(6000)]) assert.throws(() => readFragment(bad)); });