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

Commit

Permalink
rust id instead of ast id
Browse files Browse the repository at this point in the history
  • Loading branch information
qwang98 committed Sep 10, 2023
1 parent 832923f commit 9a5093f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
34 changes: 17 additions & 17 deletions src/frontend/python/chiquito/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ class SuperCircuit:
def __init__(self: SuperCircuit):
self.ast = ASTSuperCircuit()
self.tables: Dict[int, LookupTable] = {}
# self.rust_ast_id: List[int] = []
# self.rust_id: List[int] = []
self.mode = SuperCircuitMode.SETUP
self.setup()
self.mode = SuperCircuitMode.NoMode

# called under setup()
def sub_circuit(self: SuperCircuit, sub_circuit: Circuit) -> Circuit:
assert self.mode == SuperCircuitMode.SETUP
if sub_circuit.rust_ast_id != 0:
if sub_circuit.rust_id != 0:
raise ValueError(
"SuperCircuit: sub_circuit() cannot be called twice on the same circuit."
)
ast_json: str = sub_circuit.get_ast_json()
sub_circuit.rust_ast_id: int = rust_chiquito.ast_to_halo2(ast_json)
self.ast.sub_circuits[sub_circuit.rust_ast_id] = sub_circuit.ast
sub_circuit.rust_id: int = rust_chiquito.ast_to_halo2(ast_json)
self.ast.sub_circuits[sub_circuit.rust_id] = sub_circuit.ast
return sub_circuit

# called under mapping()
# generates TraceWitness for sub_circuit
def map(self: SuperCircuit, sub_circuit: Circuit, args: Any) -> TraceWitness:
assert self.mode == SuperCircuitMode.Mapping
witness: TraceWitness = sub_circuit.gen_witness(args)
if sub_circuit.rust_ast_id == 0:
if sub_circuit.rust_id == 0:
raise ValueError(
"SuperCircuit: must call sub_circuit() before calling map() on a Circuit."
)
self.ast.witnesses[sub_circuit.rust_ast_id] = witness
self.ast.witnesses[sub_circuit.rust_id] = witness
return witness

# called at the outermost level
Expand Down Expand Up @@ -88,22 +88,22 @@ def gen_witness(self: SuperCircuit, args: Any) -> Dict[int, TraceWitness]:
# return json.dumps(self.ast, cls=CustomEncoder, indent=4)

def halo2_mock_prover(self: SuperCircuit, witnesses: Dict[int, TraceWitness]):
for rust_ast_id, witness in witnesses.items():
for rust_id, witness in witnesses.items():
witness_json: str = witness.get_witness_json()
if rust_ast_id not in self.ast.sub_circuits:
if rust_id not in self.ast.sub_circuits:
raise ValueError(
f"SuperCircuit.halo2_mock_prover(): TraceWitness with rust_ast_id {rust_ast_id} not found in sub_circuits."
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_ast_id)
rust_chiquito.add_witness_to_ast(witness_json, rust_id)
for sub_circuit_id in self.ast.sub_circuits:
pass

# def halo2_mock_prover(self: Circuit, witness: TraceWitness):
# if self.rust_ast_id == 0:
# if self.rust_id == 0:
# ast_json: str = self.get_ast_json()
# self.rust_ast_id: int = rust_chiquito.ast_to_halo2(ast_json)
# self.rust_id: int = rust_chiquito.ast_to_halo2(ast_json)
# witness_json: str = witness.get_witness_json()
# rust_chiquito.halo2_mock_prover(witness_json, self.rust_ast_id)
# rust_chiquito.halo2_mock_prover(witness_json, self.rust_id)


class CircuitMode(Enum):
Expand All @@ -117,7 +117,7 @@ class Circuit:
def __init__(self: Circuit, super_circuit=None, imports=None):
self.ast = ASTCircuit()
self.witness = TraceWitness()
self.rust_ast_id = 0
self.rust_id = 0
self.super_circuit = super_circuit
self.imports = imports
self.mode = CircuitMode.SETUP
Expand Down Expand Up @@ -232,11 +232,11 @@ def get_ast_json(self: Circuit) -> str:
return json.dumps(self.ast, cls=CustomEncoder, indent=4)

def halo2_mock_prover(self: Circuit, witness: TraceWitness):
if self.rust_ast_id == 0:
if self.rust_id == 0:
ast_json: str = self.get_ast_json()
self.rust_ast_id: int = rust_chiquito.ast_to_halo2(ast_json)
self.rust_id: int = rust_chiquito.ast_to_halo2(ast_json)
witness_json: str = witness.get_witness_json()
rust_chiquito.halo2_mock_prover(witness_json, self.rust_ast_id)
rust_chiquito.halo2_mock_prover(witness_json, self.rust_id)

def __str__(self: Circuit) -> str:
return self.ast.__str__()
Expand Down
28 changes: 14 additions & 14 deletions src/frontend/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,47 +54,47 @@ pub fn chiquito_ast_to_halo2(ast_json: &str) -> UUID {
uuid
}

pub fn chiquito_add_witness_to_ast(witness_json: &str, ast_id: UUID) {
pub fn chiquito_add_witness_to_ast(witness_json: &str, rust_id: UUID) {
let witness: TraceWitness<Fr> =
serde_json::from_str(witness_json).expect("Json deserialization to TraceWitness failed.");

CIRCUIT_MAP.with(|circuit_map| {
let mut circuit_map = circuit_map.borrow_mut();
let circuit_map_store = circuit_map.get_mut(&ast_id).unwrap();
let circuit_map_store = circuit_map.get_mut(&rust_id).unwrap();
circuit_map_store.3 = Some(witness);
});

println!("Added TraceWitness to ast_id: {:?}", ast_id);
println!("Added TraceWitness to rust_id: {:?}", rust_id);
}

fn add_assignment_generator_to_ast(assignment_generator: AssignmentGenerator<Fr, ()>, ast_id: UUID) {
fn add_assignment_generator_to_ast(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(&ast_id).unwrap();
let circuit_map_store = circuit_map.get_mut(&rust_id).unwrap();
circuit_map_store.2 = Some(assignment_generator);
});

println!("Added AssignmentGenerator to ast_id: {:?}", ast_id);
println!("Added AssignmentGenerator to rust_id: {:?}", rust_id);
}

pub fn chiquito_super_circuit_halo2_mock_prover(ast_ids: Vec<UUID>) {
pub fn chiquito_super_circuit_halo2_mock_prover(rust_ids: Vec<UUID>) {
let mut ctx = SuperCircuitContext::<Fr, ()>::default();

// super_circuit def
let config = config(SingleRowCellManager {}, SimpleStepSelectorBuilder {});
for ast_id in ast_ids.clone() {
let circuit_map_store = uuid_to_halo2(ast_id);
for rust_id in rust_ids.clone() {
let circuit_map_store = uuid_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, ast_id);
add_assignment_generator_to_ast(assignment, rust_id);
}

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

let mut mapping_ctx = MappingContext::default();
for ast_id in ast_ids {
let circuit_map_store = uuid_to_halo2(ast_id);
for rust_id in rust_ids {
let circuit_map_store = uuid_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 Down Expand Up @@ -128,10 +128,10 @@ fn uuid_to_halo2(uuid: UUID) -> CircuitMapStore {
})
}

pub fn chiquito_halo2_mock_prover(witness_json: &str, ast_id: UUID) {
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(ast_id);
let (_, compiled, assignment_generator, _) = uuid_to_halo2(rust_id);
let circuit: ChiquitoHalo2Circuit<_> = ChiquitoHalo2Circuit::new(
compiled,
assignment_generator.map(|g| g.generate_with_witness(trace_witness)),
Expand Down

0 comments on commit 9a5093f

Please sign in to comment.