Skip to content

Commit

Permalink
Code formatting. Change var to let and const
Browse files Browse the repository at this point in the history
  • Loading branch information
OBrezhniev committed Sep 14, 2023
1 parent 1405005 commit e2d060f
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 298 deletions.
140 changes: 71 additions & 69 deletions c/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const chai = require("chai");
const assert = chai.assert;

const fs = require("fs");
var tmp = require("tmp-promise");
const tmp = require("tmp-promise");
const path = require("path");

const util = require("util");
const { F1Field } = require("ffjavascript");
const {F1Field} = require("ffjavascript");
const exec = util.promisify(require("child_process").exec);

const readR1cs = require("r1csfile").readR1cs;
Expand All @@ -16,11 +16,13 @@ const readWtns = require("snarkjs").wtns.exportJson;

module.exports = c_tester;

BigInt.prototype.toJSON = function() { return this.toString() }
BigInt.prototype.toJSON = function () {
return this.toString()
}

async function c_tester(circomInput, _options) {
async function c_tester(circomInput, _options) {

assert(await compiler_above_version("2.0.0"),"Wrong compiler version. Must be at least 2.0.0");
assert(await compiler_above_version("2.0.0"), "Wrong compiler version. Must be at least 2.0.0");

const baseName = path.basename(circomInput, ".circom");
const options = Object.assign({}, _options);
Expand All @@ -30,43 +32,43 @@ async function c_tester(circomInput, _options) {
options.sym = true;
options.json = options.json || false; // costraints in json format
options.r1cs = true;
options.compile = (typeof options.recompile === 'undefined')? true : options.recompile; // by default compile
options.compile = (typeof options.recompile === 'undefined') ? true : options.recompile; // by default compile

if (typeof options.output === 'undefined') {
tmp.setGracefulCleanup();
const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
//console.log(dir.path);
options.output = dir.path;
if (typeof options.output === 'undefined') {
tmp.setGracefulCleanup();
const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true});
//console.log(dir.path);
options.output = dir.path;
} else {
try {
try {
await fs.promises.access(options.output);
} catch (err) {
assert(options.compile,"Cannot set recompile to false if the output path does not exist");
await fs.promises.mkdir(options.output, { recursive: true });
}
} catch (err) {
assert(options.compile, "Cannot set recompile to false if the output path does not exist");
await fs.promises.mkdir(options.output, {recursive: true});
}
}
if (options.compile) {
await compile(baseName, circomInput, options);
await compile(baseName, circomInput, options);
} else {
const jsPath = path.join(options.output, baseName+"_js");
try {
await fs.promises.access(jsPath);
} catch (err) {
assert(false,"Cannot set recompile to false if the "+jsPath+" folder does not exist");
}
const jsPath = path.join(options.output, baseName + "_js");
try {
await fs.promises.access(jsPath);
} catch (err) {
assert(false, "Cannot set recompile to false if the " + jsPath + " folder does not exist");
}
}
return new CTester(options.output, baseName, run);
}

async function compile (baseName, fileName, options) {
async function compile(baseName, fileName, options) {
let flags = "--c ";
if (options.include) {
if (Array.isArray(options.include)) {
for (let i=0; i<options.include.length;i++) {
flags += "-l "+options.include[i] + " ";
for (let i = 0; i < options.include.length; i++) {
flags += "-l " + options.include[i] + " ";
}
} else {
flags += "-l "+options.include+ " ";
flags += "-l " + options.include + " ";
}
}
if (options.sym) flags += "--sym ";
Expand All @@ -78,20 +80,20 @@ async function compile (baseName, fileName, options) {
if (options.verbose) flags += "--verbose ";

try {
let b = await exec("circom " + flags + fileName);
if (options.verbose) {
let b = await exec("circom " + flags + fileName);
if (options.verbose) {
console.log(b.stdout);
}
}
if (b.stderr) {
console.error(b.stderr);
}
} catch (e) {
assert(false,
"circom compiler error \n" + e);
assert(false,
"circom compiler error \n" + e);
}

const c_folder = path.join(options.output, baseName+"_cpp/")
let b = await exec("make -C "+c_folder);
const c_folder = path.join(options.output, baseName + "_cpp/")
let b = await exec("make -C " + c_folder);
if (b.stderr) {
console.error(b.stderr);
}
Expand All @@ -100,7 +102,7 @@ async function compile (baseName, fileName, options) {
class CTester {

constructor(dir, baseName, witnessCalculator) {
this.dir=dir;
this.dir = dir;
this.baseName = baseName;
this.witnessCalculator = witnessCalculator;
}
Expand All @@ -110,22 +112,22 @@ class CTester {
}

async calculateWitness(input) {
const inputjson = JSON.stringify(input);
const inputFile = path.join(this.dir, this.baseName+"_cpp/" + this.baseName + ".json");
const wtnsFile = path.join(this.dir, this.baseName+"_cpp/" + this.baseName + ".wtns");
const runc = path.join(this.dir, this.baseName+"_cpp/" + this.baseName);
fs.writeFile(inputFile, inputjson, function(err) {
if (err) throw err;
});
await exec("cd " + path.join(this.dir, this.baseName+"_cpp/"));
const inputjson = JSON.stringify(input);
const inputFile = path.join(this.dir, this.baseName + "_cpp/" + this.baseName + ".json");
const wtnsFile = path.join(this.dir, this.baseName + "_cpp/" + this.baseName + ".wtns");
const runc = path.join(this.dir, this.baseName + "_cpp/" + this.baseName);
fs.writeFile(inputFile, inputjson, function (err) {
if (err) throw err;
});
await exec("cd " + path.join(this.dir, this.baseName + "_cpp/"));
let proc = await exec(runc + " " + inputFile + " " + wtnsFile);
if (proc.stdout !== "") {
console.log(proc.stdout);
}
if (proc.stderr !== "") {
console.error(proc.stderr);
}
return await readBinWitnessFile(wtnsFile);
return await readBinWitnessFile(wtnsFile);
}

async loadSymbols() {
Expand All @@ -136,9 +138,9 @@ class CTester {
"utf8"
);
const lines = symsStr.split("\n");
for (let i=0; i<lines.length; i++) {
for (let i = 0; i < lines.length; i++) {
const arr = lines[i].split(",");
if (arr.length!=4) continue;
if (arr.length != 4) continue;
this.symbols[arr[3]] = {
labelIdx: Number(arr[0]),
varIdx: Number(arr[1]),
Expand All @@ -150,7 +152,7 @@ class CTester {
async loadConstraints() {
const self = this;
if (this.constraints) return;
const r1cs = await readR1cs(path.join(this.dir, this.baseName + ".r1cs"),{
const r1cs = await readR1cs(path.join(this.dir, this.baseName + ".r1cs"), {
loadConstraints: true,
loadMap: false,
getFieldFromPrime: (p, singlethread) => new F1Field(p)
Expand All @@ -169,16 +171,16 @@ class CTester {
function checkObject(prefix, eOut) {

if (Array.isArray(eOut)) {
for (let i=0; i<eOut.length; i++) {
checkObject(prefix + "["+i+"]", eOut[i]);
for (let i = 0; i < eOut.length; i++) {
checkObject(prefix + "[" + i + "]", eOut[i]);
}
} else if ((typeof eOut == "object")&&(eOut.constructor.name == "Object")) {
} else if ((typeof eOut == "object") && (eOut.constructor.name == "Object")) {
for (let k in eOut) {
checkObject(prefix + "."+k, eOut[k]);
checkObject(prefix + "." + k, eOut[k]);
}
} else {
if (typeof self.symbols[prefix] == "undefined") {
assert(false, "Output variable not defined: "+ prefix);
assert(false, "Output variable not defined: " + prefix);
}
const ba = actualOut[self.symbols[prefix].varIdx].toString();
const be = eOut.toString();
Expand Down Expand Up @@ -206,7 +208,7 @@ class CTester {
async checkConstraints(witness) {
const self = this;
if (!self.constraints) await self.loadConstraints();
for (let i=0; i<self.constraints.length; i++) {
for (let i = 0; i < self.constraints.length; i++) {
checkConstraint(self.constraints[i]);
}

Expand All @@ -216,7 +218,7 @@ class CTester {
const a = evalLC(constraint[0]);
const b = evalLC(constraint[1]);
const c = evalLC(constraint[2]);
assert (F.isZero(F.sub(F.mul(a,b), c)), "Constraint doesn't match");
assert(F.isZero(F.sub(F.mul(a, b), c)), "Constraint doesn't match");
}

function evalLC(lc) {
Expand All @@ -225,7 +227,7 @@ class CTester {
for (let w in lc) {
v = F.add(
v,
F.mul( lc[w], F.e(witness[w]) )
F.mul(lc[w], F.e(witness[w]))
);
}
return v;
Expand All @@ -234,26 +236,26 @@ class CTester {

}

function version_to_list ( v ) {
return v.split(".").map(function(x) {
return parseInt(x, 10);
function version_to_list(v) {
return v.split(".").map(function (x) {
return parseInt(x, 10);
});
}

function check_versions ( v1, v2 ) {
function check_versions(v1, v2) {
//check if v1 is newer than or equal to v2
for (let i = 0; i < v2.length; i++) {
if (v1[i] > v2[i]) return true;
if (v1[i] < v2[i]) return false;
if (v1[i] > v2[i]) return true;
if (v1[i] < v2[i]) return false;
}
return true;
}

async function compiler_above_version(v) {
let output = (await exec('circom --version')).stdout;
let compiler_version = version_to_list(output.slice(output.search(/\d/),-1));
let compiler_version = version_to_list(output.slice(output.search(/\d/), -1));
let vlist = version_to_list(v);
return check_versions ( compiler_version, vlist );
return check_versions(compiler_version, vlist);
}

async function readBinWitnessFile(fileName) {
Expand All @@ -262,19 +264,19 @@ async function readBinWitnessFile(fileName) {
}

function fromArray8(arr) { //returns a BigInt
var res = BigInt(0);
let res = BigInt(0);
const radix = BigInt(0x100);
for (let i = arr.length-1 ; i>=0; i--) {
res = res*radix + BigInt(arr[i]);
for (let i = arr.length - 1; i >= 0; i--) {
res = res * radix + BigInt(arr[i]);
}
return res;
}

function fromArray8ToUint(arr) { //returns a BigInt
var res = 0;
let res = 0;
const radix = 8;
for (let i = arr.length-1 ; i>=0; i--) {
res = res*radix + arr[i];
for (let i = arr.length - 1; i >= 0; i--) {
res = res * radix + arr[i];
}
return res;
}
54 changes: 29 additions & 25 deletions test/multiplier2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,40 @@ describe("Simple test", function () {
it("Checking the compilation of a simple circuit generating wasm", async function () {

const circuit = await wasm_tester(
path.join(__dirname, "Multiplier2.circom")
);
path.join(__dirname, "Multiplier2.circom")
);
const w = await circuit.calculateWitness({a: 2, b: 4});
await circuit.checkConstraints(w);
});

it("Checking the compilation of a simple circuit generating wasm in a given folder", async function () {
const circuit = await wasm_tester(
path.join(__dirname, "Multiplier2.circom"),
{ output : path.join(__dirname),
}
);
path.join(__dirname, "Multiplier2.circom"),
{
output: path.join(__dirname),
}
);
const w = await circuit.calculateWitness({a: 2, b: 4});
await circuit.checkConstraints(w);
});

it("Checking the compilation of a simple circuit generating wasm in a given folder without recompiling", async function () {
const circuit = await wasm_tester(
path.join(__dirname, "Multiplier2.circom"),
{ output : path.join(__dirname),
recompile : false,
}
);
path.join(__dirname, "Multiplier2.circom"),
{
output: path.join(__dirname),
recompile: false,
}
);
const w = await circuit.calculateWitness({a: 6, b: 3});
await circuit.checkConstraints(w);

});

it("Checking the compilation of a simple circuit generating C", async function () {
const circuit = await c_tester(
path.join(__dirname, "Multiplier2.circom")
);
path.join(__dirname, "Multiplier2.circom")
);
try {
const w = await circuit.calculateWitness({a: 2, b: 4});
await circuit.checkConstraints(w);
Expand All @@ -65,10 +67,11 @@ describe("Simple test", function () {

it("Checking the compilation of a simple circuit generating C in a given folder", async function () {
const circuit = await c_tester(
path.join(__dirname, "Multiplier2.circom"),
{ output : path.join(__dirname),
}
);
path.join(__dirname, "Multiplier2.circom"),
{
output: path.join(__dirname),
}
);
try {
const w = await circuit.calculateWitness({a: 2, b: 4});
await circuit.checkConstraints(w);
Expand All @@ -83,11 +86,12 @@ describe("Simple test", function () {

it("Checking the compilation of a simple circuit generating C in a given folder without recompiling", async function () {
const circuit = await c_tester(
path.join(__dirname, "Multiplier2.circom"),
{ output : path.join(__dirname),
recompile : false,
}
);
path.join(__dirname, "Multiplier2.circom"),
{
output: path.join(__dirname),
recompile: false,
}
);
try {
const w = await circuit.calculateWitness({a: 6, b: 3});
await circuit.checkConstraints(w);
Expand Down
Loading

0 comments on commit e2d060f

Please sign in to comment.