diff --git a/Cargo.toml b/Cargo.toml index dc1a8b461..1803c74a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ resolver = "2" [workspace.package] version = "0.5.0" -authors = [ "arkworks contributors" ] +authors = ["arkworks contributors"] homepage = "https://arkworks.rs" repository = "https://github.com/arkworks-rs/algebra" categories = ["cryptography"] @@ -41,6 +41,100 @@ rustdoc-args = ["--html-in-header katex-header.html"] [workspace.metadata.release] dependent-version = "fix" +[workspace.lints] +rust.rust_2018_idioms = { level = "deny", priority = -1 } +rust.unreachable_pub = "warn" +rust.unused_must_use = "deny" +rustdoc.all = "warn" +# rust.unnameable-types = "warn" + +[workspace.lints.clippy] +# These are some of clippy's nursery (i.e., experimental) lints that we like. +# By default, nursery lints are allowed. Some of the lints below have made good +# suggestions which we fixed. The others didn't have any findings, so we can +# assume they don't have that many false positives. Let's enable them to +# prevent future problems. +borrow_as_ptr = "warn" +branches_sharing_code = "warn" +clear_with_drain = "warn" +cloned_instead_of_copied = "warn" +collection_is_never_read = "warn" +dbg_macro = "warn" +derive_partial_eq_without_eq = "warn" +empty_line_after_doc_comments = "warn" +empty_line_after_outer_attr = "warn" +enum_glob_use = "warn" +equatable_if_let = "warn" +explicit_into_iter_loop = "warn" +explicit_iter_loop = "warn" +flat_map_option = "warn" +from_iter_instead_of_collect = "warn" +if_not_else = "warn" +if_then_some_else_none = "warn" +implicit_clone = "warn" +imprecise_flops = "warn" +iter_on_empty_collections = "warn" +iter_on_single_items = "warn" +iter_with_drain = "warn" +iter_without_into_iter = "warn" +large_stack_frames = "warn" +manual_assert = "warn" +manual_clamp = "warn" +manual_is_variant_and = "warn" +manual_string_new = "warn" +match_same_arms = "warn" +missing_const_for_fn = "warn" +mutex_integer = "warn" +naive_bytecount = "warn" +needless_bitwise_bool = "warn" +needless_continue = "warn" +needless_for_each = "warn" +needless_pass_by_ref_mut = "warn" +nonstandard_macro_braces = "warn" +option_as_ref_cloned = "warn" +or_fun_call = "warn" +path_buf_push_overwrite = "warn" +read_zero_byte_vec = "warn" +redundant_clone = "warn" +redundant_else = "warn" +single_char_pattern = "warn" +string_lit_as_bytes = "warn" +string_lit_chars_any = "warn" +suboptimal_flops = "warn" +suspicious_operation_groupings = "warn" +trailing_empty_array = "warn" +trait_duplication_in_bounds = "warn" +transmute_undefined_repr = "warn" +trivial_regex = "warn" +tuple_array_conversions = "warn" +type_repetition_in_bounds = "warn" +uninhabited_references = "warn" +unnecessary_self_imports = "warn" +unnecessary_struct_initialization = "warn" +unnested_or_patterns = "warn" +unused_peekable = "warn" +unused_rounding = "warn" +use_self = "warn" +useless_let_if_seq = "warn" +while_float = "warn" +zero_sized_map_values = "warn" + +# These are nursery lints which have findings. Allow them for now. Some are not +# quite mature enough for use in our codebase and some we don't really want. +# Explicitly listing should make it easier to fix in the future. +as_ptr_cast_mut = "allow" +cognitive_complexity = "allow" +debug_assert_with_mut_call = "allow" +doc_markdown = "allow" +fallible_impl_from = "allow" +future_not_send = "allow" +needless_collect = "allow" +non_send_fields_in_send_ty = "allow" +redundant_pub_crate = "allow" +significant_drop_in_scrutinee = "allow" +significant_drop_tightening = "allow" +too_long_first_doc_paragraph = "allow" + [workspace.dependencies] ark-ec = { version = "0.5.0", path = "./ec", default-features = false } ark-ff = { version = "0.5.0", path = "./ff", default-features = false } @@ -51,7 +145,7 @@ ark-serialize = { version = "0.5.0", path = "./serialize", default-features = fa ark-serialize-derive = { version = "0.5.0", path = "./serialize-derive" } ark-std = { version = "0.5.0", default-features = false } -ark-algebra-bench-templates = { version = "0.5.0", path = "./bench-templates", default-features = false } +ark-algebra-bench-templates = { version = "0.5.0", path = "./bench-templates", default-features = false } ark-algebra-test-templates = { version = "0.5.0", path = "./test-templates", default-features = false } ark-test-curves = { version = "0.5.0", path = "./test-curves", default-features = false } diff --git a/ec/src/hashing/tests.rs b/ec/src/hashing/tests.rs index 8805d23c2..015e55cbd 100644 --- a/ec/src/hashing/tests.rs +++ b/ec/src/hashing/tests.rs @@ -1,3 +1,4 @@ +#![allow(clippy::bool_assert_comparison)] use crate::hashing::curve_maps::parity; use ark_test_curves::bls12_381::{Fq, Fq2, Fq6}; diff --git a/ff/src/biginteger/arithmetic.rs b/ff/src/biginteger/arithmetic.rs index 227c75593..e886c9569 100644 --- a/ff/src/biginteger/arithmetic.rs +++ b/ff/src/biginteger/arithmetic.rs @@ -291,20 +291,20 @@ mod tests { #[test] fn test_adc_no_carry() { // Test addition without initial carry - let mut carry = 0; - let result = adc_no_carry(5u64, 10u64, &mut carry); + let carry = 0; + let result = adc_no_carry(5u64, 10u64, &carry); assert_eq!(result, 15); // 5 + 10 = 15 assert_eq!(carry, 0); // No carry should be generated // Test addition with a non-zero initial carry - let mut carry = 1; - let result = adc_no_carry(5u64, 10u64, &mut carry); + let carry = 1; + let result = adc_no_carry(5u64, 10u64, &carry); assert_eq!(result, 16); // 5 + 10 + 1 = 16 assert_eq!(carry, 1); // No overflow, so carry remains 1 // Test addition that causes a carry - let mut carry = 1; - let result = adc_no_carry(u64::MAX, 1u64, &mut carry); + let carry = 1; + let result = adc_no_carry(u64::MAX, 1u64, &carry); assert_eq!(result, 1); // u64::MAX + 1 + 1 -> 1 assert_eq!(carry, 1); // Carry is 1 due to overflow } diff --git a/ff/src/biginteger/tests.rs b/ff/src/biginteger/tests.rs index 232b7e9c0..7821444f5 100644 --- a/ff/src/biginteger/tests.rs +++ b/ff/src/biginteger/tests.rs @@ -1,3 +1,4 @@ +#![allow(clippy::bool_assert_comparison)] use crate::{biginteger::BigInteger, UniformRand}; use num_bigint::BigUint; diff --git a/ff/src/fields/field_hashers/expander/tests.rs b/ff/src/fields/field_hashers/expander/tests.rs index 2b59496f8..a73a33ea2 100644 --- a/ff/src/fields/field_hashers/expander/tests.rs +++ b/ff/src/fields/field_hashers/expander/tests.rs @@ -51,6 +51,7 @@ fn expander() { } #[derive(Copy, Clone)] +#[allow(clippy::upper_case_acronyms)] pub enum ExpID { XMD(HashID), XOF(XofID), diff --git a/ff/src/fields/mod.rs b/ff/src/fields/mod.rs index dcacfe356..68465a5d1 100644 --- a/ff/src/fields/mod.rs +++ b/ff/src/fields/mod.rs @@ -371,8 +371,8 @@ pub fn batch_inversion_and_mul(v: &mut [F], coeff: &F) { let num_elem_per_thread = max(num_elems / num_cpus_available, min_elements_per_thread); // Batch invert in parallel, without copying the vector - v.par_chunks_mut(num_elem_per_thread).for_each(|mut chunk| { - serial_batch_inversion_and_mul(&mut chunk, coeff); + v.par_chunks_mut(num_elem_per_thread).for_each(|chunk| { + serial_batch_inversion_and_mul(chunk, coeff); }); } diff --git a/ff/src/fields/models/cubic_extension.rs b/ff/src/fields/models/cubic_extension.rs index 7b475ca45..53a43e368 100644 --- a/ff/src/fields/models/cubic_extension.rs +++ b/ff/src/fields/models/cubic_extension.rs @@ -457,6 +457,7 @@ impl From for CubicExtField

{ } impl From for CubicExtField

{ + #[allow(clippy::unconditional_recursion)] fn from(other: bool) -> Self { other.into() } @@ -518,6 +519,7 @@ impl Div<&CubicExtField

> for CubicExtField

{ type Output = Self; #[inline] + #[allow(clippy::suspicious_arithmetic_impl)] fn div(mut self, other: &Self) -> Self { self *= &other.inverse().unwrap(); self diff --git a/ff/src/fields/models/fp/mod.rs b/ff/src/fields/models/fp/mod.rs index 9c5082d35..87ceda04e 100644 --- a/ff/src/fields/models/fp/mod.rs +++ b/ff/src/fields/models/fp/mod.rs @@ -720,6 +720,7 @@ impl, const N: usize> Div<&Fp> for Fp { /// Returns `self * other.inverse()` if `other.inverse()` is `Some`, and /// panics otherwise. #[inline] + #[allow(clippy::suspicious_arithmetic_impl)] fn div(mut self, other: &Self) -> Self { self *= &other.inverse().unwrap(); self diff --git a/ff/src/fields/models/quadratic_extension.rs b/ff/src/fields/models/quadratic_extension.rs index 19ebe8361..914ea3b4a 100644 --- a/ff/src/fields/models/quadratic_extension.rs +++ b/ff/src/fields/models/quadratic_extension.rs @@ -619,6 +619,7 @@ impl Div<&QuadExtField

> for QuadExtField

{ type Output = Self; #[inline] + #[allow(clippy::suspicious_arithmetic_impl)] fn div(mut self, other: &Self) -> Self { self *= &other.inverse().unwrap(); self diff --git a/poly/Cargo.toml b/poly/Cargo.toml index a0a8d94cb..5b6fba95e 100644 --- a/poly/Cargo.toml +++ b/poly/Cargo.toml @@ -15,6 +15,9 @@ metadata.docs.rs.workspace = true package.metadata.release.workspace = true keywords = ["cryptography", "finite-fields", "fft", "polynomials"] +[lints] +workspace = true + [dependencies] ark-ff.workspace = true ark-serialize = { workspace = true, features = ["derive"] } @@ -24,20 +27,30 @@ educe.workspace = true hashbrown.workspace = true [target.'cfg(all(target_has_atomic = "8", target_has_atomic = "16", target_has_atomic = "32", target_has_atomic = "64", target_has_atomic = "ptr"))'.dependencies] -ahash = { version = "0.8", default-features = false} +ahash = { version = "0.8", default-features = false } [target.'cfg(not(all(target_has_atomic = "8", target_has_atomic = "16", target_has_atomic = "32", target_has_atomic = "64", target_has_atomic = "ptr")))'.dependencies] fnv = { version = "1.0", default-features = false } [dev-dependencies] -ark-test-curves = { path = "../test-curves", default-features = false, features = [ "bls12_381_curve", "bn384_small_two_adicity_curve", "mnt4_753_curve"] } +ark-test-curves = { path = "../test-curves", default-features = false, features = [ + "bls12_381_curve", + "bn384_small_two_adicity_curve", + "mnt4_753_curve", +] } criterion = "0.5.1" [features] default = [] -std = [ "ark-std/std", "ark-ff/std", "ark-serialize/std" ] -parallel = [ "std", "ark-ff/parallel", "rayon", "ark-std/parallel", "ark-serialize/parallel" ] +std = ["ark-std/std", "ark-ff/std", "ark-serialize/std"] +parallel = [ + "std", + "ark-ff/parallel", + "rayon", + "ark-std/parallel", + "ark-serialize/parallel", +] [[bench]] diff --git a/poly/benches/common.rs b/poly/benches/common.rs index c8c4550ef..7b87f4741 100644 --- a/poly/benches/common.rs +++ b/poly/benches/common.rs @@ -3,7 +3,8 @@ /// * `interval = log_interval` /// * `min = ceil(log_2(min_degree))` /// * `max = ceil(log_2(max_degree))` -pub fn size_range( +#[allow(dead_code)] +pub(crate) fn size_range( log_interval: usize, min_degree: usize, max_degree: usize, diff --git a/poly/benches/dense_uv_polynomial.rs b/poly/benches/dense_uv_polynomial.rs index 55d8e1bf1..066d42cc9 100644 --- a/poly/benches/dense_uv_polynomial.rs +++ b/poly/benches/dense_uv_polynomial.rs @@ -1,4 +1,3 @@ -extern crate criterion; mod common; use ark_ff::{FftField, Field}; @@ -33,15 +32,15 @@ fn default_size_range() -> Vec { ) } -fn setup_bench(c: &mut Criterion, name: &str, bench_fn: fn(&mut Bencher, &usize)) { +fn setup_bench(c: &mut Criterion, name: &str, bench_fn: fn(&mut Bencher<'_>, &usize)) { let mut group = c.benchmark_group(name); - for degree in default_size_range().iter() { + for degree in &default_size_range() { group.bench_with_input(BenchmarkId::from_parameter(degree), degree, bench_fn); } group.finish(); } -fn bench_sparse_poly_evaluate(b: &mut Bencher, non_zero_entries: &usize) { +fn bench_sparse_poly_evaluate(b: &mut Bencher<'_>, non_zero_entries: &usize) { const MAX_DEGREE: usize = 1 << 15; // Per benchmark setup let mut rng = &mut ark_std::test_rng(); @@ -56,7 +55,7 @@ fn bench_sparse_poly_evaluate(b: &mut Bencher, non_zero_entries: &usiz }); } -fn bench_poly_evaluate(b: &mut Bencher, degree: &usize) { +fn bench_poly_evaluate(b: &mut Bencher<'_>, degree: &usize) { // Per benchmark setup let mut rng = &mut ark_std::test_rng(); let poly = DensePolynomial::::rand(*degree, &mut rng); @@ -67,7 +66,7 @@ fn bench_poly_evaluate(b: &mut Bencher, degree: &usize) { }); } -fn bench_poly_add(b: &mut Bencher, degree: &usize) { +fn bench_poly_add(b: &mut Bencher<'_>, degree: &usize) { // Per benchmark setup let mut rng = &mut ark_std::test_rng(); let poly_one = DensePolynomial::::rand(*degree, &mut rng); @@ -78,7 +77,7 @@ fn bench_poly_add(b: &mut Bencher, degree: &usize) { }); } -fn bench_poly_add_assign(b: &mut Bencher, degree: &usize) { +fn bench_poly_add_assign(b: &mut Bencher<'_>, degree: &usize) { // Per benchmark setup let mut rng = &mut ark_std::test_rng(); let mut poly_one = DensePolynomial::::rand(*degree, &mut rng); @@ -89,7 +88,7 @@ fn bench_poly_add_assign(b: &mut Bencher, degree: &usize) { }); } -fn bench_div_by_vanishing_poly(b: &mut Bencher, degree: &usize) { +fn bench_div_by_vanishing_poly(b: &mut Bencher<'_>, degree: &usize) { // Per benchmark setup let mut rng = &mut ark_std::test_rng(); let p = DensePolynomial::::rand(*degree, &mut rng); diff --git a/poly/benches/fft.rs b/poly/benches/fft.rs index 47ccb0dbc..cc06d547f 100644 --- a/poly/benches/fft.rs +++ b/poly/benches/fft.rs @@ -1,4 +1,3 @@ -extern crate criterion; mod common; use ark_ff::FftField; @@ -43,11 +42,11 @@ fn default_size_range_mnt6_753() -> Vec { fn setup_bench( c: &mut Criterion, name: &str, - bench_fn: fn(&mut Bencher, &usize), + bench_fn: fn(&mut Bencher<'_>, &usize), size_range: &[usize], ) { let mut group = c.benchmark_group(name); - for degree in size_range.iter() { + for degree in size_range { group.bench_with_input(BenchmarkId::from_parameter(degree), degree, bench_fn); } group.finish(); @@ -69,7 +68,7 @@ fn fft_setup_with_domain_size>( (domain, a) } -fn bench_fft_in_place>(b: &mut Bencher, degree: &usize) { +fn bench_fft_in_place>(b: &mut Bencher<'_>, degree: &usize) { // Per benchmark setup let (domain, mut a) = fft_setup::(*degree); b.iter(|| { @@ -79,7 +78,7 @@ fn bench_fft_in_place>(b: &mut Bencher, degr } fn bench_large_domain_fft_in_place>( - b: &mut Bencher, + b: &mut Bencher<'_>, degree: &usize, ) { // Per benchmark setup @@ -90,7 +89,7 @@ fn bench_large_domain_fft_in_place>( }); } -fn bench_ifft_in_place>(b: &mut Bencher, degree: &usize) { +fn bench_ifft_in_place>(b: &mut Bencher<'_>, degree: &usize) { // Per benchmark setup let (domain, mut a) = fft_setup::(*degree); b.iter(|| { @@ -99,7 +98,10 @@ fn bench_ifft_in_place>(b: &mut Bencher, deg }); } -fn bench_coset_fft_in_place>(b: &mut Bencher, degree: &usize) { +fn bench_coset_fft_in_place>( + b: &mut Bencher<'_>, + degree: &usize, +) { // Per benchmark setup let (domain, mut a) = fft_setup::(*degree); let coset_domain = domain.get_coset(F::GENERATOR).unwrap(); @@ -109,7 +111,10 @@ fn bench_coset_fft_in_place>(b: &mut Bencher }); } -fn bench_coset_ifft_in_place>(b: &mut Bencher, degree: &usize) { +fn bench_coset_ifft_in_place>( + b: &mut Bencher<'_>, + degree: &usize, +) { // Per benchmark setup let (domain, mut a) = fft_setup::(*degree); let coset_domain = domain.get_coset(F::GENERATOR).unwrap(); diff --git a/poly/benches/sparse_multilinear.rs b/poly/benches/sparse_multilinear.rs index 68c2d60a2..2444bd21c 100644 --- a/poly/benches/sparse_multilinear.rs +++ b/poly/benches/sparse_multilinear.rs @@ -14,7 +14,7 @@ fn arithmetic_op_bench(c: &mut Criterion) { let mut group = c.benchmark_group("SparseMultilinear"); for nv in NUM_VARIABLES_RANGE { - let num_nonzero_entries = 1 << (dbg!(nv) / 2); + let num_nonzero_entries = 1 << (nv / 2); group.bench_with_input( BenchmarkId::new(format!("Add with num_vars = {nv}"), num_nonzero_entries), &num_nonzero_entries, diff --git a/poly/src/domain/mixed_radix.rs b/poly/src/domain/mixed_radix.rs index 220544861..dee82de02 100644 --- a/poly/src/domain/mixed_radix.rs +++ b/poly/src/domain/mixed_radix.rs @@ -89,7 +89,7 @@ impl EvaluationDomain for MixedRadixEvaluationDomain { let size_as_field_element = F::from(size); let size_inv = size_as_field_element.inverse()?; - Some(MixedRadixEvaluationDomain { + Some(Self { size, log_size_of_group, size_as_field_element, @@ -103,7 +103,7 @@ impl EvaluationDomain for MixedRadixEvaluationDomain { } fn get_coset(&self, offset: F) -> Option { - Some(MixedRadixEvaluationDomain { + Some(Self { offset, offset_inv: offset.inverse()?, offset_pow_size: offset.pow([self.size]), @@ -124,11 +124,7 @@ impl EvaluationDomain for MixedRadixEvaluationDomain { let two_adicity = k_adicity(2, num_coeffs); let two_part = 2u64.checked_pow(two_adicity)?; - if num_coeffs == q_part * two_part { - Some(num_coeffs as usize) - } else { - None - } + (num_coeffs == q_part * two_part).then_some(num_coeffs as usize) } #[inline] diff --git a/poly/src/domain/radix2/fft.rs b/poly/src/domain/radix2/fft.rs index 5ecf813a5..94fdff536 100644 --- a/poly/src/domain/radix2/fft.rs +++ b/poly/src/domain/radix2/fft.rs @@ -366,7 +366,7 @@ const MIN_INPUT_SIZE_FOR_PARALLELIZATION: usize = 1 << 10; const LOG_ROOTS_OF_UNITY_PARALLEL_SIZE: u32 = 7; #[inline] -fn bitrev(a: u64, log_len: u32) -> u64 { +const fn bitrev(a: u64, log_len: u32) -> u64 { a.reverse_bits().wrapping_shr(64 - log_len) } diff --git a/poly/src/domain/radix2/mod.rs b/poly/src/domain/radix2/mod.rs index 3fb3fb93a..ce51723ae 100644 --- a/poly/src/domain/radix2/mod.rs +++ b/poly/src/domain/radix2/mod.rs @@ -73,7 +73,7 @@ impl EvaluationDomain for Radix2EvaluationDomain { let size_as_field_element = F::from(size); let size_inv = size_as_field_element.inverse()?; - Some(Radix2EvaluationDomain { + Some(Self { size, log_size_of_group, size_as_field_element, @@ -87,7 +87,7 @@ impl EvaluationDomain for Radix2EvaluationDomain { } fn get_coset(&self, offset: F) -> Option { - Some(Radix2EvaluationDomain { + Some(Self { offset, offset_inv: offset.inverse()?, offset_pow_size: offset.pow([self.size]), diff --git a/poly/src/domain/utils.rs b/poly/src/domain/utils.rs index d25bb9cc7..7bfc7f175 100644 --- a/poly/src/domain/utils.rs +++ b/poly/src/domain/utils.rs @@ -54,19 +54,18 @@ pub(crate) fn compute_powers(size: usize, g: F) -> Vec { let res: Vec = (0..num_cpus_used) .into_par_iter() .flat_map(|i| { - let offset = g.pow(&[(i * num_elem_per_thread) as u64]); + let offset = g.pow([(i * num_elem_per_thread) as u64]); // Compute the size that this chunks' output should be // (num_elem_per_thread, unless there are less than num_elem_per_thread elements remaining) let num_elements_to_compute = min(size - i * num_elem_per_thread, num_elem_per_thread); - let res = compute_powers_and_mul_by_const_serial(num_elements_to_compute, g, offset); - res + compute_powers_and_mul_by_const_serial(num_elements_to_compute, g, offset) }) .collect(); res } #[cfg(feature = "parallel")] -fn log2_floor(num: usize) -> u32 { +const fn log2_floor(num: usize) -> u32 { if num == 0 { 0 } else { @@ -128,7 +127,7 @@ pub(crate) fn parallel_fft, F: FftField>( // the second coset is (g, g^{1 + num_cosets}, g^{1 + 2*num_cosets}, etc.) // These are cosets with generator g^{num_cosets}, and varying shifts. let mut tmp = vec![vec![T::zero(); coset_size]; num_cosets]; - let new_omega = omega.pow(&[num_cosets as u64]); + let new_omega = omega.pow([num_cosets as u64]); let new_two_adicity = ark_ff::utils::k_adicity(2, coset_size as u64); // For each coset, we first build a polynomial of degree |coset size|, @@ -138,8 +137,8 @@ pub(crate) fn parallel_fft, F: FftField>( .enumerate() .for_each(|(k, kth_poly_coeffs)| { // Shuffle into a sub-FFT - let omega_k = omega.pow(&[k as u64]); - let omega_step = omega.pow(&[(k * coset_size) as u64]); + let omega_k = omega.pow([k as u64]); + let omega_step = omega.pow([(k * coset_size) as u64]); let mut elt = F::one(); // Construct kth_poly_coeffs, which is a polynomial whose evaluations on this coset @@ -159,18 +158,22 @@ pub(crate) fn parallel_fft, F: FftField>( // pretty bad parallelism. Exact complexity per thread atm is // `2N + (N/num threads)log(N/num threads)` field muls Compare to the time // complexity of serial is Nlog(N) field muls), with log(N) in [15, 25] - for i in 0..coset_size { - for c in 0..num_threads { - let idx = i + (c * coset_size); - // t = the value of a corresponding to the ith element of the sth coset. - let mut t = a[idx]; - // elt = g^{k * idx} - t *= elt; - kth_poly_coeffs[i] += t; - elt *= &omega_step; - } - elt *= &omega_k; - } + kth_poly_coeffs + .iter_mut() + .enumerate() + .take(coset_size) + .for_each(|(i, coeff)| { + for c in 0..num_threads { + let idx = i + (c * coset_size); + // Compute the value of `a` corresponding to the `i`th element of the `c`th coset. + let mut t = a[idx]; + // Multiply by `g^{k * idx}` + t *= elt; + *coeff += t; + elt *= &omega_step; + } + elt *= &omega_k; + }); // Perform sub-FFT // Since the sub-FFT is mutative, after this point diff --git a/poly/src/evaluations/multivariate/multilinear/dense.rs b/poly/src/evaluations/multivariate/multilinear/dense.rs index 6ee811bc9..b71c66db6 100644 --- a/poly/src/evaluations/multivariate/multilinear/dense.rs +++ b/poly/src/evaluations/multivariate/multilinear/dense.rs @@ -153,8 +153,26 @@ impl DenseMultilinearExtension { } } -impl AsRef> for DenseMultilinearExtension { - fn as_ref(&self) -> &DenseMultilinearExtension { +impl<'a, F: Field> IntoIterator for &'a DenseMultilinearExtension { + type IntoIter = ark_std::slice::Iter<'a, F>; + type Item = &'a F; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +impl<'a, F: Field> IntoIterator for &'a mut DenseMultilinearExtension { + type IntoIter = ark_std::slice::IterMut<'a, F>; + type Item = &'a mut F; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} + +impl AsRef for DenseMultilinearExtension { + fn as_ref(&self) -> &Self { self } } @@ -205,7 +223,7 @@ impl MultilinearExtension for DenseMultilinearExtension { partial_point.len() <= self.num_vars, "invalid size of partial point" ); - let mut poly = self.evaluations.to_vec(); + let mut poly = self.evaluations.clone(); let nv = self.num_vars; let dim = partial_point.len(); // evaluate single variable of partial point from left to right @@ -221,7 +239,7 @@ impl MultilinearExtension for DenseMultilinearExtension { } fn to_evaluations(&self) -> Vec { - self.evaluations.to_vec() + self.evaluations.clone() } } @@ -240,9 +258,9 @@ impl Index for DenseMultilinearExtension { } impl Add for DenseMultilinearExtension { - type Output = DenseMultilinearExtension; + type Output = Self; - fn add(self, other: DenseMultilinearExtension) -> Self { + fn add(self, other: Self) -> Self { &self + &other } } @@ -274,16 +292,14 @@ impl AddAssign for DenseMultilinearExtension { } } -impl<'a, F: Field> AddAssign<&'a DenseMultilinearExtension> for DenseMultilinearExtension { - fn add_assign(&mut self, other: &'a DenseMultilinearExtension) { +impl<'a, F: Field> AddAssign<&'a Self> for DenseMultilinearExtension { + fn add_assign(&mut self, other: &'a Self) { *self = &*self + other; } } -impl<'a, F: Field> AddAssign<(F, &'a DenseMultilinearExtension)> - for DenseMultilinearExtension -{ - fn add_assign(&mut self, (f, other): (F, &'a DenseMultilinearExtension)) { +impl<'a, F: Field> AddAssign<(F, &'a Self)> for DenseMultilinearExtension { + fn add_assign(&mut self, (f, other): (F, &'a Self)) { let other = Self { num_vars: other.num_vars, evaluations: cfg_iter!(other.evaluations).map(|x| f * x).collect(), @@ -293,7 +309,7 @@ impl<'a, F: Field> AddAssign<(F, &'a DenseMultilinearExtension)> } impl Neg for DenseMultilinearExtension { - type Output = DenseMultilinearExtension; + type Output = Self; fn neg(self) -> Self::Output { Self::Output { @@ -304,9 +320,9 @@ impl Neg for DenseMultilinearExtension { } impl Sub for DenseMultilinearExtension { - type Output = DenseMultilinearExtension; + type Output = Self; - fn sub(self, other: DenseMultilinearExtension) -> Self { + fn sub(self, other: Self) -> Self { &self - &other } } @@ -325,14 +341,14 @@ impl SubAssign for DenseMultilinearExtension { } } -impl<'a, F: Field> SubAssign<&'a DenseMultilinearExtension> for DenseMultilinearExtension { - fn sub_assign(&mut self, other: &'a DenseMultilinearExtension) { +impl<'a, F: Field> SubAssign<&'a Self> for DenseMultilinearExtension { + fn sub_assign(&mut self, other: &'a Self) { *self = &*self - other; } } impl Mul for DenseMultilinearExtension { - type Output = DenseMultilinearExtension; + type Output = Self; fn mul(self, scalar: F) -> Self::Output { &self * &scalar @@ -438,9 +454,10 @@ mod tests { /// utility: evaluate multilinear extension (in form of data array) at a random point fn evaluate_data_array(data: &[F], point: &[F]) -> F { - if data.len() != (1 << point.len()) { - panic!("Data size mismatch with number of variables. ") - } + assert!( + data.len() == (1 << point.len()), + "Data size mismatch with number of variables. " + ); let nv = point.len(); let mut a = data.to_vec(); diff --git a/poly/src/evaluations/multivariate/multilinear/mod.rs b/poly/src/evaluations/multivariate/multilinear/mod.rs index bedb97cd1..3b4c2659c 100644 --- a/poly/src/evaluations/multivariate/multilinear/mod.rs +++ b/poly/src/evaluations/multivariate/multilinear/mod.rs @@ -44,6 +44,7 @@ type DefaultHasher = fnv::FnvHasher; /// /// Index represents a point, which is a vector in {0,1}^`num_vars` in little /// endian form. For example, `0b1011` represents `P(1,1,0,1)` +#[allow(clippy::trait_duplication_in_bounds)] pub trait MultilinearExtension: Sized + Clone @@ -87,7 +88,7 @@ pub trait MultilinearExtension: } /// swap the bits of `x` from position `a..a+n` to `b..b+n` and from `b..b+n` to `a..a+n` in little endian order -pub(crate) fn swap_bits(x: usize, a: usize, b: usize, n: usize) -> usize { +pub(crate) const fn swap_bits(x: usize, a: usize, b: usize, n: usize) -> usize { let a_bits = (x >> a) & ((1usize << n) - 1); let b_bits = (x >> b) & ((1usize << n) - 1); let local_xor_mask = a_bits ^ b_bits; diff --git a/poly/src/evaluations/multivariate/multilinear/sparse.rs b/poly/src/evaluations/multivariate/multilinear/sparse.rs index 75c8ad4b1..ea8f05319 100644 --- a/poly/src/evaluations/multivariate/multilinear/sparse.rs +++ b/poly/src/evaluations/multivariate/multilinear/sparse.rs @@ -77,12 +77,6 @@ impl SparseMultilinearExtension { } map.entry(index).or_insert(F::rand(rng)); } - let mut buf = Vec::new(); - for (arg, v) in map.iter() { - if *v != F::zero() { - buf.push((*arg, *v)); - } - } let evaluations = hashmap_to_treemap(&map); Self { num_vars, @@ -94,7 +88,7 @@ impl SparseMultilinearExtension { /// Convert the sparse multilinear polynomial to dense form. pub fn to_dense_multilinear_extension(&self) -> DenseMultilinearExtension { let mut evaluations: Vec<_> = (0..(1 << self.num_vars)).map(|_| F::zero()).collect(); - for (&i, &v) in self.evaluations.iter() { + for (&i, &v) in &self.evaluations { evaluations[i] = v; } DenseMultilinearExtension::from_evaluations_vec(self.num_vars, evaluations) @@ -178,7 +172,7 @@ impl MultilinearExtension for SparseMultilinearExtension { let dim = focus.len(); let mut result = HashMap::with_hasher(core::hash::BuildHasherDefault::::default()); - for src_entry in last.iter() { + for src_entry in &last { let old_idx = *src_entry.0; let gz = pre[old_idx & ((1 << dim) - 1)]; let new_idx = old_idx >> dim; @@ -239,9 +233,9 @@ impl Polynomial for SparseMultilinearExtension { } impl Add for SparseMultilinearExtension { - type Output = SparseMultilinearExtension; + type Output = Self; - fn add(self, other: SparseMultilinearExtension) -> Self { + fn add(self, other: Self) -> Self { &self + &other } } @@ -287,16 +281,14 @@ impl AddAssign for SparseMultilinearExtension { } } -impl<'a, F: Field> AddAssign<&'a SparseMultilinearExtension> for SparseMultilinearExtension { - fn add_assign(&mut self, other: &'a SparseMultilinearExtension) { +impl<'a, F: Field> AddAssign<&'a Self> for SparseMultilinearExtension { + fn add_assign(&mut self, other: &'a Self) { *self = &*self + other; } } -impl<'a, F: Field> AddAssign<(F, &'a SparseMultilinearExtension)> - for SparseMultilinearExtension -{ - fn add_assign(&mut self, (f, other): (F, &'a SparseMultilinearExtension)) { +impl<'a, F: Field> AddAssign<(F, &'a Self)> for SparseMultilinearExtension { + fn add_assign(&mut self, (f, other): (F, &'a Self)) { if !self.is_zero() && !other.is_zero() { assert_eq!( other.num_vars, self.num_vars, @@ -316,7 +308,7 @@ impl<'a, F: Field> AddAssign<(F, &'a SparseMultilinearExtension)> } impl Neg for SparseMultilinearExtension { - type Output = SparseMultilinearExtension; + type Output = Self; fn neg(self) -> Self::Output { let ev: Vec<_> = cfg_iter!(self.evaluations) @@ -331,9 +323,9 @@ impl Neg for SparseMultilinearExtension { } impl Sub for SparseMultilinearExtension { - type Output = SparseMultilinearExtension; + type Output = Self; - fn sub(self, other: SparseMultilinearExtension) -> Self { + fn sub(self, other: Self) -> Self { &self - &other } } @@ -352,8 +344,8 @@ impl SubAssign for SparseMultilinearExtension { } } -impl<'a, F: Field> SubAssign<&'a SparseMultilinearExtension> for SparseMultilinearExtension { - fn sub_assign(&mut self, other: &'a SparseMultilinearExtension) { +impl<'a, F: Field> SubAssign<&'a Self> for SparseMultilinearExtension { + fn sub_assign(&mut self, other: &'a Self) { *self = &*self - other; } } @@ -393,17 +385,17 @@ impl Debug for SparseMultilinearExtension { /// Utility: Convert tuples to hashmap. fn tuples_to_treemap(tuples: &[(usize, F)]) -> BTreeMap { - BTreeMap::from_iter(tuples.iter().map(|(i, v)| (*i, *v))) + tuples.iter().map(|(i, v)| (*i, *v)).collect() } fn treemap_to_hashmap( map: &BTreeMap, ) -> HashMap> { - HashMap::from_iter(map.iter().map(|(i, v)| (*i, *v))) + map.iter().map(|(i, v)| (*i, *v)).collect() } fn hashmap_to_treemap(map: &HashMap) -> BTreeMap { - BTreeMap::from_iter(map.iter().map(|(i, v)| (*i, *v))) + map.iter().map(|(i, v)| (*i, *v)).collect() } #[cfg(test)] diff --git a/poly/src/evaluations/univariate/mod.rs b/poly/src/evaluations/univariate/mod.rs index 6ba6f1fb8..9bfe7e449 100644 --- a/poly/src/evaluations/univariate/mod.rs +++ b/poly/src/evaluations/univariate/mod.rs @@ -32,7 +32,7 @@ impl> Evaluations { } /// Construct `Self` from evaluations and a domain. - pub fn from_vec_and_domain(evals: Vec, domain: D) -> Self { + pub const fn from_vec_and_domain(evals: Vec, domain: D) -> Self { Self { evals, domain } } @@ -49,7 +49,7 @@ impl> Evaluations { } /// Return the domain `self` is defined over - pub fn domain(&self) -> D { + pub const fn domain(&self) -> D { self.domain } } @@ -73,11 +73,9 @@ impl<'a, F: FftField, D: EvaluationDomain> Mul<&'a Evaluations> for &Ev } } -impl<'a, F: FftField, D: EvaluationDomain> MulAssign<&'a Evaluations> - for Evaluations -{ +impl<'a, F: FftField, D: EvaluationDomain> MulAssign<&'a Self> for Evaluations { #[inline] - fn mul_assign(&mut self, other: &'a Evaluations) { + fn mul_assign(&mut self, other: &'a Self) { assert_eq!(self.domain, other.domain, "domains are unequal"); ark_std::cfg_iter_mut!(self.evals) .zip(&other.evals) @@ -109,11 +107,9 @@ impl<'a, F: FftField, D: EvaluationDomain> Add<&'a Evaluations> for &Ev } } -impl<'a, F: FftField, D: EvaluationDomain> AddAssign<&'a Evaluations> - for Evaluations -{ +impl<'a, F: FftField, D: EvaluationDomain> AddAssign<&'a Self> for Evaluations { #[inline] - fn add_assign(&mut self, other: &'a Evaluations) { + fn add_assign(&mut self, other: &'a Self) { assert_eq!(self.domain, other.domain, "domains are unequal"); ark_std::cfg_iter_mut!(self.evals) .zip(&other.evals) @@ -132,11 +128,9 @@ impl<'a, F: FftField, D: EvaluationDomain> Sub<&'a Evaluations> for &Ev } } -impl<'a, F: FftField, D: EvaluationDomain> SubAssign<&'a Evaluations> - for Evaluations -{ +impl<'a, F: FftField, D: EvaluationDomain> SubAssign<&'a Self> for Evaluations { #[inline] - fn sub_assign(&mut self, other: &'a Evaluations) { + fn sub_assign(&mut self, other: &'a Self) { assert_eq!(self.domain, other.domain, "domains are unequal"); ark_std::cfg_iter_mut!(self.evals) .zip(&other.evals) @@ -155,11 +149,9 @@ impl<'a, F: FftField, D: EvaluationDomain> Div<&'a Evaluations> for &Ev } } -impl<'a, F: FftField, D: EvaluationDomain> DivAssign<&'a Evaluations> - for Evaluations -{ +impl<'a, F: FftField, D: EvaluationDomain> DivAssign<&'a Self> for Evaluations { #[inline] - fn div_assign(&mut self, other: &'a Evaluations) { + fn div_assign(&mut self, other: &'a Self) { assert_eq!(self.domain, other.domain, "domains are unequal"); let mut other_copy = other.clone(); batch_inversion(other_copy.evals.as_mut_slice()); diff --git a/poly/src/lib.rs b/poly/src/lib.rs index 327134689..3dc52d65a 100644 --- a/poly/src/lib.rs +++ b/poly/src/lib.rs @@ -1,13 +1,6 @@ //! This crate implements functions for manipulating polynomials over finite //! fields, including FFTs. #![cfg_attr(not(feature = "std"), no_std)] -#![warn( - unused, - future_incompatible, - nonstandard_style, - rust_2018_idioms, - rust_2021_compatibility -)] #![forbid(unsafe_code)] #![allow( clippy::many_single_char_names, diff --git a/poly/src/polynomial/mod.rs b/poly/src/polynomial/mod.rs index d37765115..5826fbcd4 100644 --- a/poly/src/polynomial/mod.rs +++ b/poly/src/polynomial/mod.rs @@ -13,6 +13,7 @@ pub mod multivariate; pub mod univariate; /// Describes the common interface for univariate and multivariate polynomials +#[allow(clippy::trait_duplication_in_bounds)] pub trait Polynomial: Sized + Clone diff --git a/poly/src/polynomial/multivariate/mod.rs b/poly/src/polynomial/multivariate/mod.rs index cd409d1c9..e4ea9bb62 100644 --- a/poly/src/polynomial/multivariate/mod.rs +++ b/poly/src/polynomial/multivariate/mod.rs @@ -142,9 +142,7 @@ impl PartialOrd for SparseTerm { /// ie. `x_1 > x_2`, `x_1^2 > x_1 * x_2`, etc. #[allow(clippy::non_canonical_partial_ord_impl)] fn partial_cmp(&self, other: &Self) -> Option { - if self.degree() != other.degree() { - Some(self.degree().cmp(&other.degree())) - } else { + if self.degree() == other.degree() { // Iterate through all variables and return the corresponding ordering // if they differ in variable numbering or power for ((cur_variable, cur_power), (other_variable, other_power)) in @@ -159,6 +157,8 @@ impl PartialOrd for SparseTerm { } } Some(Ordering::Equal) + } else { + Some(self.degree().cmp(&other.degree())) } } } @@ -177,9 +177,9 @@ mod tests { #[derive(MontConfig)] #[modulus = "5"] #[generator = "2"] - pub struct F5Config; + pub(crate) struct F5Config; - pub type F5 = Fp64>; + pub(crate) type F5 = Fp64>; #[test] fn test_sparse_term_combine() { diff --git a/poly/src/polynomial/multivariate/sparse.rs b/poly/src/polynomial/multivariate/sparse.rs index 564e8137f..c186cd562 100644 --- a/poly/src/polynomial/multivariate/sparse.rs +++ b/poly/src/polynomial/multivariate/sparse.rs @@ -169,9 +169,9 @@ impl DenseMVPolynomial for SparsePolynomial { } impl Add for SparsePolynomial { - type Output = SparsePolynomial; + type Output = Self; - fn add(self, other: SparsePolynomial) -> Self { + fn add(self, other: Self) -> Self { &self + &other } } @@ -215,14 +215,14 @@ impl<'a, F: Field, T: Term> Add<&'a SparsePolynomial> for &SparsePolynomia } } -impl<'a, F: Field, T: Term> AddAssign<&'a SparsePolynomial> for SparsePolynomial { - fn add_assign(&mut self, other: &'a SparsePolynomial) { +impl<'a, F: Field, T: Term> AddAssign<&'a Self> for SparsePolynomial { + fn add_assign(&mut self, other: &'a Self) { *self = &*self + other; } } -impl<'a, F: Field, T: Term> AddAssign<(F, &'a SparsePolynomial)> for SparsePolynomial { - fn add_assign(&mut self, (f, other): (F, &'a SparsePolynomial)) { +impl<'a, F: Field, T: Term> AddAssign<(F, &'a Self)> for SparsePolynomial { + fn add_assign(&mut self, (f, other): (F, &'a Self)) { let other = Self { num_vars: other.num_vars, terms: other @@ -237,10 +237,10 @@ impl<'a, F: Field, T: Term> AddAssign<(F, &'a SparsePolynomial)> for Spars } impl Neg for SparsePolynomial { - type Output = SparsePolynomial; + type Output = Self; #[inline] - fn neg(mut self) -> SparsePolynomial { + fn neg(mut self) -> Self { for coeff in &mut self.terms { (coeff).0 = -coeff.0; } @@ -258,9 +258,9 @@ impl<'a, F: Field, T: Term> Sub<&'a SparsePolynomial> for &SparsePolynomia } } -impl<'a, F: Field, T: Term> SubAssign<&'a SparsePolynomial> for SparsePolynomial { +impl<'a, F: Field, T: Term> SubAssign<&'a Self> for SparsePolynomial { #[inline] - fn sub_assign(&mut self, other: &'a SparsePolynomial) { + fn sub_assign(&mut self, other: &'a Self) { *self = &*self - other; } } @@ -308,13 +308,7 @@ mod tests { random_terms.push((Fr::rand(rng), SparseTerm::new(vec![]))); for _ in 1..num_terms { let term = (0..l) - .filter_map(|i| { - if rng.gen_bool(0.5) { - Some((i, rng.gen_range(1..(d + 1)))) - } else { - None - } - }) + .filter_map(|i| rng.gen_bool(0.5).then(|| (i, rng.gen_range(1..(d + 1))))) .collect(); let coeff = Fr::rand(rng); random_terms.push((coeff, SparseTerm::new(term))); @@ -331,8 +325,8 @@ mod tests { SparsePolynomial::zero() } else { let mut result_terms = Vec::new(); - for (cur_coeff, cur_term) in cur.terms.iter() { - for (other_coeff, other_term) in other.terms.iter() { + for (cur_coeff, cur_term) in &cur.terms { + for (other_coeff, other_term) in &other.terms { let mut term = cur_term.0.clone(); term.extend(other_term.0.clone()); result_terms.push((*cur_coeff * *other_coeff, SparseTerm::new(term))); @@ -384,7 +378,7 @@ mod tests { point.push(Fr::rand(rng)); } let mut total = Fr::zero(); - for (coeff, term) in p.terms.iter() { + for (coeff, term) in &p.terms { let mut summand = *coeff; for var in term.iter() { let eval = point.get(var.0).unwrap(); diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index 075351523..52d024c70 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -84,8 +84,7 @@ impl DensePolynomial { .par_chunks(num_elem_per_thread) .enumerate() .map(|(i, chunk)| { - Self::horner_evaluate(&chunk, point) - * point.pow(&[(i * num_elem_per_thread) as u64]) + Self::horner_evaluate(chunk, point) * point.pow([(i * num_elem_per_thread) as u64]) }) .sum() } @@ -153,26 +152,23 @@ impl DenseUVPolynomial for DensePolynomial { impl DensePolynomial { /// Multiply `self` by the vanishing polynomial for the domain `domain`. /// Returns the result of the multiplication. - pub fn mul_by_vanishing_poly>(&self, domain: D) -> DensePolynomial { + pub fn mul_by_vanishing_poly>(&self, domain: D) -> Self { let mut shifted = vec![F::zero(); domain.size()]; shifted.extend_from_slice(&self.coeffs); cfg_iter_mut!(shifted) .zip(&self.coeffs) .for_each(|(s, c)| *s -= c); - DensePolynomial::from_coefficients_vec(shifted) + Self::from_coefficients_vec(shifted) } /// Divide `self` by the vanishing polynomial for the domain `domain`. /// Returns the quotient and remainder of the division. - pub fn divide_by_vanishing_poly>( - &self, - domain: D, - ) -> (DensePolynomial, DensePolynomial) { + pub fn divide_by_vanishing_poly>(&self, domain: D) -> (Self, Self) { let domain_size = domain.size(); if self.coeffs.len() < domain_size { // If degree(self) < len(Domain), then the quotient is zero, and the entire polynomial is the remainder - (DensePolynomial::::zero(), self.clone()) + (Self::zero(), self.clone()) } else { // Compute the quotient // @@ -206,8 +202,8 @@ impl DensePolynomial { .zip("ient_vec) .for_each(|(s, c)| *s += c); - let quotient = DensePolynomial::::from_coefficients_vec(quotient_vec); - let remainder = DensePolynomial::::from_coefficients_vec(remainder_vec); + let quotient = Self::from_coefficients_vec(quotient_vec); + let remainder = Self::from_coefficients_vec(remainder_vec); (quotient, remainder) } } @@ -223,7 +219,7 @@ impl DensePolynomial { /// Perform a naive n^2 multiplication of `self` by `other`. pub fn naive_mul(&self, other: &Self) -> Self { if self.is_zero() || other.is_zero() { - DensePolynomial::zero() + Self::zero() } else { let mut result = vec![F::zero(); self.degree() + other.degree() + 1]; for (i, self_coeff) in self.coeffs.iter().enumerate() { @@ -231,7 +227,7 @@ impl DensePolynomial { result[i + j] += &(*self_coeff * other_coeff); } } - DensePolynomial::from_coefficients_vec(result) + Self::from_coefficients_vec(result) } } } @@ -356,8 +352,8 @@ impl<'a, F: Field> Add<&'a SparsePolynomial> for &DensePolynomial { } } -impl<'a, F: Field> AddAssign<&'a DensePolynomial> for DensePolynomial { - fn add_assign(&mut self, other: &'a DensePolynomial) { +impl<'a, F: Field> AddAssign<&'a Self> for DensePolynomial { + fn add_assign(&mut self, other: &'a Self) { if other.is_zero() { self.truncate_leading_zeros(); return; @@ -382,8 +378,8 @@ impl<'a, F: Field> AddAssign<&'a DensePolynomial> for DensePolynomial { } } -impl<'a, F: Field> AddAssign<(F, &'a DensePolynomial)> for DensePolynomial { - fn add_assign(&mut self, (f, other): (F, &'a DensePolynomial)) { +impl<'a, F: Field> AddAssign<(F, &'a Self)> for DensePolynomial { + fn add_assign(&mut self, (f, other): (F, &'a Self)) { // No need to modify self if other is zero if other.is_zero() { return; @@ -458,10 +454,10 @@ impl<'a, F: Field> AddAssign<&'a SparsePolynomial> for DensePolynomial { } impl Neg for DensePolynomial { - type Output = DensePolynomial; + type Output = Self; #[inline] - fn neg(mut self) -> DensePolynomial { + fn neg(mut self) -> Self { self.coeffs.iter_mut().for_each(|coeff| { *coeff = -*coeff; }); @@ -534,9 +530,9 @@ impl<'a, F: Field> Sub<&'a SparsePolynomial> for &DensePolynomial { } } -impl<'a, F: Field> SubAssign<&'a DensePolynomial> for DensePolynomial { +impl<'a, F: Field> SubAssign<&'a Self> for DensePolynomial { #[inline] - fn sub_assign(&mut self, other: &'a DensePolynomial) { + fn sub_assign(&mut self, other: &'a Self) { if self.is_zero() { self.coeffs.resize(other.coeffs.len(), F::zero()); } else if other.is_zero() { @@ -618,10 +614,10 @@ impl Mul for &DensePolynomial { } impl Mul for DensePolynomial { - type Output = DensePolynomial; + type Output = Self; #[inline] - fn mul(self, elem: F) -> DensePolynomial { + fn mul(self, elem: F) -> Self { &self * elem } } @@ -717,10 +713,10 @@ mod tests { #[derive(MontConfig)] #[modulus = "5"] #[generator = "2"] - pub struct F5Config; + pub(crate) struct F5Config; let rng = &mut test_rng(); - pub type F5 = Fp64>; + pub(crate) type F5 = Fp64>; // if the leading coefficient were uniformly sampled from all of F, this // test would fail with high probability ~99.9% diff --git a/poly/src/polynomial/univariate/mod.rs b/poly/src/polynomial/univariate/mod.rs index 06d3c77e0..9332df2fb 100644 --- a/poly/src/polynomial/univariate/mod.rs +++ b/poly/src/polynomial/univariate/mod.rs @@ -3,7 +3,7 @@ use crate::{DenseUVPolynomial, EvaluationDomain, Evaluations, Polynomial}; use ark_ff::{FftField, Field, Zero}; use ark_std::{borrow::Cow, vec::*}; -use DenseOrSparsePolynomial::*; +use DenseOrSparsePolynomial::{DPolynomial, SPolynomial}; mod dense; mod sparse; @@ -48,7 +48,7 @@ impl<'a, F: Field> From<&'a SparsePolynomial> for DenseOrSparsePolynomial<'a, } impl<'a, F: Field> From> for DensePolynomial { - fn from(other: DenseOrSparsePolynomial<'a, F>) -> DensePolynomial { + fn from(other: DenseOrSparsePolynomial<'a, F>) -> Self { match other { DPolynomial(p) => p.into_owned(), SPolynomial(p) => p.into_owned().into(), @@ -96,7 +96,7 @@ impl DenseOrSparsePolynomial<'_, F> { fn iter_with_index(&self) -> Vec<(usize, F)> { match self { SPolynomial(p) => p.to_vec(), - DPolynomial(p) => p.iter().cloned().enumerate().collect(), + DPolynomial(p) => p.iter().copied().enumerate().collect(), } } @@ -126,7 +126,7 @@ impl DenseOrSparsePolynomial<'_, F> { for (i, div_coeff) in divisor.iter_with_index() { remainder[cur_q_degree + i] -= &(cur_q_coeff * div_coeff); } - while let Some(true) = remainder.coeffs.last().map(|c| c.is_zero()) { + while remainder.coeffs.last().map(|c| c.is_zero()) == Some(true) { remainder.coeffs.pop(); } } diff --git a/poly/src/polynomial/univariate/sparse.rs b/poly/src/polynomial/univariate/sparse.rs index 7357ad429..9f71554ef 100644 --- a/poly/src/polynomial/univariate/sparse.rs +++ b/poly/src/polynomial/univariate/sparse.rs @@ -102,9 +102,9 @@ impl Polynomial for SparsePolynomial { } impl Add for SparsePolynomial { - type Output = SparsePolynomial; + type Output = Self; - fn add(self, other: SparsePolynomial) -> Self { + fn add(self, other: Self) -> Self { &self + &other } } @@ -162,16 +162,16 @@ impl<'a, F: Field> Add<&'a SparsePolynomial> for &SparsePolynomial { } } -impl<'a, F: Field> AddAssign<&'a SparsePolynomial> for SparsePolynomial { +impl<'a, F: Field> AddAssign<&'a Self> for SparsePolynomial { // TODO: Reduce number of clones - fn add_assign(&mut self, other: &'a SparsePolynomial) { + fn add_assign(&mut self, other: &'a Self) { self.coeffs = (self.clone() + other.clone()).coeffs; } } -impl<'a, F: Field> AddAssign<(F, &'a SparsePolynomial)> for SparsePolynomial { +impl<'a, F: Field> AddAssign<(F, &'a Self)> for SparsePolynomial { // TODO: Reduce number of clones - fn add_assign(&mut self, (f, other): (F, &'a SparsePolynomial)) { + fn add_assign(&mut self, (f, other): (F, &'a Self)) { self.coeffs = (self.clone() + other.clone()).coeffs; for i in 0..self.coeffs.len() { self.coeffs[i].1 *= f; @@ -180,10 +180,10 @@ impl<'a, F: Field> AddAssign<(F, &'a SparsePolynomial)> for SparsePolynomial< } impl Neg for SparsePolynomial { - type Output = SparsePolynomial; + type Output = Self; #[inline] - fn neg(mut self) -> SparsePolynomial { + fn neg(mut self) -> Self { for (_, coeff) in &mut self.coeffs { *coeff = -*coeff; } @@ -191,10 +191,10 @@ impl Neg for SparsePolynomial { } } -impl<'a, F: Field> SubAssign<&'a SparsePolynomial> for SparsePolynomial { +impl<'a, F: Field> SubAssign<&'a Self> for SparsePolynomial { // TODO: Reduce number of clones #[inline] - fn sub_assign(&mut self, other: &'a SparsePolynomial) { + fn sub_assign(&mut self, other: &'a Self) { let self_copy = -self.clone(); self.coeffs = (self_copy + other.clone()).coeffs; } @@ -256,16 +256,16 @@ impl SparsePolynomial { #[allow(clippy::or_fun_call)] pub fn mul(&self, other: &Self) -> Self { if self.is_zero() || other.is_zero() { - SparsePolynomial::zero() + Self::zero() } else { let mut result = BTreeMap::new(); - for (i, self_coeff) in self.coeffs.iter() { - for (j, other_coeff) in other.coeffs.iter() { + for (i, self_coeff) in &self.coeffs { + for (j, other_coeff) in &other.coeffs { let cur_coeff = result.entry(i + j).or_insert(F::zero()); *cur_coeff += &(*self_coeff * other_coeff); } } - SparsePolynomial::from_coefficients_vec(result.into_iter().collect()) + Self::from_coefficients_vec(result.into_iter().collect()) } } @@ -301,18 +301,19 @@ impl From> for DensePolynomial { for (i, coeff) in other.coeffs { result[i] = coeff; } - DensePolynomial::from_coefficients_vec(result) + Self::from_coefficients_vec(result) } } impl From> for SparsePolynomial { - fn from(dense_poly: DensePolynomial) -> SparsePolynomial { - SparsePolynomial::from_coefficients_vec( + fn from(dense_poly: DensePolynomial) -> Self { + Self::from_coefficients_vec( dense_poly .coeffs() .iter() .enumerate() - .filter_map(|(i, coeff)| (!coeff.is_zero()).then(|| (i, *coeff))) + .filter(|&(_, coeff)| (!coeff.is_zero())) + .map(|(i, coeff)| (i, *coeff)) .collect(), ) } diff --git a/serialize/src/impls.rs b/serialize/src/impls.rs index aa72cbcb2..fe36faad5 100644 --- a/serialize/src/impls.rs +++ b/serialize/src/impls.rs @@ -26,7 +26,7 @@ impl CanonicalSerialize for bool { mut writer: W, _compress: Compress, ) -> Result<(), SerializationError> { - writer.write(&[*self as u8])?; + writer.write_all(&[*self as u8])?; Ok(()) }