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 bigInt = require("big-integer"); const utils = require("../../src/utils"); const loadR1cs = require("r1csfile").load; const ZqField = require("ffjavascript").ZqField; 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.wasmWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".wasm")); options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym")); options.r1csFileName = path.join(dir.path, baseName + ".r1cs"); const promisesArr = []; promisesArr.push(new Promise(fulfill => options.wasmWriteStream.on("finish", fulfill))); await compiler(circomFile, options); await Promise.all(promisesArr); 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) { return await this.witnessCalculator.calculateWitness(input); } 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