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

Apply clippy fixes #238

Open
wants to merge 3 commits 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
26 changes: 9 additions & 17 deletions circom/src/compilation_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,9 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
config.dat_file
);
println!(
"{} {}/{}, {}, {}, {}, {}, {}, {} and {}",
"{} {}/main.cpp, circom.hpp, calcwit.hpp, calcwit.cpp, fr.hpp, fr.cpp, fr.asm and Makefile",
Colour::Green.paint("Written successfully:"),
&config.c_folder,
"main.cpp".to_string(),
"circom.hpp".to_string(),
"calcwit.hpp".to_string(),
"calcwit.cpp".to_string(),
"fr.hpp".to_string(),
"fr.cpp".to_string(),
"fr.asm".to_string(),
"Makefile".to_string()
&config.c_folder
);
}

Expand All @@ -64,7 +56,7 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
let result = wat_to_wasm(&config.wat_file, &config.wasm_file);
match result {
Result::Err(report) => {
Report::print_reports(&[report], &FileLibrary::new());
Report::print_reports(&[*report], &FileLibrary::new());
return Err(());
}
Result::Ok(()) => {
Expand All @@ -78,7 +70,7 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
std::fs::remove_file(&config.wat_file).unwrap();
match result {
Result::Err(report) => {
Report::print_reports(&[report], &FileLibrary::new());
Report::print_reports(&[*report], &FileLibrary::new());
return Err(());
}
Result::Ok(()) => {
Expand All @@ -99,7 +91,7 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
}


fn wat_to_wasm(wat_file: &str, wasm_file: &str) -> Result<(), Report> {
fn wat_to_wasm(wat_file: &str, wasm_file: &str) -> Result<(), Box<Report>> {
use std::fs::read_to_string;
use std::fs::File;
use std::io::BufWriter;
Expand All @@ -112,19 +104,19 @@ fn wat_to_wasm(wat_file: &str, wasm_file: &str) -> Result<(), Report> {
let result_wasm_contents = parser::parse::<Wat>(&buf);
match result_wasm_contents {
Result::Err(error) => {
Result::Err(Report::error(
Result::Err(Box::new(Report::error(
format!("Error translating the circuit from wat to wasm.\n\nException encountered when parsing WAT: {}", error),
ReportCode::ErrorWat2Wasm,
))
)))
}
Result::Ok(mut wat) => {
let wasm_contents = wat.module.encode();
match wasm_contents {
Result::Err(error) => {
Result::Err(Report::error(
Result::Err(Box::new(Report::error(
format!("Error translating the circuit from wat to wasm.\n\nException encountered when encoding WASM: {}", error),
ReportCode::ErrorWat2Wasm,
))
)))
}
Result::Ok(wasm_contents) => {
let file = File::create(wasm_file).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion circom/src/execution_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn generate_json_constraints(
debug: &DebugWriter,
exporter: &dyn ConstraintExporter,
) -> Result<(), ()> {
if let Ok(()) = exporter.json_constraints(&debug) {
if let Ok(()) = exporter.json_constraints(debug) {
println!("{} {}", Colour::Green.paint("Constraints written in:"), debug.json_constraints);
Result::Ok(())
} else {
Expand Down
24 changes: 13 additions & 11 deletions circom/src/input_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ pub struct Input {
}


const R1CS: &'static str = "r1cs";
const WAT: &'static str = "wat";
const WASM: &'static str = "wasm";
const CPP: &'static str = "cpp";
const JS: &'static str = "js";
const DAT: &'static str = "dat";
const SYM: &'static str = "sym";
const JSON: &'static str = "json";
const R1CS: &str = "r1cs";
const WAT: &str = "wat";
const WASM: &str = "wasm";
const CPP: &str = "cpp";
const JS: &str = "js";
const DAT: &str = "dat";
const SYM: &str = "sym";
const JSON: &str = "json";


impl Input {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Input {
),
wat_flag:input_processing::get_wat(&matches),
wasm_flag: input_processing::get_wasm(&matches),
c_flag: c_flag,
c_flag,
r1cs_flag: input_processing::get_r1cs(&matches),
sym_flag: input_processing::get_sym(&matches),
main_inputs_flag: input_processing::get_main_inputs_log(&matches),
Expand All @@ -109,13 +109,15 @@ impl Input {
})
}

#[allow(clippy::ptr_arg)]
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
}

#[allow(clippy::ptr_arg)]
fn build_output(output_path: &PathBuf, filename: &str, ext: &str) -> PathBuf {
let mut file = output_path.clone();
file.push(format!("{}.{}",filename,ext));
Expand All @@ -127,7 +129,7 @@ impl Input {
}

pub fn input_file(&self) -> &str {
&self.input_program.to_str().unwrap()
self.input_program.to_str().unwrap()
}
pub fn r1cs_file(&self) -> &str {
self.out_r1cs.to_str().unwrap()
Expand Down Expand Up @@ -257,7 +259,7 @@ mod input_processing {
(_, true, _, _) => Ok(SimplificationStyle::O1),
(_, _, true, _) => {
let o_2_argument = matches.value_of("simplification_rounds").unwrap();
let rounds_r = usize::from_str_radix(o_2_argument, 10);
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))}}
Expand Down
2 changes: 1 addition & 1 deletion circom/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod input_user;
mod parser_user;
mod type_analysis_user;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION: &str = env!("CARGO_PKG_VERSION");


use ansi_term::Colour;
Expand Down
25 changes: 12 additions & 13 deletions circom_algebra/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::{HashMap, HashSet, BTreeSet};
use std::fmt::{Display, Formatter};
use std::hash::Hash;

#[derive(Default)]
pub enum ArithmeticExpression<C>
where
C: Hash + Eq,
Expand All @@ -30,6 +31,7 @@ where
b: HashMap<C, BigInt>,
c: HashMap<C, BigInt>,
},
#[default]
NonQuadratic,
}
impl<C: Default + Clone + Display + Hash + Eq> Display for ArithmeticExpression<C> {
Expand Down Expand Up @@ -80,11 +82,7 @@ impl<C: Default + Clone + Display + Hash + Eq> PartialEq for ArithmeticExpressio
}
}

impl<C: Default + Clone + Display + Hash + Eq> Default for ArithmeticExpression<C> {
fn default() -> Self {
ArithmeticExpression::NonQuadratic
}
}


impl<C: Default + Clone + Display + Hash + Eq> ArithmeticExpression<C> {
pub fn new() -> ArithmeticExpression<C> {
Expand Down Expand Up @@ -237,7 +235,7 @@ impl<C: Default + Clone + Display + Hash + Eq> ArithmeticExpression<C> {
let inverse_constant = modular_arithmetic::div(
&BigInt::from(1),
constant,
&field
field
)?;
ArithmeticExpression::multiply_coefficients_by_constant(&inverse_constant, coefficients, field);
debug_assert!(ArithmeticExpression::valid_hashmap_for_expression(coefficients));
Expand Down Expand Up @@ -898,7 +896,7 @@ impl<C: Default + Clone + Display + Hash + Eq> Substitution<C> {
let symbol = substitution.from;
let mut coefficients = substitution.to;
ArithmeticExpression::initialize_hashmap_for_expression(&mut coefficients);
coefficients.insert(symbol, BigInt::from(-1 % field));
coefficients.insert(symbol, -1 % field);
let arith = ArithmeticExpression::Linear { coefficients };
ArithmeticExpression::transform_expression_to_constraint_form(arith, field).unwrap()
}
Expand Down Expand Up @@ -1060,7 +1058,7 @@ impl<C: Default + Clone + Display + Hash + Eq> Constraint<C> {
) -> Substitution<C> {
debug_assert!(Constraint::is_linear(&constraint));
debug_assert!(constraint.c.contains_key(signal));
let raw_expression = Constraint::clear_signal(constraint.c, &signal, field);
let raw_expression = Constraint::clear_signal(constraint.c, signal, field);
Substitution { from: signal.clone(), to: raw_expression }
}

Expand All @@ -1071,7 +1069,7 @@ impl<C: Default + Clone + Display + Hash + Eq> Constraint<C> {
) -> (BigInt, Substitution<C>) {
debug_assert!(Constraint::is_linear(&constraint));
debug_assert!(constraint.c.contains_key(signal));
let (coefficient, raw_expression) = Constraint::clear_signal_not_normalized(constraint.c, &signal, field);
let (coefficient, raw_expression) = Constraint::clear_signal_not_normalized(constraint.c, signal, field);
(coefficient, Substitution {from: signal.clone(), to: raw_expression})
}

Expand Down Expand Up @@ -1110,7 +1108,7 @@ impl<C: Default + Clone + Display + Hash + Eq> Constraint<C> {
key: &C,
field: &BigInt,
) -> HashMap<C, BigInt> {
let key_value = symbols.remove(&key).unwrap();
let key_value = symbols.remove(key).unwrap();
assert!(!key_value.is_zero());
let value_to_the_right = modular_arithmetic::mul(&key_value, &BigInt::from(-1), field);
ArithmeticExpression::initialize_hashmap_for_expression(&mut symbols);
Expand All @@ -1128,7 +1126,7 @@ impl<C: Default + Clone + Display + Hash + Eq> Constraint<C> {
key: &C,
field: &BigInt,
) -> (BigInt, HashMap<C, BigInt>) {
let key_value = symbols.remove(&key).unwrap();
let key_value = symbols.remove(key).unwrap();
assert!(!key_value.is_zero());
let value_to_the_right = modular_arithmetic::mul(&key_value, &BigInt::from(-1), field);
ArithmeticExpression::initialize_hashmap_for_expression(&mut symbols);
Expand Down Expand Up @@ -1220,6 +1218,7 @@ impl Constraint<usize> {
let c = apply_raw_offset(&self.c, offset);
Constraint::new(a, b, c)
}
#[allow(clippy::ptr_arg)]
pub fn apply_witness(&self, witness: &Vec<usize>) -> Constraint<usize> {
let a = apply_vectored_correspondence(&self.a, witness);
let b = apply_vectored_correspondence(&self.b, witness);
Expand All @@ -1233,7 +1232,7 @@ type RawExpr<C> = HashMap<C, BigInt>;

fn apply_vectored_correspondence(
symbols: &HashMap<usize, BigInt>,
map: &Vec<usize>,
map: &[usize],
) -> HashMap<usize, BigInt> {
let mut mapped = HashMap::new();
for (s, v) in symbols {
Expand All @@ -1256,7 +1255,7 @@ where
let id = if key.eq(&constant_coefficient) {
ArithmeticExpression::constant_coefficient()
} else {
map.get(&key).expect(&format!("Unknown signal: {}", key)).clone()
map.get(key).unwrap_or_else(|| panic!("Unknown signal: {}", key)).clone()
};
coefficients_as_correspondence.insert(id, value.clone());
}
Expand Down
6 changes: 6 additions & 0 deletions circom_algebra/src/constraint_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub struct ConstraintStorage {
constraints: Vec<CompressedConstraint>,
}

impl Default for ConstraintStorage {
fn default() -> Self {
Self::new()
}
}

impl ConstraintStorage {
pub fn new() -> ConstraintStorage {
ConstraintStorage { field_tracker: FieldTracker::new(), constraints: Vec::new() }
Expand Down
24 changes: 12 additions & 12 deletions circom_algebra/src/modular_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn sub(left: &BigInt, right: &BigInt, field: &BigInt) -> BigInt {
pub fn div(left: &BigInt, right: &BigInt, field: &BigInt) -> Result<BigInt, ArithmeticError> {
let right_inverse = right
.mod_inverse(field)
.map_or(Result::Err(ArithmeticError::DivisionByZero), |a| Result::Ok(a))?;
.map_or(Result::Err(ArithmeticError::DivisionByZero), Result::Ok)?;
let res = mul(left, &right_inverse, field);
Result::Ok(res)
}
Expand Down Expand Up @@ -73,21 +73,21 @@ pub fn multi_inv(values: &Vec<BigInt>, field: &BigInt) -> Vec<BigInt>{
let mut partials : Vec<BigInt> = Vec::new();
partials.push(one.clone());
for i in 0..values.len(){
partials.push(mul(partials.get(partials.len()-1).unwrap(),
partials.push(mul(partials.last().unwrap(),
values.get(i).unwrap(),
&field));
field));
}
let mut inverse = div(&one,
partials.get(partials.len()-1).unwrap(),
&field).ok().unwrap();
partials.last().unwrap(),
field).ok().unwrap();
let mut outputs : Vec<BigInt> = vec![BigInt::from(0); partials.len()];
let mut i = values.len();
while i > 0{
outputs[i-1] = mul(partials.get(i-1).unwrap(), &inverse, &field);
inverse = mul(&inverse, values.get(i-1).unwrap(), &field);
i = i - 1;
outputs[i-1] = mul(partials.get(i-1).unwrap(), &inverse, field);
inverse = mul(&inverse, values.get(i-1).unwrap(), field);
i -= 1;
}
return outputs;
outputs
}

//Bit operations
Expand All @@ -114,7 +114,7 @@ pub fn shift_l(left: &BigInt, right: &BigInt, field: &BigInt) -> Result<BigInt,
if right <= &top {
let usize_repr = right
.to_usize()
.map_or(Result::Err(ArithmeticError::DivisionByZero), |a| Result::Ok(a))?;
.map_or(Result::Err(ArithmeticError::DivisionByZero), Result::Ok)?;
let value = modulus(&((left * &num_traits::pow(two, usize_repr)) & &mask(field)), field);
Result::Ok(value)
} else {
Expand All @@ -127,7 +127,7 @@ pub fn shift_r(left: &BigInt, right: &BigInt, field: &BigInt) -> Result<BigInt,
if right <= &top {
let usize_repr = right
.to_usize()
.map_or(Result::Err(ArithmeticError::DivisionByZero), |a| Result::Ok(a))?;
.map_or(Result::Err(ArithmeticError::DivisionByZero), Result::Ok)?;
let value = left / &num_traits::pow(two, usize_repr);
Result::Ok(value)
} else {
Expand Down Expand Up @@ -243,7 +243,7 @@ mod tests {
if let Result::Ok(res) = mod_op(&a, &b, &field) {
assert_eq!(a, res)
} else {
assert!(false);
unreachable!();
}
}
#[test]
Expand Down
Loading