Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Circom language support #6204

Merged
merged 6 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@
[submodule "vendor/grammars/chapel-tmbundle"]
path = vendor/grammars/chapel-tmbundle
url = https://github.com/chapel-lang/chapel-tmbundle
[submodule "vendor/grammars/circom-highlighting-vscode"]
path = vendor/grammars/circom-highlighting-vscode
url = https://github.com/iden3/circom-highlighting-vscode.git
[submodule "vendor/grammars/clarity.tmbundle"]
path = vendor/grammars/clarity.tmbundle
url = https://github.com/hirosystems/clarity.tmbundle.git
Expand Down
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ vendor/grammars/ceylon-sublimetext:
- source.ceylon
vendor/grammars/chapel-tmbundle:
- source.chapel
vendor/grammars/circom-highlighting-vscode:
- source.circom
vendor/grammars/clarity.tmbundle:
- source.clar
vendor/grammars/cmake.tmbundle:
Expand Down
8 changes: 8 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,14 @@ ChucK:
codemirror_mode: clike
codemirror_mime_type: text/x-java
language_id: 57
Circom:
type: programming
ace_mode: text
extensions:
- ".circom"
color: "#707575"
tm_scope: source.circom
language_id: 1042332086
Cirru:
type: programming
color: "#ccccff"
Expand Down
101 changes: 101 additions & 0 deletions samples/Circom/binsum.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
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/>.
*/

/*

Binary Sum
==========

This component creates a binary sum componet of ops operands and n bits each operand.

e is Number of carries: Depends on the number of operands in the input.

Main Constraint:
in[0][0] * 2^0 + in[0][1] * 2^1 + ..... + in[0][n-1] * 2^(n-1) +
+ in[1][0] * 2^0 + in[1][1] * 2^1 + ..... + in[1][n-1] * 2^(n-1) +
+ ..
+ in[ops-1][0] * 2^0 + in[ops-1][1] * 2^1 + ..... + in[ops-1][n-1] * 2^(n-1) +
===
out[0] * 2^0 + out[1] * 2^1 + + out[n+e-1] *2(n+e-1)

To waranty binary outputs:

out[0] * (out[0] - 1) === 0
out[1] * (out[0] - 1) === 0
.
.
.
out[n+e-1] * (out[n+e-1] - 1) == 0

*/


/*
This function calculates the number of extra bits in the output to do the full sum.
*/
pragma circom 2.0.0;

function nbits(a) {
var n = 1;
var r = 0;
while (n-1<a) {
r++;
n *= 2;
}
return r;
}


template BinSum(n, ops) {
var nout = nbits((2**n -1)*ops);
signal input in[ops][n];
signal output out[nout];

var lin = 0;
var lout = 0;

var k;
var j;

var e2;

e2 = 1;
for (k=0; k<n; k++) {
for (j=0; j<ops; j++) {
lin += in[j][k] * e2;
}
e2 = e2 + e2;
}

e2 = 1;
for (k=0; k<nout; k++) {
out[k] <-- (lin >> k) & 1;

// Ensure out is binary
out[k] * (out[k] - 1) === 0;

lout += out[k] * e2;

e2 = e2+e2;
}

// Ensure the sum;

lin === lout;
}
43 changes: 43 additions & 0 deletions samples/Circom/switcher.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

/*
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/>.
*/

/*
Assume sel is binary.

If sel == 0 then outL = L and outR=R
If sel == 1 then outL = R and outR=L

*/

pragma circom 2.0.0;

template Switcher() {
signal input sel;
signal input L;
signal input R;
signal output outL;
signal output outR;

signal aux;

aux <== (R-L)*sel; // We create aux in order to have only one multiplication
outL <== aux + L;
outR <== -aux + R;
}
45 changes: 45 additions & 0 deletions samples/Circom/xor3.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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/>.
*/

/* Xor3 function for sha256

out = a ^ b ^ c =>

out = a+b+c - 2*a*b - 2*a*c - 2*b*c + 4*a*b*c =>

out = a*( 1 - 2*b - 2*c + 4*b*c ) + b + c - 2*b*c =>

mid = b*c
out = a*( 1 - 2*b -2*c + 4*mid ) + b + c - 2 * mid

*/
pragma circom 2.0.0;

template Xor3(n) {
signal input a[n];
signal input b[n];
signal input c[n];
signal output out[n];
signal mid[n];

for (var k=0; k<n; k++) {
mid[k] <== b[k]*c[k];
out[k] <== a[k] * (1 -2*b[k] -2*c[k] +4*mid[k]) + b[k] + c[k] -2*mid[k];
}
}
1 change: 1 addition & 0 deletions vendor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **Chapel:** [chapel-lang/chapel-tmbundle](https://github.com/chapel-lang/chapel-tmbundle)
- **Checksums:** [Alhadis/language-etc](https://github.com/Alhadis/language-etc)
- **ChucK:** [textmate/java.tmbundle](https://github.com/textmate/java.tmbundle)
- **Circom:** [iden3/circom-highlighting-vscode](https://github.com/iden3/circom-highlighting-vscode)
- **Cirru:** [Cirru/sublime-cirru](https://github.com/Cirru/sublime-cirru)
- **Clarion:** [fushnisoft/SublimeClarion](https://github.com/fushnisoft/SublimeClarion)
- **Clarity:** [hirosystems/clarity.tmbundle](https://github.com/hirosystems/clarity.tmbundle)
Expand Down
1 change: 1 addition & 0 deletions vendor/grammars/circom-highlighting-vscode
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: circom-highlighting-vscode
version: 1e292d5457d9ba3a3e9832b5e4378cdd65a6cfb7
type: git_submodule
homepage: https://github.com/iden3/circom-highlighting-vscode.git
license: mit
licenses:
- sources: COPYING
text: |-
MIT License

Copyright (c) 2022 0Kims Association <https://0kims.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
notices: []