mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 03:06:42 +01:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
700412f23d | ||
|
|
832077fbe9 | ||
|
|
0df0ac712d | ||
|
|
67a35ee400 | ||
|
|
680e3fe139 | ||
|
|
f05c4e1338 | ||
|
|
597deb1eaa | ||
|
|
7a1c606ca6 | ||
|
|
6642d4cf93 | ||
|
|
da0c60a919 | ||
|
|
534efcf355 | ||
|
|
a43154241e | ||
|
|
859c98d2a4 | ||
|
|
8048a5ef7d |
12
TUTORIAL.md
12
TUTORIAL.md
@@ -156,7 +156,7 @@ snarkjs verify
|
|||||||
|
|
||||||
This command will use `verification_key.json`, `proof.json` and `public.json` to verify that is valid.
|
This command will use `verification_key.json`, `proof.json` and `public.json` to verify that is valid.
|
||||||
|
|
||||||
Here we are veifying that we know a witness that the public inputs and the outputs matches the ones in the `public.json` file.
|
Here we are verifying that we know a witness that the public inputs and the outputs matches the ones in the `public.json` file.
|
||||||
|
|
||||||
|
|
||||||
If the proof is ok, you will see `OK` or `INVALID` if not ok.
|
If the proof is ok, you will see `OK` or `INVALID` if not ok.
|
||||||
@@ -182,7 +182,7 @@ The verifier contract deployed in the last step has a `view` function called `ve
|
|||||||
|
|
||||||
This function will return true if the proof and the inputs are valid.
|
This function will return true if the proof and the inputs are valid.
|
||||||
|
|
||||||
To facilitiate the call, you can use snarkjs to generate the parameters of the call by typing:
|
To facilitate the call, you can use snarkjs to generate the parameters of the call by typing:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
snarkjs generatecall
|
snarkjs generatecall
|
||||||
@@ -192,7 +192,7 @@ Just cut and paste the output to the parameters field of the `verifyProof` metho
|
|||||||
|
|
||||||
If every thing works ok, this method should return true.
|
If every thing works ok, this method should return true.
|
||||||
|
|
||||||
If you change any bit in the parameters, the result will be veryfiable false.
|
If you change any bit in the parameters, the result will be verifiably false.
|
||||||
|
|
||||||
|
|
||||||
## Bonus track
|
## Bonus track
|
||||||
@@ -227,19 +227,19 @@ template Multiplier() {
|
|||||||
component main = Multiplier();
|
component main = Multiplier();
|
||||||
```
|
```
|
||||||
|
|
||||||
A nice thing of circom language is that you can split a <== into two independent acions: <-- and ===
|
A nice thing of the circom language is that you can split a <== into two independent actions: <-- and ===
|
||||||
|
|
||||||
The <-- and --> operators assign a value to a signal without creating any constraints.
|
The <-- and --> operators assign a value to a signal without creating any constraints.
|
||||||
|
|
||||||
The === operator adds a constraint without assigning any value to any signal.
|
The === operator adds a constraint without assigning any value to any signal.
|
||||||
|
|
||||||
The circuit has also another problem: the operation works in Zr, so we need to guarantee the multiplication does not overflow. This can be done by binarizing the inputs and checking the ranges, but we will reserve it for future tutorials.
|
The circuit also has another problem: the operation works in Zr, so we need to guarantee the multiplication does not overflow. This can be done by converting the inputs to binary and checking the ranges, but we will reserve it for future tutorials.
|
||||||
|
|
||||||
## Where to go from here:
|
## Where to go from here:
|
||||||
|
|
||||||
You may want to read the [README](https://github.com/iden3/circom) to learn more features about circom.
|
You may want to read the [README](https://github.com/iden3/circom) to learn more features about circom.
|
||||||
|
|
||||||
You can also check a a library with many basic circuits lib binaritzations, comparators, eddsa, hashes, merkle trees etc [here](https://github.com/iden3/circomlib) (Work in progress).
|
You can also check a library with many basic circuits lib binarizations, comparators, eddsa, hashes, merkle trees etc [here](https://github.com/iden3/circomlib) (Work in progress).
|
||||||
|
|
||||||
|
|
||||||
Or a exponentiation in the Baby Jub curve [here](https://github.com/iden3/circomlib) (Work in progress).
|
Or a exponentiation in the Baby Jub curve [here](https://github.com/iden3/circomlib) (Work in progress).
|
||||||
|
|||||||
44
circuit.json
44
circuit.json
@@ -1,44 +0,0 @@
|
|||||||
{
|
|
||||||
"mainCode": "{\n}\n",
|
|
||||||
"signalName2Idx": {
|
|
||||||
"one": 0,
|
|
||||||
"main.out": 1
|
|
||||||
},
|
|
||||||
"components": [
|
|
||||||
{
|
|
||||||
"name": "main",
|
|
||||||
"params": {},
|
|
||||||
"template": "A",
|
|
||||||
"inputSignals": 0
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"componentName2Idx": {
|
|
||||||
"main": 0
|
|
||||||
},
|
|
||||||
"signals": [
|
|
||||||
{
|
|
||||||
"names": [
|
|
||||||
"one"
|
|
||||||
],
|
|
||||||
"triggerComponents": []
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"names": [
|
|
||||||
"main.out"
|
|
||||||
],
|
|
||||||
"triggerComponents": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"constraints": [],
|
|
||||||
"templates": {
|
|
||||||
"A": "function(ctx) {\n ctx.setSignal(\"out\", [], \"3\");\n ctx.assert(ctx.getSignal(\"out\", []), \"3\");\n}\n"
|
|
||||||
},
|
|
||||||
"functions": {},
|
|
||||||
"nPrvInputs": 0,
|
|
||||||
"nPubInputs": 0,
|
|
||||||
"nInputs": 0,
|
|
||||||
"nOutputs": 0,
|
|
||||||
"nVars": 1,
|
|
||||||
"nConstants": 1,
|
|
||||||
"nSignals": 2
|
|
||||||
}
|
|
||||||
3
cli.js
3
cli.js
@@ -35,6 +35,7 @@ const argv = require("yargs")
|
|||||||
.help("h")
|
.help("h")
|
||||||
.alias("h", "help")
|
.alias("h", "help")
|
||||||
.alias("v", "verbose")
|
.alias("v", "verbose")
|
||||||
|
.alias("f", "fast")
|
||||||
.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
|
||||||
@@ -56,7 +57,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).then( (cir) => {
|
compiler(fullFileName, {reduceConstraints: !argv.fast}).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) => {
|
||||||
|
|||||||
74
package-lock.json
generated
74
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "circom",
|
"name": "circom",
|
||||||
"version": "0.0.27",
|
"version": "0.0.33",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -119,6 +119,15 @@
|
|||||||
"resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.43.tgz",
|
"resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.43.tgz",
|
||||||
"integrity": "sha512-9dULc9jsKmXl0Aeunug8wbF+58n+hQoFjqClN7WeZwGLh0XJUWyJJ9Ee+Ep+Ql/J9fRsTVaeThp8MhiCCrY0Jg=="
|
"integrity": "sha512-9dULc9jsKmXl0Aeunug8wbF+58n+hQoFjqClN7WeZwGLh0XJUWyJJ9Ee+Ep+Ql/J9fRsTVaeThp8MhiCCrY0Jg=="
|
||||||
},
|
},
|
||||||
|
"bindings": {
|
||||||
|
"version": "1.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
|
||||||
|
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"file-uri-to-path": "1.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"brace-expansion": {
|
"brace-expansion": {
|
||||||
"version": "1.1.11",
|
"version": "1.1.11",
|
||||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||||
@@ -423,10 +432,13 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"eslint-utils": {
|
"eslint-utils": {
|
||||||
"version": "1.3.1",
|
"version": "1.4.2",
|
||||||
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.4.2.tgz",
|
||||||
"integrity": "sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q==",
|
"integrity": "sha512-eAZS2sEUMlIeCjBeubdj45dmBHQwPHWyBcT1VSYB7o9x9WRRqKxyUoiXlRjyAwzN7YEzHJlYg0NmzDRWx6GP4Q==",
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"eslint-visitor-keys": "^1.0.0"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"eslint-visitor-keys": {
|
"eslint-visitor-keys": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
@@ -542,6 +554,12 @@
|
|||||||
"flat-cache": "^2.0.1"
|
"flat-cache": "^2.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"file-uri-to-path": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"find-up": {
|
"find-up": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
|
||||||
@@ -807,6 +825,18 @@
|
|||||||
"nomnom": ">= 1.5.x"
|
"nomnom": ">= 1.5.x"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"keccak": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/keccak/-/keccak-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-rKe/lRr0KGhjoz97cwg+oeT1Rj/Y4cjae6glArioUC8JBF9ROGZctwIaaruM7d7naovME4Q8WcQSO908A8qcyQ==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"bindings": "^1.2.1",
|
||||||
|
"inherits": "^2.0.3",
|
||||||
|
"nan": "^2.2.1",
|
||||||
|
"safe-buffer": "^5.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"lcid": {
|
"lcid": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz",
|
||||||
@@ -841,9 +871,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lodash": {
|
"lodash": {
|
||||||
"version": "4.17.11",
|
"version": "4.17.15",
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
||||||
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==",
|
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"map-age-cleaner": {
|
"map-age-cleaner": {
|
||||||
@@ -920,6 +950,12 @@
|
|||||||
"integrity": "sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=",
|
"integrity": "sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"nan": {
|
||||||
|
"version": "2.14.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz",
|
||||||
|
"integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"natural-compare": {
|
"natural-compare": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
||||||
@@ -1183,6 +1219,12 @@
|
|||||||
"tslib": "^1.9.0"
|
"tslib": "^1.9.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"safe-buffer": {
|
||||||
|
"version": "5.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||||
|
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"safer-buffer": {
|
"safer-buffer": {
|
||||||
"version": "2.1.2",
|
"version": "2.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
||||||
@@ -1229,15 +1271,17 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"snarkjs": {
|
"snarkjs": {
|
||||||
"version": "0.1.9",
|
"version": "0.1.14",
|
||||||
"resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.1.9.tgz",
|
"resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.1.14.tgz",
|
||||||
"integrity": "sha512-UMiONT6f86bTB0AyT7bC+QsMgv2wwMk4qz7CYvTK6assojA4poEocJuEc5wl/awHsibQTbX2zNqqnO+IAYMfTA==",
|
"integrity": "sha512-mNsWx5K0ojz73689ZARwqyY62ENvW43movC+WMEHVYsFdcX9lpG+ZjiJGvnQh7LkYg2WY2lFzsXTUZI35TxqeA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"big-integer": "^1.6.35",
|
"big-integer": "^1.6.43",
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.2.0",
|
||||||
"eslint": "^5.3.0",
|
"escape-string-regexp": "^1.0.5",
|
||||||
"yargs": "^12.0.2"
|
"eslint": "^5.16.0",
|
||||||
|
"keccak": "^2.0.0",
|
||||||
|
"yargs": "^12.0.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source-map": {
|
"source-map": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "circom",
|
"name": "circom",
|
||||||
"version": "0.0.27",
|
"version": "0.0.33",
|
||||||
"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.16.0",
|
"eslint": "^5.16.0",
|
||||||
"eslint-plugin-mocha": "^5.3.0",
|
"eslint-plugin-mocha": "^5.3.0",
|
||||||
"jison": "^0.4.18",
|
"jison": "^0.4.18",
|
||||||
"snarkjs": "0.1.9"
|
"snarkjs": "0.1.14"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 ';'
|
||||||
{
|
{
|
||||||
|
|||||||
330
parser/jaz.js
330
parser/jaz.js
File diff suppressed because one or more lines are too long
@@ -33,7 +33,13 @@ const parser = require("../parser/jaz.js").parser;
|
|||||||
|
|
||||||
const timeout = ms => new Promise(res => setTimeout(res, ms));
|
const timeout = ms => new Promise(res => setTimeout(res, ms));
|
||||||
|
|
||||||
async function compile(srcFile) {
|
async function compile(srcFile, options) {
|
||||||
|
if (!options) {
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
if (typeof options.reduceConstraints === "undefined") {
|
||||||
|
options.reduceConstraints = true;
|
||||||
|
}
|
||||||
const fullFileName = srcFile;
|
const fullFileName = srcFile;
|
||||||
const fullFilePath = path.dirname(fullFileName);
|
const fullFilePath = path.dirname(fullFileName);
|
||||||
|
|
||||||
@@ -70,7 +76,9 @@ async function compile(srcFile) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
classifySignals(ctx);
|
classifySignals(ctx);
|
||||||
|
|
||||||
reduceConstants(ctx);
|
reduceConstants(ctx);
|
||||||
|
if (options.reduceConstraints) {
|
||||||
|
|
||||||
// Repeat while reductions are performed
|
// Repeat while reductions are performed
|
||||||
let oldNConstrains = -1;
|
let oldNConstrains = -1;
|
||||||
@@ -78,6 +86,7 @@ async function compile(srcFile) {
|
|||||||
oldNConstrains = ctx.constraints.length;
|
oldNConstrains = ctx.constraints.length;
|
||||||
reduceConstrains(ctx);
|
reduceConstrains(ctx);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
generateWitnessNames(ctx);
|
generateWitnessNames(ctx);
|
||||||
|
|
||||||
@@ -268,7 +277,12 @@ function reduceConstrains(ctx) {
|
|||||||
c.a={ type: "LINEARCOMBINATION", values: {} };
|
c.a={ type: "LINEARCOMBINATION", values: {} };
|
||||||
c.b={ type: "LINEARCOMBINATION", values: {} };
|
c.b={ type: "LINEARCOMBINATION", values: {} };
|
||||||
c.c={ type: "LINEARCOMBINATION", values: {} };
|
c.c={ type: "LINEARCOMBINATION", values: {} };
|
||||||
isolatedSignal.category = "constant";
|
|
||||||
|
let lSignal = ctx.signals[isolatedSignal];
|
||||||
|
while (lSignal.equivalence) {
|
||||||
|
lSignal = ctx.signals[lSignal.equivalence];
|
||||||
|
}
|
||||||
|
lSignal.category = "constant";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
src/exec.js
14
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") {
|
||||||
@@ -397,6 +399,12 @@ function execInstantiateComponet(ctx, vr, fn) {
|
|||||||
|
|
||||||
function execFunctionCall(ctx, ast) {
|
function execFunctionCall(ctx, ast) {
|
||||||
|
|
||||||
|
if (ast.name == "log") {
|
||||||
|
const v = exec(ctx, ast.params[0]);
|
||||||
|
console.log(v.value.toString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const scopeLevel = getScopeLevel(ctx, ast.name);
|
const scopeLevel = getScopeLevel(ctx, ast.name);
|
||||||
if (scopeLevel == -1) return error(ctx, ast, "Function not defined: " + ast.name);
|
if (scopeLevel == -1) return error(ctx, ast, "Function not defined: " + ast.name);
|
||||||
const fnc = getScope(ctx, ast.name);
|
const fnc = getScope(ctx, ast.name);
|
||||||
@@ -750,7 +758,7 @@ function execAnd(ctx, ast) {
|
|||||||
if (!a.value || !b.value) return { type: "NUMBER" };
|
if (!a.value || !b.value) return { type: "NUMBER" };
|
||||||
return {
|
return {
|
||||||
type: "NUMBER",
|
type: "NUMBER",
|
||||||
value: (a.value.neq(0) && a.value.neq(0)) ? bigInt(1) : bigInt(0)
|
value: (a.value.neq(0) && b.value.neq(0)) ? bigInt(1) : bigInt(0)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -764,7 +772,7 @@ function execOr(ctx, ast) {
|
|||||||
if (!a.value || !b.value) return { type: "NUMBER" };
|
if (!a.value || !b.value) return { type: "NUMBER" };
|
||||||
return {
|
return {
|
||||||
type: "NUMBER",
|
type: "NUMBER",
|
||||||
value: (a.value.neq(0) || a.value.neq(0)) ? bigInt(1) : bigInt(0)
|
value: (a.value.neq(0) || b.value.neq(0)) ? bigInt(1) : bigInt(0)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1038,6 +1046,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]);
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -414,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;
|
||||||
|
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) {
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ describe("Sum test", () => {
|
|||||||
|
|
||||||
const witness = circuit.calculateWitness({ "i": 111});
|
const witness = circuit.calculateWitness({ "i": 111});
|
||||||
assert(witness[0].equals(bigInt(1)));
|
assert(witness[0].equals(bigInt(1)));
|
||||||
assert(witness[1].equals(bigInt(111)));
|
assert(witness[1].equals(bigInt(111*111)));
|
||||||
assert(witness[2].equals(bigInt(111)));
|
assert(witness[2].equals(bigInt(111)));
|
||||||
});
|
});
|
||||||
// it("Should assign signal ERROR", async () => {
|
// it("Should assign signal ERROR", async () => {
|
||||||
@@ -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
test/circuits/compute.circom
Normal file
17
test/circuits/compute.circom
Normal file
@@ -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();
|
||||||
@@ -8,7 +8,7 @@ template X() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
i === r;
|
i === r;
|
||||||
out <== r;
|
out <== i*i;
|
||||||
}
|
}
|
||||||
|
|
||||||
component main = X();
|
component main = X();
|
||||||
|
|||||||
Reference in New Issue
Block a user