Browse Source

compute block added

feature/witness_bin
Jordi Baylina 5 years ago
parent
commit
f05c4e1338
No known key found for this signature in database GPG Key ID: 7480C80C1BE43112
7 changed files with 225 additions and 164 deletions
  1. +10
    -7
      package-lock.json
  2. +13
    -0
      parser/jaz.jison
  3. +165
    -157
      parser/jaz.js
  4. +2
    -0
      src/exec.js
  5. +8
    -0
      src/gencode.js
  6. +10
    -0
      test/cases.js
  7. +17
    -0
      test/circuits/compute.circom

+ 10
- 7
package-lock.json

@ -432,10 +432,13 @@
} }
}, },
"eslint-utils": { "eslint-utils": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.3.1.tgz",
"integrity": "sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q==",
"dev": true
"version": "1.4.2",
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.4.2.tgz",
"integrity": "sha512-eAZS2sEUMlIeCjBeubdj45dmBHQwPHWyBcT1VSYB7o9x9WRRqKxyUoiXlRjyAwzN7YEzHJlYg0NmzDRWx6GP4Q==",
"dev": true,
"requires": {
"eslint-visitor-keys": "^1.0.0"
}
}, },
"eslint-visitor-keys": { "eslint-visitor-keys": {
"version": "1.0.0", "version": "1.0.0",
@ -868,9 +871,9 @@
} }
}, },
"lodash": { "lodash": {
"version": "4.17.11",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==",
"version": "4.17.15",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
"dev": true "dev": true
}, },
"map-age-cleaner": { "map-age-cleaner": {

+ 13
- 0
parser/jaz.jison

@ -40,6 +40,7 @@ if { return 'if'; }
else { return 'else'; } else { return 'else'; }
for { return 'for'; } for { return 'for'; }
while { return 'while'; } while { return 'while'; }
compute { return 'compute'; }
do { return 'do'; } do { return 'do'; }
return { return 'return'; } return { return 'return'; }
include { return 'include'; } include { return 'include'; }
@ -198,6 +199,10 @@ statment
{ {
$$ = $1; $$ = $1;
} }
| computeStatment
{
$$ = $1;
}
| returnStatment | returnStatment
{ {
$$ = $1; $$ = $1;
@ -302,6 +307,14 @@ doWhileStatment
} }
; ;
computeStatment
: 'compute' statment
{
$$ = { type: "COMPUTE", body: $2 };
setLines($$, @1, @2);
}
;
returnStatment returnStatment
: 'return' expression ';' : 'return' expression ';'
{ {

+ 165
- 157
parser/jaz.js
File diff suppressed because it is too large
View File


+ 2
- 0
src/exec.js

@ -131,6 +131,8 @@ function exec(ctx, ast) {
return execFunctionCall(ctx, ast); return execFunctionCall(ctx, ast);
} else if (ast.type == "BLOCK") { } else if (ast.type == "BLOCK") {
return execBlock(ctx, ast); return execBlock(ctx, ast);
} else if (ast.type == "COMPUTE") {
return ;
} else if (ast.type == "FOR") { } else if (ast.type == "FOR") {
return execFor(ctx, ast); return execFor(ctx, ast);
} else if (ast.type == "WHILE") { } else if (ast.type == "WHILE") {

+ 8
- 0
src/gencode.js

@ -116,6 +116,8 @@ function gen(ctx, ast) {
return genFunctionCall(ctx, ast); return genFunctionCall(ctx, ast);
} else if (ast.type == "BLOCK") { } else if (ast.type == "BLOCK") {
return genBlock(ctx, ast); return genBlock(ctx, ast);
} else if (ast.type == "COMPUTE") {
return genCompute(ctx, ast);
} else if (ast.type == "FOR") { } else if (ast.type == "FOR") {
return genFor(ctx, ast); return genFor(ctx, ast);
} else if (ast.type == "WHILE") { } else if (ast.type == "WHILE") {
@ -256,6 +258,12 @@ function genWhile(ctx, ast) {
return `while (bigInt(${condition}).neq(bigInt(0))) {\n${body}\n}\n`; return `while (bigInt(${condition}).neq(bigInt(0))) {\n${body}\n}\n`;
} }
function genCompute(ctx, ast) {
const body = gen(ctx, ast.body);
if (ctx.error) return;
return `{\n${body}\n}\n`;
}
function genIf(ctx, ast) { function genIf(ctx, ast) {
const condition = gen(ctx, ast.condition); const condition = gen(ctx, ast.condition);
if (ctx.error) return; if (ctx.error) return;

+ 10
- 0
test/cases.js

@ -49,4 +49,14 @@ describe("Sum test", () => {
// await compiler(path.join(__dirname, "circuits", "assignsignal.circom")); // await compiler(path.join(__dirname, "circuits", "assignsignal.circom"));
// }, /Cannot assign to a signal .*/); // }, /Cannot assign to a signal .*/);
// }); // });
it("Should compile a code with compute", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "compute.circom"));
const circuit = new snarkjs.Circuit(cirDef);
const witness = circuit.calculateWitness({ "x": 6});
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(37)));
assert(witness[2].equals(bigInt(6)));
});
}); });

+ 17
- 0
test/circuits/compute.circom

@ -0,0 +1,17 @@
template X() {
signal input x;
signal output y;
signal x2;
signal x3;
var a;
compute {
a = (x*x*x+6)/x;
y <-- a;
}
x2 <== x*x;
x3 <== x2*x;
x*y === x3+6;
}
component main = X();

Loading…
Cancel
Save