Skip to content

Commit 17c2215

Browse files
authored
Fix clippy (#264)
* fix: clippy * fix: Remove unused code * fix: clippy
1 parent 6bb49ea commit 17c2215

27 files changed

Lines changed: 116 additions & 260 deletions

‎bitvm/src/bigint/inv.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use crate::bigint::BigIntImpl;
2-
use crate::pseudo::OP_NDUP;
32
use crate::treepp::*;
4-
use core::ops::{Mul, Rem, Sub};
5-
use num_bigint::BigUint;
6-
use num_traits::Num;
73

84
impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
95
pub fn div2() -> Script {

‎bitvm/src/bigint/std.rs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
3838
chunk_vec.resize(LIMB_SIZE as usize, false);
3939

4040
let mut elem = 0u32;
41-
for (i, chunk_i) in chunk_vec.iter().enumerate() {
42-
if *chunk_i {
41+
for (i, chunk_i) in chunk_vec.into_iter().enumerate() {
42+
if chunk_i {
4343
elem += 1 << i;
4444
}
4545
}
@@ -79,8 +79,8 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
7979
chunk_vec.resize(32, false);
8080

8181
let mut elem = 0u32;
82-
for i in 0..32_usize {
83-
if chunk_vec[i] {
82+
for (i, chunk_i) in chunk_vec.into_iter().enumerate() {
83+
if chunk_i {
8484
elem += 1 << i;
8585
}
8686
}
@@ -343,7 +343,6 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
343343
/// Generates a vector of TransformStep struct that encodes all the information needed to
344344
/// convert BigInt form one limbsize represention (source) to another (target).
345345
/// used as a helper function for `transform_limbsize`
346-
347346
fn get_transform_steps(source_limb_size: u32, target_limb_size: u32) -> Vec<TransformStep> {
348347
//define an empty vector to store Transform steps
349348
let mut transform_steps: Vec<TransformStep> = Vec::new();
@@ -428,7 +427,6 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
428427
/// - If the source or target limb size is greater than number of bits, fails with assertion error.
429428
/// - If the elements do not fit on the stack. (few satck elements are also used for intermediate computation).
430429
/// - The number of bits in the BigInt must be 32 or larger.
431-
432430
pub fn transform_limbsize(source_limb_size: u32, target_limb_size: u32) -> Script {
433431
// ensure that source and target limb sizes are between 0 and 31 inclusive
434432
assert!(

‎bitvm/src/bn254/ell_coeffs.rs‎

Lines changed: 3 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ use ark_ec::bn::g2::G2Prepared as ark_G2Prepared;
66
use ark_ec::bn::{BnConfig, TwistType};
77
use ark_ec::pairing::{MillerLoopOutput, Pairing, PairingOutput};
88
use ark_ec::short_weierstrass::Affine;
9-
use ark_ec::short_weierstrass::SWCurveConfig;
109
use ark_ec::{AffineRepr, CurveGroup};
1110
use ark_ff::Field;
1211
use ark_ff::{AdditiveGroup, CyclotomicMultSubgroup};
13-
use ark_std::cfg_chunks_mut;
1412
use itertools::Itertools;
1513
use num_traits::One;
1614

@@ -27,63 +25,6 @@ pub struct G2Prepared {
2725
// aka. line in miller loop.
2826
pub type EllCoeff = (ark_bn254::Fq2, ark_bn254::Fq2, ark_bn254::Fq2);
2927

30-
#[derive(Clone, Copy, Debug)]
31-
struct G2HomProjective {
32-
x: ark_bn254::Fq2,
33-
y: ark_bn254::Fq2,
34-
z: ark_bn254::Fq2,
35-
}
36-
37-
impl G2HomProjective {
38-
fn double_in_place(&mut self, two_inv: &ark_bn254::Fq) -> EllCoeff {
39-
// Formula for line function when working with
40-
// homogeneous projective coordinates.
41-
42-
let mut a = self.x * self.y;
43-
a.mul_assign_by_fp(two_inv);
44-
let b = self.y.square();
45-
let c = self.z.square();
46-
let e = ark_bn254::g2::Config::COEFF_B * (c.double() + c);
47-
let f = e.double() + e;
48-
let mut g = b + f;
49-
g.mul_assign_by_fp(two_inv);
50-
let h = (self.y + self.z).square() - (b + c);
51-
let i = e - b;
52-
let j = self.x.square();
53-
let e_square = e.square();
54-
55-
self.x = a * (b - f);
56-
self.y = g.square() - (e_square.double() + e_square);
57-
self.z = b * h;
58-
match ark_bn254::Config::TWIST_TYPE {
59-
TwistType::M => (i, j.double() + j, -h),
60-
TwistType::D => (-h, j.double() + j, i),
61-
}
62-
}
63-
64-
fn add_in_place(&mut self, q: &ark_bn254::G2Affine) -> EllCoeff {
65-
// Formula for line function when working with
66-
// homogeneous projective coordinates.
67-
let theta = self.y - (q.y * self.z);
68-
let lambda = self.x - (q.x * self.z);
69-
let c = theta.square();
70-
let d = lambda.square();
71-
let e = lambda * d;
72-
let f = self.z * c;
73-
let g = self.x * d;
74-
let h = e + f - g.double();
75-
self.x = lambda * h;
76-
self.y = theta * (g - h) - (e * self.y);
77-
self.z *= e;
78-
let j = theta * q.x - (lambda * q.y);
79-
80-
match ark_bn254::Config::TWIST_TYPE {
81-
TwistType::M => (j, -theta, lambda),
82-
TwistType::D => (lambda, -theta, j),
83-
}
84-
}
85-
}
86-
8728
impl Default for G2Prepared {
8829
fn default() -> Self {
8930
Self::from(ark_bn254::G2Affine::generator())
@@ -323,7 +264,8 @@ impl AffinePairing for BnAffinePairing {
323264
})
324265
.collect::<Vec<_>>();
325266

326-
let mut f = cfg_chunks_mut!(pairs, 4)
267+
let mut f = pairs
268+
.chunks_mut(4)
327269
.map(|pairs| {
328270
let mut f = ark_bn254::Fq12::one();
329271
for i in (1..Config::ATE_LOOP_COUNT.len()).rev() {
@@ -381,43 +323,8 @@ impl AffinePairing for BnAffinePairing {
381323
#[cfg(test)]
382324
mod tests {
383325
use super::*;
384-
use ark_bn254::{Fq, Fq2};
385-
386-
use ark_ec::short_weierstrass::SWCurveConfig;
387-
use ark_ff::{Field, UniformRand};
326+
use ark_ff::UniformRand;
388327
use ark_std::test_rng;
389-
use num_traits::One;
390-
391-
use super::G2HomProjective;
392-
use ark_ff::AdditiveGroup;
393-
394-
#[test]
395-
fn test_double_in_place() {
396-
let mut rng = test_rng();
397-
let two_inv = Fq::one().double().inverse().unwrap();
398-
let mut r = G2HomProjective {
399-
x: Fq2::rand(&mut rng),
400-
y: Fq2::rand(&mut rng),
401-
z: Fq2::rand(&mut rng),
402-
};
403-
404-
println!("1/2 = {:?}\n\n", two_inv.to_string());
405-
println!("COEFF_B = {}\n\n", ark_bn254::g2::Config::COEFF_B);
406-
println!("before double line:");
407-
println!("r.x = {:?}", r.x.to_string());
408-
println!("r.y = {:?}", r.y.to_string());
409-
println!("r.z = {:?}\n\n", r.z.to_string());
410-
411-
let s = r.double_in_place(&two_inv);
412-
413-
println!("after double line:");
414-
println!("r.x = {:?}", r.x.to_string());
415-
println!("r.y = {:?}", r.y.to_string());
416-
println!("r.z = {:?}", r.z.to_string());
417-
println!("s.0 = {:?}", s.0.to_string());
418-
println!("s.1 = {:?}", s.1.to_string());
419-
println!("s.2 = {:?}", s.2.to_string());
420-
}
421328

422329
#[test]
423330
fn test_affine_vs_projective() {

‎bitvm/src/bn254/msm.rs‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) fn dfs_with_constant_mul(
7979
index: u32,
8080
depth: u32,
8181
mask: u32,
82-
p_mul: &Vec<ark_bn254::G1Affine>,
82+
p_mul: &[ark_bn254::G1Affine],
8383
) -> Script {
8484
if depth == 0 {
8585
return script! {
@@ -115,7 +115,7 @@ fn generate_lookup_tables(
115115
q: ark_bn254::G1Affine,
116116
window: usize,
117117
) -> (Vec<Vec<ark_bn254::G1Affine>>, Vec<Script>) {
118-
let num_tables = (Fr::N_BITS as usize + window - 1) / window;
118+
let num_tables = (Fr::N_BITS as usize).div_ceil(window);
119119

120120
let mut all_tables_scr = vec![];
121121
let mut all_tables = vec![];
@@ -147,7 +147,7 @@ fn get_query_for_table_index(
147147
window: usize,
148148
table_index: usize,
149149
) -> (u32, Script) {
150-
let num_tables: u32 = (Fr::N_BITS + window as u32 - 1) / window as u32;
150+
let num_tables: u32 = Fr::N_BITS.div_ceil(window as u32);
151151
// Split Scalar into bits and group window size
152152
let chunks = fq_to_bits(scalar.into_bigint(), window); // {a_0, ..,a_N}
153153
// Get Scalar slice (w-bit segment) at index position i.e. a_i
@@ -209,7 +209,7 @@ fn accumulate_addition_chain_for_a_scalar_mul(
209209
) -> Vec<(ark_bn254::G1Affine, Script, Vec<Hint>)> {
210210
let mut all_tables_result: Vec<(ark_bn254::G1Affine, Script, Vec<Hint>)> = vec![];
211211

212-
let num_tables = (Fr::N_BITS as usize + window - 1) / window;
212+
let num_tables = (Fr::N_BITS as usize).div_ceil(window);
213213
let tables = generate_lookup_tables(base, window);
214214

215215
let mut prev = init_acc;
@@ -306,8 +306,8 @@ mod test {
306306
for _ in 0..5 {
307307
let fq = ark_bn254::Fr::rand(&mut prng);
308308
let window = (u32::rand(&mut prng) % WINDOW_G1_MSM) + 1;
309-
let num_tables: u32 = (Fr::N_BITS + window as u32 - 1) / window as u32;
310-
let random_index = u32::rand(&mut prng) % num_tables as u32;
309+
let num_tables: u32 = Fr::N_BITS.div_ceil(window);
310+
let random_index = u32::rand(&mut prng) % num_tables;
311311
let (value, slice_scr) =
312312
get_query_for_table_index(fq, window as usize, random_index as usize);
313313

@@ -353,8 +353,7 @@ mod test {
353353

354354
let fq = ark_bn254::Fr::rand(&mut prng);
355355

356-
let (value, slice_scr) =
357-
get_query_for_table_index(fq, window as usize, table_index as usize);
356+
let (value, slice_scr) = get_query_for_table_index(fq, window, table_index as usize);
358357

359358
let selected_table = (
360359
tables.0[table_index as usize].clone(),

‎bitvm/src/chunk/api.rs‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,25 @@ pub mod type_conversion_utils {
8686
}
8787
}
8888

89-
pub fn utils_signatures_from_raw_witnesses(raw_wits: &Vec<RawWitness>) -> Signatures {
89+
#[allow(clippy::needless_range_loop)]
90+
pub fn utils_signatures_from_raw_witnesses(raw_wits: &[RawWitness]) -> Signatures {
9091
assert_eq!(raw_wits.len(), NUM_PUBS + NUM_U256 + NUM_HASH);
9192
let mut asigs = vec![];
9293
for i in 0..NUM_PUBS {
93-
let a: wots256::Signature =
94-
wots256::raw_witness_to_signature(&Witness::from_slice(&raw_wits[i]))
95-
.try_into()
96-
.unwrap();
94+
let a = wots256::raw_witness_to_signature(&Witness::from_slice(&raw_wits[i]));
9795
asigs.push(a);
9896
}
9997
let mut bsigs = vec![];
10098
for i in 0..NUM_U256 {
101-
let a: wots256::Signature =
102-
wots256::raw_witness_to_signature(&Witness::from_slice(&raw_wits[i + NUM_PUBS]))
103-
.try_into()
104-
.unwrap();
99+
let a =
100+
wots256::raw_witness_to_signature(&Witness::from_slice(&raw_wits[i + NUM_PUBS]));
105101
bsigs.push(a);
106102
}
107103
let mut csigs = vec![];
108104
for i in 0..NUM_HASH {
109-
let a: wots_hash::Signature = wots_hash::raw_witness_to_signature(
110-
&Witness::from_slice(&raw_wits[i + NUM_PUBS + NUM_U256]),
111-
)
112-
.try_into()
113-
.unwrap();
105+
let a = wots_hash::raw_witness_to_signature(&Witness::from_slice(
106+
&raw_wits[i + NUM_PUBS + NUM_U256],
107+
));
114108
csigs.push(a);
115109
}
116110
let asigs = asigs.try_into().unwrap();
@@ -155,8 +149,7 @@ pub mod type_conversion_utils {
155149
let mut apubs = vec![];
156150
let mut bpubs = vec![];
157151
let mut cpubs = vec![];
158-
for idx in 0..commits_public_keys.len() {
159-
let f = commits_public_keys[idx];
152+
for (idx, f) in commits_public_keys.into_iter().enumerate() {
160153
if idx < NUM_PUBS {
161154
let p: wots256::PublicKey = f.public_key.clone().try_into().unwrap();
162155
apubs.push(p);
@@ -420,6 +413,7 @@ mod test {
420413
Ok(())
421414
}
422415

416+
#[allow(clippy::type_complexity)]
423417
pub(crate) fn read_map_from_file(
424418
filename: &str,
425419
) -> Result<HashMap<u32, Vec<Vec<u8>>>, Box<dyn Error>> {
@@ -488,6 +482,7 @@ mod test {
488482
write_map_to_file(&obj, filename).unwrap();
489483
}
490484

485+
#[allow(clippy::needless_range_loop)]
491486
pub fn read_asserts_from_file(filename: &str) -> Assertions {
492487
let res = read_map_from_file(filename).unwrap();
493488
let proof_vec = res.get(&0).unwrap();
@@ -769,6 +764,8 @@ mod test {
769764
assert!(res.success);
770765
println!("DONE");
771766
}
767+
768+
#[allow(clippy::needless_range_loop)]
772769
fn sign_assertions(assn: Assertions) -> Signatures {
773770
let (ps, fs, hs) = (assn.0, assn.1, assn.2);
774771
let secret = MOCK_SECRET;
@@ -845,8 +842,8 @@ mod test {
845842
let partial_scripts = api_generate_partial_script(&mock_vk);
846843

847844
let mut script_cache = HashMap::new();
848-
for i in 0..partial_scripts.len() {
849-
script_cache.insert(i as u32, vec![partial_scripts[i].clone()]);
845+
for (i, script) in partial_scripts.into_iter().enumerate() {
846+
script_cache.insert(i as u32, vec![script]);
850847
}
851848

852849
write_scripts_to_separate_files(script_cache, "tapnode");

‎bitvm/src/chunk/api_compiletime_utils.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub(crate) fn generate_segments_using_mock_vk_and_mock_proof() -> Vec<Segment> {
195195
generate_segments_using_mock_proof(mock_vk, true)
196196
}
197197

198-
pub(crate) fn partial_scripts_from_segments(segments: &Vec<Segment>) -> Vec<treepp::Script> {
198+
pub(crate) fn partial_scripts_from_segments(segments: &[Segment]) -> Vec<treepp::Script> {
199199
fn serialize_element_types(elems: &[ElementType]) -> String {
200200
// 1. Convert each variant to its string representation.
201201
let joined = elems
@@ -236,8 +236,7 @@ pub(crate) fn partial_scripts_from_segments(segments: &Vec<Segment>) -> Vec<tree
236236
});
237237
}
238238

239-
for i in 0..segments.len() {
240-
let seg = &segments[i];
239+
for seg in segments {
241240
let scr_type = seg.scr_type.clone();
242241
if scr_type == ScriptType::NonDeterministic {
243242
continue;
@@ -270,7 +269,7 @@ pub(crate) fn partial_scripts_from_segments(segments: &Vec<Segment>) -> Vec<tree
270269
}
271270

272271
pub(crate) fn bitcom_scripts_from_segments(
273-
segments: &Vec<Segment>,
272+
segments: &[Segment],
274273
wots_pubkeys: PublicKeys,
275274
) -> Vec<treepp::Script> {
276275
let mut bitcom_scripts: Vec<treepp::Script> = vec![];

0 commit comments

Comments
 (0)