Compare commits

...

13 Commits

Author SHA1 Message Date
Jordi Baylina
dcfb9ab8b4 0.0.19 2018-10-29 07:09:54 +01:00
Jordi Baylina
bfdf17fd89 Fix undefined if 2018-10-29 07:09:28 +01:00
Jordi Baylina
9d0b27a7e8 Tests added and Comparators 2018-10-26 17:34:02 +02:00
Jordi Baylina
4d79038fd8 0.0.18 2018-10-25 17:04:13 +02:00
Jordi Baylina
95755c4afe remove more memory 2018-10-25 17:04:01 +02:00
Jordi Baylina
afc60ec033 0.0.17 2018-10-25 10:25:39 +02:00
Jordi Baylina
77393e2d0c Increase memory in cli.js 2018-10-25 10:25:32 +02:00
Jordi Baylina
2db08a0a34 0.0.16 2018-10-25 09:44:34 +02:00
Jordi Baylina
23255de508 deps 2018-10-25 09:44:14 +02:00
Jordi Baylina
7c03ae4033 0.0.15 2018-10-24 20:28:08 +02:00
Jordi Baylina
5e58584a01 Verbose and error if main is not defined 2018-10-24 20:27:34 +02:00
Jordi Baylina
cb9a5b536e 0.0.14 2018-10-24 20:06:09 +02:00
Jordi Baylina
70c88be334 One and only one file compilation at a time 2018-10-24 20:05:50 +02:00
26 changed files with 352 additions and 147 deletions

View File

@@ -17,6 +17,9 @@
along with circom. If not, see <https://www.gnu.org/licenses/>. along with circom. If not, see <https://www.gnu.org/licenses/>.
*/ */
include "comparators.circom";
template Num2Bits(n) { template Num2Bits(n) {
signal input in; signal input in;
signal output out[n]; signal output out[n];
@@ -43,3 +46,27 @@ template Bits2Num(n) {
lc1 ==> out; lc1 ==> out;
} }
template Num2BitsNeg(n) {
signal input in;
signal output out[n];
var lc1=0;
component isZero;
isZero = IsZero();
var neg = n == 0 ? 0 : 2**n - in;
for (var i = 0; i<n; i++) {
out[i] <-- (neg >> i) & 1;
out[i] * (out[i] -1 ) === 0;
lc1 += out[i] * 2**i;
}
in ==> isZero.in;
lc1 + isZero.out * 2**n === 2**n - in;
}

View File

@@ -0,0 +1,55 @@
include "bitify.circom";
include "binsum.circom";
template IsZero() {
signal input in;
signal output out;
signal inv;
inv <-- in!=0 ? 1/in : 0;
out <== -in*inv +1;
in*out === 0;
}
template IsEqual() {
signal input in[2];
signal output out;
component isz = IsZero();
in[1] - in[0] ==> isz.in;
isz.out ==> out;
}
// N is the number of bits the input have.
// The MSF is the sign bit.
template LessThan(n) {
signal input in[2];
signal output out;
component num2Bits0;
component num2Bits1;
component adder;
adder = BinSum(n, 2);
num2Bits0 = Num2Bits(n);
num2Bits1 = Num2BitsNeg(n);
in[0] ==> num2Bits0.in;
in[1] ==> num2Bits1.in;
var i;
for (i=0;i<n;i++) {
num2Bits0.out[i] ==> adder.in[0][i];
num2Bits1.out[i] ==> adder.in[1][i];
}
adder.out[n-1] ==> out;
}

View File

@@ -1,67 +0,0 @@
/*
Copyright 2018 0KIMS association.
This file is part of circom (Zero Knowledge Circuit Compiler).
circom is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
circom is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with circom. If not, see <https://www.gnu.org/licenses/>.
*/
template XOR() {
signal input a;
signal input b;
signal output out;
out <== a + b - 2*a*b;
}
template AND() {
signal input a;
signal input b;
signal output out;
out <== a*b;
}
template OR() {
signal input a;
signal input b;
signal output out;
out <== a + b - a*b;
}
template NOT() {
signal input in;
signal output out;
out <== 1 + in - 2*in;
}
template NAND() {
signal input a;
signal input b;
signal output out;
out <== 1 - a*b;
}
template NOR() {
signal input a;
signal input b;
signal output out;
out <== a*b + 1 - a - b;
}

View File

@@ -18,7 +18,7 @@
*/ */
include "sha256compression.circom"; include "sha256compression.circom";
include "bitify.circom" include "../bitify.circom"
template Sha256_2() { template Sha256_2() {
signal input a; signal input a;

View File

@@ -20,7 +20,7 @@
include "constants.circom"; include "constants.circom";
include "t1.circom"; include "t1.circom";
include "t2.circom"; include "t2.circom";
include "binsum.circom"; include "../binsum.circom";
include "sigmaplus.circom"; include "sigmaplus.circom";
template Sha256compression() { template Sha256compression() {

View File

@@ -17,7 +17,7 @@
along with circom. If not, see <https://www.gnu.org/licenses/>. along with circom. If not, see <https://www.gnu.org/licenses/>.
*/ */
include "binsum.circom" include "../binsum.circom"
include "sigma.circom" include "sigma.circom"
template SigmaPlus() { template SigmaPlus() {

View File

@@ -17,7 +17,7 @@
along with circom. If not, see <https://www.gnu.org/licenses/>. along with circom. If not, see <https://www.gnu.org/licenses/>.
*/ */
include "binsum.circom"; include "../binsum.circom";
include "sigma.circom"; include "sigma.circom";
include "ch.circom"; include "ch.circom";

View File

@@ -17,7 +17,7 @@
along with circom. If not, see <https://www.gnu.org/licenses/>. along with circom. If not, see <https://www.gnu.org/licenses/>.
*/ */
include "binsum.circom"; include "../binsum.circom";
include "sigma.circom"; include "sigma.circom";
include "maj.circom" include "maj.circom"

28
cli.js
View File

@@ -34,6 +34,7 @@ const argv = require("yargs")
.alias("o", "output") .alias("o", "output")
.help("h") .help("h")
.alias("h", "help") .alias("h", "help")
.alias("v", "verbose")
.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
@@ -41,15 +42,34 @@ const argv = require("yargs")
repo directory at https://github.com/iden3/circom `) repo directory at https://github.com/iden3/circom `)
.argv; .argv;
const fullFileName = path.resolve(process.cwd(), argv._[0]);
let inputFile;
if (argv._.length == 0) {
inputFile = "circuit.circom";
} else if (argv._.length == 1) {
inputFile = argv._[0];
} else {
console.log("Only one circuit at a time is permited");
process.exit(1);
}
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).then( (cir) => { compiler(fullFileName).then( (cir) => {
fs.writeFileSync(outName, JSON.stringify(cir, null, 1), "utf8"); fs.writeFileSync(outName, JSON.stringify(cir, null, 1), "utf8");
process.exit(0);
}, (err) => { }, (err) => {
console.log(err); // console.log(err);
console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column} ${err.errStr}`); if (err.pos) {
console.error(JSON.stringify(err.ast, null, 1)); console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column} ${err.errStr}`);
} else {
console.log(err.message);
if (argv.verbose) console.log(err.stack);
}
if (err.ast) {
console.error(JSON.stringify(err.ast, null, 1));
}
process.exit(1); process.exit(1);
}); });

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "circom", "name": "circom",
"version": "0.0.13", "version": "0.0.19",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "circom", "name": "circom",
"version": "0.0.13", "version": "0.0.19",
"description": "Language to generate logic circuits", "description": "Language to generate logic circuits",
"main": "index.js", "main": "index.js",
"directories": { "directories": {
@@ -38,6 +38,6 @@
"eslint": "^5.0.1", "eslint": "^5.0.1",
"eslint-plugin-mocha": "^5.0.0", "eslint-plugin-mocha": "^5.0.0",
"jison": "^0.4.18", "jison": "^0.4.18",
"snarkjs": "0.1.4" "snarkjs": "0.1.5"
} }
} }

View File

@@ -62,8 +62,13 @@ async function compile(srcFile) {
fileName: fullFileName fileName: fullFileName
}; };
exec(ctx, ast); exec(ctx, ast);
if (!ctx.components["main"]) {
throw new Error("A main component must be defined");
}
classifySignals(ctx); classifySignals(ctx);
reduceConstants(ctx); reduceConstants(ctx);

View File

@@ -98,6 +98,8 @@ function exec(ctx, ast) {
return execGte(ctx, ast); return execGte(ctx, ast);
} else if (ast.op == "==") { } else if (ast.op == "==") {
return execEq(ctx, ast); return execEq(ctx, ast);
} else if (ast.op == "!=") {
return execNeq(ctx, ast);
} else if (ast.op == "?") { } else if (ast.op == "?") {
return execTerCon(ctx, ast); return execTerCon(ctx, ast);
} else { } else {
@@ -545,15 +547,17 @@ function execFor(ctx, ast) {
let v = exec(ctx, ast.condition); let v = exec(ctx, ast.condition);
if (ctx.error) return; if (ctx.error) return;
while ((v.value.neq(0))&&(!ctx.returnValue)) { if (typeof v.value != "undefined") {
exec(ctx, ast.body); while ((v.value.neq(0))&&(!ctx.returnValue)) {
if (ctx.error) return; exec(ctx, ast.body);
if (ctx.error) return;
exec(ctx, ast.step); exec(ctx, ast.step);
if (ctx.error) return; if (ctx.error) return;
v = exec(ctx, ast.condition); v = exec(ctx, ast.condition);
if (ctx.error) return; if (ctx.error) return;
}
} }
} }
@@ -561,12 +565,14 @@ function execWhile(ctx, ast) {
let v = exec(ctx, ast.condition); let v = exec(ctx, ast.condition);
if (ctx.error) return; if (ctx.error) return;
while ((v.value.neq(0))&&(!ctx.returnValue)) { if (typeof v.value != "undefined") {
exec(ctx, ast.body); while ((v.value.neq(0))&&(!ctx.returnValue)) {
if (ctx.error) return; exec(ctx, ast.body);
if (ctx.error) return;
v = exec(ctx, ast.condition); v = exec(ctx, ast.condition);
if (ctx.error) return; if (ctx.error) return;
}
} }
} }
@@ -574,13 +580,15 @@ function execIf(ctx, ast) {
let v = exec(ctx, ast.condition); let v = exec(ctx, ast.condition);
if (ctx.error) return; if (ctx.error) return;
if ((v.value.neq(0))&&(!ctx.returnValue)) { if (typeof v.value != "undefined") {
exec(ctx, ast.then); if ((v.value.neq(0))&&(!ctx.returnValue)) {
if (ctx.error) return; exec(ctx, ast.then);
} else {
if (ast.else) {
exec(ctx, ast.else);
if (ctx.error) return; if (ctx.error) return;
} else {
if (ast.else) {
exec(ctx, ast.else);
if (ctx.error) return;
}
} }
} }
} }
@@ -680,6 +688,20 @@ function execEq(ctx, ast) {
}; };
} }
function execNeq(ctx, ast) {
const a = exec(ctx, ast.values[0]);
if (ctx.error) return;
if (a.type != "NUMBER") return { type: "NUMBER" };
const b = exec(ctx, ast.values[1]);
if (ctx.error) return;
if (b.type != "NUMBER") return { type: "NUMBER" };
if (!a.value || !b.value) return { type: "NUMBER" };
return {
type: "NUMBER",
value: a.value.eq(b.value) ? bigInt(0) : bigInt(1)
};
}
function execBAnd(ctx, ast) { function execBAnd(ctx, ast) {
const a = exec(ctx, ast.values[0]); const a = exec(ctx, ast.values[0]);

View File

@@ -83,6 +83,8 @@ function gen(ctx, ast) {
return genGte(ctx, ast); return genGte(ctx, ast);
} else if (ast.op == "==") { } else if (ast.op == "==") {
return genEq(ctx, ast); return genEq(ctx, ast);
} else if (ast.op == "!=") {
return genNeq(ctx, ast);
} else if (ast.op == "?") { } else if (ast.op == "?") {
return genTerCon(ctx, ast); return genTerCon(ctx, ast);
} else { } else {
@@ -314,7 +316,9 @@ function genVariable(ctx, ast) {
if (ctx.error) return; if (ctx.error) return;
} }
if (!v) {
return error(ctx, ast, "Invalid left operand");
}
if (v.type == "VARIABLE") { if (v.type == "VARIABLE") {
return `ctx.getVar("${ast.name}",[${sels.join(",")}])`; return `ctx.getVar("${ast.name}",[${sels.join(",")}])`;
} else if (v.type == "SIGNAL") { } else if (v.type == "SIGNAL") {
@@ -530,7 +534,15 @@ function genEq(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 `bigInt(${a}).eq(bigInt(${b})) ? 1 : 0`; return `(bigInt(${a}).eq(bigInt(${b})) ? 1 : 0)`;
}
function genNeq(ctx, ast) {
const a = gen(ctx, ast.values[0]);
if (ctx.error) return;
const b = gen(ctx, ast.values[1]);
if (ctx.error) return;
return `(bigInt(${a}).eq(bigInt(${b})) ? 0 : 1)`;
} }
function genUMinus(ctx, ast) { function genUMinus(ctx, ast) {

14
test/cases.js Normal file
View File

@@ -0,0 +1,14 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const crypto = require("crypto");
const compiler = require("../index.js");
const assert = chai.assert;
describe("Sum test", () => {
it("Should compile a code with an undefined if", async() => {
await compiler(path.join(__dirname, "circuits", "undefinedif.circom"));
});
});

View File

@@ -0,0 +1,4 @@
include "../../circuits/comparators.circom";
component main = IsEqual();

View File

@@ -0,0 +1,5 @@
include "../../circuits/comparators.circom";
component main = IsZero();

View File

@@ -0,0 +1,4 @@
include "../../circuits/comparators.circom";
component main = LessThan(32);

View File

@@ -1,5 +1,5 @@
include "../../circuits/sha256/bitify.circom" include "../../circuits/bitify.circom"
include "../../circuits/sha256/binsum.circom" include "../../circuits/binsum.circom"
template A() { template A() {
signal private input a; signal private input a;

View File

@@ -0,0 +1,14 @@
template X() {
signal input i;
signal input j;
signal output out;
if (i == 0) {
out <-- i;
}
else {
out <-- j;
}
}
component main = X();

77
test/comparators.js Normal file
View File

@@ -0,0 +1,77 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const crypto = require("crypto");
const compiler = require("../index.js");
const assert = chai.assert;
describe("Sum test", () => {
it("Should create a iszero circuit", async() => {
const cirDef = await compiler(path.join(__dirname, "circuits", "iszero.circom"));
const circuit = new snarkjs.Circuit(cirDef);
let witness;
witness = circuit.calculateWitness({ "in": 111});
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
witness = circuit.calculateWitness({ "in": 0 });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(1)));
});
it("Should create a isequal circuit", async() => {
const cirDef = await compiler(path.join(__dirname, "circuits", "isequal.circom"));
const circuit = new snarkjs.Circuit(cirDef);
let witness;
witness = circuit.calculateWitness({ "in[0]": "111", "in[1]": "222" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
witness = circuit.calculateWitness({ "in[0]": "444", "in[1]": "444" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(1)));
});
it("Should create a comparison", async() => {
const cirDef = await compiler(path.join(__dirname, "circuits", "lessthan.circom"));
const circuit = new snarkjs.Circuit(cirDef);
let witness;
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(1)));
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(1)));
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(1)));
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
});
});

View File

@@ -0,0 +1,22 @@
const snarkjs = require("snarkjs");
const bigInt = snarkjs.bigInt;
module.exports = function hexBits(cir, witness, sig, nBits) {
let v = bigInt(0);
for (let i=nBits-1; i>=0; i--) {
v = v.shiftLeft(1);
const name = sig+"["+i+"]";
const idx = cir.getSignalIdx(name);
const vbit = bigInt(witness[idx].toString());
if (vbit.equals(bigInt(1))) {
v = v.add(bigInt(1));
} else if (vbit.equals(bigInt(0))) {
v;
} else {
console.log("Not Binary: "+name);
}
}
return v.toString(16);
};

View File

@@ -1,4 +0,0 @@
{
"a": "111",
"b": "222"
}

View File

@@ -8,51 +8,11 @@ const compiler = require("../index.js");
const assert = chai.assert; const assert = chai.assert;
const sha256 = require("./helpers/sha256"); const sha256 = require("./helpers/sha256");
const bigInt = require("big-integer");
function hexBits(cir, witness, sig, nBits) { // const printSignal = require("./helpers/printsignal");
let v = bigInt(0);
for (let i=nBits-1; i>=0; i--) {
v = v.shiftLeft(1);
const name = sig+"["+i+"]";
const idx = cir.getSignalIdx(name);
const vbit = bigInt(witness[idx].toString());
if (vbit.equals(bigInt(1))) {
v = v.add(bigInt(1));
} else if (vbit.equals(bigInt(0))) {
v;
} else {
console.log("Not Binary: "+name);
}
}
return v.toString(16);
}
describe("SHA256 test", () => { describe("SHA256 test", () => {
it("Should create a constant circuit", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "constants_test.circom"));
assert.equal(cirDef.nVars, 2);
const circuit = new snarkjs.Circuit(cirDef);
const witness = circuit.calculateWitness({ "in": "0xd807aa98" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt("0xd807aa98")));
});
it("Should create a sum circuit", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "sum_test.circom"));
assert.equal(cirDef.nVars, 101);
const circuit = new snarkjs.Circuit(cirDef);
const witness = circuit.calculateWitness({ "a": "111", "b": "222" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt("333")));
});
it("Should calculate a hash", async () => { it("Should calculate a hash", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom")); const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
const circuit = new snarkjs.Circuit(cirDef); const circuit = new snarkjs.Circuit(cirDef);

35
test/sum.js Normal file
View File

@@ -0,0 +1,35 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const crypto = require("crypto");
const compiler = require("../index.js");
const assert = chai.assert;
describe("Sum test", () => {
it("Should create a constant circuit", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "constants_test.circom"));
assert.equal(cirDef.nVars, 2);
const circuit = new snarkjs.Circuit(cirDef);
const witness = circuit.calculateWitness({ "in": "0xd807aa98" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt("0xd807aa98")));
});
it("Should create a sum circuit", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "sum_test.circom"));
assert.equal(cirDef.nVars, 101);
const circuit = new snarkjs.Circuit(cirDef);
const witness = circuit.calculateWitness({ "a": "111", "b": "222" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt("333")));
});
});