mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 03:06:42 +01:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0df0ac712d | ||
|
|
67a35ee400 | ||
|
|
680e3fe139 | ||
|
|
f05c4e1338 | ||
|
|
597deb1eaa | ||
|
|
7a1c606ca6 | ||
|
|
6642d4cf93 | ||
|
|
a43154241e |
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.
|
||||
|
||||
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.
|
||||
@@ -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.
|
||||
|
||||
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
|
||||
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 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
|
||||
@@ -227,19 +227,19 @@ template 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 === 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:
|
||||
|
||||
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).
|
||||
|
||||
19
package-lock.json
generated
19
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.0.29",
|
||||
"version": "0.0.32",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -432,10 +432,13 @@
|
||||
}
|
||||
},
|
||||
"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": {
|
||||
"version": "1.0.0",
|
||||
@@ -868,9 +871,9 @@
|
||||
}
|
||||
},
|
||||
"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
|
||||
},
|
||||
"map-age-cleaner": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.0.29",
|
||||
"version": "0.0.32",
|
||||
"description": "Language to generate logic circuits",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
|
||||
@@ -40,6 +40,7 @@ if { return 'if'; }
|
||||
else { return 'else'; }
|
||||
for { return 'for'; }
|
||||
while { return 'while'; }
|
||||
compute { return 'compute'; }
|
||||
do { return 'do'; }
|
||||
return { return 'return'; }
|
||||
include { return 'include'; }
|
||||
@@ -198,6 +199,10 @@ statment
|
||||
{
|
||||
$$ = $1;
|
||||
}
|
||||
| computeStatment
|
||||
{
|
||||
$$ = $1;
|
||||
}
|
||||
| returnStatment
|
||||
{
|
||||
$$ = $1;
|
||||
@@ -302,6 +307,14 @@ doWhileStatment
|
||||
}
|
||||
;
|
||||
|
||||
computeStatment
|
||||
: 'compute' statment
|
||||
{
|
||||
$$ = { type: "COMPUTE", body: $2 };
|
||||
setLines($$, @1, @2);
|
||||
}
|
||||
;
|
||||
|
||||
returnStatment
|
||||
: 'return' expression ';'
|
||||
{
|
||||
|
||||
330
parser/jaz.js
330
parser/jaz.js
File diff suppressed because one or more lines are too long
@@ -77,8 +77,8 @@ async function compile(srcFile, options) {
|
||||
|
||||
classifySignals(ctx);
|
||||
|
||||
reduceConstants(ctx);
|
||||
if (options.reduceConstraints) {
|
||||
reduceConstants(ctx);
|
||||
|
||||
// Repeat while reductions are performed
|
||||
let oldNConstrains = -1;
|
||||
|
||||
@@ -131,6 +131,8 @@ function exec(ctx, ast) {
|
||||
return execFunctionCall(ctx, ast);
|
||||
} else if (ast.type == "BLOCK") {
|
||||
return execBlock(ctx, ast);
|
||||
} else if (ast.type == "COMPUTE") {
|
||||
return ;
|
||||
} else if (ast.type == "FOR") {
|
||||
return execFor(ctx, ast);
|
||||
} else if (ast.type == "WHILE") {
|
||||
@@ -1044,6 +1046,8 @@ function execSignalAssign(ctx, ast) {
|
||||
}
|
||||
|
||||
function execConstrain(ctx, ast) {
|
||||
ast.fileName = ctx.fileName;
|
||||
ast.filePath = ctx.filePath;
|
||||
const a = exec(ctx, ast.values[0]);
|
||||
if (ctx.error) return;
|
||||
const b = exec(ctx, ast.values[1]);
|
||||
|
||||
@@ -116,6 +116,8 @@ function gen(ctx, ast) {
|
||||
return genFunctionCall(ctx, ast);
|
||||
} else if (ast.type == "BLOCK") {
|
||||
return genBlock(ctx, ast);
|
||||
} else if (ast.type == "COMPUTE") {
|
||||
return genCompute(ctx, ast);
|
||||
} else if (ast.type == "FOR") {
|
||||
return genFor(ctx, ast);
|
||||
} else if (ast.type == "WHILE") {
|
||||
@@ -256,6 +258,12 @@ function genWhile(ctx, ast) {
|
||||
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) {
|
||||
const condition = gen(ctx, ast.condition);
|
||||
if (ctx.error) return;
|
||||
@@ -414,11 +422,13 @@ function genConstrain(ctx, ast) {
|
||||
if (ctx.error) return;
|
||||
const b = gen(ctx, ast.values[1]);
|
||||
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) {
|
||||
return genVarAssignement(ctx, ast) + ";\n" + genConstrain(ctx, ast);
|
||||
// return genVarAssignement(ctx, ast) + ";\n" + genConstrain(ctx, ast);
|
||||
return genVarAssignement(ctx, ast);
|
||||
}
|
||||
|
||||
function genVarAddAssignement(ctx, ast) {
|
||||
|
||||
@@ -49,4 +49,14 @@ describe("Sum test", () => {
|
||||
// await compiler(path.join(__dirname, "circuits", "assignsignal.circom"));
|
||||
// }, /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();
|
||||
Reference in New Issue
Block a user