Skip to content

Commit

Permalink
wip component declaration and wiring data content
Browse files Browse the repository at this point in the history
  • Loading branch information
namnc committed Jan 15, 2024
1 parent 7bbbafb commit b2f59a3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 17 deletions.
23 changes: 14 additions & 9 deletions src/assets/circuit.circom
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ template InnerProd () {

variable_A = 100;

for (var i = 0; i < 3; i++) {
variable_A = variable_A + 10;
}
// for (var i = 0; i < 3; i++) {
// variable_A = variable_A + 10;
// }

if ( variable_A < 50) {
variable_A = variable_A * 2;
} else {
variable_A = variable_A / 2;
}
// if ( variable_A < 50) {
// variable_A = variable_A * 2;
// } else {
// variable_A = variable_A / 2;
// }

component c = libt(20);
c.input_A <== input_A;
c.input_B <== input_B;
c.ip <== ip;

ip <== input_A + input_B + variable_A;

variable_A = libf(variable_A);
// variable_A = libf(variable_A);

// component c = libt();
}
Expand Down
4 changes: 2 additions & 2 deletions src/assets/lib.circom
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pragma circom 2.0.0;

template libt () {
template libt (x) {
signal input input_A;
signal input input_B;
signal output ip;

var variable_A;

variable_A = 100;
variable_A = x;

ip <== input_A + input_B + variable_A;
}
Expand Down
2 changes: 1 addition & 1 deletion src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn execute_expression(
if functions.contains(id) {
// let ret = ctx.get_data_item("RETURN").unwrap().get_u32().unwrap();
// runtime.pop_context();
Ok(ret)
Ok(0)
} else {
// runtime.pop_context();
Ok(0)
Expand Down
52 changes: 52 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ impl Context {
}
}

/// Declares a new component.
pub fn declare_component(&mut self, name: &str) -> Result<u32, RuntimeError> {
debug!("Declaring component {}", name);
let component_id = self.generate_id();
if self.values.contains_key(name) {
Err(RuntimeError::DataItemAlreadyDeclared)
} else {
self.values
.insert(name.to_string(), DataItem::new(DataType::Component));
self.set_data_item(name, DataContent::Wiring(HashMap::new()))?;
Ok(component_id)
}
}

/// Declares a new const value as a signal.
/// Sets the value of the signal to the given value. This being the signal id.
pub fn declare_const(&mut self, value: u32) -> Result<(), RuntimeError> {
Expand Down Expand Up @@ -282,6 +296,25 @@ impl Context {
Ok((signal_name, signal_id))
}

// TODO: array auto var should support multi-dimension, right now 1
// TODO: temporary implementation, need to be reviewed
/// Creates a unique signal for an array element based on its indices and assigns it a unique identifier.
pub fn declare_component_array(
&mut self,
name: &str,
indices: Vec<u32>,
) -> Result<(String, u32), RuntimeError> {
let mut component_name = name.to_string();

for indice in indices {
component_name.push_str(&format!("_{}", indice));
}

let component_id = self.declare_component(&component_name)?;

Ok((component_name, component_id))
}

/// Creates a unique variable for an array element based on its indices and assigns it a unique identifier.
pub fn declare_var_array(&mut self, name: &str, indices: Vec<u32>) -> Result<(), RuntimeError> {
let mut var_name = name.to_string();
Expand All @@ -292,19 +325,23 @@ impl Context {

self.declare_variable(name)
}


}

/// Data type
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DataType {
Signal,
Variable,
Component
}

/// Data content
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DataContent {
Scalar(u32),
Wiring(HashMap<String, String>),
Array(Vec<DataContent>),
}

Expand Down Expand Up @@ -351,6 +388,18 @@ impl DataItem {
match &self.content {
Some(DataContent::Scalar(value)) => Ok(*value),
Some(DataContent::Array(_)) => Err(RuntimeError::NotAScalar),
Some(DataContent::Wiring(_)) => Err(RuntimeError::NotAScalar),
None => Err(RuntimeError::EmptyDataItem),
}
}

/// Gets the name value if the content is a wiring.
/// Returns an error if the content is not a wiring or not set.
pub fn get_wiring(&self) -> Result<HashMap<String, String>, RuntimeError> {
match &self.content {
Some(DataContent::Scalar(_)) => Err(RuntimeError::NotAWiring),
Some(DataContent::Array(_)) => Err(RuntimeError::NotAWiring),
Some(DataContent::Wiring(value)) => Ok(value.clone()),
None => Err(RuntimeError::EmptyDataItem),
}
}
Expand All @@ -363,6 +412,7 @@ impl DataItem {
array.get(index).ok_or(RuntimeError::IndexOutOfBounds)
}
Some(DataContent::Scalar(_)) => Err(RuntimeError::NotAnArray),
Some(DataContent::Wiring(_)) => Err(RuntimeError::NotAnArray),
None => Err(RuntimeError::EmptyDataItem),
}
}
Expand Down Expand Up @@ -399,6 +449,8 @@ pub enum RuntimeError {
NotAnArray,
#[error("Data Item content is not a scalar")]
NotAScalar,
#[error("Data Item content is not a component wiring")]
NotAWiring,
#[error("Cannot modify an already set signal")]
SignalAlreadySet,
}
32 changes: 27 additions & 5 deletions src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ pub fn traverse_statement(

match xtype {
VariableType::Component => {
todo!("Component declaration not handled")
// Here when a component is declared it will not be instantiated
// Instead we need to keep track of the inputs and outputs signals of the component
// And when the output signals are all wired we instantiated the component, i.e. do the traverse of the template statements
// However at the declaration we should have all the arguments
traverse_declaration(ac, runtime, name, xtype, &dim_u32_vec)
}
VariableType::Var => traverse_declaration(ac, runtime, name, xtype, &dim_u32_vec),
VariableType::Signal(_, _) => {
Expand Down Expand Up @@ -134,6 +138,12 @@ pub fn traverse_statement(
DataType::Variable => {
execute_statement(ac, runtime, stmt, program_archive)?;
}
DataType::Component => {
//Here we deal with wiring
//lhs is a component wire and rhs is a signal

// we also check to complete template if all wiring is done
}
}
}
Ok(())
Expand Down Expand Up @@ -246,11 +256,11 @@ pub fn traverse_expression(
traverse_sequence_of_statements(ac, runtime, _body, _program_archive, true)?;

if functions.contains(id) {
let ret = ctx.get_data_item("RETURN").unwrap().get_u32().unwrap();
runtime.pop_context();
Ok(ret.to_string())
// let ret = ctx.get_data_item("RETURN").unwrap().get_u32().unwrap();
// runtime.pop_context();
Ok(id.to_string())
} else {
runtime.pop_context();
// runtime.pop_context();
Ok(id.to_string())
}

Expand Down Expand Up @@ -344,6 +354,18 @@ pub fn traverse_declaration(
ctx.declare_variable(var_name)?;
}
}
VariableType::Component => {
// Here now we have component with empty wiring
if is_array {
for &i in dim_u32_vec {
let (name, id) = ctx.declare_component_array(var_name, vec![i])?;
ac.add_var(id, &name);
}
} else {
let signal_id = ctx.declare_component(var_name)?;
ac.add_var(signal_id, var_name);
}
}
_ => unimplemented!(),
}

Expand Down

0 comments on commit b2f59a3

Please sign in to comment.