This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fixed-graph-py, generating zero-knowledge proof symbolic graphs π΄β¦
β¦ββ οΈ misogynists, bigots, xenophobics, liars: we are coming for you
- Loading branch information
1 parent
9dc9288
commit e0d6405
Showing
15 changed files
with
1,162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# debug, info, error | ||
LOG_LEVEL=debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# this repom | ||
./api/*pyc | ||
.DS_Store | ||
/results/*txt | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.PHONY: install | ||
install: | ||
@poetry install | ||
|
||
.PHONY: clean | ||
clean: | ||
@find . -iname '*.py[co]' -delete | ||
@find . -iname '__pycache__' -delete | ||
@rm -rf .tox | ||
@rm -rf .pytest_cache | ||
@poetry env remove --all | ||
|
||
.PHONY: lint | ||
lint: | ||
@tox -e lint | ||
|
||
.PHONY: test | ||
test: | ||
@pytest -vvv | ||
|
||
.PHONY: example | ||
example: | ||
@poetry run python src/main.py -e | ||
|
||
.PHONY: api | ||
api: | ||
@poetry run python src/main.py -a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,274 @@ | ||
## prototyping a fixed computational graph | ||
|
||
<br> | ||
|
||
a high-level way of thinking about zero-knowledge proofs is to prove a statement where: | ||
|
||
> a function `f` evaluated at inputs `x_i` results in an output `(y_1,...,y_n)`, i.e., `f(x_1, ..., x_n) = (y_1, ..., y_n)`. | ||
|
||
this function can be expressed as a fixed computational graph, where nodes are integers and relationships between nodes are related by operations such as **multiplication or addition relationships**. this graph defines a symbolic graph, which is filled in a later operation. | ||
|
||
in addition, some nodes can be related to others with an **equality relationship** on which the node's value is computed outside of the graph and constrained by a **hint**. | ||
|
||
|
||
|
||
|
||
<br> | ||
|
||
--- | ||
|
||
### setting up | ||
|
||
<br> | ||
|
||
|
||
```bash | ||
β tree . | ||
. | ||
βββ Makefile | ||
βββ README.md | ||
βββ pyproject.toml | ||
βββ src | ||
βΒ Β βββ __init__.py | ||
βΒ Β βββ examples.py | ||
βΒ Β βββ graph.py | ||
βΒ Β βββ main.py | ||
βΒ Β βββ utils.py | ||
βββ tests | ||
βΒ Β βββ __init__.py | ||
βΒ Β βββ test_examples.py | ||
βΒ Β βββ test_graph.py | ||
βββ tox.ini | ||
``` | ||
|
||
<br> | ||
|
||
this project utilizes [poetry](https://python-poetry.org/docs/) to set a customized virtual environment and installation: | ||
|
||
|
||
|
||
```bash | ||
β brew install poetry | ||
β make install | ||
``` | ||
|
||
<br> | ||
|
||
configuration details can be found at `pyproject.toml`. in addition, an `.env` file should be created, and the logging level should be set to its environment variable: | ||
|
||
|
||
```bash | ||
β cp .env_example .env | ||
β vim .env | ||
``` | ||
|
||
<br> | ||
|
||
note that even though the project has only one environment variable, this design choice is the most appropriate for production code. | ||
|
||
<br> | ||
|
||
--- | ||
|
||
### running examples without constraints | ||
|
||
<br> | ||
|
||
two examples of quadratic and cubic polynomial equations are illustrated in `src/examples.py`, and can be run with `LOG_LEVEL=debug` as follows: | ||
|
||
```bash | ||
β make example | ||
|
||
β CALCULATING: f(x) = x^2 + 5 + x, x = 3 | ||
π¨ New graph created with root node at 0x102fb2960 | ||
π¨ Multiplication node for ['0x102fb2960', '0x102fb2960'] created at 0x102fb28a0 | ||
π¨ Constant node with val=5 created at 0x102fb29f0 | ||
π¨ Addition node for ['0x102fb28a0', '0x102fb29f0'] created at 0x102fb2a20 | ||
π¨ Addition node for ['0x102fb2a20', '0x102fb2960'] created at 0x102fb2840 | ||
π¨ Starting filling nodes with x=3 | ||
β β Found multiplication: 3 * 3 = 9 | ||
β β Found constant: 5 | ||
β β Found addition: 9 + 5 = 14 | ||
β β Found addition: 14 + 3 = 17 | ||
π¨ Finished filling nodes for graph at 0x102fb2960 | ||
β RESULT: 17 | ||
|
||
β CALCULATING: f(x) = x^3 + x^2 + 2x + 3, x = 5: | ||
π¨ New graph created with root node at 0x102fb2a50 | ||
π¨ Multiplication node for ['0x102fb2a50', '0x102fb2a50'] created at 0x102fb2900 | ||
π¨ Multiplication node for ['0x102fb2900', '0x102fb2a50'] created at 0x102fb29c0 | ||
π¨ Addition node for ['0x102fb2900', '0x102fb29c0'] created at 0x102fb2990 | ||
π¨ Constant node with val=2 created at 0x102fb28d0 | ||
π¨ Multiplication node for ['0x102fb2a50', '0x102fb28d0'] created at 0x102fb27b0 | ||
π¨ Addition node for ['0x102fb2990', '0x102fb27b0'] created at 0x102fb2a80 | ||
π¨ Constant node with val=3 created at 0x102fb2b70 | ||
π¨ Addition node for ['0x102fb2a80', '0x102fb2b70'] created at 0x102fb2b40 | ||
π¨ Starting filling nodes with x=5 | ||
β β Found multiplication: 5 * 5 = 25 | ||
β β Found multiplication: 25 * 5 = 125 | ||
β β Found addition: 25 + 125 = 150 | ||
β β Found constant: 2 | ||
β β Found multiplication: 5 * 2 = 10 | ||
β β Found addition: 150 + 10 = 160 | ||
β β Found constant: 3 | ||
β β Found addition: 160 + 3 = 163 | ||
π¨ Finished filling nodes for graph at 0x102fb2a50 | ||
β RESULT: 163 | ||
``` | ||
|
||
<br> | ||
|
||
or, with `LOG_LEVEL=info`: | ||
|
||
```bash | ||
β CALCULATING: f(x) = x^2 + 5 + x, x = 3 | ||
β β Found multiplication: 3 * 3 = 9 | ||
β β Found constant: 5 | ||
β β Found addition: 9 + 5 = 14 | ||
β β Found addition: 14 + 3 = 17 | ||
β RESULT: 17 | ||
|
||
β CALCULATING: f(x) = x^3 + x^2 + 2x + 3, x = 5: | ||
β β Found multiplication: 5 * 5 = 25 | ||
β β Found multiplication: 25 * 5 = 125 | ||
β β Found addition: 25 + 125 = 150 | ||
β β Found constant: 2 | ||
β β Found multiplication: 5 * 2 = 10 | ||
β β Found addition: 150 + 10 = 160 | ||
β β Found constant: 3 | ||
β β Found addition: 160 + 3 = 163 | ||
β RESULT: 163 | ||
``` | ||
|
||
<br> | ||
|
||
--- | ||
|
||
### running examples with constraints | ||
|
||
<br> | ||
|
||
|
||
a simple API (starter) to `hint` arbitrary nodes is available, and it natively supports asynchronous hints and support for parallelization for filling the graph. | ||
|
||
an example is illustrated at `src/examples.py`, which can be run with `LOG_LEVEL=debug`: | ||
|
||
|
||
```bash | ||
β make api | ||
|
||
β CALCULATING: f(x) = 8 / (x + 1), x = 3: | ||
π¨ New graph created with root node at 0x100f5a960 | ||
π¨ Constant node with val=1 created at 0x100f5a8a0 | ||
π¨ Addition node for ['0x100f5a960', '0x100f5a8a0'] created at 0x100f5a9f0 | ||
π¨ Equality node for 0x100f5a9f0 created at 0x100f5aa20 | ||
π¨ New graph created with root node at 0x100f5a900 | ||
π¨ Constant node with val=8 created at 0x100f5a9c0 | ||
π¨ Multiplication node for ['0x100f5a900', '0x100f5a9c0'] created at 0x100f5a990 | ||
π¨ Starting filling nodes with x=3 | ||
β β Found constant: 1 | ||
β β Found addition: 3 + 1 = 4 | ||
β β Found equality: exiting at constrained node val=4 | ||
π¨ Getting last constrained node from graph at 0x100f5a960 | ||
π¨ Starting filling nodes with x=4 | ||
β β Found constant: 8 | ||
β β Found multiplication: 4 * 8 = 32 | ||
π¨ Finished filling nodes for graph at 0x100f5a900 | ||
β RESULT: 32 | ||
|
||
π¨ Nodes 0x100842990 and 0x100842840 are equal with val=4 | ||
β All constraints were met | ||
π¨ Finished filling nodes for graph at 0x100842ab0 | ||
β RESULT: 4 | ||
``` | ||
|
||
<br> | ||
|
||
or, with `LOG_LEVEL=info`: | ||
|
||
```bash | ||
β CALCULATING: f(x) = 8 / (x + 1), x = 3: | ||
β β Found constant: 1 | ||
β β Found addition: 3 + 1 = 4 | ||
β β Found equality: exiting at constrained node val=4 | ||
β β Found constant: 8 | ||
β β Found multiplication: 4 * 8 = 32 | ||
β RESULT: 32 | ||
|
||
β All constraints were met | ||
β RESULT: 4 | ||
``` | ||
|
||
<br> | ||
|
||
in addition, the examples above are also wrapped with a CLI tool, which could be run with: | ||
|
||
``` | ||
β python src/main.py | ||
usage: main.py [-h] [-e] [-a] | ||
πΎ Fixed Computational Graph Example πΎ | ||
options: | ||
-h, --help show this help message and exit | ||
-e Run examples for quadratic and cubic polynomial functions. | ||
-a Run an example that utilizes a hint for a constrained none. | ||
``` | ||
|
||
<br> | ||
|
||
Once again, this design choice was picked to enhance the project's characteristics of production code. For instance, it would be straightforward to extend this CLI tool to accept hint inputs for arbitrary nodes, and to create visual graph plots based on the node type at `Node()`, among other features. | ||
|
||
<br> | ||
|
||
--- | ||
|
||
### Running tests | ||
|
||
<br> | ||
|
||
Unit tests for the main classes (`graph.py` and `examples.py`), including edge cases, can be tested with: | ||
|
||
```bash | ||
β make test | ||
|
||
======================================== test session starts =============================== | ||
|
||
collected 17 items | ||
tests/test_examples.py::PolynomialTests::test_cubic PASSED [ 5%] | ||
tests/test_examples.py::PolynomialTests::test_hint_example PASSED [ 11%] | ||
tests/test_examples.py::PolynomialTests::test_quadratic PASSED [ 17%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_add PASSED [ 23%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_assert_equal PASSED [ 29%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_check_constraints PASSED [ 35%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_constant PASSED [ 41%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_eq PASSED [ 47%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_fill_nodes PASSED [ 52%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_get_last_constrained_node PASSED [ 58%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_init PASSED [ 64%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_instantiation PASSED [ 70%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_mul PASSED [ 76%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_private_methods_general PASSED [ 82%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_private_methods_parsing_addition PASSED [ 88%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_private_methods_parsing_equality PASSED [ 94%] | ||
tests/test_graph.py::UnitTestsForBuilder::test_private_methods_parsing_mul PASSED [100%] | ||
|
||
===================================== 17 passed in 0.01s =============================== | ||
``` | ||
|
||
<br> | ||
|
||
--- | ||
|
||
### developing | ||
|
||
|
||
<br> | ||
|
||
development resources such as lint and cleaning the language's cache are available with: | ||
|
||
```bash | ||
β make lint | ||
β make clean | ||
``` |
Oops, something went wrong.