Skip to content

Commit

Permalink
style: run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
curryrasul committed Jun 3, 2024
1 parent fada008 commit 57afbca
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 61 deletions.
99 changes: 56 additions & 43 deletions src/circom/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::{Path, PathBuf};
use input_processing::SimplificationStyle;
use std::path::{Path, PathBuf};

pub struct Input {
pub input_program: PathBuf,
Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct Input {
pub no_rounds: usize,
pub flag_verbose: bool,
pub prime: String,
pub link_libraries : Vec<PathBuf>
pub link_libraries: Vec<PathBuf>,
}

#[allow(dead_code)]
Expand All @@ -53,7 +53,7 @@ impl Input {

let c_flag = input_processing::get_c(&matches);

if c_flag && (file_name == "main" || file_name == "fr" || file_name == "calcwit"){
if c_flag && (file_name == "main" || file_name == "fr" || file_name == "calcwit") {
println!("The name {} is reserved in Circom when using de --c flag. The files generated for your circuit will use the name {}_c instead of {}.", file_name, file_name, file_name);
file_name = format!("{}_c", file_name)
};
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Input {
&format!("{}_substitutions", file_name),
JSON,
),
wat_flag:input_processing::get_wat(&matches),
wat_flag: input_processing::get_wat(&matches),
wasm_flag: input_processing::get_wasm(&matches),
c_flag,
r1cs_flag: input_processing::get_r1cs(&matches),
Expand All @@ -93,28 +93,32 @@ impl Input {
json_constraint_flag: input_processing::get_json_constraints(&matches),
json_substitution_flag: input_processing::get_json_substitutions(&matches),
print_ir_flag: input_processing::get_ir(&matches),
no_rounds: if let SimplificationStyle::O2(r) = o_style { r } else { 0 },
no_rounds: if let SimplificationStyle::O2(r) = o_style {
r
} else {
0
},
fast_flag: o_style == SimplificationStyle::O0,
reduced_simplification_flag: o_style == SimplificationStyle::O1,
parallel_simplification_flag: input_processing::get_parallel_simplification(&matches),
inspect_constraints_flag: input_processing::get_inspect_constraints(&matches),
flag_old_heuristics: input_processing::get_flag_old_heuristics(&matches),
flag_verbose: input_processing::get_flag_verbose(&matches),
flag_verbose: input_processing::get_flag_verbose(&matches),
prime: input_processing::get_prime(&matches)?,
link_libraries
link_libraries,
})
}

fn build_folder(output_path: &Path, filename: &str, ext: &str) -> PathBuf {
let mut file = output_path.to_path_buf();
let folder_name = format!("{}_{}",filename,ext);
let folder_name = format!("{}_{}", filename, ext);
file.push(folder_name);
file
}

pub fn build_output(output_path: &Path, filename: &str, ext: &str) -> PathBuf {
let mut file = output_path.to_path_buf();
file.push(format!("{}.{}",filename,ext));
file.push(format!("{}.{}", filename, ext));
file
}

Expand Down Expand Up @@ -211,7 +215,7 @@ impl Input {
pub fn no_rounds(&self) -> usize {
self.no_rounds
}
pub fn prime(&self) -> String{
pub fn prime(&self) -> String {
self.prime.clone()
}
}
Expand All @@ -227,7 +231,11 @@ pub mod input_processing {
if route.is_file() {
Result::Ok(route)
} else {
let route = if route.to_str().is_some() { ": ".to_owned() + route.to_str().unwrap()} else { "".to_owned() };
let route = if route.to_str().is_some() {
": ".to_owned() + route.to_str().unwrap()
} else {
"".to_owned()
};
Result::Err(eprintln!("Input file does not exist {}", route))
}
}
Expand All @@ -242,25 +250,33 @@ pub mod input_processing {
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum SimplificationStyle { O0, O1, O2(usize) }
pub enum SimplificationStyle {
O0,
O1,
O2(usize),
}
pub fn get_simplification_style(matches: &ArgMatches) -> Result<SimplificationStyle, ()> {

let o_0 = matches.is_present("no_simplification");
let o_1 = matches.is_present("reduced_simplification");
let o_2 = matches.is_present("full_simplification");
let o_2round = matches.is_present("simplification_rounds");
match (o_0, o_1, o_2round, o_2) {
(true, _, _, _) => Ok(SimplificationStyle::O0),
(_, true, _, _) => Ok(SimplificationStyle::O1),
(_, _, true, _) => {
(_, _, true, _) => {
let o_2_argument = matches.value_of("simplification_rounds").unwrap();
let rounds_r = o_2_argument.parse::<usize>();
if let Result::Ok(no_rounds) = rounds_r {
if no_rounds == 0 { Ok(SimplificationStyle::O1) }
else {Ok(SimplificationStyle::O2(no_rounds))}}
else { Result::Err(log::error!("invalid number of rounds")) }
},

if let Result::Ok(no_rounds) = rounds_r {
if no_rounds == 0 {
Ok(SimplificationStyle::O1)
} else {
Ok(SimplificationStyle::O2(no_rounds))
}
} else {
Result::Err(log::error!("invalid number of rounds"))
}
}

(false, false, false, true) => Ok(SimplificationStyle::O2(usize::MAX)),
(false, false, false, false) => Ok(SimplificationStyle::O2(usize::MAX)),
}
Expand Down Expand Up @@ -317,26 +333,23 @@ pub mod input_processing {
matches.is_present("flag_old_heuristics")
}
pub fn get_prime(matches: &ArgMatches) -> Result<String, ()> {

match matches.is_present("prime"){
true =>
{
let prime_value = matches.value_of("prime").unwrap();
if prime_value == "bn128"
|| prime_value == "bls12381"
|| prime_value == "goldilocks"
|| prime_value == "grumpkin"
|| prime_value == "pallas"
|| prime_value == "vesta"
|| prime_value == "secq256r1"
{
Ok(String::from(matches.value_of("prime").unwrap()))
}
else{
Result::Err(log::error!("Invalid prime number"))
}
}

match matches.is_present("prime") {
true => {
let prime_value = matches.value_of("prime").unwrap();
if prime_value == "bn128"
|| prime_value == "bls12381"
|| prime_value == "goldilocks"
|| prime_value == "grumpkin"
|| prime_value == "pallas"
|| prime_value == "vesta"
|| prime_value == "secq256r1"
{
Ok(String::from(matches.value_of("prime").unwrap()))
} else {
Result::Err(log::error!("Invalid prime number"))
}
}

false => Ok(String::from("bn128")),
}
}
Expand Down Expand Up @@ -449,8 +462,8 @@ pub mod input_processing {
.short("l")
.takes_value(true)
.multiple(true)
.number_of_values(1)
.display_order(330)
.number_of_values(1)
.display_order(330)
.help("Adds directory to library search path"),
)
.arg(
Expand Down
2 changes: 1 addition & 1 deletion src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use bmr16_mpz::{
use circom_program_structure::ast::ExpressionInfixOpcode;
use log::debug;
use serde::{Deserialize, Serialize};
use sim_circuit::circuit::CircuitError as SimCircuitError;
use sim_circuit::circuit::{Circuit as SimCircuit, Gate as SimGate, Node as SimNode, Operation};
use std::collections::HashMap;
use thiserror::Error;
use sim_circuit::circuit::CircuitError as SimCircuitError;

/// Types of gates that can be used in an arithmetic circuit.
#[derive(Debug, Serialize, Deserialize)]
Expand Down
8 changes: 4 additions & 4 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::runtime::{
};
use circom_circom_algebra::num_traits::ToPrimitive;
use circom_program_structure::ast::{
Access, AssignOp, Expression, ExpressionInfixOpcode, ExpressionPrefixOpcode, Statement
Access, AssignOp, Expression, ExpressionInfixOpcode, ExpressionPrefixOpcode, Statement,
};
use circom_program_structure::program_archive::ProgramArchive;
use std::cell::RefCell;
Expand Down Expand Up @@ -272,9 +272,9 @@ pub fn process_expression(
Expression::InfixOp {
lhe, infix_op, rhe, ..
} => handle_infix_op(ac, runtime, program_archive, infix_op, lhe, rhe),
Expression::PrefixOp {
prefix_op, rhe, ..
} => handle_prefix_op(ac, runtime, program_archive, prefix_op, rhe),
Expression::PrefixOp { prefix_op, rhe, .. } => {
handle_prefix_op(ac, runtime, program_archive, prefix_op, rhe)
}
Expression::Number(_, value) => {
let signal_gen = runtime.get_signal_gen();
let access = runtime
Expand Down
30 changes: 17 additions & 13 deletions tests/prefix_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ fn test_prefix_ops() {
let sim_circuit = circuit.build_sim_circuit().unwrap();

let circuit_input = vec![
0, 1, 2, // actual inputs
0, u32::MAX // constants - FIXME: should not need to provide these
0,
1,
2, // actual inputs
0,
u32::MAX, // constants - FIXME: should not need to provide these
];

let res = sim_circuit.execute(&circuit_input).unwrap();

assert_eq!(res, vec![
0, // -0

1, // !0
0, // !1
0, // !2

0b_11111111_11111111_11111111_11111111, // ~0
0b_11111111_11111111_11111111_11111110, // ~1
0b_11111111_11111111_11111111_11111101, // ~2
]);
assert_eq!(
res,
vec![
0, // -0
1, // !0
0, // !1
0, // !2
0b_11111111_11111111_11111111_11111111, // ~0
0b_11111111_11111111_11111111_11111110, // ~1
0b_11111111_11111111_11111111_11111101, // ~2
]
);
}

0 comments on commit 57afbca

Please sign in to comment.