Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Bring (py)Chiquito in (#86)
Browse files Browse the repository at this point in the history
Resolve #82 
- Add `scr/python.rs` contains Python Module
- `frontend/python/chiquito` is home for python-source
- dependencies = ["py_ecc"] included in `pyproject.toml` so users don't
have to `pip install py_ecc`
- add `fibonacci.py` to examples
  • Loading branch information
gnart33 authored Aug 21, 2023
1 parent 75d813b commit ecc80f1
Show file tree
Hide file tree
Showing 21 changed files with 2,294 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/target
/Cargo.lock
.vscode
.env
__pycache__
*.so
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ authors = ["Leo Lara <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = { version = "0.19.1", features = ["extension-module"] }
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", features = [
"circuit-params",
"circuit-params",
], tag = "v2023_04_20" }
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = "0.3.2", features = [ "derive_serde" ] }
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = "0.3.2", features = [
"derive_serde",
] }
polyexen = { git = "https://github.com/Dhole/polyexen.git", rev = "67148a8aabb000a61a169b2533b2526c5b76c42f" }
num-bigint = { version = "0.4", features = ["rand"] }
uuid = { version = "1.4.0", features = ["v1", "rng"] }
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ precommit:
cargo clippy --all-targets --all-features -- -D warnings
cargo fmt --all -- --check

.PHONY: precommit
build:
cargo build
maturin develop

.PHONY: precommit build
45 changes: 41 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

Chiquito is a step-based high-level rust DSL (pychiquito is a python DSL for chiquito) that provides better syntax and abstraction for constraint building and column placement when writing plonkish circuits and has a Halo2 backend, and other backends are in the works. It allows the user to manipulate an AST that’s compiled to a Chiquito Halo2 backend, which can be integrated into any Halo2 circuit.

It's **HIGHLY RECOMMENDED** that you read the [design principles](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#design-principles), [architecture, and specific terms](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#architecture) of a Chiquito circuit before getting started.
It's **HIGHLY RECOMMENDED** that you read the [design principles](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#design-principles), [architecture, and specific terms](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#architecture) of a Chiquito circuit before getting started.

You can also learn about the project's [current status](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#project-status-as-of-april-2023)) and its [next steps](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#vision-and-next-steps).
You can also learn about the project's [current status](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#project-status-as-of-april-2023) and its [next steps](https://github.com/privacy-scaling-explorations/chiquito/blob/main/Appendix.md/#vision-and-next-steps).

# Getting Started

## Setup

Run the following in command line to add Chiquito as a dependency for your project:

```
cargo add --git https://github.com/privacy-scaling-explorations/chiquito
cargo add --git https://github.com/privacy-scaling-explorations/chiquito
```

Use the following examples to understand how Chiquito works or use them as starting templates for building your own Chiquito circuit.
Expand All @@ -22,29 +24,64 @@ Refer to the Appendix on the [exposed user functions](https://github.com/privacy

Refer to [Testing and Links](#testing-and-links) on detailed API documentations.


## Example: Fibonacci Circuit

Run the following in command line:

```
cargo run --example fibonacci
```

This example demonstrates how to construct signals, step types, constraints, and witness generation in Chiquito. Best for first time Chiquito users.

## Example: MiMC7 Circuit

TODO: annotate this code example

This example demonstrates how to construct a lookup table and use external inputs as trace arguments in Chiquito, besides covering concepts in the Fibonacci example. MiMC7 is a zk-friendly hashing function.

## Example: zkEVM Bytecode Circuit

https://github.com/privacy-scaling-explorations/zkevm-circuits/pull/1348

This example rewrites the zkEVM bytecode circuit using Chiquito and passes all original tests. It demonstrates how Chiquito can standardize and simplify larger scale circuits on the production level.

## Python bindings

There are 2 ways to install chiquito python bindings

### Install chiquito with pip

To use chiquito in Python, just need to install it with pip

```bash
pip install chiquito
```

### Build from source

Chiquito is built in Rust. First [install Rust](https://www.rust-lang.org/tools/install), then clone this repo and enter the repo directory.

```bash
git clone https://github.com/privacy-scaling-explorations/chiquito
cd chiquito
```

Then build the python bindings with maturin

```bash
python -m venv .env
source .env/bin/activate
pip install maturin
maturin develop
```

# Testing and Links

**API documentation**: `cargo doc --no-deps --package chiquito --open`

Currently API documentation is only written for exposed user functions, which are scattered across the DSL, constraint builder, compiler, and AST. **Refer to the following subdirectories for specific functions:**

- Circuit building (DSL): https://qwang98.github.io/chiquito/chiquito/dsl/index.html
- Constraint building (constraint builder): https://qwang98.github.io/chiquito/chiquito/dsl/cb/index.html
- Witness generation (compiler): https://qwang98.github.io/chiquito/chiquito/compiler/trait.WitnessGenContext.html
Expand Down
62 changes: 62 additions & 0 deletions examples/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from __future__ import annotations
from typing import Tuple

from chiquito.dsl import Circuit, StepType
from chiquito.cb import eq
from chiquito.query import Queriable
from chiquito.util import F


class Fibonacci(Circuit):
def setup(self: Fibonacci):
self.a: Queriable = self.forward("a")
self.b: Queriable = self.forward("b")

self.fibo_step = self.step_type(FiboStep(self, "fibo_step"))
self.fibo_last_step = self.step_type(FiboLastStep(self, "fibo_last_step"))

self.pragma_first_step(self.fibo_step)
self.pragma_last_step(self.fibo_last_step)
self.pragma_num_steps(11)

def trace(self: Fibonacci, args: Any):
self.add(self.fibo_step, (1, 1))
a = 1
b = 2
for i in range(1, 10):
self.add(self.fibo_step, (a, b))
prev_a = a
a = b
b += prev_a
self.add(self.fibo_last_step, (a, b))


class FiboStep(StepType):
def setup(self: FiboStep):
self.c = self.internal("c")
self.constr(eq(self.circuit.a + self.circuit.b, self.c))
self.transition(eq(self.circuit.b, self.circuit.a.next()))
self.transition(eq(self.c, self.circuit.b.next()))

def wg(self: FiboStep, args: Tuple[int, int]):
a_value, b_value = args
self.assign(self.circuit.a, F(a_value))
self.assign(self.circuit.b, F(b_value))
self.assign(self.c, F(a_value + b_value))


class FiboLastStep(StepType):
def setup(self: FiboLastStep):
self.c = self.internal("c")
self.constr(eq(self.circuit.a + self.circuit.b, self.c))

def wg(self: FiboLastStep, values=Tuple[int, int]):
a_value, b_value = values
self.assign(self.circuit.a, F(a_value))
self.assign(self.circuit.b, F(b_value))
self.assign(self.c, F(a_value + b_value))


fibo = Fibonacci()
fibo_witness = fibo.gen_witness(None)
fibo.halo2_mock_prover(fibo_witness)
21 changes: 21 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

[build-system]
requires = ["maturin>=1.1,<2.0"]
build-backend = "maturin"

[project]
name = "chiquito"
dependencies = ["py_ecc"]
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]


[tool.maturin]
bindings = 'pyo3'
features = ["pyo3/extension-module"]
python-source = "src/frontend/python"
module-name = "chiquito.rust_chiquito"
2 changes: 1 addition & 1 deletion src/frontend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod dsl;
pub mod pychiquito;
pub mod python;
Empty file.
Loading

0 comments on commit ecc80f1

Please sign in to comment.