arnaucube
arnaucube/circomPublic Self-hosted repository
assert working
Jordi Baylina jordi@baylina.cat
Mon, 24 Aug 2020 09:31:35 +0200
commit

aa2d768465a6a83642bc1a93de2dfb125e3714a0

parent

f02ceb2508f4f32fc49d8df43948734dcc6a7d92

M cli.js → cli.js

@@ -24,6 +24,7 @@

const fs = require("fs"); const path = require("path"); const Scalar = require("ffjavascript").Scalar; +const stringifyBigInts = require("ffjavascript").utils.stringifyBigInts; const fastFile = require("fastfile"); const compiler = require("./src/compiler");

@@ -153,7 +154,7 @@ console.log(err.message);

if (argv.verbose) console.log(err.stack); } if (err.ast) { - console.error(JSON.stringify(err.ast, null, 1)); + console.error(JSON.stringify(stringifyBigInts(err.ast), null, 1)); } process.exit(1); });
M package-lock.json → package-lock.json

@@ -203,11 +203,11 @@ "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz",

"integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=" }, "circom_runtime": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.2.tgz", - "integrity": "sha512-qh1GsfxXLGAhLBcIQWrFHO/zu6kuXQQL1xC3+P8KRETvynTbBVSb1ul3dCiXyetm7lVNNdhb1/UoOCEZ6yu1RQ==", + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.4.tgz", + "integrity": "sha512-sQWeEBD3o2jIdrKPf3VDu7DNfP+NfscYO/pxi73FE0qQW8TXTfwno8Grdl++h6OKWbzvWJdG5jQvS+WGKjpMOg==", "requires": { - "ffjavascript": "0.2.4", + "ffjavascript": "0.2.10", "fnv-plus": "^1.3.1" } },

@@ -334,9 +334,9 @@ "integrity": "sha1-zR9rpHfFY4xAyX7ZtXLbW6tdgzE=",

"dev": true }, "ejs": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.3.tgz", - "integrity": "sha512-wmtrUGyfSC23GC/B1SMv2ogAUgbQEtDmTIhfqielrG5ExIM9TP4UoYdi90jLF1aTcsWCJNEO0UrgKzP0y3nTSg==", + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.5.tgz", + "integrity": "sha512-dldq3ZfFtgVTJMLjOe+/3sROTzALlL9E34V4/sDtUd/KlBSS0s6U1/+WPE1B4sj9CXHJpL1M6rhNJnc9Wbal9w==", "requires": { "jake": "^10.6.1" }

@@ -571,9 +571,9 @@ "resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.12.tgz",

"integrity": "sha512-0EZo2y5eW8X0oiDDRvcnufjVxlM96CQL5hvmRQtbRABWlCkH73IHwkzl0qOSdxtchaMr+0TSB7GVqaVEixRr1Q==" }, "ffiasm": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/ffiasm/-/ffiasm-0.1.0.tgz", - "integrity": "sha512-fQI7b6oye7Yw3t28ku9XuhPSwwzKMeqB0pkIaGPrKl9s/ipV90o8J4Ge0s5F0UsH5Mu2wfr3hFQ2sGF+vQk5Sg==", + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/ffiasm/-/ffiasm-0.1.1.tgz", + "integrity": "sha512-irMMHiR9JJ7BVBrAhtliUawxVdPYSdyl81taUYJ4C1mJ0iw2ueThE/qtr0J8B83tsIY8HJvh0lg5F+6ClK4xpA==", "requires": { "big-integer": "^1.6.48", "ejs": "^3.0.1",

@@ -581,13 +581,24 @@ "yargs": "^15.3.1"

} }, "ffjavascript": { - "version": "0.2.4", - "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.4.tgz", - "integrity": "sha512-XFeWcjUDFPavN+DDOxhE8p5MOhZQJc9oO1Sj4ml1pyjqNhS1ujEamcjFyK0cctdnat61i7lvpTYzdtS3RYDC8w==", + "version": "0.2.10", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.10.tgz", + "integrity": "sha512-GQI6gHYYG5/iD4Kt3VzezzK7fARJzP0zkc82V/+JAdjfeKBXhDSo5rpKFuK3cDcrdW0Fu2emuYNMEAuFqhEQvQ==", "requires": { "big-integer": "^1.6.48", - "wasmcurves": "0.0.4", + "wasmcurves": "0.0.5", "worker-threads": "^1.0.0" + }, + "dependencies": { + "wasmcurves": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.0.5.tgz", + "integrity": "sha512-BmI4GXLjLawGg2YkvHa8zRsnWec+d1uwoxE+Iov8cqOpDL7GA5XO2pk2yuDbXHMzwIug2exnKot3baRZ86R0pA==", + "requires": { + "big-integer": "^1.6.42", + "blakejs": "^1.1.0" + } + } } }, "ffwasm": {

@@ -1163,6 +1174,16 @@ "fastfile": {

"version": "0.0.7", "resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.7.tgz", "integrity": "sha512-Zk7sdqsV6DsN/rhjULDfCCowPiMDsziTMFicdkrKN80yybr/6YFf9H91ELXN85dVEf6EYkVR5EHkZNc0dMqZKA==" + }, + "ffjavascript": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.4.tgz", + "integrity": "sha512-XFeWcjUDFPavN+DDOxhE8p5MOhZQJc9oO1Sj4ml1pyjqNhS1ujEamcjFyK0cctdnat61i7lvpTYzdtS3RYDC8w==", + "requires": { + "big-integer": "^1.6.48", + "wasmcurves": "0.0.4", + "worker-threads": "^1.0.0" + } } } },
M package.json → package.json

@@ -30,10 +30,10 @@ "url": "https://github.com/iden3/circom.git"

}, "dependencies": { "chai": "^4.2.0", - "circom_runtime": "0.1.2", + "circom_runtime": "0.1.4", "fastfile": "0.0.12", - "ffiasm": "0.1.0", - "ffjavascript": "0.2.4", + "ffiasm": "0.1.1", + "ffjavascript": "0.2.10", "ffwasm": "0.0.7", "fnv-plus": "^1.3.1", "r1csfile": "0.0.14",
M ports/c/builder.js → ports/c/builder.js

@@ -95,6 +95,10 @@ checkConstraint(a, b, strErr) {

this.ops.push({op: "CHECKCONSTRAINT", a, b, strErr}); } + checkAssert(a, strErr) { + this.ops.push({op: "CHECKASSERT", a, strErr}); + } + log(val) { this.ops.push({op: "LOG", val}); }

@@ -215,6 +219,8 @@ } else if (o.op == "FNCALL") {

code.push(`${o.fnName}(ctx, ${o.retLabel}, ${o.params.join(",")});`); } else if (o.op == "CHECKCONSTRAINT") { code.push(`ctx->checkConstraint(__cIdx, ${ref2src(o.a)}, ${ref2src(o.b)}, "${o.strErr}");`); + } else if (o.op == "CHECKASSERT") { + code.push(`ctx->checkAssert(__cIdx, ${ref2src(o.a)}, "${o.strErr}");`); } else if (o.op == "LOG") { code.push(`ctx->log(${ref2src(o.val)});`); }
M ports/wasm/build_runtime.js → ports/wasm/build_runtime.js

@@ -619,6 +619,35 @@ )

)); } + function buildCheckAssert() { + const f = module.addFunction("checkAssert"); + f.addParam("cIdx", "i32"); + f.addParam("pA", "i32"); + f.addParam("pStr", "i32"); + + const c = f.getCodeBuilder(); + + f.addCode(ifSanityCheck(c, + c.if ( + c.i32_eqz( + c.call( + "Fr_isTrue", + c.getLocal("pA"), + ) + ), + c.call( + "error", + c.i32_const(errs.ASSERT_DOES_NOT_MATCH.code), + c.i32_const(errs.ASSERT_DOES_NOT_MATCH.pointer), + c.getLocal("cIdx"), + c.getLocal("pA"), + c.getLocal("pStr"), + c.i32_const(0) + ) + ) + )); + } + function buildGetNVars() { const f = module.addFunction("getNVars"); f.setReturnType("i32");

@@ -823,6 +852,7 @@ buildComponentStarted();

buildComponentFinished(); buildCheckConstraint(); + buildCheckAssert(); buildGetNVars(); buildGetFrLen();
M ports/wasm/builder.js → ports/wasm/builder.js

@@ -98,6 +98,9 @@ checkConstraint(a, b, strErr) {

this.ops.push({op: "CHECKCONSTRAINT", a, b, strErr}); } + checkAssert(a, strErr) { + this.ops.push({op: "CHECKASSERT", a, strErr}); + } concat(cb) { this.ops.push(...cb.ops);

@@ -329,6 +332,15 @@ "checkConstraint",

c.getLocal("cIdx"), this.fnBuilder._deRefFr(c, o.a), this.fnBuilder._deRefFr(c, o.b), + c.i32_const(this.fnBuilder.builder.module.allocString(o.strErr)) + ) + ); + } else if (o.op == "CHECKASSERT") { + code.push( + c.call( + "checkAssert", + c.getLocal("cIdx"), + this.fnBuilder._deRefFr(c, o.a), c.i32_const(this.fnBuilder.builder.module.allocString(o.strErr)) ) );
M ports/wasm/errs.js → ports/wasm/errs.js

@@ -7,4 +7,5 @@ ACCESSING_NOT_ASSIGNED_SIGNAL: {code: 5, str: "Accessing a not assigned signal"},

SIGNAL_ASSIGNED_TWICE: {code: 6, str: "Signal assigned twice"}, CONSTRAIN_DOES_NOT_MATCH: {code: 7, str: "Constraint doesn't match"}, MAPISINPUT_DONT_MATCH: {code: 8, str: "MapIsInput don't match"}, + ASSERT_DOES_NOT_MATCH: {code: 9, str: "Assert not satisfied"}, };
M src/construction_phase.js → src/construction_phase.js

@@ -567,9 +567,13 @@ }

return; } if (ast.name == "assert") { + ast.fileName = ctx.fileName; + ast.filePath = ctx.filePath; + const v = exec(ctx, ast.params[0]); const ev = val(ctx, v, ast); - if (ctx.F.isZero(ev)) return ctx.throwError(ast, "Assertion failed"); + if ((typeof ev.v !== "undefined")&&(ctx.F.isZero(ev.v))) return ctx.throwError(ast, "Assertion failed"); + return; } const fnc = ctx.functions[ast.name];
M src/gencode.js → src/gencode.js

@@ -871,6 +871,12 @@ const vRef = gen(ctx, ast.params[0]);

ctx.codeBuilder.log(toRefA_Fr1(ctx, ast.params[0], vRef)); return vRef; } + if (ast.name == "assert") { + const strErr = ast.fileName + ":" + ast.first_line + ":" + ast.first_column; + const vRef = gen(ctx, ast.params[0]); + ctx.codeBuilder.checkAssert(toRefA_Fr1(ctx, ast.params[0], vRef), strErr); + return vRef; + } const params = []; for (let i=0; i<ast.params.length; i++) { const pRef = gen(ctx, ast.params[i]);