Skip to content

Commit 83665c3

Browse files
authored
pass wasi instance directly to circom_runtime (#8)
1 parent 8c01087 commit 83665c3

File tree

3 files changed

+68
-28
lines changed

3 files changed

+68
-28
lines changed

Diff for: app/Prover/Prove.js

+63-24
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,77 @@
11
import { WASI, PreopenDirectory, File, OpenFile, ConsoleStdout } from "@bjorn3/browser_wasi_shim/dist";
2-
import { groth16, wtns } from "snarkjs";
2+
import { groth16 } from "snarkjs";
33

4-
async function load_external_file(path) {
5-
const response = await fetch(path);
6-
const buffer = await response.arrayBuffer();
7-
return new File(buffer);
8-
}
4+
import * as fastFile from "fastfile";
5+
import { WitnessCalculatorBuilder } from "circom_runtime";
96

107
async function _fullProve(input) {
11-
const wasmFile = "circuit.wasm";
128
const zkeyFile = "circuit_final.zkey";
13-
let fds = [
9+
let instance = await createWASMInstance();
10+
const w = {
11+
type: "mem"
12+
};
13+
await wtnsCalculate(input, instance, w);
14+
const { proof, publicSignals } = await groth16.prove(zkeyFile, w);
15+
return { proof, inputs: publicSignals };
16+
}
17+
18+
export const fullProveImpl = input => () => _fullProve(input);
19+
20+
async function createWASMInstance() {
21+
let memorySize = 32767;
22+
let memory;
23+
let memoryAllocated = false;
24+
while (!memoryAllocated) {
25+
try {
26+
memory = new WebAssembly.Memory({ initial: memorySize });
27+
memoryAllocated = true;
28+
} catch (err) {
29+
if (memorySize === 1) {
30+
throw err;
31+
}
32+
console.warn("Could not allocate " + memorySize * 1024 * 64 + " bytes. This may cause severe instability. Trying with " + memorySize * 1024 * 64 / 2 + " bytes");
33+
memorySize = Math.floor(memorySize / 2);
34+
}
35+
}
36+
37+
const fds = [
1438
new OpenFile(new File([])), // stdin
1539
ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
1640
ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
1741
new PreopenDirectory("/", [
1842
["circuit.bin", await load_external_file("circuit.bin")]
1943
])
2044
];
45+
2146
const wasi = new WASI([], [], fds, { debug: true });
22-
let options = {
23-
initializeWasiReactorModuleInstance: (instance) => {
24-
wasi.initialize(instance);
25-
},
26-
additionalWASMImports: {
27-
wasi_snapshot_preview1: wasi.wasiImport
28-
}
29-
};
30-
const w = {
31-
type: "mem"
32-
};
33-
await wtns.calculate(input, wasmFile, w, options);
34-
const {proof, publicSignals} = await groth16.prove(zkeyFile, w);
35-
return {proof, inputs: publicSignals};
36-
}
3747

38-
export const fullProveImpl = input => () => _fullProve(input);
48+
const fdWasm = await fastFile.readExisting("circuit.wasm");
49+
const code = await fdWasm.read(fdWasm.totalSize);
50+
await fdWasm.close();
51+
52+
const wasmModule = await WebAssembly.compile(code);
53+
54+
const instance = await WebAssembly.instantiate(wasmModule, { wasi_snapshot_preview1: wasi.wasiImport });
55+
56+
wasi.initialize(instance);
57+
58+
return instance;
59+
60+
};
61+
62+
async function wtnsCalculate(input, wasm, wtnsFileName) {
63+
64+
const wc = await WitnessCalculatorBuilder(wasm);
65+
const fdWtns = await fastFile.createOverride(wtnsFileName);
66+
67+
const w = await wc.calculateWTNSBin(input);
68+
69+
await fdWtns.write(w);
70+
await fdWtns.close();
71+
};
72+
73+
async function load_external_file(path) {
74+
const response = await fetch(path);
75+
const buffer = await response.arrayBuffer();
76+
return new File(buffer);
77+
};

Diff for: package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
"keccak": "^3.0.0",
3838
"secp256k1": "^5.0.0",
3939
"snarkjs": "github:l-adic/snarkjs#actually-use-wasm-options",
40-
"circom_runtime": "github:l-adic/circom_runtime#bbfe3eeb68e18dfcb4d5e24668e536b030741e31"
40+
"circom_runtime": "github:l-adic/circom_runtime#968aaa5c7cf9f22dba72d939632778d78e57c56f"
4141
},
4242
"overrides": {
43-
"circom_runtime": "github:l-adic/circom_runtime#bbfe3eeb68e18dfcb4d5e24668e536b030741e31"
43+
"circom_runtime": "github:l-adic/circom_runtime#968aaa5c7cf9f22dba72d939632778d78e57c56f"
4444
},
4545
"parcel": {
4646
"aliases": {

0 commit comments

Comments
 (0)