const chai = require("chai"); const assert = chai.assert; const fs = require("fs"); var tmp = require("tmp-promise"); const path = require("path"); const compiler = require("../../src/compiler"); const utils = require("../../src/utils"); const loadR1cs = require("r1csfile").load; const ZqField = require("ffjavascript").ZqField; const fastFile = require("fastfile"); const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder; module.exports = wasm_tester; async function wasm_tester(circomFile, _options) { tmp.setGracefulCleanup(); const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true }); // console.log(dir.path); const baseName = path.basename(circomFile, ".circom"); const options = Object.assign({}, _options); options.wasmFile = await fastFile.createOverride(path.join(dir.path, baseName + ".wasm")); options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym")); options.r1csFileName = path.join(dir.path, baseName + ".r1cs"); await compiler(circomFile, options); await options.wasmFile.close(); const wasm = await fs.promises.readFile(path.join(dir.path, baseName + ".wasm")); const wc = await WitnessCalculatorBuilder(wasm); return new WasmTester(dir, baseName, wc); } class WasmTester { constructor(dir, baseName, witnessCalculator) { this.dir=dir; this.baseName = baseName; this.witnessCalculator = witnessCalculator; } async release() { await this.dir.cleanup(); } async calculateWitness(input, sanityCheck) { return await this.witnessCalculator.calculateWitness(input, sanityCheck); } async loadSymbols() { if (this.symbols) return; this.symbols = {}; const symsStr = await fs.promises.readFile( path.join(this.dir.path, this.baseName + ".sym"), "utf8" ); const lines = symsStr.split("\n"); for (let i=0; i ${v}`); } return lines.join("\n"); } async checkConstraints(witness) { const self = this; if (!self.constraints) await self.loadConstraints(); for (let i=0; i