Skip to content

Commit

Permalink
Steve/jupyter notebook tutorial part 4 (#15)
Browse files Browse the repository at this point in the history
* updated tutorial

* update tutorial

* new error emerge after updating submodule, was fully debugged previously

* updated rust bindings and formatted python files

* minor

* updated submodule

* cargo fmt

* cleaned latest merge main; debugged and updated all tutorial files

* addressed leo's comments'

* removed num_step_instances

* updated dependencies and deleted unwanted functions
  • Loading branch information
qwang98 authored Aug 17, 2023
1 parent a61b2a8 commit 09ef47a
Show file tree
Hide file tree
Showing 12 changed files with 968 additions and 184 deletions.
106 changes: 59 additions & 47 deletions examples/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,85 @@
from __future__ import annotations
from typing import Tuple

# from chiquito import (dsl, cb, query, util)
# from dsl import Circuit, StepType
# from cb import eq
# from query import Queriable
# from util import F

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


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)
# self.pragma_disable_q_enable()

# self.expose(self.b, First())
# self.expose(self.a, Last())
# self.expose(self.a, Step(1))
class FiboFirstStep(StepType):
def setup(self):
self.c = self.internal("c")
self.constr(eq(self.circuit.a, 1))
self.constr(eq(self.circuit.b, 1))
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()))
self.transition(eq(self.circuit.n, self.circuit.n.next()))

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))
def wg(self, args):
a_value, b_value, n_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))
self.assign(self.circuit.n, F(n_value))


class FiboStep(StepType):
def setup(self: FiboStep):
def setup(self):
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()))
self.transition(eq(self.circuit.n, self.circuit.n.next()))

def wg(self: FiboStep, args: Tuple[int, int]):
a_value, b_value = args
def wg(self, args):
a_value, b_value, n_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))
self.assign(self.circuit.n, F(n_value))


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

def wg(self: FiboLastStep, values=Tuple[int, int]):
a_value, b_value = values
def wg(self, args):
a_value, b_value, n_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))
self.assign(self.circuit.n, F(n_value))


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

self.fibo_first_step = self.step_type(FiboFirstStep(self, "fibo_first_step"))
self.fibo_step = self.step_type(FiboStep(self, "fibo_step"))
self.padding = self.step_type(Padding(self, "padding"))

self.pragma_num_steps(10)
self.pragma_first_step(self.fibo_first_step)
self.pragma_last_step(self.padding)

self.expose(self.b, Last())
self.expose(self.n, Last())

def trace(self, n):
self.add(self.fibo_first_step, (1, 1, n))
a = 1
b = 2
for i in range(1, n):
self.add(self.fibo_step, (a, b, n))
prev_a = a
a = b
b += prev_a
while self.needs_padding():
self.add(self.padding, (a, b, n))


fibo = Fibonacci()
fibo_witness = fibo.gen_witness(None)
fibo_witness = fibo.gen_witness(7)
fibo.halo2_mock_prover(fibo_witness)
19 changes: 0 additions & 19 deletions examples/simple.py

This file was deleted.

5 changes: 2 additions & 3 deletions examples/tutorial_pt2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
"# Part 2: Quick Start\n",
"PyChiquito requires using a Python virtual environment for its dependencies, which you should have setup following the [PyChiquito README](https://github.com/qwang98/PyChiquito#readme).\n",
"\n",
"Specifically, after cloning PyChiquito, you need to run the following commands in your local repo (NOT in this Jupyter Notebook), till you see `(.env)` at the start of your command line, which means that you've successfully enabled the virtual environment. You also need to install `maturin` and `py_ecc` dependencies to your local environment and build the project using `maturin develop`. Again, these are all done in your local command line.\n",
"Specifically, after cloning PyChiquito, you need to run the following commands in your local repo (NOT in this Jupyter Notebook), till you see `(.env)` at the start of your command line, which means that you've successfully enabled the virtual environment. You also need to install required dependencies to your local environment and build the project using `maturin develop`. Again, these are all done in your local command line.\n",
"\n",
"```\n",
"python3 -m venv .env\n",
"\n",
"source .env/bin/activate\n",
"\n",
"pip install maturin\n",
"pip install py_ecc\n",
"pip install -r requirements.txt\n",
"\n",
"maturin develop\n",
"```\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_pt3_ch1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"| 3 | 5 | 8 |\n",
"|| ... ||\n",
"\n",
"Besides assigning values to these signals, we also need to define the mathematical relationships among them, which we call \"constraints: in Chiquito. For each row:\n",
"Besides assigning values to these signals, we also need to define the mathematical relationships among them, which we call \"constraints\" in Chiquito. For each row:\n",
"- a + b == c\n",
"- b = a in the next row\n",
"- c = b in the next row\n",
Expand Down
Loading

0 comments on commit 09ef47a

Please sign in to comment.