mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 11:16:42 +01:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80846667ea | ||
|
|
7181c372d9 | ||
|
|
aecc28a79b | ||
|
|
51ff27b9c6 | ||
|
|
6985892f86 | ||
|
|
bacb7afde7 | ||
|
|
d04eff6c0d | ||
|
|
230894921e | ||
|
|
64029e1842 | ||
|
|
700412f23d | ||
|
|
832077fbe9 | ||
|
|
0df0ac712d | ||
|
|
67a35ee400 |
14
cli.js
14
cli.js
@@ -34,8 +34,16 @@ const argv = require("yargs")
|
|||||||
.alias("o", "output")
|
.alias("o", "output")
|
||||||
.help("h")
|
.help("h")
|
||||||
.alias("h", "help")
|
.alias("h", "help")
|
||||||
.alias("v", "verbose")
|
.option("verbose", {
|
||||||
.alias("f", "fast")
|
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
|
.epilogue(`Copyright (C) 2018 0kims association
|
||||||
This program comes with ABSOLUTELY NO WARRANTY;
|
This program comes with ABSOLUTELY NO WARRANTY;
|
||||||
This is free software, and you are welcome to redistribute it
|
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 fullFileName = path.resolve(process.cwd(), inputFile);
|
||||||
const outName = argv.output ? argv.output : "circuit.json";
|
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");
|
fs.writeFileSync(outName, JSON.stringify(cir, null, 1), "utf8");
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}, (err) => {
|
}, (err) => {
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "circom",
|
"name": "circom",
|
||||||
"version": "0.0.31",
|
"version": "0.0.35",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "circom",
|
"name": "circom",
|
||||||
"version": "0.0.31",
|
"version": "0.0.35",
|
||||||
"description": "Language to generate logic circuits",
|
"description": "Language to generate logic circuits",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"directories": {
|
"directories": {
|
||||||
|
|||||||
171
src/compiler.js
171
src/compiler.js
@@ -65,7 +65,8 @@ async function compile(srcFile, options) {
|
|||||||
functions: {},
|
functions: {},
|
||||||
functionParams: {},
|
functionParams: {},
|
||||||
filePath: fullFilePath,
|
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");
|
throw new Error("A main component must be defined");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ctx.verbose) console.log("Classify Signals");
|
||||||
classifySignals(ctx);
|
classifySignals(ctx);
|
||||||
|
|
||||||
|
if (ctx.verbose) console.log("Reduce Constraints");
|
||||||
reduceConstants(ctx);
|
reduceConstants(ctx);
|
||||||
if (options.reduceConstraints) {
|
if (options.reduceConstraints) {
|
||||||
|
|
||||||
@@ -217,6 +220,7 @@ function generateWitnessNames(ctx) {
|
|||||||
function reduceConstants(ctx) {
|
function reduceConstants(ctx) {
|
||||||
const newConstraints = [];
|
const newConstraints = [];
|
||||||
for (let i=0; i<ctx.constraints.length; i++) {
|
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]);
|
const c = lc.canonize(ctx, ctx.constraints[i]);
|
||||||
if (!lc.isZero(c)) {
|
if (!lc.isZero(c)) {
|
||||||
newConstraints.push(c);
|
newConstraints.push(c);
|
||||||
@@ -226,66 +230,135 @@ function reduceConstants(ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function reduceConstrains(ctx) {
|
function reduceConstrains(ctx) {
|
||||||
const newConstraints = [];
|
indexVariables();
|
||||||
for (let i=0; i<ctx.constraints.length; i++) {
|
let possibleConstraints = Object.keys(ctx.constraints);
|
||||||
const c = ctx.constraints[i];
|
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];
|
||||||
|
|
||||||
// Swap a and b if b has more variables.
|
// Swap a and b if b has more variables.
|
||||||
if (Object.keys(c.b).length > Object.keys(c.a).length) {
|
if (Object.keys(c.b).length > Object.keys(c.a).length) {
|
||||||
const aux = c.a;
|
const aux = c.a;
|
||||||
c.a=c.b;
|
c.a=c.b;
|
||||||
c.b=aux;
|
c.b=aux;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mov to C if possible.
|
// Mov to C if possible.
|
||||||
if (isConstant(c.a)) {
|
if (isConstant(c.a)) {
|
||||||
const ct = {type: "NUMBER", value: c.a.values["one"]};
|
const ct = {type: "NUMBER", value: c.a.values["one"]};
|
||||||
c.c = lc.add(lc.mul(c.b, ct), c.c);
|
c.c = lc.add(lc.mul(c.b, ct), c.c);
|
||||||
c.a = { type: "LINEARCOMBINATION", values: {} };
|
c.a = { type: "LINEARCOMBINATION", values: {} };
|
||||||
c.b = { type: "LINEARCOMBINATION", values: {} };
|
c.b = { type: "LINEARCOMBINATION", values: {} };
|
||||||
}
|
}
|
||||||
if (isConstant(c.b)) {
|
if (isConstant(c.b)) {
|
||||||
const ct = {type: "NUMBER", value: c.b.values["one"]};
|
const ct = {type: "NUMBER", value: c.b.values["one"]};
|
||||||
c.c = lc.add(lc.mul(c.a, ct), c.c);
|
c.c = lc.add(lc.mul(c.a, ct), c.c);
|
||||||
c.a = { type: "LINEARCOMBINATION", values: {} };
|
c.a = { type: "LINEARCOMBINATION", values: {} };
|
||||||
c.b = { type: "LINEARCOMBINATION", values: {} };
|
c.b = { type: "LINEARCOMBINATION", values: {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lc.isZero(c.a) || lc.isZero(c.b)) {
|
if (lc.isZero(c.a) || lc.isZero(c.b)) {
|
||||||
const isolatedSignal = getFirstInternalSignal(ctx, c.c);
|
const isolatedSignal = getFirstInternalSignal(ctx, c.c);
|
||||||
if (isolatedSignal) {
|
if (isolatedSignal) {
|
||||||
const isolatedSignalEquivalence = {
|
|
||||||
type: "LINEARCOMBINATION",
|
let lSignal = ctx.signals[isolatedSignal];
|
||||||
values: {}
|
while (lSignal.equivalence) {
|
||||||
};
|
lSignal = ctx.signals[lSignal.equivalence];
|
||||||
const invCoef = c.c.values[isolatedSignal].modInv(__P__);
|
}
|
||||||
for (const s in c.c.values) {
|
|
||||||
if (s != isolatedSignal) {
|
|
||||||
const v = __P__.minus(c.c.values[s]).times(invCoef).mod(__P__);
|
const isolatedSignalEquivalence = {
|
||||||
if (!v.isZero()) {
|
type: "LINEARCOMBINATION",
|
||||||
isolatedSignalEquivalence.values[s] = v;
|
values: {}
|
||||||
|
};
|
||||||
|
const invCoef = c.c.values[isolatedSignal].modInv(__P__);
|
||||||
|
for (const s in c.c.values) {
|
||||||
|
if (s != isolatedSignal) {
|
||||||
|
const v = __P__.minus(c.c.values[s]).times(invCoef).mod(__P__);
|
||||||
|
if (!v.isZero()) {
|
||||||
|
isolatedSignalEquivalence.values[s] = v;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (let j=0; j<newConstraints.length; j++) {
|
for (let j in lSignal.inConstraints) {
|
||||||
newConstraints[j] = lc.substitute(newConstraints[j], isolatedSignal, isolatedSignalEquivalence);
|
if ((j!=i)&&(ctx.constraints[j])) {
|
||||||
|
ctx.constraints[j] = lc.substitute(ctx.constraints[j], isolatedSignal, isolatedSignalEquivalence);
|
||||||
|
linkSignalsConstraint(j);
|
||||||
|
if (j<i) {
|
||||||
|
nextPossibleConstraints[j] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.constraints[i] = null;
|
||||||
|
|
||||||
|
lSignal.category = "constant";
|
||||||
|
} else {
|
||||||
|
if (lc.isZero(c.c)) ctx.constraints[i] = null;
|
||||||
}
|
}
|
||||||
for (let j=i+1; j<ctx.constraints.length; j++ ) {
|
|
||||||
ctx.constraints[j] = lc.substitute(ctx.constraints[j], isolatedSignal, isolatedSignalEquivalence);
|
|
||||||
}
|
|
||||||
c.a={ type: "LINEARCOMBINATION", values: {} };
|
|
||||||
c.b={ type: "LINEARCOMBINATION", values: {} };
|
|
||||||
c.c={ type: "LINEARCOMBINATION", values: {} };
|
|
||||||
isolatedSignal.category = "constant";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
possibleConstraints = Object.keys(nextPossibleConstraints);
|
||||||
|
}
|
||||||
|
unindexVariables();
|
||||||
|
|
||||||
if (!lc.isZero(c)) {
|
// Pack the constraints
|
||||||
newConstraints.push(c);
|
let o = 0;
|
||||||
|
for (let i=0; i<ctx.constraints.length; i++) {
|
||||||
|
if (ctx.constraints[i]) {
|
||||||
|
if (o != i) {
|
||||||
|
ctx.constraints[o] = ctx.constraints[i];
|
||||||
|
}
|
||||||
|
o++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx.constraints = newConstraints;
|
ctx.constraints.length = o;
|
||||||
|
|
||||||
|
function indexVariables() {
|
||||||
|
for (let i=0; i<ctx.constraints.length; i++) linkSignalsConstraint(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
function linkSignalsConstraint(cidx) {
|
||||||
|
const ct = ctx.constraints[cidx];
|
||||||
|
for (let k in ct.a.values) linkSignal(k, cidx);
|
||||||
|
for (let k in ct.b.values) linkSignal(k, cidx);
|
||||||
|
for (let k in ct.c.values) linkSignal(k, cidx);
|
||||||
|
}
|
||||||
|
|
||||||
|
function unindexVariables() {
|
||||||
|
for (let s in ctx.signals) {
|
||||||
|
let lSignal = ctx.signals[s];
|
||||||
|
while (lSignal.equivalence) {
|
||||||
|
lSignal = ctx.signals[lSignal.equivalence];
|
||||||
|
}
|
||||||
|
if (lSignal.inConstraints) delete lSignal.inConstraints;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
function unlinkSignal(signalName, cidx) {
|
||||||
|
let lSignal = ctx.signals[signalName];
|
||||||
|
while (lSignal.equivalence) {
|
||||||
|
lSignal = ctx.signals[lSignal.equivalence];
|
||||||
|
}
|
||||||
|
if ((lSignal.inConstraints)&&(lSignal.inConstraints[cidx])) {
|
||||||
|
delete lSignal.inConstraints[cidx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
function linkSignal(signalName, cidx) {
|
||||||
|
let lSignal = ctx.signals[signalName];
|
||||||
|
while (lSignal.equivalence) {
|
||||||
|
lSignal = ctx.signals[lSignal.equivalence];
|
||||||
|
}
|
||||||
|
if (!lSignal.inConstraints) lSignal.inConstraints = {};
|
||||||
|
lSignal.inConstraints[cidx] = true;
|
||||||
|
}
|
||||||
|
|
||||||
function getFirstInternalSignal(ctx, l) {
|
function getFirstInternalSignal(ctx, l) {
|
||||||
for (let k in l.values) {
|
for (let k in l.values) {
|
||||||
|
|||||||
217
src/exec.js
217
src/exec.js
@@ -181,24 +181,6 @@ function iterateSelectors(ctx, sizes, baseName, fn) {
|
|||||||
return res;
|
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) {
|
function getScope(ctx, name, selectors) {
|
||||||
|
|
||||||
@@ -221,11 +203,57 @@ function getScope(ctx, name, selectors) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
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;
|
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) {
|
function getScopeLevel(ctx, name) {
|
||||||
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
||||||
if (ctx.scopes[i][name]) return i;
|
if (ctx.scopes[i][name]) return i;
|
||||||
@@ -249,11 +277,14 @@ function execTemplateDef(ctx, ast) {
|
|||||||
}
|
}
|
||||||
scope[ast.name] = {
|
scope[ast.name] = {
|
||||||
type: "TEMPLATE",
|
type: "TEMPLATE",
|
||||||
params: ast.params,
|
value: {
|
||||||
block: ast.block,
|
type: "TEMPLATE",
|
||||||
fileName: ctx.fileName,
|
params: ast.params,
|
||||||
filePath: ctx.filePath,
|
block: ast.block,
|
||||||
scopes: copyScope(ctx.scopes)
|
fileName: ctx.fileName,
|
||||||
|
filePath: ctx.filePath,
|
||||||
|
scopes: copyScope(ctx.scopes)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,11 +297,14 @@ function execFunctionDef(ctx, ast) {
|
|||||||
ctx.functionParams[ast.name] = ast.params;
|
ctx.functionParams[ast.name] = ast.params;
|
||||||
scope[ast.name] = {
|
scope[ast.name] = {
|
||||||
type: "FUNCTION",
|
type: "FUNCTION",
|
||||||
params: ast.params,
|
value: {
|
||||||
block: ast.block,
|
type: "FUNCTION",
|
||||||
fileName: ctx.fileName,
|
params: ast.params,
|
||||||
filePath: ctx.filePath,
|
block: ast.block,
|
||||||
scopes: copyScope(ctx.scopes)
|
fileName: ctx.fileName,
|
||||||
|
filePath: ctx.filePath,
|
||||||
|
scopes: copyScope(ctx.scopes)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,15 +328,18 @@ 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";
|
ctx.components[fullName] = "UNINSTANTIATED";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: "COMPONENT",
|
type: "COMPONENT",
|
||||||
fullName: fullName
|
fullName: fullName
|
||||||
};
|
};
|
||||||
});
|
})
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: "VARIABLE",
|
type: "VARIABLE",
|
||||||
@@ -378,7 +415,10 @@ function execInstantiateComponet(ctx, vr, fn) {
|
|||||||
|
|
||||||
const scope = {};
|
const scope = {};
|
||||||
for (let i=0; i< template.params.length; i++) {
|
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]);
|
ctx.components[ctx.currentComponent].params[template.params[i]] = extractValue(paramValues[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,7 +470,10 @@ function execFunctionCall(ctx, ast) {
|
|||||||
|
|
||||||
const scope = {};
|
const scope = {};
|
||||||
for (let i=0; i< fnc.params.length; i++) {
|
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;
|
ctx.fileName = fnc.fileName;
|
||||||
@@ -472,21 +515,24 @@ function execDeclareSignal(ctx, ast) {
|
|||||||
sizes.push( size.value.toJSNumber() );
|
sizes.push( size.value.toJSNumber() );
|
||||||
}
|
}
|
||||||
|
|
||||||
scope[ast.name.name] = iterateSelectors(ctx, sizes, baseName, function(fullName) {
|
scope[ast.name.name] = {
|
||||||
ctx.signals[fullName] = {
|
type: "SIGNAL",
|
||||||
fullName: fullName,
|
value: iterateSelectors(ctx, sizes, baseName, function(fullName) {
|
||||||
direction: ast.declareType == "SIGNALIN" ? "IN" : (ast.declareType == "SIGNALOUT" ? "OUT" : ""),
|
ctx.signals[fullName] = {
|
||||||
private: ast.private,
|
fullName: fullName,
|
||||||
component: ctx.currentComponent,
|
direction: ast.declareType == "SIGNALIN" ? "IN" : (ast.declareType == "SIGNALOUT" ? "OUT" : ""),
|
||||||
equivalence: "",
|
private: ast.private,
|
||||||
alias: [fullName]
|
component: ctx.currentComponent,
|
||||||
};
|
equivalence: "",
|
||||||
ctx.components[ctx.currentComponent].signals.push(fullName);
|
alias: [fullName]
|
||||||
return {
|
};
|
||||||
type: "SIGNAL",
|
ctx.components[ctx.currentComponent].signals.push(fullName);
|
||||||
fullName: fullName,
|
return {
|
||||||
};
|
type: "SIGNAL",
|
||||||
});
|
fullName: fullName,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
};
|
||||||
return {
|
return {
|
||||||
type: "VARIABLE",
|
type: "VARIABLE",
|
||||||
name: ast.name.name,
|
name: ast.name.name,
|
||||||
@@ -509,12 +555,15 @@ function execDeclareVariable(ctx, ast) {
|
|||||||
sizes.push( size.value.toJSNumber() );
|
sizes.push( size.value.toJSNumber() );
|
||||||
}
|
}
|
||||||
|
|
||||||
scope[ast.name.name] = iterateSelectors(ctx, sizes, "", function() {
|
scope[ast.name.name] = {
|
||||||
return {
|
type: "VARIABLE",
|
||||||
type: "NUMBER",
|
value: iterateSelectors(ctx, sizes, "", function() {
|
||||||
value: bigInt(0)
|
return {
|
||||||
};
|
type: "NUMBER",
|
||||||
});
|
value: bigInt(0)
|
||||||
|
};
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: "VARIABLE",
|
type: "VARIABLE",
|
||||||
@@ -631,19 +680,20 @@ function execVarAssignement(ctx, ast) {
|
|||||||
} else {
|
} else {
|
||||||
v = ast.values[0];
|
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 (ctx.error) return;
|
||||||
|
|
||||||
if ((typeof(num) != "object")||(num == null)) return error(ctx, ast, "Variable not defined");
|
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 (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]);
|
const res = exec(ctx, ast.values[1]);
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
|
|
||||||
setScope(ctx, v.name, v.selectors, res);
|
setScopeRef(ctx, v.name, sels, res);
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
@@ -915,13 +965,13 @@ function execMul(ctx, ast) {
|
|||||||
function execVarAddAssignement(ctx, ast) {
|
function execVarAddAssignement(ctx, ast) {
|
||||||
const res = execAdd(ctx,{ values: [ast.values[0], ast.values[1]] } );
|
const res = execAdd(ctx,{ values: [ast.values[0], ast.values[1]] } );
|
||||||
if (ctx.error) return;
|
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) {
|
function execVarMulAssignement(ctx, ast) {
|
||||||
const res = execMul(ctx,{ values: [ast.values[0], ast.values[1]] } );
|
const res = execMul(ctx,{ values: [ast.values[0], ast.values[1]] } );
|
||||||
if (ctx.error) return;
|
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) {
|
function execPlusPlusRight(ctx, ast) {
|
||||||
@@ -929,7 +979,7 @@ function execPlusPlusRight(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||||
return resBefore;
|
return resBefore;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -937,7 +987,7 @@ function execPlusPlusLeft(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||||
return resAfter;
|
return resAfter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -946,7 +996,7 @@ function execMinusMinusRight(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||||
return resBefore;
|
return resBefore;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -954,7 +1004,7 @@ function execMinusMinusLeft(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } );
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
execVarAssignement(ctx, { values: [ast.values[0], resAfter] });
|
execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] });
|
||||||
return resAfter;
|
return resAfter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -996,7 +1046,12 @@ function execSignalAssign(ctx, ast) {
|
|||||||
|
|
||||||
let sDest=ctx.signals[dst.fullName];
|
let sDest=ctx.signals[dst.fullName];
|
||||||
if (!sDest) return error(ctx, ast, "Invalid signal: "+dst.fullName);
|
if (!sDest) return error(ctx, ast, "Invalid signal: "+dst.fullName);
|
||||||
while (sDest.equivalence) sDest=ctx.signals[sDest.equivalence];
|
|
||||||
|
let isOut = (sDest.component == "main")&&(sDest.direction=="OUT");
|
||||||
|
while (sDest.equivalence) {
|
||||||
|
sDest=ctx.signals[sDest.equivalence];
|
||||||
|
isOut = isOut || ((sDest.component == "main")&&(sDest.direction=="OUT"));
|
||||||
|
}
|
||||||
|
|
||||||
if (sDest.value) return error(ctx, ast, "Signals cannot be assigned twice");
|
if (sDest.value) return error(ctx, ast, "Signals cannot be assigned twice");
|
||||||
|
|
||||||
@@ -1024,10 +1079,20 @@ function execSignalAssign(ctx, ast) {
|
|||||||
|
|
||||||
let assignValue = true;
|
let assignValue = true;
|
||||||
if (src.type == "SIGNAL") {
|
if (src.type == "SIGNAL") {
|
||||||
sDest.equivalence = src.fullName;
|
let sSrc = ctx.signals[src.fullName];
|
||||||
sDest.alias = sDest.alias.concat(src.alias);
|
let isIn = (sSrc.component == "main")&&(sSrc.direction == "IN");
|
||||||
while (sDest.equivalence) sDest=ctx.signals[sDest.equivalence];
|
while (sSrc.equivalence) {
|
||||||
assignValue = false;
|
sSrc=ctx.signals[sSrc.equivalence];
|
||||||
|
isIn = isIn || ((sSrc.component == "main")&&(sSrc.direction == "IN"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if an out is assigned directly to an input.
|
||||||
|
if ((!isIn)||(!isOut)) {
|
||||||
|
sDest.equivalence = src.fullName;
|
||||||
|
sDest.alias = sDest.alias.concat(src.alias);
|
||||||
|
while (sDest.equivalence) sDest=ctx.signals[sDest.equivalence];
|
||||||
|
assignValue = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (assignValue) {
|
if (assignValue) {
|
||||||
@@ -1046,6 +1111,8 @@ function execSignalAssign(ctx, ast) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function execConstrain(ctx, ast) {
|
function execConstrain(ctx, ast) {
|
||||||
|
ast.fileName = ctx.fileName;
|
||||||
|
ast.filePath = ctx.filePath;
|
||||||
const a = exec(ctx, ast.values[0]);
|
const a = exec(ctx, ast.values[0]);
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const b = exec(ctx, ast.values[1]);
|
const b = exec(ctx, ast.values[1]);
|
||||||
@@ -1058,6 +1125,8 @@ function execConstrain(ctx, ast) {
|
|||||||
ctx.constraints.push(lc.toQEQ(res));
|
ctx.constraints.push(lc.toQEQ(res));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ctx.constraints.length % 10000 == 0) console.log("Constraints: " + ctx.constraints.length);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -422,11 +422,13 @@ function genConstrain(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const b = gen(ctx, ast.values[1]);
|
const b = gen(ctx, ast.values[1]);
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
return `ctx.assert(${a}, ${b})`;
|
const strErr = ast.fileName + ":" + ast.first_line + ":" + ast.first_column;
|
||||||
|
return `ctx.assert(${a}, ${b}, \"${strErr}\")`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genSignalAssignConstrain(ctx, ast) {
|
function genSignalAssignConstrain(ctx, ast) {
|
||||||
return genVarAssignement(ctx, ast) + ";\n" + genConstrain(ctx, ast);
|
// return genVarAssignement(ctx, ast) + ";\n" + genConstrain(ctx, ast);
|
||||||
|
return genVarAssignement(ctx, ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
function genVarAddAssignement(ctx, ast) {
|
function genVarAddAssignement(ctx, ast) {
|
||||||
|
|||||||
@@ -59,4 +59,9 @@ describe("Sum test", () => {
|
|||||||
assert(witness[1].equals(bigInt(37)));
|
assert(witness[1].equals(bigInt(37)));
|
||||||
assert(witness[2].equals(bigInt(6)));
|
assert(witness[2].equals(bigInt(6)));
|
||||||
});
|
});
|
||||||
|
it("Should compile a code with compute", async () => {
|
||||||
|
const cirDef = await compiler(path.join(__dirname, "circuits", "inout.circom"));
|
||||||
|
|
||||||
|
assert.equal(cirDef.constraints.length, 1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
18
test/circuits/inout.circom
Normal file
18
test/circuits/inout.circom
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
template Internal() {
|
||||||
|
signal input in;
|
||||||
|
signal output out;
|
||||||
|
|
||||||
|
out <== in;
|
||||||
|
}
|
||||||
|
|
||||||
|
template InOut() {
|
||||||
|
signal input in;
|
||||||
|
signal output out;
|
||||||
|
|
||||||
|
component internal = Internal();
|
||||||
|
|
||||||
|
internal.in <== in;
|
||||||
|
internal.out ==> out;
|
||||||
|
}
|
||||||
|
|
||||||
|
component main = InOut();
|
||||||
Reference in New Issue
Block a user