-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Steve/jupyter notebook tutorial part 4 (#15)
* 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
Showing
12 changed files
with
968 additions
and
184 deletions.
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
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) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.