Skip to content

Commit

Permalink
pass wasi instance directly to circom_runtime (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
martyall authored Jul 25, 2024
1 parent 8c01087 commit 83665c3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
87 changes: 63 additions & 24 deletions app/Prover/Prove.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,77 @@
import { WASI, PreopenDirectory, File, OpenFile, ConsoleStdout } from "@bjorn3/browser_wasi_shim/dist";
import { groth16, wtns } from "snarkjs";
import { groth16 } from "snarkjs";

async function load_external_file(path) {
const response = await fetch(path);
const buffer = await response.arrayBuffer();
return new File(buffer);
}
import * as fastFile from "fastfile";
import { WitnessCalculatorBuilder } from "circom_runtime";

async function _fullProve(input) {
const wasmFile = "circuit.wasm";
const zkeyFile = "circuit_final.zkey";
let fds = [
let instance = await createWASMInstance();
const w = {
type: "mem"
};
await wtnsCalculate(input, instance, w);
const { proof, publicSignals } = await groth16.prove(zkeyFile, w);
return { proof, inputs: publicSignals };
}

export const fullProveImpl = input => () => _fullProve(input);

async function createWASMInstance() {
let memorySize = 32767;
let memory;
let memoryAllocated = false;
while (!memoryAllocated) {
try {
memory = new WebAssembly.Memory({ initial: memorySize });
memoryAllocated = true;
} catch (err) {
if (memorySize === 1) {
throw err;
}
console.warn("Could not allocate " + memorySize * 1024 * 64 + " bytes. This may cause severe instability. Trying with " + memorySize * 1024 * 64 / 2 + " bytes");
memorySize = Math.floor(memorySize / 2);
}
}

const fds = [
new OpenFile(new File([])), // stdin
ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
new PreopenDirectory("/", [
["circuit.bin", await load_external_file("circuit.bin")]
])
];

const wasi = new WASI([], [], fds, { debug: true });
let options = {
initializeWasiReactorModuleInstance: (instance) => {
wasi.initialize(instance);
},
additionalWASMImports: {
wasi_snapshot_preview1: wasi.wasiImport
}
};
const w = {
type: "mem"
};
await wtns.calculate(input, wasmFile, w, options);
const {proof, publicSignals} = await groth16.prove(zkeyFile, w);
return {proof, inputs: publicSignals};
}

export const fullProveImpl = input => () => _fullProve(input);
const fdWasm = await fastFile.readExisting("circuit.wasm");
const code = await fdWasm.read(fdWasm.totalSize);
await fdWasm.close();

const wasmModule = await WebAssembly.compile(code);

const instance = await WebAssembly.instantiate(wasmModule, { wasi_snapshot_preview1: wasi.wasiImport });

wasi.initialize(instance);

return instance;

};

async function wtnsCalculate(input, wasm, wtnsFileName) {

const wc = await WitnessCalculatorBuilder(wasm);
const fdWtns = await fastFile.createOverride(wtnsFileName);

const w = await wc.calculateWTNSBin(input);

await fdWtns.write(w);
await fdWtns.close();
};

async function load_external_file(path) {
const response = await fetch(path);
const buffer = await response.arrayBuffer();
return new File(buffer);
};
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"keccak": "^3.0.0",
"secp256k1": "^5.0.0",
"snarkjs": "github:l-adic/snarkjs#actually-use-wasm-options",
"circom_runtime": "github:l-adic/circom_runtime#bbfe3eeb68e18dfcb4d5e24668e536b030741e31"
"circom_runtime": "github:l-adic/circom_runtime#968aaa5c7cf9f22dba72d939632778d78e57c56f"
},
"overrides": {
"circom_runtime": "github:l-adic/circom_runtime#bbfe3eeb68e18dfcb4d5e24668e536b030741e31"
"circom_runtime": "github:l-adic/circom_runtime#968aaa5c7cf9f22dba72d939632778d78e57c56f"
},
"parcel": {
"aliases": {
Expand Down

0 comments on commit 83665c3

Please sign in to comment.