diff --git a/app/Prover/Prove.js b/app/Prover/Prove.js index 38c3fb5..02bc7da 100644 --- a/app/Prover/Prove.js +++ b/app/Prover/Prove.js @@ -1,16 +1,40 @@ 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}`)), @@ -18,21 +42,36 @@ async function _fullProve(input) { ["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); \ No newline at end of file + 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); +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c9c586f..4e94599 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "ISC", "dependencies": { "@bjorn3/browser_wasi_shim": "^0.3.0", - "circom_runtime": "github:l-adic/circom_runtime#bbfe3eeb68e18dfcb4d5e24668e536b030741e31", + "circom_runtime": "github:l-adic/circom_runtime#968aaa5c7cf9f22dba72d939632778d78e57c56f", "ethjs-provider-http": "^0.1.6", "keccak": "^3.0.0", "secp256k1": "^5.0.0", @@ -2523,7 +2523,8 @@ }, "node_modules/circom_runtime": { "version": "0.1.25", - "resolved": "git+ssh://git@github.com/l-adic/circom_runtime.git#bbfe3eeb68e18dfcb4d5e24668e536b030741e31", + "resolved": "git+ssh://git@github.com/l-adic/circom_runtime.git#968aaa5c7cf9f22dba72d939632778d78e57c56f", + "integrity": "sha512-FgRBPiuCQ2kbjwfYrxmsq+9A8NRv07d8JUwCruqQvLloFTy4N8hupnEvIA7megt6IiB3b/d81jy18wClT5NlhA==", "license": "Apache-2.0", "dependencies": { "ffjavascript": "0.3.0" diff --git a/package.json b/package.json index 2db6b9a..355a801 100644 --- a/package.json +++ b/package.json @@ -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": {