From 4d308d2698b38a25cf43ad92500eca13b604a090 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Wed, 8 Nov 2023 16:24:13 +0000 Subject: [PATCH] fix: clippy --- bberg/src/bberg_codegen.rs | 4 +-- bberg/src/circuit_builder.rs | 53 +++++++++++++++++------------------ bberg/src/flavor_builder.rs | 40 +++++++++++++------------- bberg/src/main.rs | 33 +++++++++------------- bberg/src/prover_builder.rs | 6 ++-- bberg/src/trace_builder.rs | 42 +++++++++++++-------------- bberg/src/verifier_builder.rs | 4 +-- 7 files changed, 87 insertions(+), 95 deletions(-) diff --git a/bberg/src/bberg_codegen.rs b/bberg/src/bberg_codegen.rs index d8e506d08..943247a53 100644 --- a/bberg/src/bberg_codegen.rs +++ b/bberg/src/bberg_codegen.rs @@ -1,5 +1,5 @@ use ast::analyzed::Analyzed; -use std::{io, str::FromStr}; +use std::io; use number::{BigInt, Bn254Field, DegreeType, FieldElement}; @@ -21,7 +21,7 @@ impl BBergCodegen { Self {} } - pub fn new_from_setup(input: &mut impl io::Read) -> Result { + pub fn new_from_setup(_input: &mut impl io::Read) -> Result { println!("warning bberg: new_from_setup not implemented"); Ok(Self {}) } diff --git a/bberg/src/circuit_builder.rs b/bberg/src/circuit_builder.rs index 0266bca6d..dfc1492bf 100644 --- a/bberg/src/circuit_builder.rs +++ b/bberg/src/circuit_builder.rs @@ -1,19 +1,18 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use std::fs::File; -use std::{fmt::Display, io::Write, process::id}; +use std::io::Write; -use ast::parsed::{SelectedExpressions, UnaryOperator}; +use ast::parsed::SelectedExpressions; // use acvm::acir::native_types::Expression; -use ast::{analyzed::Identity, asm_analysis::DegreeStatement, parsed::BinaryOperator}; +use ast::analyzed::Identity; use itertools::Itertools; -use num_bigint::BigUint; use ast::analyzed::{ AlgebraicBinaryOperator, AlgebraicExpression as Expression, AlgebraicUnaryOperator, Analyzed, - IdentityKind, Reference, + IdentityKind, }; -use num_traits::{identities, One}; -use number::{BigInt, DegreeType, FieldElement}; + +use number::{DegreeType, FieldElement}; // use super::circuit_data::CircuitData; @@ -206,7 +205,7 @@ pub(crate) fn analyzed_to_cpp( let shifted_polys: Vec = collected_shifts.drain().collect_vec(); dbg!(shifted_polys.clone()); - let (all_cols, unshifted, to_be_shifted, shifted, all_cols_with_shifts) = + let (all_cols, unshifted, to_be_shifted, _shifted, all_cols_with_shifts) = get_all_col_names(fixed, witness, &shifted_polys); let num_cols = all_cols_with_shifts.len(); @@ -285,10 +284,10 @@ namespace arithmetization {{ fn create_relation_hpp( name: &str, - sub_relations: &Vec, - identities: &Vec, + sub_relations: &[String], + identities: &[BBIdentity], row_type: &String, - all_rows_and_shifts: &Vec, + all_rows_and_shifts: &[String], ) -> String { let includes = relation_includes(); let class_boilerplate = relation_class_boilerplate(name, sub_relations, identities); @@ -314,8 +313,8 @@ namespace proof_system::{name}_vm {{ fn relation_class_boilerplate( name: &str, - sub_relations: &Vec, - identities: &Vec, + sub_relations: &[String], + identities: &[BBIdentity], ) -> String { // TODO: MOVE ELSEWHERE: We add one to all degrees because we have an extra scaling factor let degrees = identities.iter().map(|(d, _)| d + 1).collect(); @@ -340,7 +339,7 @@ fn get_export(name: &str) -> String { ) } -fn get_relation_code(ids: &Vec) -> String { +fn get_relation_code(ids: &[String]) -> String { let mut relation_code = r#" template void static accumulate( @@ -392,17 +391,17 @@ fn relation_includes() -> &'static str { } // Yucky that everything is allocated into vecs here -fn create_row_type_items(names: &Vec) -> Vec { +fn create_row_type_items(names: &[String]) -> Vec { names .iter() - .map(|name| format!(" FF {} {{}};", name.replace(".", "_"))) + .map(|name| format!(" FF {} {{}};", name.replace('.', "_"))) .collect::>() } fn get_all_col_names( fixed: &[(&str, Vec)], witness: &[(&str, Vec)], - to_be_shifted: &Vec, + to_be_shifted: &[String], ) -> ( Vec, Vec, @@ -413,14 +412,14 @@ fn get_all_col_names( let fixed_names: Vec = fixed .iter() .map(|(name, _)| { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); n.to_owned() }) .collect(); let witness_names: Vec = witness .iter() .map(|(name, _)| { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); n.to_owned() }) .collect(); @@ -451,14 +450,14 @@ fn get_all_col_names( ( all_cols, unshifted, - to_be_shifted.clone(), + to_be_shifted.to_vec(), shifted, with_shifts, ) } // Each vm will need to have a row which is a combination of all of the witness columns -fn create_row_type(all_rows: &Vec) -> String { +fn create_row_type(all_rows: &[String]) -> String { let all_annotated = create_row_type_items(all_rows); let row_type = format!( @@ -470,11 +469,11 @@ fn create_row_type(all_rows: &Vec) -> String { row_type } -fn get_cols_in_identity_macro(all_rows_and_shifts: &Vec) -> String { +fn get_cols_in_identity_macro(all_rows_and_shifts: &[String]) -> String { let make_view_per_row = all_rows_and_shifts .iter() .map(|row_name| { - let name = row_name.replace(".", "_"); + let name = row_name.replace('.', "_"); format!("[[maybe_unused]] auto {name} = View(new_term.{name}); \\") }) .collect::>() @@ -514,7 +513,7 @@ fn create_subrelation(index: usize, preamble: String, identity: &mut BBIdentity) let id = &identity.1; // TODO: TEMP HACK: Part of the main_FIRST hack below - to switch off constraints on the first row - identity.0 += identity.0 + 1; + identity.0 += 1; format!( "//Contribution {index} {{\n{preamble} @@ -535,7 +534,7 @@ fn craft_expression( Expression::Number(n) => (1, format!("FF({})", n.to_arbitrary_integer())), Expression::Reference(polyref) => { assert_eq!(polyref.index, None); - let mut poly_name = format!("{}", &polyref.name.replace(".", "_")); + let mut poly_name = polyref.name.replace('.', "_").to_string(); let mut degree = 1; if polyref.next { // NOTE: Naive algorithm to collect all shifted polys @@ -545,7 +544,7 @@ fn craft_expression( // TODO(HORRIBLE): TEMP, add in a relation that turns off shifts on the last row poly_name = format!("{poly_name} * (-main_LAST + FF(1))"); - degree = degree + 1; + degree += 1; } (degree, poly_name) } diff --git a/bberg/src/flavor_builder.rs b/bberg/src/flavor_builder.rs index dae21eb87..f9e357524 100644 --- a/bberg/src/flavor_builder.rs +++ b/bberg/src/flavor_builder.rs @@ -2,25 +2,25 @@ pub(crate) fn create_flavor_hpp( name: &str, - relations: &Vec, - all_cols: &Vec, - shifted: &Vec, - // shifted: &Vec, + relations: &[String], + all_cols: &[String], + shifted: &[String], + // shifted: &[String], ) -> String { - let includes = flavor_includes(name, &relations); + let includes = flavor_includes(name, relations); let num_witness = all_cols.len(); let num_all = num_witness + shifted.len(); // Note: includes all witness shifts // TODO: for now we include a shift OF ALL witness wires, however this is not necessarily true let precomputed = witness_get(all_cols, 0, false); - let witness_str = create_witness_entities(&all_cols); - let all_shift = witness_get(&shifted, num_witness, true); + let witness_str = create_witness_entities(all_cols); + let all_shift = witness_get(shifted, num_witness, true); dbg!(&all_shift); let all_entities_get_wires = make_wires_set( - &[all_cols.clone(), shifted.clone()] + &[all_cols.to_vec(), shifted.to_vec()] .into_iter() .flatten() .collect::>(), @@ -34,7 +34,7 @@ pub(crate) fn create_flavor_hpp( .collect::>(), ); - let commitment_labels_class = create_commitment_labels(&all_cols); + let commitment_labels_class = create_commitment_labels(all_cols); let verification_commitments = create_verifier_commitments(); @@ -245,7 +245,7 @@ class {name}Flavor : public {name}FlavorBase) -> String { +fn flavor_includes(name: &str, _relations: &[String]) -> String { // TODO: when there are multiple relations generated, they will need to be known in this file // TODO: Get the path for generated / other relations from self @@ -267,10 +267,10 @@ fn flavor_includes(name: &str, _relations: &Vec) -> String { ) } -fn create_precomputed_entities(fixed: &Vec) -> String { +fn create_precomputed_entities(fixed: &[String]) -> String { let mut name_set = String::new(); for name in fixed { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); name_set.push_str(&format!("{n}, ", n = n)); } @@ -286,10 +286,10 @@ fn create_precomputed_entities(fixed: &Vec) -> String { get_selectors } -fn witness_get(witness: &Vec, offset: usize, shift: bool) -> String { +fn witness_get(witness: &[String], offset: usize, shift: bool) -> String { let mut return_string = String::new(); for (i, name) in witness.iter().enumerate() { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); let n = if shift { format!("{}_shift", n) } else { n }; let index = i + offset; return_string.push_str(&format!( @@ -309,11 +309,11 @@ fn witness_get(witness: &Vec, offset: usize, shift: bool) -> String { // fn wi // Takes in a set of wire names and outputs wrapped get_wires function -fn make_wires_set(set: &Vec) -> String { +fn make_wires_set(set: &[String]) -> String { let mut wires = String::new(); for name in set.iter() { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); wires.push_str(&format!( "{n}, ", @@ -323,7 +323,7 @@ fn make_wires_set(set: &Vec) -> String { wires } -fn create_witness_entities(witness: &Vec) -> String { +fn create_witness_entities(witness: &[String]) -> String { let data_types = witness_get(witness, 0, false); let get_wires = make_wires_set(witness); @@ -342,10 +342,10 @@ fn create_witness_entities(witness: &Vec) -> String { ) } -fn create_labels(all_ents: &Vec) -> String { +fn create_labels(all_ents: &[String]) -> String { let mut labels = String::new(); for name in all_ents { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); labels.push_str(&format!( "Base::{n} = \"{n}\"; ", @@ -355,7 +355,7 @@ fn create_labels(all_ents: &Vec) -> String { labels } -fn create_commitment_labels(all_ents: &Vec) -> String { +fn create_commitment_labels(all_ents: &[String]) -> String { let labels = create_labels(all_ents); format!( diff --git a/bberg/src/main.rs b/bberg/src/main.rs index 55c49655c..17cf630fe 100644 --- a/bberg/src/main.rs +++ b/bberg/src/main.rs @@ -4,7 +4,7 @@ use acvm::acir::circuit::Opcode; use acvm::brillig_vm::brillig::BinaryFieldOp; use acvm::brillig_vm::brillig::Label; use acvm::brillig_vm::brillig::RegisterIndex; -use rand::distributions::Alphanumeric; + use rand::Rng; use std::fs; use std::io::Write; @@ -51,12 +51,12 @@ fn main() { // Read in file called bytecode.acir let bytecode = fs::read("bytecode.acir").expect("Unable to read file"); // Convert the read-in base64 file into Vec - let decoded = base64::decode(&bytecode).expect("Failed to decode base64"); - let bytecode = Vec::from(decoded); + let decoded = base64::decode(bytecode).expect("Failed to decode base64"); + let bytecode = decoded; // Create a new circuit from the bytecode instance let circuit: Circuit = - Circuit::deserialize_circuit(&*bytecode).expect("Failed to deserialize circuit"); + Circuit::deserialize_circuit(&bytecode).expect("Failed to deserialize circuit"); println!("circuit: {:?}", circuit); @@ -114,22 +114,21 @@ fn construct_main(program: Opcode) -> Vec { } }; - println!(""); - println!(""); + println!(); + println!(); trace.iter().for_each(|i| println!("{:?}", i)); - println!(""); - println!(""); + println!(); + println!(); // Label of [index], String, where index is the generated name of the jump, we will place a jump label there when // we encounter it to prove - let mut index = 0; let mut labels: HashMap = HashMap::new(); - for instr in trace { + for (index, instr) in trace.into_iter().enumerate() { println!("{:?}", instr); - println!(""); - println!(""); - println!(""); + println!(); + println!(); + println!(); // powdr_asm.push_str(&instr.to_string()); // If we require a label to be placed at the jump location then we add it @@ -205,9 +204,6 @@ fn construct_main(program: Opcode) -> Vec { } _ => println!("not implemented"), } - - // Increment the index in the instruction array - index += 1; } println!("main_asm: {:?}", main_asm); @@ -230,13 +226,12 @@ fn gen_label() -> String { fn print_register(r_index: RegisterIndex) -> String { let num = r_index.to_usize(); - format!("r{}", num.to_string()).to_owned() + format!("r{}", num).to_owned() } // Read the preamble from the brillig.asm machine fn get_preamble() -> String { - let preamble = fs::read_to_string("brillig.asm").expect("Unable to read file"); - preamble + fs::read_to_string("brillig.asm").expect("Unable to read file") } fn extract_brillig(opcodes: Vec) -> Opcode { diff --git a/bberg/src/prover_builder.rs b/bberg/src/prover_builder.rs index 4384493ca..062a7e268 100644 --- a/bberg/src/prover_builder.rs +++ b/bberg/src/prover_builder.rs @@ -120,14 +120,14 @@ fn includes_cpp(name: &str) -> String { ) } -pub fn prover_builder_cpp(name: &str, fixed: &Vec, to_be_shifted: &Vec) -> String { +pub fn prover_builder_cpp(name: &str, fixed: &[String], to_be_shifted: &[String]) -> String { let include_str = includes_cpp(name); // Create the wire assignments, prover_polynomial = key let fixed_assignments = fixed .iter() .map(|name| { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); format!("prover_polynomials.{n} = key->{n};", n = n) }) .collect::>() @@ -136,7 +136,7 @@ pub fn prover_builder_cpp(name: &str, fixed: &Vec, to_be_shifted: &Vec{n}; diff --git a/bberg/src/trace_builder.rs b/bberg/src/trace_builder.rs index 609a7ba59..8a60e91cd 100644 --- a/bberg/src/trace_builder.rs +++ b/bberg/src/trace_builder.rs @@ -1,21 +1,19 @@ -use number::FieldElement; - use crate::circuit_builder::BBFiles; pub trait TraceBuilder { fn create_trace_builder_cpp( &mut self, name: &str, - fixed: &Vec, - witness: &Vec, - to_be_shifted: &Vec, + fixed: &[String], + witness: &[String], + to_be_shifted: &[String], ) -> String; fn create_trace_builder_hpp( &mut self, name: &str, - fixed: &Vec, - shifted: &Vec, + fixed: &[String], + shifted: &[String], ) -> String; } @@ -60,7 +58,7 @@ fn trace_hpp_includes(name: &str) -> String { ) } -fn build_shifts(fixed: &Vec) -> String { +fn build_shifts(fixed: &[String]) -> String { let shift_assign: Vec = fixed .iter() .map(|name| format!("row.{name}_shift = rows[(i) % rows.size()].{name};")) @@ -78,7 +76,7 @@ fn build_shifts(fixed: &Vec) -> String { ) } -fn build_empty_row(all_cols: &Vec) -> String { +fn build_empty_row(all_cols: &[String]) -> String { // The empty row turns off all constraints when the ISLAST flag is set // We must check that this column exists, and return an error to the user if it is not found let is_last = all_cols.iter().find(|name| name.contains("ISLAST")); @@ -113,28 +111,28 @@ impl TraceBuilder for BBFiles { fn create_trace_builder_cpp( &mut self, name: &str, - fixed: &Vec, - witness: &Vec, - to_be_shifted: &Vec, + fixed: &[String], + witness: &[String], + to_be_shifted: &[String], ) -> String { // We are assuming that the order of the columns in the trace file is the same as the order in the witness file - let includes = trace_cpp_includes(&self.rel, &name); + let includes = trace_cpp_includes(&self.rel, name); let row_import = format!("using Row = {name}_vm::Row;"); // NOTE: Both of these are also calculated elsewhere, this is extra work // TODO: Recalculated! - let num_cols = fixed.len() + witness.len() * 2; // (2* as shifts) + let _num_cols = fixed.len() + witness.len() * 2; // (2* as shifts) let fixed_name = fixed .iter() .map(|name| { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); n.to_string() }) .collect::>(); let witness_name = witness .iter() .map(|name| { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); n.to_string() }) .collect::>(); @@ -147,13 +145,13 @@ impl TraceBuilder for BBFiles { // let empty_row = build_empty_row(&all_names); - let compute_polys_assignemnt = all_names + let _compute_polys_assignemnt = all_names .iter() .map(|name| format!("polys.{name}[i] = rows[i].{name};",)) .collect::>() .join("\n"); - let all_poly_shifts = &witness_name + let _all_poly_shifts = &witness_name .iter() .map(|name| format!("polys.{name}_shift = rows[i].{name}_shift;")) .collect::>() @@ -162,7 +160,7 @@ impl TraceBuilder for BBFiles { let fixed_rows = fixed .iter() .map(|name| { - let n = name.replace(".", "_"); + let n = name.replace('.', "_"); format!("current_row.{n} = read_field(constant_file);") }) .collect::>() @@ -256,10 +254,10 @@ inline std::vector read_both_file_into_cols( fn create_trace_builder_hpp( &mut self, name: &str, - all_cols: &Vec, - to_be_shifted: &Vec, + all_cols: &[String], + to_be_shifted: &[String], ) -> String { - let includes = trace_hpp_includes(&name); + let includes = trace_hpp_includes(name); let num_polys = all_cols.len(); let num_cols = all_cols.len() + to_be_shifted.len(); diff --git a/bberg/src/verifier_builder.rs b/bberg/src/verifier_builder.rs index 790a94fba..e1aa2dfe1 100644 --- a/bberg/src/verifier_builder.rs +++ b/bberg/src/verifier_builder.rs @@ -76,11 +76,11 @@ fn includes_cpp(name: &str) -> String { ) } -pub fn verifier_builder_cpp(name: &str, all_wires: &Vec) -> String { +pub fn verifier_builder_cpp(name: &str, all_wires: &[String]) -> String { let include_str = includes_cpp(name); let wire_commitments = all_wires.iter().map(|name|{ - let n = name.replace(".","_"); + let n = name.replace('.',"_"); format!("commitments.{n} = transcript.template receive_from_prover(commitment_labels.{n});", n=n) }).collect::>().join("\n");