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

Complete Basic Version of the Circom G4 grammar #1

Merged
merged 40 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9d0e273
Enhanced grammar
KyrylR Jul 17, 2024
c494d23
Almost finished with default circom (up to 2.1.1)
KyrylR Jul 24, 2024
afa2dba
Completed alpha version of the Circom grammar
KyrylR Jul 24, 2024
83b8f90
Uncommented custom grammar
KyrylR Jul 24, 2024
f50997f
Tested grammar and finished it
KyrylR Jul 25, 2024
2afbcf8
Prettify
KyrylR Jul 25, 2024
d823ba9
Optimized and fixed
KyrylR Jul 25, 2024
de8124c
Added test command
KyrylR Jul 25, 2024
4dae646
Fixed rule order in primary rule
KyrylR Jul 25, 2024
ed2db5f
Added keywords
KyrylR Jul 29, 2024
b203723
Added CI
KyrylR Jul 29, 2024
3340dff
Updated CI
KyrylR Jul 29, 2024
134bb24
Updated CI
KyrylR Jul 29, 2024
f09a0cd
Improved lexer structures
KyrylR Jul 29, 2024
02c8c30
Fixed parser rules
KyrylR Jul 29, 2024
38754a6
Fixed bug with component main
KyrylR Jul 30, 2024
d8e3d4b
Fixed bug with component main
KyrylR Jul 30, 2024
3fde741
Improved grammar consistency
KyrylR Aug 3, 2024
b077169
Fixed grammar (hanging comma)
KyrylR Aug 3, 2024
97095c5
Added function and template blocks
KyrylR Aug 3, 2024
cbecc86
Cleaned up
KyrylR Aug 3, 2024
f14d459
Optimised grammar
KyrylR Aug 3, 2024
30376cc
Fixed forUpdate
KyrylR Aug 9, 2024
1b73e7d
Fixed forUpdate
KyrylR Aug 9, 2024
99e313a
Fixed assignment to expression
KyrylR Aug 9, 2024
641fd54
Fixed bug and added basic operations to lexer
KyrylR Aug 11, 2024
660fe2f
Fixed typo
KyrylR Aug 11, 2024
342b9b3
Added tags. Refactored
KyrylR Aug 12, 2024
4f2e1cd
Changed order of ternary and unary expressions
KyrylR Aug 13, 2024
a163d38
Added more tests
KyrylR Aug 13, 2024
ec5efe7
Added ability to accept log without args
KyrylR Aug 13, 2024
da136d5
Minor grammar fixes
KyrylR Aug 16, 2024
5adeaab
Improved parsing speed
KyrylR Aug 16, 2024
50fe98e
Updated README
KyrylR Aug 19, 2024
a1d2a47
Fixed some compatibility issues
KyrylR Aug 19, 2024
5f3965d
Handled too weak prediction mode
KyrylR Aug 19, 2024
8949bb1
Updated refs
KyrylR Aug 19, 2024
81f4b39
Added more test files
KyrylR Aug 19, 2024
e83c155
Fixed LINE_COMMENT lexer rule
KyrylR Aug 20, 2024
2ec9add
Fixed precedence of ternary operation
KyrylR Aug 21, 2024
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
30 changes: 30 additions & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Bug Report
description: File a bug report
labels: ['bug']
assignees:
- KyrylR
body:
- type: markdown
attributes:
value: Thanks for taking the time to fill out this bug report!
- type: input
id: version
attributes:
label: "Project version"
placeholder: "1.2.3"
validations:
required: true
- type: textarea
id: what-happened
attributes:
label: What happened?
description: A brief description of what happened and what you expected to happen
validations:
required: true
- type: textarea
id: reproduction-steps
attributes:
label: "Minimal reproduction steps"
description: "The minimal steps needed to reproduce the bug"
validations:
required: true
13 changes: 13 additions & 0 deletions .github/ISSUE_TEMPLATE/feature-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Feature request
description: Suggest a new feature
labels: ['feature']
assignees:
- KyrylR
body:
- type: textarea
id: feature-description
attributes:
label: "Describe the feature"
description: "A description of what you would like to see in the project"
validations:
required: true
4 changes: 4 additions & 0 deletions .github/ISSUE_TEMPLATE/other-issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
name: Other issue
about: Other kind of issue
---
14 changes: 14 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: setup

description: setup

runs:
using: composite
steps:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.21.x'
- name: Install dependencies
shell: bash
run: go get .
25 changes: 25 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: "checks"

on:
push:
branches:
- main
pull_request:
branches:
- main
- dev

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v4
with:
submodules: true

- name: Setup
uses: ./.github/actions/setup

- name: Run tests
run: make test
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
dist
dist
!parser/antlr-4.13.1-complete.jar
!parser/generate.sh
!parser/generate.go
parser/*

iden3-circuits
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "iden3-circuits"]
path = iden3-circuits
url = https://github.com/iden3/circuits.git
[submodule "circomlib"]
path = circomlib
url = https://github.com/iden3/circomlib.git
23 changes: 4 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
# Define variables
GRAMMAR_DIR = ./grammar
OUTPUT_DIR = ./dist
GRAMMAR_FILE = $(GRAMMAR_DIR)/Circom.g4
ANTLR4_GO = java -jar /usr/local/lib/antlr-4.13.1-complete.jar
.PHONY: test

.PHONY: all clean
test:
go generate ./...
go test

all: clean $(OUTPUT_DIR) $(OUTPUT_DIR)/grammar
$(ANTLR4_GO) -o $(OUTPUT_DIR) $(GRAMMAR_FILE)
cp -r $(OUTPUT_DIR)/grammar/* $(OUTPUT_DIR)
rm -rf $(OUTPUT_DIR)/grammar

$(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR)

$(OUTPUT_DIR)/grammar:
mkdir -p $(OUTPUT_DIR)/grammar

clean:
rm -rf $(OUTPUT_DIR)
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
# Circom G4 Grammar Spec
# Circom G4 Grammar Specification

## Playing Around
This repository provides a Circom grammar implementation in G4 format using the [ANTLR](https://www.antlr.org) tool.

If you would like to try it out, first go to [antlr.org](https://www.antlr.org/download.html) and download the latest **complete** version.
Do not forget to modify the Makefile afterward.
## Grammar Compatibility

Then, after you generate the bindings with the __make__ command, you can compile it with the Java compiler and use the
commands mentioned in the docs to play around with the grammar: [Getting Started with ANTLR v4](https://github.com/antlr/antlr4/blob/dev/doc/getting-started.md).
Source: [Circom Parser Grammar](https://github.com/iden3/circom/blob/master/parser/src/lang.lalrpop)

### Issues

- The grammar described in this repository is unofficial. Consequently, the following issues may arise:
- The grammar may not be up-to-date.
- Some structures that are valid in the official grammar may not be valid in this version due to factors such as redundancy or complexity.

Despite this, we strive to cover 99.9% of the existing Circom code, offering a simple and efficient structure. The parser is capable of handling both `circomlib` and `iden3-circuits`.

## Contribution

To contribute, first clone the repository:

```shell
git clone [email protected]:distributed-lab/circom-parser.git
cd circom-parser
```

Next, install the dependencies:

```shell
go mod tidy
```

Then, synchronize the git submodules:

```shell
git submodule update --init
git submodule update --remote
```

After initializing the submodules, you are ready to generate the Go bindings for the grammar and run tests with a single command:

```shell
make
```
35 changes: 35 additions & 0 deletions base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"os"
"testing"

"path/filepath"
)

// TestParseAllCircuits recursively parses all circuit files in the directory
func TestParseAllCircuits(t *testing.T) {
baseDirs := []string{"iden3-circuits/circuits", "circomlib/circuits", "data"}

for _, baseDir := range baseDirs {
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() && filepath.Ext(path) == ".circom" {
t.Run(path, func(t *testing.T) {
if err := ParseFile(path); err != nil {
t.Errorf("Failed to parse %s: %v", path, err)
}
})
}

return nil
})

if err != nil {
t.Fatalf("Error walking the path %s: %v", baseDir, err)
}
}
}
1 change: 1 addition & 0 deletions circomlib
Submodule circomlib added at cff5ab
Empty file removed data/.gitkeep
Empty file.
64 changes: 64 additions & 0 deletions data/Example1.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
pragma circom 2.0.0;
pragma custom_templates;

include "montgomery.circom";
include "mux3.circom";
include "babyjub.circom";

include "../../../node_modules/circomlib/circuits/gates.circom";
include "../node_modules/circomlib/circuits/bitify.circom";

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

template A(a, b, c) {}

template nbits(a) {
var o_u_t;
var o$o;
var x[3] = [2,8,4];
var z[n]; // where n is a parameter of a template
var dbl[16][2] = base;

lc1 - 1 === out + 1;

var y[5] = someFunction(n);

signal output out[2];
signal intermediate[4];

signal out <== 2;

var e4 = 1 && 0;

signal input in;
signal output out[n];
var lc1=0;
var e2=1;
for (var i = 0; i<n; i++) {
out[i] <-- (in >> i) & 1;
out[i] * (out[i] -1 ) === 0;
lc1 += out[i] * e2;
e2 = e2+e2;
}
component temp_a = A(n);
out <== temp_a.c;
lc1 === in;

if(i < n){
signal out <== 2;
i = out;
}
outA <== i;
}

component main {public [in1]}= A();

// Some comment
38 changes: 38 additions & 0 deletions data/Example2.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pragma circom 2.0.0;

function someFunc(a) {
var (k1, k2) = (1, 2);

(k1, k2) = (3, 2);

return 2;
}

template B(a) {
signal input b;
signal output c;

var (k1, k2) = (1, 2);

k2 = someFunc(a);

k2++;

(k1, k2) = (1, 2);

c <== a + b;
}

template parallel A(a1) {
signal output {a} a;

signal input b;
signal input c;

component aliasCheck = parallel B(a1);
aliasCheck.b <== b;

a <== b * c;
}

component main {public[b]} = A(2);
Loading
Loading