Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom path-mapped dependencies #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const compiler = require("./src/compiler");

const version = require("./package").version;

const parsePathMap = require("./utils/parse_path_map");

const argv = require("yargs")
.version(version)
.usage("circom [input source circuit file] -r [output r1cs file] -c [output c file] -w [output wasm file] -t [output wat file] -s [output sym file]")
Expand All @@ -43,6 +45,11 @@ const argv = require("yargs")
.alias("n", "newThreadTemplates")
.help("h")
.alias("h", "help")
.option("path-map", {
alias: "a",
type: "array",
description: "Allow a given path for imports. A list of paths can be supplied by separating them with a comma."
})
.option("verbose", {
alias: "v",
type: "boolean",
Expand Down Expand Up @@ -80,10 +87,12 @@ async function run() {
const r1csName = typeof(argv.r1cs) === "string" ? argv.r1cs : fileName + ".r1cs";
const symName = typeof(argv.sym) === "string" ? argv.sym : fileName + ".sym";


const options = {};
options.reduceConstraints = !argv.fast;
options.verbose = argv.verbose || false;
options.sanityCheck = argv.sanitycheck;
options.pathMap = {};

if (argv.csource) {
options.cSourceFileName = cSourceName;
Expand Down Expand Up @@ -115,6 +124,9 @@ async function run() {
options.prime = Scalar.fromString(argv.prime);
}

if (argv.pathMap) {
options.pathMap = parsePathMap(argv.pathMap);
}

await compiler(fullFileName, options);

Expand Down
1 change: 1 addition & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async function compile(srcFile, options) {
ctx.verbose= options.verbose || false;
ctx.mainComponent = options.mainComponent || "main";
ctx.newThreadTemplates = options.newThreadTemplates;
ctx.pathMap = options.pathMap;

measures.constructionPhase = -performance.now();
constructionPhase(ctx, srcFile);
Expand Down
14 changes: 13 additions & 1 deletion src/construction_phase.js
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,19 @@ function createRefs(ctx, ast, scope) {
} else if (ast.type == "FUNCTIONDEF") {
ast.refId = define(ast.name, {t: "F"});
} else if (ast.type == "INCLUDE") {
const incFileName = path.resolve(ctx.filePath, ast.file);
const match = ast.file.match(/@(.+)\//);
let incFileName;
if (match) {
const key = match[1];
if (ctx.pathMap[key] == undefined) {
return ctx.throwError(ast, "Included path-mapped file not found:" + ast.file);
}
const val = ctx.pathMap[key];
incFileName = path.resolve(ast.file.replace("@" + key, val));
} else {
incFileName = path.resolve(ctx.filePath, ast.file);
}

const incFilePath = path.dirname(incFileName);

ctx.includedFiles = ctx.includedFiles || [];
Expand Down
10 changes: 10 additions & 0 deletions test/circuits/pathMap.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include "@customlib/pathMapDep.circom";

template Foo() {
signal input in;
component bar = Bar();

bar.in <== in;
}

component main = Foo();
6 changes: 6 additions & 0 deletions test/circuits/pathMapDep.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
template Bar() {
signal input in;
signal output out;

out <== in;
}
21 changes: 21 additions & 0 deletions test/compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require("path");
const fs = require("fs");
const shelljs = require("shelljs");
const expect = require("chai").expect;

describe("compiler tests", function () {
this.timeout(100000);

it('should import a path-mapped dependency', function() {
const r1csFile = "test." + Date.now().toString() + ".r1cs";
if (fs.existsSync(r1csFile)) {
console.error("Please delete " + r1csFile + " before running this test");
return;
}
const cliPath = path.join(__dirname, "..", "cli.js");
const cmd = "node " + cliPath + " ./test/circuits/pathMap.circom -a @customlib=./test/circuits/ -r " + r1csFile;
shelljs.exec(cmd);
expect(fs.existsSync(r1csFile)).to.equal(true);
fs.unlinkSync(r1csFile);
});
});
23 changes: 23 additions & 0 deletions utils/parse_path_map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = parsePathMap;

function parsePathMap(pathMap) {
const result = {};
const regex = /@(.+)=(.+)/
for (let i = 0; i < pathMap.length; i ++) {
const match = pathMap[i].match(regex);
if (!match) {
console.log("Invalid --path-map / -a value. Its format should be @alias=path.");
process.exit(1);
}
const alias = match[1];
const path = match[2];

if (Object.keys(result).indexOf(alias) !== -1) {
console.log("Duplicate --path-map / -a value.");
process.exit(1);
}

result[alias] = path;
}
return result;
}