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

Commit

Permalink
hooked up frontend and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
qwang98 committed Sep 10, 2023
1 parent 9a5093f commit 38b0a5e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
10 changes: 7 additions & 3 deletions examples/mimc7.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def trace(self, args):

self.add(self.mimc7_first_step, (x_value, k_value, c_value, row_value))

for i in range(1, ROUND_KEYS):
for i in range(1, ROUNDS):
row_value += F(1)
x_value += k_value + c_value
x_value = F(x_value**7)
c_value = F(ROUND_KEYS[i])

self.add(self.mimc_step, (x_value, k_value, c_value, row_value))
self.add(self.mimc7_step, (x_value, k_value, c_value, row_value))

row_value += F(1)
x_value += k_value + c_value
Expand Down Expand Up @@ -176,4 +176,8 @@ def mapping(self, args):
self.map(self.mimc7_circuit, (x_in_value, k_value))


Mimc7SuperCircuit()
mimc7 = Mimc7SuperCircuit()
mimc7_witnesses = mimc7.gen_witness((1, 2))
# for key, value in mimc7_witnesses.items():
# print(f"{key}: {str(value)}")
mimc7.halo2_mock_prover(mimc7_witnesses)
5 changes: 2 additions & 3 deletions src/frontend/python/chiquito/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ def halo2_mock_prover(self: SuperCircuit, witnesses: Dict[int, TraceWitness]):
raise ValueError(
f"SuperCircuit.halo2_mock_prover(): TraceWitness with rust_id {rust_id} not found in sub_circuits."
)
rust_chiquito.add_witness_to_ast(witness_json, rust_id)
for sub_circuit_id in self.ast.sub_circuits:
pass
rust_chiquito.add_witness_to_rust_id(witness_json, rust_id)
rust_chiquito.super_circuit_halo2_mock_prover(list(self.ast.sub_circuits.keys()))

# def halo2_mock_prover(self: Circuit, witness: TraceWitness):
# if self.rust_id == 0:
Expand Down
52 changes: 33 additions & 19 deletions src/frontend/python/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pyo3::{
prelude::*,
types::{PyLong, PyString},
types::{PyLong, PyString, PyList},
};

use crate::{
Expand Down Expand Up @@ -34,6 +34,7 @@ thread_local! {
pub static CIRCUIT_MAP: CircuitMap = RefCell::new(HashMap::new());
}

/// Parses JSON into `ast::Circuit` and compile. Generates a Rust UUID. Inserts tuple of (`ast::Circuit`, `ChiquitoHalo2`, `AssignmentGenerator`, _) to `CIRCUIT_MAP` with the Rust UUID as the key. Return the Rust UUID to Python. The last field of the tuple, `TraceWitness`, is left as None, for `chiquito_add_witness_to_rust_id` to insert.
pub fn chiquito_ast_to_halo2(ast_json: &str) -> UUID {
let circuit: Circuit<Fr, ()> =
serde_json::from_str(ast_json).expect("Json deserialization to Circuit failed.");
Expand All @@ -54,7 +55,8 @@ pub fn chiquito_ast_to_halo2(ast_json: &str) -> UUID {
uuid
}

pub fn chiquito_add_witness_to_ast(witness_json: &str, rust_id: UUID) {
/// Parses JSON into `TraceWitness` and insert it into `CIRCUIT_MAP` with `rust_id` as the key.
pub fn chiquito_add_witness_to_rust_id(witness_json: &str, rust_id: UUID) {
let witness: TraceWitness<Fr> =
serde_json::from_str(witness_json).expect("Json deserialization to TraceWitness failed.");

Expand All @@ -67,7 +69,7 @@ pub fn chiquito_add_witness_to_ast(witness_json: &str, rust_id: UUID) {
println!("Added TraceWitness to rust_id: {:?}", rust_id);
}

fn add_assignment_generator_to_ast(assignment_generator: AssignmentGenerator<Fr, ()>, rust_id: UUID) {
fn add_assignment_generator_to_rust_id(assignment_generator: AssignmentGenerator<Fr, ()>, rust_id: UUID) {
CIRCUIT_MAP.with(|circuit_map| {
let mut circuit_map = circuit_map.borrow_mut();
let circuit_map_store = circuit_map.get_mut(&rust_id).unwrap();
Expand All @@ -77,24 +79,25 @@ fn add_assignment_generator_to_ast(assignment_generator: AssignmentGenerator<Fr,
println!("Added AssignmentGenerator to rust_id: {:?}", rust_id);
}

/// Compile a `ChiquitoHalo2SuperCircuit` object from a list of `rust_ids`, each corresponding to a sub-circuit. The `ChiquitoHalo2SuperCircuit` object is then passed to `MockProver` for verification. `TraceWitness`, if any, should have been inserted to each rust_id prior to invoking this function.
pub fn chiquito_super_circuit_halo2_mock_prover(rust_ids: Vec<UUID>) {
let mut ctx = SuperCircuitContext::<Fr, ()>::default();
let mut super_circuit_ctx = SuperCircuitContext::<Fr, ()>::default();

// super_circuit def
let config = config(SingleRowCellManager {}, SimpleStepSelectorBuilder {});
for rust_id in rust_ids.clone() {
let circuit_map_store = uuid_to_halo2(rust_id);
let circuit_map_store = rust_id_to_halo2(rust_id);
let (circuit, chiquito_halo2, assignment_generator, witness) = circuit_map_store;
let assignment = ctx.sub_circuit_with_ast(config.clone(), circuit);
add_assignment_generator_to_ast(assignment, rust_id);
let assignment = super_circuit_ctx.sub_circuit_with_ast(config.clone(), circuit);
add_assignment_generator_to_rust_id(assignment, rust_id);
}

let super_circuit = ctx.compile();
let super_circuit = super_circuit_ctx.compile();
let compiled = chiquitoSuperCircuit2Halo2(&super_circuit);

let mut mapping_ctx = MappingContext::default();
for rust_id in rust_ids {
let circuit_map_store = uuid_to_halo2(rust_id);
let circuit_map_store = rust_id_to_halo2(rust_id);
let (circuit, chiquito_halo2, assignment_generator, witness) = circuit_map_store;
if witness.is_some() {
mapping_ctx.map_with_witness(&assignment_generator.unwrap(), witness.unwrap());
Expand All @@ -121,17 +124,19 @@ pub fn chiquito_super_circuit_halo2_mock_prover(rust_ids: Vec<UUID>) {
}
}

fn uuid_to_halo2(uuid: UUID) -> CircuitMapStore {
/// Returns the (`ast::Circuit`, `ChiquitoHalo2`, `AssignmentGenerator`, `TraceWitness`) tuple corresponding to `rust_id`.
fn rust_id_to_halo2(uuid: UUID) -> CircuitMapStore {
CIRCUIT_MAP.with(|circuit_map| {
let circuit_map = circuit_map.borrow();
circuit_map.get(&uuid).unwrap().clone()
})
}

/// Runs `MockProver` for a single circuit given JSON of `TraceWitness` and `rust_id` of the circuit.
pub fn chiquito_halo2_mock_prover(witness_json: &str, rust_id: UUID) {
let trace_witness: TraceWitness<Fr> =
serde_json::from_str(witness_json).expect("Json deserialization to TraceWitness failed.");
let (_, compiled, assignment_generator, _) = uuid_to_halo2(rust_id);
let (_, compiled, assignment_generator, _) = rust_id_to_halo2(rust_id);
let circuit: ChiquitoHalo2Circuit<_> = ChiquitoHalo2Circuit::new(
compiled,
assignment_generator.map(|g| g.generate_with_witness(trace_witness)),
Expand Down Expand Up @@ -1816,24 +1821,32 @@ fn ast_to_halo2(json: &PyString) -> u128 {
}

#[pyfunction]
fn halo2_mock_prover(witness_json: &PyString, ast_uuid: &PyLong) {
fn halo2_mock_prover(witness_json: &PyString, rust_id: &PyLong) {
chiquito_halo2_mock_prover(
witness_json.to_str().expect("PyString convertion failed."),
ast_uuid.extract().expect("PyLong convertion failed."),
rust_id.extract().expect("PyLong convertion failed."),
);
}

#[pyfunction]
fn add_witness_to_ast(witness_json: &PyString, ast_uuid: &PyLong) {
chiquito_add_witness_to_ast(
fn add_witness_to_rust_id(witness_json: &PyString, rust_id: &PyLong) {
chiquito_add_witness_to_rust_id(
witness_json.to_str().expect("PyString convertion failed."),
ast_uuid.extract().expect("PyLong convertion failed."),
rust_id.extract().expect("PyLong convertion failed."),
);
}

#[pyfunction]
fn super_circuit_halo2_mock_prover() {

fn super_circuit_halo2_mock_prover(rust_ids: &PyList) {
let uuids = rust_ids.iter().map(|rust_id| {
rust_id
.downcast::<PyLong>()
.expect("PyAny downcast failed.")
.extract()
.expect("PyLong convertion failed.")
}).collect::<Vec<UUID>>();

chiquito_super_circuit_halo2_mock_prover(uuids)
}

#[pymodule]
Expand All @@ -1842,6 +1855,7 @@ fn rust_chiquito(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(convert_and_print_trace_witness, m)?)?;
m.add_function(wrap_pyfunction!(ast_to_halo2, m)?)?;
m.add_function(wrap_pyfunction!(halo2_mock_prover, m)?)?;
m.add_function(wrap_pyfunction!(add_witness_to_ast, m)?)?;
m.add_function(wrap_pyfunction!(add_witness_to_rust_id, m)?)?;
m.add_function(wrap_pyfunction!(super_circuit_halo2_mock_prover, m)?)?;
Ok(())
}

0 comments on commit 38b0a5e

Please sign in to comment.