Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types support #82

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/arithmetic_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cli::ValueType;
use crate::compiler::{ArithmeticGate, CircuitError};
use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string};
Expand All @@ -20,6 +21,7 @@ pub struct CircuitInfo {
pub input_name_to_wire_index: HashMap<String, u32>,
pub constants: HashMap<String, ConstantInfo>,
pub output_name_to_wire_index: HashMap<String, u32>,
pub value_type: ValueType,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -233,6 +235,7 @@ mod tests {
.collect(),
constants: Default::default(),
output_name_to_wire_index: [("output0".to_string(), 3)].iter().cloned().collect(),
value_type: ValueType::Sint,
},
gates: vec![
ArithmeticGate {
Expand Down Expand Up @@ -295,6 +298,7 @@ mod tests {
.iter()
.cloned()
.collect(),
value_type: ValueType::Sint,
},
"
2 4
Expand Down
32 changes: 29 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use clap::Parser;
use std::path::{Path, PathBuf};

use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ValueType {
#[serde(rename = "sint")]
#[default]
Sint,
#[serde(rename = "sfloat")]
Sfloat,
}

#[derive(Parser)]
#[clap(name = "Arithmetic Circuits Compiler")]
#[command(disable_help_subcommand = true)]
Expand All @@ -22,11 +34,25 @@ pub struct Args {
default_value = "./output/"
)]
pub output: PathBuf,

/// Default type that's used for values in a MPC backend
#[arg(
short,
long,
value_enum,
help = "Type that'll be used for values in a MPC backend",
default_value_t = ValueType::Sint,
)]
pub value_type: ValueType,
}

impl Args {
pub fn new(input: PathBuf, output: PathBuf) -> Self {
Self { input, output }
pub fn new(input: PathBuf, output: PathBuf, value_type: ValueType) -> Self {
Self {
input,
output,
value_type,
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::{
arithmetic_circuit::{ArithmeticCircuit, CircuitInfo, ConstantInfo},
cli::ValueType,
program::ProgramError,
topological_sort::topological_sort,
};
Expand Down Expand Up @@ -174,6 +175,7 @@ pub struct Compiler {
signals: HashMap<u32, Signal>,
nodes: HashMap<u32, Node>,
gates: Vec<ArithmeticGate>,
value_type: ValueType,
}

impl Compiler {
Expand All @@ -185,6 +187,7 @@ impl Compiler {
signals: HashMap::new(),
nodes: HashMap::new(),
gates: Vec::new(),
value_type: Default::default(),
}
}

Expand Down Expand Up @@ -338,6 +341,12 @@ impl Compiler {
Ok(())
}

pub fn update_type(&mut self, value_type: ValueType) -> Result<(), CircuitError> {
self.value_type = value_type;

Ok(())
}

/// Generates a circuit report with input and output signals information.
pub fn generate_circuit_report(&self) -> Result<CircuitReport, CircuitError> {
// Split input and output nodes
Expand Down Expand Up @@ -536,6 +545,7 @@ impl Compiler {
.iter()
.map(|(name, node_id)| (name.clone(), node_id_to_wire_id[node_id]))
.collect(),
value_type: self.value_type,
},
gates: new_gates,
})
Expand Down
2 changes: 2 additions & 0 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub fn compile(args: &Args) -> Result<Compiler, ProgramError> {
_ => return Err(ProgramError::MainExpressionNotACall),
}

compiler.update_type(args.value_type)?;

Ok(compiler)
}

Expand Down
10 changes: 7 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use circom_2_arithc::{cli::Args, program::compile};
use circom_2_arithc::{
cli::{Args, ValueType},
program::compile,
};
use sim_circuit::{simulate, NumberU32};
use std::collections::HashMap;

Expand All @@ -10,7 +13,7 @@ pub fn simulation_test<
input: Input,
expected_output: Output,
) {
let compiler_input = Args::new(test_file_path.into(), "./".into());
let compiler_input = Args::new(test_file_path.into(), "./".into(), ValueType::Sint);
let circuit = compile(&compiler_input).unwrap().build_circuit().unwrap();

let input = input
Expand Down Expand Up @@ -186,10 +189,11 @@ mod integration_tests {
let compiler_input = Args::new(
"tests/circuits/integration/indexOutOfBounds.circom".into(),
"./".into(),
ValueType::Sint,
);
let circuit = compile(&compiler_input);

assert_eq!(circuit.is_err(), true);
assert!(circuit.is_err());
assert_eq!(
circuit.unwrap_err().to_string(),
"Runtime error: Index out of bounds"
Expand Down