Skip to content

Commit

Permalink
Merge pull request #64 from namnc/issue-38-main-io
Browse files Browse the repository at this point in the history
I will just merge this. It has duplication but it does not do circuit traversal, should be fine.
  • Loading branch information
namnc authored May 28, 2024
2 parents e41f4bf + 9dd0375 commit e00f394
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ impl ArithmeticGate {
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct ArithmeticCircuit {
node_count: u32,
inputs: HashMap<u32, String>,
outputs: HashMap<u32, String>,
signals: HashMap<u32, Signal>,
nodes: HashMap<u32, Node>,
gates: Vec<ArithmeticGate>,
Expand All @@ -176,12 +178,22 @@ impl ArithmeticCircuit {
pub fn new() -> ArithmeticCircuit {
ArithmeticCircuit {
node_count: 0,
inputs: HashMap::new(),
outputs: HashMap::new(),
signals: HashMap::new(),
nodes: HashMap::new(),
gates: Vec::new(),
}
}

pub fn add_inputs(&mut self, inputs: HashMap<u32, String>) {
self.inputs.extend(inputs);
}

pub fn add_outputs(&mut self, outputs: HashMap<u32, String>) {
self.outputs.extend(outputs);
}

/// Adds a new signal to the circuit.
pub fn add_signal(
&mut self,
Expand All @@ -207,6 +219,16 @@ impl ArithmeticCircuit {
Ok(())
}

pub fn get_signals(&self, filter: String) -> HashMap<u32, String> {
let mut ret = HashMap::new();
for (signal_id, signal) in self.signals.iter() {
if signal.name.starts_with(filter.as_str()) {
ret.insert(*signal_id, signal.name.to_string());
}
}
ret
}

/// Adds a new gate to the circuit.
pub fn add_gate(
&mut self,
Expand Down
20 changes: 20 additions & 0 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
process::{process_expression, process_statements},
runtime::{DataAccess, DataType, Runtime, RuntimeError},
};
use circom_compiler::num_traits::sign;
use circom_program_structure::ast::Expression;
use std::io;
use thiserror::Error;
Expand Down Expand Up @@ -51,6 +52,25 @@ pub fn build_circuit(input: &Input) -> Result<ArithmeticCircuit, ProgramError> {
// Process the main component
let statements = template_data.get_body_as_vec();
process_statements(&mut circuit, &mut runtime, &program_archive, statements)?;

for (ikey, (ivs, ivh)) in template_data.get_inputs().iter() {
// println!("{ikey}:{ivs}");
let filter = format!("0.{}", ikey);
circuit.add_inputs(circuit.get_signals(filter));
// for ivhs in ivh.iter() {
// println!("{ivhs}");
// }
}

for (okey, (ovs, ovh)) in template_data.get_outputs().iter() {
// println!("{okey}:{ovs}");
let filter = format!("0.{}", okey);
let signals = circuit.get_signals(filter);
circuit.add_outputs(signals);
// for ovhs in ovh.iter() {
// println!("{ovhs}");
// }
}
}
_ => return Err(ProgramError::MainExpressionNotACall),
}
Expand Down

0 comments on commit e00f394

Please sign in to comment.