chore(ci): Add browser testing (#365)

This commit is contained in:
Blaine Bublitz
2023-05-18 05:11:20 +01:00
committed by GitHub
parent e094f553c0
commit 2eef5d17d5
5 changed files with 1271 additions and 0 deletions

View File

@@ -91,3 +91,33 @@ jobs:
- name: Run tests
run: npm run testv12
test-browser:
name: Test browser
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
node-version: ["18"]
steps:
- name: Checkout project
uses: actions/checkout@v2
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
check-latest: true
cache: "npm"
- name: Install browser dependencies
working-directory: browser_tests
run: npm ci
- name: Run browser tests
working-directory: browser_tests
run: npm test

1145
browser_tests/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
{
"name": "snarkjs-browser-tests",
"private": true,
"type": "module",
"scripts": {
"test": "node test/launch-groth16.js"
},
"devDependencies": {
"puppeteer": "20.2.0",
"st": "3.0.0"
}
}

View File

@@ -0,0 +1,40 @@
<head>
<script src="/build/snarkjs.js"></script>
</head>
<body>
<script type="module">
const ptau_final = "https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_10.ptau";
const r1cs = "/test/circuit/circuit.r1cs";
const wasm = "/test/circuit/circuit.wasm";
const zkey_0 = { type: "mem" };
const zkey_1 = { type: "mem" };
const zkey_final = { type: "mem" };
const wtns = { type: "mem" };
await snarkjs.zKey.newZKey(r1cs, ptau_final, zkey_0);
await snarkjs.zKey.contribute(zkey_0, zkey_1, "p2_C1", "pa_Entropy1");
await snarkjs.zKey.beacon(zkey_1, zkey_final, "B3", "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", 10);
const verifyFromR1csResult = await snarkjs.zKey.verifyFromR1cs(r1cs, ptau_final, zkey_final);
console.assert(verifyFromR1csResult);
const verifyFromInit = await snarkjs.zKey.verifyFromInit(zkey_0, ptau_final, zkey_final);
console.assert(verifyFromInit);
const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final);
await snarkjs.wtns.calculate({ a: 11, b: 2 }, wasm, wtns);
await snarkjs.wtns.check(r1cs, wtns);
const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns);
const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof);
console.assert(verified);
shutdown();
</script>
</body>

View File

@@ -0,0 +1,44 @@
import puppeteer from "puppeteer";
import st from "st";
import http from "http";
import url from "url";
const projectRoot = new URL("../..", import.meta.url);
const mount = st({ path: url.fileURLToPath(projectRoot), passthrough: true });
const server = http
.createServer((req, res) => {
mount(req, res, () => res.end());
})
.listen(1337);
const browser = await puppeteer.launch({
headless: "new",
args: [
// Necessary to have WebCrypto on localhost
"--allow-insecure-localhost",
// Necessary to download the PTAU file from AWS within the tests
"--disable-web-security"
],
});
const page = await browser.newPage();
page.on("console", (msg) => {
if (msg.type === "assert") {
throw new Error(msg.text());
} else {
console.log(msg.text());
}
});
page.on("pageerror", (err) => {
throw err;
});
await page.exposeFunction("shutdown", async () => {
await browser.close();
server.close();
});
await page.goto("http://localhost:1337/browser_tests/test/groth16.html");