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

cargo fmt #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 45 additions & 17 deletions circom/src/compilation_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use program_structure::error_code::ReportCode;
use program_structure::file_definition::FileLibrary;
use crate::VERSION;


pub struct CompilerConfig {
pub js_folder: String,
pub wasm_name: String,
Expand All @@ -25,17 +24,25 @@ pub struct CompilerConfig {
}

pub fn compile(config: CompilerConfig) -> Result<(), ()> {


if config.c_flag || config.wat_flag || config.wasm_flag{
if config.c_flag || config.wat_flag || config.wasm_flag {
let circuit = compiler_interface::run_compiler(
config.vcp,
Config { debug_output: config.debug_output, produce_input_log: config.produce_input_log, wat_flag: config.wat_flag },
VERSION
Config {
debug_output: config.debug_output,
produce_input_log: config.produce_input_log,
wat_flag: config.wat_flag,
},
VERSION,
)?;

if config.c_flag {
compiler_interface::write_c(&circuit, &config.c_folder, &config.c_run_name, &config.c_file, &config.dat_file)?;
compiler_interface::write_c(
&circuit,
&config.c_folder,
&config.c_run_name,
&config.c_file,
&config.dat_file,
)?;
println!(
"{} {} and {}",
Colour::Green.paint("Written successfully:"),
Expand All @@ -45,7 +52,7 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
println!(
"{} {}/{}, {}, {}, {}, {}, {}, {} and {}",
Colour::Green.paint("Written successfully:"),
&config.c_folder,
&config.c_folder,
"main.cpp".to_string(),
"circom.hpp".to_string(),
"calcwit.hpp".to_string(),
Expand All @@ -56,10 +63,15 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
"Makefile".to_string()
);
}

match (config.wat_flag, config.wasm_flag) {
(true, true) => {
compiler_interface::write_wasm(&circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
compiler_interface::write_wasm(
&circuit,
&config.js_folder,
&config.wasm_name,
&config.wat_file,
)?;
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wat_file);
let result = wat_to_wasm(&config.wat_file, &config.wasm_file);
match result {
Expand All @@ -68,12 +80,21 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
return Err(());
}
Result::Ok(()) => {
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wasm_file);
println!(
"{} {}",
Colour::Green.paint("Written successfully:"),
config.wasm_file
);
}
}
}
(false, true) => {
compiler_interface::write_wasm(&circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
compiler_interface::write_wasm(
&circuit,
&config.js_folder,
&config.wasm_name,
&config.wat_file,
)?;
let result = wat_to_wasm(&config.wat_file, &config.wasm_file);
std::fs::remove_file(&config.wat_file).unwrap();
match result {
Expand All @@ -82,23 +103,30 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
return Err(());
}
Result::Ok(()) => {
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wasm_file);
println!(
"{} {}",
Colour::Green.paint("Written successfully:"),
config.wasm_file
);
}
}
}
(true, false) => {
compiler_interface::write_wasm(&circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
compiler_interface::write_wasm(
&circuit,
&config.js_folder,
&config.wasm_name,
&config.wat_file,
)?;
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wat_file);
}
(false, false) => {}
}
}


Ok(())
}


fn wat_to_wasm(wat_file: &str, wasm_file: &str) -> Result<(), Report> {
use std::fs::read_to_string;
use std::fs::File;
Expand Down
11 changes: 7 additions & 4 deletions circom/src/execution_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use constraint_writers::debug_writer::DebugWriter;
use constraint_writers::ConstraintExporter;
use program_structure::program_archive::ProgramArchive;


pub struct ExecutionConfig {
pub r1cs: String,
pub sym: String,
Expand All @@ -13,7 +12,7 @@ pub struct ExecutionConfig {
pub flag_s: bool,
pub flag_f: bool,
pub flag_p: bool,
pub flag_old_heuristics:bool,
pub flag_old_heuristics: bool,
pub flag_verbose: bool,
pub inspect_constraints_flag: bool,
pub sym_flag: bool,
Expand All @@ -38,7 +37,7 @@ pub fn execute_project(
flag_verbose: config.flag_verbose,
inspect_constraints: config.inspect_constraints_flag,
flag_old_heuristics: config.flag_old_heuristics,
prime : config.prime,
prime: config.prime,
};
let custom_gates = program_archive.custom_gates;
let (exporter, vcp) = build_circuit(program_archive, build_config)?;
Expand All @@ -54,7 +53,11 @@ pub fn execute_project(
Result::Ok(vcp)
}

fn generate_output_r1cs(file: &str, exporter: &dyn ConstraintExporter, custom_gates: bool) -> Result<(), ()> {
fn generate_output_r1cs(
file: &str,
exporter: &dyn ConstraintExporter,
custom_gates: bool,
) -> Result<(), ()> {
if let Result::Ok(()) = exporter.r1cs(file, custom_gates) {
println!("{} {}", Colour::Green.paint("Written successfully:"), file);
Result::Ok(())
Expand Down
108 changes: 59 additions & 49 deletions circom/src/input_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ pub struct Input {
pub no_rounds: usize,
pub flag_verbose: bool,
pub prime: String,
pub link_libraries : Vec<PathBuf>
pub link_libraries: Vec<PathBuf>,
}


const R1CS: &'static str = "r1cs";
const WAT: &'static str = "wat";
const WASM: &'static str = "wasm";
Expand All @@ -44,7 +43,6 @@ const DAT: &'static str = "dat";
const SYM: &'static str = "sym";
const JSON: &'static str = "json";


impl Input {
pub fn new() -> Result<Input, ()> {
use ansi_term::Colour;
Expand All @@ -56,7 +54,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!("{}", Colour::Yellow.paint(format!("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 All @@ -70,10 +68,10 @@ impl Input {
out_r1cs: Input::build_output(&output_path, &file_name, R1CS),
out_wat_code: Input::build_output(&output_js_path, &file_name, WAT),
out_wasm_code: Input::build_output(&output_js_path, &file_name, WASM),
out_js_folder: output_js_path.clone(),
out_wasm_name: file_name.clone(),
out_c_folder: output_c_path.clone(),
out_c_run_name: file_name.clone(),
out_js_folder: output_js_path.clone(),
out_wasm_name: file_name.clone(),
out_c_folder: output_c_path.clone(),
out_c_run_name: file_name.clone(),
out_c_code: Input::build_output(&output_c_path, &file_name, CPP),
out_c_dat: Input::build_output(&output_c_path, &file_name, DAT),
out_sym: Input::build_output(&output_path, &file_name, SYM),
Expand All @@ -82,7 +80,7 @@ impl Input {
&format!("{}_constraints", 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: c_flag,
r1cs_flag: input_processing::get_r1cs(&matches),
Expand All @@ -97,22 +95,22 @@ impl Input {
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: &PathBuf, filename: &str, ext: &str) -> PathBuf {
let mut file = output_path.clone();
let folder_name = format!("{}_{}",filename,ext);
file.push(folder_name);
file
let folder_name = format!("{}_{}", filename, ext);
file.push(folder_name);
file
}

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

Expand Down Expand Up @@ -206,7 +204,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 @@ -221,8 +219,15 @@ 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() };
Result::Err(eprintln!("{}", Colour::Red.paint("Input file does not exist".to_owned() + &route)))
let route = if route.to_str().is_some() {
": ".to_owned() + route.to_str().unwrap()
} else {
"".to_owned()
};
Result::Err(eprintln!(
"{}",
Colour::Red.paint("Input file does not exist".to_owned() + &route)
))
}
}

Expand All @@ -236,25 +241,33 @@ 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 = usize::from_str_radix(o_2_argument, 10);
if let Result::Ok(no_rounds) = rounds_r {
if no_rounds == 0 { Ok(SimplificationStyle::O1) }
else {Ok(SimplificationStyle::O2(no_rounds))}}
else { Result::Err(eprintln!("{}", Colour::Red.paint("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(eprintln!("{}", Colour::Red.paint("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 @@ -311,25 +324,22 @@ 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"
{
Ok(String::from(matches.value_of("prime").unwrap()))
}
else{
Result::Err(eprintln!("{}", Colour::Red.paint("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"
{
Ok(String::from(matches.value_of("prime").unwrap()))
} else {
Result::Err(eprintln!("{}", Colour::Red.paint("invalid prime number")))
}
}

false => Ok(String::from("bn128")),
}
}
Expand Down Expand Up @@ -449,8 +459,8 @@ 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
Loading