mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 11:16:42 +01:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80846667ea | ||
|
|
7181c372d9 | ||
|
|
aecc28a79b | ||
|
|
51ff27b9c6 |
14
cli.js
14
cli.js
@@ -34,8 +34,16 @@ const argv = require("yargs")
|
||||
.alias("o", "output")
|
||||
.help("h")
|
||||
.alias("h", "help")
|
||||
.alias("v", "verbose")
|
||||
.alias("f", "fast")
|
||||
.option("verbose", {
|
||||
alias: "v",
|
||||
type: "boolean",
|
||||
description: "Run with verbose logging"
|
||||
})
|
||||
.option("fast", {
|
||||
alias: "f",
|
||||
type: "boolean",
|
||||
description: "Do not optimize constraints"
|
||||
})
|
||||
.epilogue(`Copyright (C) 2018 0kims association
|
||||
This program comes with ABSOLUTELY NO WARRANTY;
|
||||
This is free software, and you are welcome to redistribute it
|
||||
@@ -57,7 +65,7 @@ if (argv._.length == 0) {
|
||||
const fullFileName = path.resolve(process.cwd(), inputFile);
|
||||
const outName = argv.output ? argv.output : "circuit.json";
|
||||
|
||||
compiler(fullFileName, {reduceConstraints: !argv.fast}).then( (cir) => {
|
||||
compiler(fullFileName, {reduceConstraints: !argv.fast, verbose: argv.verbose}).then( (cir) => {
|
||||
fs.writeFileSync(outName, JSON.stringify(cir, null, 1), "utf8");
|
||||
process.exit(0);
|
||||
}, (err) => {
|
||||
|
||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.0.34",
|
||||
"version": "0.0.35",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.0.34",
|
||||
"version": "0.0.35",
|
||||
"description": "Language to generate logic circuits",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
|
||||
@@ -65,7 +65,8 @@ async function compile(srcFile, options) {
|
||||
functions: {},
|
||||
functionParams: {},
|
||||
filePath: fullFilePath,
|
||||
fileName: fullFileName
|
||||
fileName: fullFileName,
|
||||
verbose: options.verbose || false
|
||||
};
|
||||
|
||||
|
||||
@@ -75,8 +76,10 @@ async function compile(srcFile, options) {
|
||||
throw new Error("A main component must be defined");
|
||||
}
|
||||
|
||||
if (ctx.verbose) console.log("Classify Signals");
|
||||
classifySignals(ctx);
|
||||
|
||||
if (ctx.verbose) console.log("Reduce Constraints");
|
||||
reduceConstants(ctx);
|
||||
if (options.reduceConstraints) {
|
||||
|
||||
@@ -217,6 +220,7 @@ function generateWitnessNames(ctx) {
|
||||
function reduceConstants(ctx) {
|
||||
const newConstraints = [];
|
||||
for (let i=0; i<ctx.constraints.length; i++) {
|
||||
if ((ctx.verbose)&&(i%10000 == 0)) console.log("reducing constants: ", i);
|
||||
const c = lc.canonize(ctx, ctx.constraints[i]);
|
||||
if (!lc.isZero(c)) {
|
||||
newConstraints.push(c);
|
||||
@@ -231,6 +235,7 @@ function reduceConstrains(ctx) {
|
||||
while (possibleConstraints.length>0) {
|
||||
let nextPossibleConstraints = {};
|
||||
for (let i in possibleConstraints) {
|
||||
if ((ctx.verbose)&&(i%10000 == 0)) console.log("reducing constraints: ", i);
|
||||
if (!ctx.constraints[i]) continue;
|
||||
const c = ctx.constraints[i];
|
||||
|
||||
|
||||
126
src/exec.js
126
src/exec.js
@@ -181,24 +181,6 @@ function iterateSelectors(ctx, sizes, baseName, fn) {
|
||||
return res;
|
||||
}
|
||||
|
||||
function setScope(ctx, name, selectors, value) {
|
||||
let l = getScopeLevel(ctx, name);
|
||||
if (l==-1) l= ctx.scopes.length-1;
|
||||
|
||||
if (selectors.length == 0) {
|
||||
ctx.scopes[l][name] = value;
|
||||
} else {
|
||||
setScopeArray(ctx.scopes[l][name], selectors);
|
||||
}
|
||||
|
||||
function setScopeArray(a, sels) {
|
||||
if (sels.length == 1) {
|
||||
a[sels[0].value] = value;
|
||||
} else {
|
||||
setScopeArray(a[sels[0]], sels.slice(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getScope(ctx, name, selectors) {
|
||||
|
||||
@@ -221,11 +203,57 @@ function getScope(ctx, name, selectors) {
|
||||
}
|
||||
|
||||
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
||||
if (ctx.scopes[i][name]) return select(ctx.scopes[i][name], sels);
|
||||
if (ctx.scopes[i][name]) return select(ctx.scopes[i][name].value, sels);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getScopeRef(ctx, name, selectors) {
|
||||
|
||||
const sels = [];
|
||||
if (selectors) {
|
||||
for (let i=0; i< selectors.length; i++) {
|
||||
const idx = exec(ctx, selectors[i]);
|
||||
if (ctx.error) return;
|
||||
|
||||
if (idx.type != "NUMBER") return error(ctx, selectors[i], "expected a number");
|
||||
sels.push( idx.value.toJSNumber() );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function select(v, s, t) {
|
||||
s = s || [];
|
||||
if (s.length == 0) return [v, sels, t];
|
||||
return select(v[s[0]], s.slice(1), t);
|
||||
}
|
||||
|
||||
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
||||
if (ctx.scopes[i][name]) return select(ctx.scopes[i][name].value, sels, ctx.scopes[i][name].type);
|
||||
}
|
||||
return [null, [], ""];
|
||||
}
|
||||
|
||||
|
||||
function setScopeRef(ctx, name, sels, value) {
|
||||
let l = getScopeLevel(ctx, name);
|
||||
if (l==-1) l= ctx.scopes.length-1;
|
||||
|
||||
if (sels.length == 0) {
|
||||
ctx.scopes[l][name].value = value;
|
||||
} else {
|
||||
setScopeArray(ctx.scopes[l][name].value, sels);
|
||||
}
|
||||
|
||||
function setScopeArray(a, sels) {
|
||||
if (sels.length == 1) {
|
||||
a[sels[0]] = value;
|
||||
} else {
|
||||
setScopeArray(a[sels[0]], sels.slice(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getScopeLevel(ctx, name) {
|
||||
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
||||
if (ctx.scopes[i][name]) return i;
|
||||
@@ -248,12 +276,15 @@ function execTemplateDef(ctx, ast) {
|
||||
return error(ctx, ast, "Name already exists: "+ast.name);
|
||||
}
|
||||
scope[ast.name] = {
|
||||
type: "TEMPLATE",
|
||||
value: {
|
||||
type: "TEMPLATE",
|
||||
params: ast.params,
|
||||
block: ast.block,
|
||||
fileName: ctx.fileName,
|
||||
filePath: ctx.filePath,
|
||||
scopes: copyScope(ctx.scopes)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -265,12 +296,15 @@ function execFunctionDef(ctx, ast) {
|
||||
}
|
||||
ctx.functionParams[ast.name] = ast.params;
|
||||
scope[ast.name] = {
|
||||
type: "FUNCTION",
|
||||
value: {
|
||||
type: "FUNCTION",
|
||||
params: ast.params,
|
||||
block: ast.block,
|
||||
fileName: ctx.fileName,
|
||||
filePath: ctx.filePath,
|
||||
scopes: copyScope(ctx.scopes)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -294,7 +328,9 @@ function execDeclareComponent(ctx, ast) {
|
||||
}
|
||||
|
||||
|
||||
scope[ast.name.name] = iterateSelectors(ctx, sizes, baseName, function(fullName) {
|
||||
scope[ast.name.name] = {
|
||||
type: "COMPONENT",
|
||||
value: iterateSelectors(ctx, sizes, baseName, function(fullName) {
|
||||
|
||||
ctx.components[fullName] = "UNINSTANTIATED";
|
||||
|
||||
@@ -302,7 +338,8 @@ function execDeclareComponent(ctx, ast) {
|
||||
type: "COMPONENT",
|
||||
fullName: fullName
|
||||
};
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
return {
|
||||
type: "VARIABLE",
|
||||
@@ -378,7 +415,10 @@ function execInstantiateComponet(ctx, vr, fn) {
|
||||
|
||||
const scope = {};
|
||||
for (let i=0; i< template.params.length; i++) {
|
||||
scope[template.params[i]] = paramValues[i];
|
||||
scope[template.params[i]] = {
|
||||
type: "VARIABLE",
|
||||
value: paramValues[i]
|
||||
};
|
||||
ctx.components[ctx.currentComponent].params[template.params[i]] = extractValue(paramValues[i]);
|
||||
}
|
||||
|
||||
@@ -430,7 +470,10 @@ function execFunctionCall(ctx, ast) {
|
||||
|
||||
const scope = {};
|
||||
for (let i=0; i< fnc.params.length; i++) {
|
||||
scope[fnc.params[i]] = paramValues[i];
|
||||
scope[fnc.params[i]] = {
|
||||
type: "VARIABLE",
|
||||
value: paramValues[i]
|
||||
};
|
||||
}
|
||||
|
||||
ctx.fileName = fnc.fileName;
|
||||
@@ -472,7 +515,9 @@ function execDeclareSignal(ctx, ast) {
|
||||
sizes.push( size.value.toJSNumber() );
|
||||
}
|
||||
|
||||
scope[ast.name.name] = iterateSelectors(ctx, sizes, baseName, function(fullName) {
|
||||
scope[ast.name.name] = {
|
||||
type: "SIGNAL",
|
||||
value: iterateSelectors(ctx, sizes, baseName, function(fullName) {
|
||||
ctx.signals[fullName] = {
|
||||
fullName: fullName,
|
||||
direction: ast.declareType == "SIGNALIN" ? "IN" : (ast.declareType == "SIGNALOUT" ? "OUT" : ""),
|
||||
@@ -486,7 +531,8 @@ function execDeclareSignal(ctx, ast) {
|
||||
type: "SIGNAL",
|
||||
fullName: fullName,
|
||||
};
|
||||
});
|
||||
})
|
||||
};
|
||||
return {
|
||||
type: "VARIABLE",
|
||||
name: ast.name.name,
|
||||
@@ -509,12 +555,15 @@ function execDeclareVariable(ctx, ast) {
|
||||
sizes.push( size.value.toJSNumber() );
|
||||
}
|
||||
|
||||
scope[ast.name.name] = iterateSelectors(ctx, sizes, "", function() {
|
||||
scope[ast.name.name] = {
|
||||
type: "VARIABLE",
|
||||
value: iterateSelectors(ctx, sizes, "", function() {
|
||||
return {
|
||||
type: "NUMBER",
|
||||
value: bigInt(0)
|
||||
};
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
return {
|
||||
type: "VARIABLE",
|
||||
@@ -631,19 +680,20 @@ function execVarAssignement(ctx, ast) {
|
||||
} else {
|
||||
v = ast.values[0];
|
||||
}
|
||||
const num = getScope(ctx, v.name, v.selectors);
|
||||
const [num, sels, typ] = getScopeRef(ctx, v.name, v.selectors);
|
||||
if (ctx.error) return;
|
||||
|
||||
if ((typeof(num) != "object")||(num == null)) return error(ctx, ast, "Variable not defined");
|
||||
|
||||
if (num.type == "COMPONENT") return execInstantiateComponet(ctx, v, ast.values[1]);
|
||||
if (typ == "COMPONENT") return execInstantiateComponet(ctx, v, ast.values[1]);
|
||||
if (ctx.error) return;
|
||||
// if (num.type == "SIGNAL") return error(ctx, ast, "Cannot assign to a signal with `=` use <-- or <== ops");
|
||||
if ((typ == "SIGNAL")&&(ast.op == "=")) return error(ctx, ast, "Cannot assign to a signal with `=` use <-- or <== ops");
|
||||
if ((["NUMBER", "COMPONENT"].indexOf(typ) >= 0 )&&(ast.op != "=")) return error(ctx, ast, `Cannot assign to a var with ${ast.op}. use = op`);
|
||||
|
||||
const res = exec(ctx, ast.values[1]);
|
||||
if (ctx.error) return;
|
||||
|
||||
setScope(ctx, v.name, v.selectors, res);
|
||||
setScopeRef(ctx, v.name, sels, res);
|
||||
|
||||
return v;
|
||||
}
|
||||
@@ -915,13 +965,13 @@ function execMul(ctx, ast) {
|
||||
function execVarAddAssignement(ctx, ast) {
|
||||
const res = execAdd(ctx,{ values: [ast.values[0], ast.values[1]] } );
|
||||
if (ctx.error) return;
|
||||
return execVarAssignement(ctx, { values: [ast.values[0], res] });
|
||||
return execVarAssignement(ctx, { op:"=", values: [ast.values[0], res] });
|
||||
}
|
||||
|
||||
function execVarMulAssignement(ctx, ast) {
|
||||
const res = execMul(ctx,{ values: [ast.values[0], ast.values[1]] } );
|
||||
if (ctx.error) return;
|
||||
return execVarAssignement(ctx, { values: [ast.values[0], res] });
|
||||
return execVarAssignement(ctx, { op:"=", values: [ast.values[0], res] });
|
||||
}
|
||||
|
||||
function execPlusPlusRight(ctx, ast) {
|
||||
@@ -929,7 +979,7 @@ function execPlusPlusRight(ctx, ast) {
|
||||
if (ctx.error) return;
|
||||
const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||
if (ctx.error) return;
|
||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
||||
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||
return resBefore;
|
||||
}
|
||||
|
||||
@@ -937,7 +987,7 @@ function execPlusPlusLeft(ctx, ast) {
|
||||
if (ctx.error) return;
|
||||
const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||
if (ctx.error) return;
|
||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
||||
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||
return resAfter;
|
||||
}
|
||||
|
||||
@@ -946,7 +996,7 @@ function execMinusMinusRight(ctx, ast) {
|
||||
if (ctx.error) return;
|
||||
const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||
if (ctx.error) return;
|
||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
||||
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||
return resBefore;
|
||||
}
|
||||
|
||||
@@ -954,7 +1004,7 @@ function execMinusMinusLeft(ctx, ast) {
|
||||
if (ctx.error) return;
|
||||
const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||
if (ctx.error) return;
|
||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
||||
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||
return resAfter;
|
||||
}
|
||||
|
||||
@@ -1075,6 +1125,8 @@ function execConstrain(ctx, ast) {
|
||||
ctx.constraints.push(lc.toQEQ(res));
|
||||
}
|
||||
|
||||
if (ctx.constraints.length % 10000 == 0) console.log("Constraints: " + ctx.constraints.length);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
{
|
||||
"mainCode": "{\n}\n",
|
||||
"signalName2Idx": {
|
||||
"one": 0,
|
||||
"main.in": 2,
|
||||
"main.out": 1,
|
||||
"main.internal.in": 2,
|
||||
"main.internal.out": 2
|
||||
},
|
||||
"components": [
|
||||
{
|
||||
"name": "main",
|
||||
"params": {},
|
||||
"template": "InOut",
|
||||
"inputSignals": 1
|
||||
},
|
||||
{
|
||||
"name": "main.internal",
|
||||
"params": {},
|
||||
"template": "Internal",
|
||||
"inputSignals": 1
|
||||
}
|
||||
],
|
||||
"componentName2Idx": {
|
||||
"main": 0,
|
||||
"main.internal": 1
|
||||
},
|
||||
"signals": [
|
||||
{
|
||||
"names": [
|
||||
"one"
|
||||
],
|
||||
"triggerComponents": []
|
||||
},
|
||||
{
|
||||
"names": [
|
||||
"main.out"
|
||||
],
|
||||
"triggerComponents": []
|
||||
},
|
||||
{
|
||||
"names": [
|
||||
"main.in",
|
||||
"main.internal.in",
|
||||
"main.internal.out"
|
||||
],
|
||||
"triggerComponents": [
|
||||
0,
|
||||
1
|
||||
]
|
||||
}
|
||||
],
|
||||
"constraints": [
|
||||
[
|
||||
{},
|
||||
{},
|
||||
{
|
||||
"1": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
|
||||
"2": "1"
|
||||
}
|
||||
]
|
||||
],
|
||||
"templates": {
|
||||
"Internal": "function(ctx) {\n ctx.setSignal(\"out\", [], ctx.getSignal(\"in\", []));\n}\n",
|
||||
"InOut": "function(ctx) {\n ctx.setPin(\"internal\", [], \"in\", [], ctx.getSignal(\"in\", []));\n ctx.setSignal(\"out\", [], ctx.getPin(\"internal\", [], \"out\", []));\n}\n"
|
||||
},
|
||||
"functions": {},
|
||||
"nPrvInputs": 0,
|
||||
"nPubInputs": 1,
|
||||
"nInputs": 1,
|
||||
"nOutputs": 1,
|
||||
"nVars": 3,
|
||||
"nConstants": 0,
|
||||
"nSignals": 3
|
||||
}
|
||||
Reference in New Issue
Block a user