Skip to content

Commit

Permalink
support more grammar and basic cli
Browse files Browse the repository at this point in the history
  • Loading branch information
chyanju committed Mar 5, 2024
1 parent bc19349 commit 656ba6e
Show file tree
Hide file tree
Showing 12 changed files with 839 additions and 714 deletions.
40 changes: 8 additions & 32 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Dev CI

on:
push:
branches: [ "main", "dev" ]
branches: [ "main", "yanju/dev" ]
pull_request:
branches: [ "main", "dev" ]
branches: [ "main", "yanju/dev" ]
workflow_dispatch:

jobs:
Expand All @@ -17,11 +17,7 @@ jobs:
python-version: '3.12'
- name: install dependencies
run: |
pip install networkx[default]
pip install beautifulsoup4
pip install pandas
pip install antlr4-tools
pip install antlr4-python3-runtime==4.13.1
pip install .
antlr4 -v 4.13.1
- name: test hello.py
run: python tests/scripts/hello.py
Expand All @@ -37,11 +33,7 @@ jobs:
python-version: '3.12'
- name: install dependencies
run: |
pip install networkx[default]
pip install beautifulsoup4
pip install pandas
pip install antlr4-tools
pip install antlr4-python3-runtime==4.13.1
pip install .
antlr4 -v 4.13.1
- name: test parsing.py
run: PYTHONPATH="./" python ./tests/scripts/parsing.py
Expand All @@ -55,11 +47,7 @@ jobs:
python-version: '3.12'
- name: install dependencies
run: |
pip install networkx[default]
pip install beautifulsoup4
pip install pandas
pip install antlr4-tools
pip install antlr4-python3-runtime==4.13.1
pip install .
antlr4 -v 4.13.1
- name: test divz
run: PYTHONPATH="./" python ./tests/scripts/test-divz.py
Expand All @@ -73,11 +61,7 @@ jobs:
python-version: '3.12'
- name: install dependencies
run: |
pip install networkx[default]
pip install beautifulsoup4
pip install pandas
pip install antlr4-tools
pip install antlr4-python3-runtime==4.13.1
pip install .
antlr4 -v 4.13.1
- name: test infoleak
run: PYTHONPATH="./" python ./tests/scripts/test-infoleak.py
Expand All @@ -91,11 +75,7 @@ jobs:
python-version: '3.12'
- name: install dependencies
run: |
pip install networkx[default]
pip install beautifulsoup4
pip install pandas
pip install antlr4-tools
pip install antlr4-python3-runtime==4.13.1
pip install .
antlr4 -v 4.13.1
- name: test rtcnst
run: PYTHONPATH="./" python ./tests/scripts/test-rtcnst.py
Expand All @@ -109,11 +89,7 @@ jobs:
python-version: '3.12'
- name: install dependencies
run: |
pip install networkx[default]
pip install beautifulsoup4
pip install pandas
pip install antlr4-tools
pip install antlr4-python3-runtime==4.13.1
pip install .
antlr4 -v 4.13.1
- name: test unused
run: PYTHONPATH="./" python ./tests/scripts/test-unused.py
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The following libraries are required for running (different components of) the t
- `pip install antlr4-tools`
- [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) (4.12.2+) for crawling benchmarks from public explorers in test suite
- [pandas](https://pandas.pydata.org/) (2.1.4+) for data analysis in test suite
- [tabulate](https://github.com/astanin/python-tabulate) (0.9.0+) for result table rendering
- <u>Leo (**7ac50d8**) for compiling and running all benchmarks enclosed</u>
- The tools is tested under this version, but newer version of Lao may also work.

Expand Down
11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "vanguard"
version = "0.0.2"
version = "0.0.3"
authors = [
{ name="Yanju Chen", email="yanju@veridise.com" },
]
Expand All @@ -18,8 +18,13 @@ dependencies = [
"beautifulsoup4>=4.12.2",
"antlr4-python3-runtime==4.13.1",
"pandas>=2.1.4",
"tabulate>=0.9.0",
"antlr4-tools",
]

[project.urls]
"Homepage" = "https://veridise.com/"
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project.scripts]
vanguard-aleo = "vanguard.aleo.run:run"
1 change: 1 addition & 0 deletions tests/scripts/dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import bs4
import pandas
import antlr4
import tabulate

if __name__ == "__main__":
print("Hello World!")
21 changes: 20 additions & 1 deletion vanguard/aleo/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from io import StringIO
from pathlib import Path
from typing import List, Union

Expand Down Expand Up @@ -33,3 +32,23 @@ def aleo2json(path: Union[str, Path]):
parser = AleoParser(stream)
tree = parser.start()
return Trees.toJsonTree(tree, None, parser)

def detect(build_path: Union[str, Path], pid: str=None, fids: List=None, detector: str=None, **kwargs):
# pid (default: main program of environment) - program id
# fids (default: all functions of selected program) - list of function ids
# detector (default: infoleak) - detector to use

# NOTE: for clarity, only one detector can be used in each call

from .grammar import AleoEnvironment
from . import detectors as dlib

env = AleoEnvironment(build_path)
_detector = getattr(dlib, "detector_infoleak") if detector is None else getattr(dlib, f"detector_{detector}")

prog = env.main if pid is None else env.programs[pid]
funcs = list(prog.functions.values()) if fids is None else [prog.functions[p] for p in fids]

# start detection
ret = [ (str(prog.id), str(fn.id)) + _detector(env, prog.id, fn.id, **kwargs) for fn in funcs ]
return ret
77 changes: 75 additions & 2 deletions vanguard/aleo/grammar/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def from_json(node):
return AleoSet.from_json(node[1])
case ["command", ["random", *_]]:
return AleoRandom.from_json(node[1])
case ["command", ["contains", *_]]:
return AleoContains.from_json(node[1])
case ["command", ["get", *_]]:
return AleoGet.from_json(node[1])
case ["command", ["get_or_use", *_]]:
return AleoGetOrUse.from_json(node[1])
case _:
raise NotImplementedError(f"Unsupported json component, got: {node}")

Expand Down Expand Up @@ -52,7 +58,7 @@ class AleoRandom(AleoCommand):
@staticmethod
def from_json(node):
match node:
case ["random", ["random_op", "rand", ".", "chacha"], *operands, "into", regacc, "as", type, ";"]:
case ["random", ["random_op", *_], *operands, "into", regacc, "as", type, ";"]:
_operands = [AleoOperand.from_json(p) for p in operands]
_regacc = AleoRegisterAccess.from_json(regacc)
_type = None
Expand Down Expand Up @@ -80,6 +86,73 @@ def __init__(self, operands, regacc, type, **kwargs):
def __str__(self):
_operands = " ".join([str(p) for p in self.operands])
return f"random.chacha {_operands} into {self.regacc} as {self.type};"

class AleoContains(AleoCommand):

@staticmethod
def from_json(node):
match node:
case ["contains", "contains", id, "[", operand, "]", "into", regacc, ";"]:
_id = AleoIdentifier.from_json(id)
_operand = AleoOperand.from_json(operand)
_regacc = AleoRegisterAccess.from_json(regacc)
return AleoContains(_id, _operand, _regacc)
case _:
raise NotImplementedError(f"Unsupported json component, got: {node}")

def __init__(self, id, operand, regacc, **kwargs):
super().__init__(**kwargs)
self.id = id
self.operand = operand
self.regacc = regacc

def __str__(self):
return f"contains {self.id}[{self.operand}] into {self.regacc};"

class AleoGet(AleoCommand):

@staticmethod
def from_json(node):
match node:
case ["get", "get", id, "[", operand, "]", "into", regacc, ";"]:
_id = AleoIdentifier.from_json(id)
_operand = AleoOperand.from_json(operand)
_regacc = AleoRegisterAccess.from_json(regacc)
return AleoGet(_id, _operand, _regacc)
case _:
raise NotImplementedError(f"Unsupported json component, got: {node}")

def __init__(self, id, operand, regacc, **kwargs):
super().__init__(**kwargs)
self.id = id
self.operand = operand
self.regacc = regacc

def __str__(self):
return f"get {self.id} [{self.operand}] into {self.regacc};"

class AleoGetOrUse(AleoCommand):

@staticmethod
def from_json(node):
match node:
case ["get_or_use", ["get_or_use_op", *_], id, "[", operand, "]", default, "into", regacc, ";"]:
_id = AleoIdentifier.from_json(id)
_operand = AleoOperand.from_json(operand)
_default = AleoOperand.from_json(default)
_regacc = AleoRegisterAccess.from_json(regacc)
return AleoGetOrUse(_id, _operand, _default, _regacc)
case _:
raise NotImplementedError(f"Unsupported json component, got: {node}")

def __init__(self, id, operand, default, regacc):
self.id = id
self.operand = operand
self.default = default
self.regacc = regacc

def __str__(self):
return f"get.or_use {self.id}[{self.operand}] {self.default} into {self.regacc};"

class AleoInstruction(AleoCommand):

Expand Down Expand Up @@ -378,4 +451,4 @@ def __init__(self, op, operand, regacc, type, **kwargs):
self.type = type

def __str__(self):
return f"{self.op} {self.operand} into {self.regacc} as {self.type}"
return f"{self.op} {self.operand} into {self.regacc} as {self.type}"
2 changes: 2 additions & 0 deletions vanguard/aleo/grammar/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ def from_json(node):
return AleoLiteral.from_json(node[1])
case ["operand", ["register_access", *_]]:
return AleoRegisterAccess.from_json(node[1])
case ["operand", ["program_id", *_]]:
return AleoProgramId.from_json(node[1])
case _:
raise NotImplementedError(f"Unsupported json component, got: {node}")

Expand Down
4 changes: 4 additions & 0 deletions vanguard/aleo/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def get_dfg_edges(env: AleoEnvironment, pid: str, fid: str, hash=True, call=Fals
# only consider non-literal
edges.append((p, inst.regacc))

case AleoAssert():
# no data flow
pass

case _:
raise NotImplementedError(f"Unsupported instruction, got: {inst}, type: {type(inst)}")

Expand Down
2 changes: 1 addition & 1 deletion vanguard/aleo/parser/AleoParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ closure_output : OUTPUT operand AS register_type SC ;
function : FUNCTION identifier CL function_input* instruction* function_output* finalize? ;
function_input : INPUT register AS value_type SC ;
function_output : OUTPUT operand AS value_type SC ;
finalize : FINALIZE identifier CL finalize_input* command+ ;
finalize : FINALIZE identifier CL finalize_input* command* ;
finalize_input : INPUT register AS finalize_type SC ;

// instructions
Expand Down
2 changes: 1 addition & 1 deletion vanguard/aleo/parser/AleoParser.interp

Large diffs are not rendered by default.

Loading

0 comments on commit 656ba6e

Please sign in to comment.