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 util = require("util"); const exec = util.promisify(require("child_process").exec); const bigInt = require("big-integer"); const utils = require("../../src/utils"); const loadR1cs = require("r1csfile").load; const ZqField = require("ffjavascript").ZqField; const buildZqField = require("ffiasm").buildZqField; module.exports = c_tester; async function c_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.cSourceWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".cpp")); options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym")); options.r1csFileName = path.join(dir.path, baseName + ".r1cs"); options.p = options.p || bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"); await compiler(circomFile, options); const source = await buildZqField(options.p, "Fr"); // console.log(dir.path); await fs.promises.writeFile(path.join(dir.path, "fr.asm"), source.asm, "utf8"); await fs.promises.writeFile(path.join(dir.path, "fr.h"), source.h, "utf8"); await fs.promises.writeFile(path.join(dir.path, "fr.c"), source.c, "utf8"); let pThread = ""; if (process.platform === "darwin") { await exec("nasm -fmacho64 --prefix _ " + ` ${path.join(dir.path, "fr.asm")}` ); } else if (process.platform === "linux") { pThread = "-pthread"; await exec("nasm -felf64 " + ` ${path.join(dir.path, "fr.asm")}` ); } else throw("Unsupported platform"); const cdir = path.join(path.dirname(require.resolve("circom_runtime")), "c"); await exec("g++" + ` ${pThread}` + ` ${path.join(cdir, "main.cpp")}` + ` ${path.join(cdir, "calcwit.cpp")}` + ` ${path.join(cdir, "utils.cpp")}` + ` ${path.join(dir.path, "fr.c")}` + ` ${path.join(dir.path, "fr.o")}` + ` ${path.join(dir.path, baseName + ".cpp")} ` + ` -o ${path.join(dir.path, baseName)}` + ` -I ${dir.path} -I${cdir}` + " -lgmp -std=c++11 -DSANITY_CHECK -g" ); // console.log(dir.path); return new CTester(dir, baseName); } class CTester { constructor(dir, baseName) { this.dir=dir; this.baseName = baseName; } async release() { await this.dir.cleanup(); } async calculateWitness(input) { await fs.promises.writeFile( path.join(this.dir.path, "in.json"), JSON.stringify(utils.stringifyBigInts(input), null, 1) ); const r = await exec(`${path.join(this.dir.path, this.baseName)}` + ` ${path.join(this.dir.path, "in.json")}` + ` ${path.join(this.dir.path, "out.json")}` ); if (r.stdout) { console.log(r.stdout); } const resStr = await fs.promises.readFile( path.join(this.dir.path, "out.json") ); const res = utils.unstringifyBigInts(JSON.parse(resStr)); return res; } 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