diff --git a/.travis.yml b/.travis.yml index 7b3372c3..e99fd798 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,9 @@ rust: - stable before_script: - - rustup component add rustfmt-preview + - rustup component add rustfmt-preview clippy - cargo fmt --all -- --check + - cargo clippy -- -D clippy::all script: - cargo build --verbose diff --git a/examples/diffie_hellman_key_exchange.rs b/examples/diffie_hellman_key_exchange.rs index 946a2f58..a0a8bb4c 100644 --- a/examples/diffie_hellman_key_exchange.rs +++ b/examples/diffie_hellman_key_exchange.rs @@ -37,7 +37,7 @@ where fn main() { let curve_name = std::env::args().nth(1); - match curve_name.as_ref().map(|s| s.as_str()) { + match curve_name.as_deref() { Some("secp256k1") => ecdh::(), Some("ristretto") => ecdh::(), Some("ed25519") => ecdh::(), diff --git a/examples/pedersen_commitment.rs b/examples/pedersen_commitment.rs index 47b00ab8..ee3c6a65 100644 --- a/examples/pedersen_commitment.rs +++ b/examples/pedersen_commitment.rs @@ -1,6 +1,8 @@ use curv::elliptic::curves::traits::ECPoint; use curv::BigInt; +use std::fmt::Debug; + /// Pedesen Commitment: /// compute c = mG + rH /// where m is the commited value, G is the group generator, @@ -14,7 +16,7 @@ use curv::BigInt; pub fn ped_com

(message: &BigInt) where - P: ECPoint, + P: ECPoint + Debug, { use curv::arithmetic::traits::Samplable; use curv::cryptographic_primitives::commitments::pedersen_commitment::PedersenCommitment; @@ -26,7 +28,11 @@ where message, &blinding_factor, ); - (com, blinding_factor); + + println!( + "\ncreated commitment with user defined randomness \n\n blinding_factor {} \n commitment: {:#?}", + blinding_factor, com + ); } fn main() { @@ -34,7 +40,7 @@ fn main() { let message_bytes = message.as_bytes(); let _message_bn = BigInt::from(message_bytes); let curve_name = std::env::args().nth(1); - match curve_name.as_ref().map(|s| s.as_str()) { + match curve_name.as_deref() { Some("secp256k1") => ped_com::(&_message_bn), Some("ristretto") => ped_com::(&_message_bn), Some("ed25519") => ped_com::(&_message_bn), diff --git a/examples/proof_of_knowledge_of_dlog.rs b/examples/proof_of_knowledge_of_dlog.rs index fa8db34d..0b3b1028 100644 --- a/examples/proof_of_knowledge_of_dlog.rs +++ b/examples/proof_of_knowledge_of_dlog.rs @@ -20,16 +20,12 @@ where let witness: P::Scalar = ECScalar::new_random(); let dlog_proof = DLogProof::

::prove(&witness); - let verified = DLogProof::verify(&dlog_proof); - match verified { - Ok(_t) => assert!(true), - Err(_e) => assert!(false), - } + assert!(DLogProof::verify(&dlog_proof).is_ok()); } fn main() { let curve_name = std::env::args().nth(1); - match curve_name.as_ref().map(|s| s.as_str()) { + match curve_name.as_deref() { Some("secp256k1") => dlog_proof::(), Some("ristretto") => dlog_proof::(), Some("ed25519") => dlog_proof::(), diff --git a/examples/verifiable_secret_sharing.rs b/examples/verifiable_secret_sharing.rs index 94cc7aeb..870770e5 100644 --- a/examples/verifiable_secret_sharing.rs +++ b/examples/verifiable_secret_sharing.rs @@ -25,14 +25,15 @@ where let (vss_scheme, secret_shares) = VerifiableSS::

::share(3, 5, &secret); - let mut shares_vec = Vec::new(); - shares_vec.push(secret_shares[0].clone()); - shares_vec.push(secret_shares[1].clone()); - shares_vec.push(secret_shares[2].clone()); - shares_vec.push(secret_shares[4].clone()); + let shares_vec = vec![ + secret_shares[0].clone(), + secret_shares[1].clone(), + secret_shares[2].clone(), + secret_shares[4].clone(), + ]; //test reconstruction - let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 1, 2, 4], &shares_vec); + let secret_reconstructed = vss_scheme.reconstruct(&[0, 1, 2, 4], &shares_vec); assert_eq!(secret, secret_reconstructed); // test secret shares are verifiable @@ -64,7 +65,7 @@ where fn main() { let curve_name = std::env::args().nth(1); - match curve_name.as_ref().map(|s| s.as_str()) { + match curve_name.as_deref() { Some("secp256k1") => secret_sharing_3_out_of_5::(), Some("ristretto") => { secret_sharing_3_out_of_5::() diff --git a/src/arithmetic/big_gmp.rs b/src/arithmetic/big_gmp.rs index e9aae51f..ea4decc6 100644 --- a/src/arithmetic/big_gmp.rs +++ b/src/arithmetic/big_gmp.rs @@ -14,12 +14,12 @@ @license GPL-3.0+ */ -use super::rand::rngs::OsRng; -use super::rand::RngCore; use super::traits::{ BitManipulation, ConvertFrom, Converter, Modulo, NumberTests, Samplable, ZeroizeBN, EGCD, }; use gmp::mpz::Mpz; +use rand::rngs::OsRng; +use rand::RngCore; use std::borrow::Borrow; use std::sync::atomic; @@ -142,7 +142,7 @@ impl EGCD for Mpz { } impl BitManipulation for Mpz { - fn set_bit(self: &mut Self, bit: usize, bit_val: bool) { + fn set_bit(&mut self, bit: usize, bit_val: bool) { if bit_val { self.setbit(bit); } else { @@ -150,7 +150,7 @@ impl BitManipulation for Mpz { } } - fn test_bit(self: &Self, bit: usize) -> bool { + fn test_bit(&self, bit: usize) -> bool { self.tstbit(bit) } } diff --git a/src/arithmetic/mod.rs b/src/arithmetic/mod.rs index 8c43ffc6..e082a631 100644 --- a/src/arithmetic/mod.rs +++ b/src/arithmetic/mod.rs @@ -14,8 +14,6 @@ @license GPL-3.0+ */ -use rand; - const HEX_RADIX: u8 = 16; pub mod big_gmp; diff --git a/src/arithmetic/traits.rs b/src/arithmetic/traits.rs index b2358f2c..1a2fb27e 100644 --- a/src/arithmetic/traits.rs +++ b/src/arithmetic/traits.rs @@ -55,8 +55,8 @@ where } pub trait BitManipulation { - fn set_bit(self: &mut Self, bit: usize, bit_val: bool); - fn test_bit(self: &Self, bit: usize) -> bool; + fn set_bit(&mut self, bit: usize, bit_val: bool); + fn test_bit(&self, bit: usize) -> bool; } pub trait ConvertFrom { diff --git a/src/cryptographic_primitives/commitments/hash_commitment.rs b/src/cryptographic_primitives/commitments/hash_commitment.rs index 01f607fe..7142ce1d 100644 --- a/src/cryptographic_primitives/commitments/hash_commitment.rs +++ b/src/cryptographic_primitives/commitments/hash_commitment.rs @@ -59,10 +59,10 @@ mod tests { let message = BigInt::sample(SECURITY_BITS); let (commitment, blind_factor) = HashCommitment::create_commitment(&message); if commitment.to_str_radix(2).len() == hex_len { - ctr_commit_len = ctr_commit_len + 1; + ctr_commit_len += 1; } if blind_factor.to_str_radix(2).len() == hex_len { - ctr_blind_len = ctr_blind_len + 1; + ctr_blind_len += 1; } } //test commitment length - works because SHA256 output length the same as sec_bits diff --git a/src/cryptographic_primitives/hashing/blake2b512.rs b/src/cryptographic_primitives/hashing/blake2b512.rs index fcfaef30..e6b99675 100644 --- a/src/cryptographic_primitives/hashing/blake2b512.rs +++ b/src/cryptographic_primitives/hashing/blake2b512.rs @@ -45,8 +45,7 @@ mod tests { #[test] // Very basic test here, TODO: suggest better testing fn create_hash_test() { - let result = - Blake::create_hash(&vec![&BigInt::one(), &BigInt::zero()], b"Zcash_RedJubjubH"); + let result = Blake::create_hash(&[&BigInt::one(), &BigInt::zero()], b"Zcash_RedJubjubH"); assert!(result > BigInt::zero()); } @@ -58,14 +57,11 @@ mod tests { P::Scalar: PartialEq + std::fmt::Debug, { let point = P::base_point2(); - let result1 = - Blake::create_hash_from_ge(&vec![&point, &P::generator()], b"Zcash_RedJubjubH"); + let result1 = Blake::create_hash_from_ge(&[&point, &P::generator()], b"Zcash_RedJubjubH"); assert!(result1.to_big_int().to_str_radix(2).len() > 240); - let result2 = - Blake::create_hash_from_ge(&vec![&P::generator(), &point], b"Zcash_RedJubjubH"); + let result2 = Blake::create_hash_from_ge(&[&P::generator(), &point], b"Zcash_RedJubjubH"); assert_ne!(result1, result2); - let result3 = - Blake::create_hash_from_ge(&vec![&P::generator(), &point], b"Zcash_RedJubjubH"); + let result3 = Blake::create_hash_from_ge(&[&P::generator(), &point], b"Zcash_RedJubjubH"); assert_eq!(result2, result3); } } diff --git a/src/cryptographic_primitives/hashing/hash_sha256.rs b/src/cryptographic_primitives/hashing/hash_sha256.rs index cb073778..54bfbcec 100644 --- a/src/cryptographic_primitives/hashing/hash_sha256.rs +++ b/src/cryptographic_primitives/hashing/hash_sha256.rs @@ -77,14 +77,14 @@ mod tests { // https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing#shavs fn vector_sha256_test() { // Empty Message - let result: BigInt = HSha256::create_hash(&vec![]); + let result: BigInt = HSha256::create_hash(&[]); assert_eq!( result.to_str_radix(16), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ); // 256 bit message - let result: BigInt = HSha256::create_hash(&vec![&BigInt::from_str_radix( + let result: BigInt = HSha256::create_hash(&[&BigInt::from_str_radix( "09fc1accc230a205e4a208e64a8f204291f581a12756392da4b8c0cf5ef02b95", 16, ) @@ -95,7 +95,7 @@ mod tests { ); // 2x128 bit messages - let result: BigInt = HSha256::create_hash(&vec![ + let result: BigInt = HSha256::create_hash(&[ &BigInt::from_str_radix("09fc1accc230a205e4a208e64a8f2042", 16).unwrap(), &BigInt::from_str_radix("91f581a12756392da4b8c0cf5ef02b95", 16).unwrap(), ]); @@ -105,7 +105,7 @@ mod tests { ); // 512 bit message - let result: BigInt = HSha256::create_hash(&vec![&BigInt::from_str_radix("5a86b737eaea8ee976a0a24da63e7ed7eefad18a101c1211e2b3650c5187c2a8a650547208251f6d4237e661c7bf4c77f335390394c37fa1a9f9be836ac28509", 16).unwrap()]); + let result: BigInt = HSha256::create_hash(&[&BigInt::from_str_radix("5a86b737eaea8ee976a0a24da63e7ed7eefad18a101c1211e2b3650c5187c2a8a650547208251f6d4237e661c7bf4c77f335390394c37fa1a9f9be836ac28509", 16).unwrap()]); assert_eq!( result.to_str_radix(16), "42e61e174fbb3897d6dd6cef3dd2802fe67b331953b06114a65c772859dfc1aa" @@ -120,11 +120,11 @@ mod tests { P::Scalar: PartialEq + std::fmt::Debug, { let point = P::base_point2(); - let result1 = HSha256::create_hash_from_ge(&vec![&point, &P::generator()]); + let result1 = HSha256::create_hash_from_ge(&[&point, &P::generator()]); assert!(result1.to_big_int().to_str_radix(2).len() > 240); - let result2 = HSha256::create_hash_from_ge(&vec![&P::generator(), &point]); + let result2 = HSha256::create_hash_from_ge(&[&P::generator(), &point]); assert_ne!(result1, result2); - let result3 = HSha256::create_hash_from_ge(&vec![&P::generator(), &point]); + let result3 = HSha256::create_hash_from_ge(&[&P::generator(), &point]); assert_eq!(result2, result3); } } diff --git a/src/cryptographic_primitives/hashing/hash_sha512.rs b/src/cryptographic_primitives/hashing/hash_sha512.rs index 6b6d8f90..2690988c 100644 --- a/src/cryptographic_primitives/hashing/hash_sha512.rs +++ b/src/cryptographic_primitives/hashing/hash_sha512.rs @@ -60,14 +60,14 @@ mod tests { // https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing#shavs fn vector_sha512_test() { // Empty message - let result: BigInt = HSha512::create_hash(&vec![]); + let result: BigInt = HSha512::create_hash(&[]); assert_eq!( result.to_str_radix(16), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" ); // 2x256 bit message - let result: BigInt = HSha512::create_hash(&vec![ + let result: BigInt = HSha512::create_hash(&[ &BigInt::from_str_radix( "c1ca70ae1279ba0b918157558b4920d6b7fba8a06be515170f202fafd36fb7f7", 16, @@ -85,7 +85,7 @@ mod tests { ); // 512 bit message - let result: BigInt = HSha512::create_hash(&vec![&BigInt::from_str_radix( + let result: BigInt = HSha512::create_hash(&[&BigInt::from_str_radix( "c1ca70ae1279ba0b918157558b4920d6b7fba8a06be515170f202fafd36fb7f79d69fad745dba6150568db1e2b728504113eeac34f527fc82f2200b462ecbf5d", 16, ) @@ -96,7 +96,7 @@ mod tests { ); // 1024 bit message - let result: BigInt = HSha512::create_hash(&vec![&BigInt::from_str_radix("fd2203e467574e834ab07c9097ae164532f24be1eb5d88f1af7748ceff0d2c67a21f4e4097f9d3bb4e9fbf97186e0db6db0100230a52b453d421f8ab9c9a6043aa3295ea20d2f06a2f37470d8a99075f1b8a8336f6228cf08b5942fc1fb4299c7d2480e8e82bce175540bdfad7752bc95b577f229515394f3ae5cec870a4b2f8", 16).unwrap()]); + let result: BigInt = HSha512::create_hash(&[&BigInt::from_str_radix("fd2203e467574e834ab07c9097ae164532f24be1eb5d88f1af7748ceff0d2c67a21f4e4097f9d3bb4e9fbf97186e0db6db0100230a52b453d421f8ab9c9a6043aa3295ea20d2f06a2f37470d8a99075f1b8a8336f6228cf08b5942fc1fb4299c7d2480e8e82bce175540bdfad7752bc95b577f229515394f3ae5cec870a4b2f8", 16).unwrap()]); assert_eq!( result.to_str_radix(16), "a21b1077d52b27ac545af63b32746c6e3c51cb0cb9f281eb9f3580a6d4996d5c9917d2a6e484627a9d5a06fa1b25327a9d710e027387fc3e07d7c4d14c6086cc" @@ -111,11 +111,11 @@ mod tests { P::Scalar: PartialEq + std::fmt::Debug, { let point = P::base_point2(); - let result1 = HSha512::create_hash_from_ge(&vec![&point, &P::generator()]); + let result1 = HSha512::create_hash_from_ge(&[&point, &P::generator()]); assert!(result1.to_big_int().to_str_radix(2).len() > 240); - let result2 = HSha512::create_hash_from_ge(&vec![&P::generator(), &point]); + let result2 = HSha512::create_hash_from_ge(&[&P::generator(), &point]); assert_ne!(result1, result2); - let result3 = HSha512::create_hash_from_ge(&vec![&P::generator(), &point]); + let result3 = HSha512::create_hash_from_ge(&[&P::generator(), &point]); assert_eq!(result2, result3); } } diff --git a/src/cryptographic_primitives/hashing/hmac_sha512.rs b/src/cryptographic_primitives/hashing/hmac_sha512.rs index e090ba94..370582db 100644 --- a/src/cryptographic_primitives/hashing/hmac_sha512.rs +++ b/src/cryptographic_primitives/hashing/hmac_sha512.rs @@ -59,20 +59,20 @@ mod tests { #[test] fn create_hmac_test() { let key = BigInt::sample(512); - let result1 = HMacSha512::create_hmac(&key, &vec![&BigInt::from(10)]); + let result1 = HMacSha512::create_hmac(&key, &[&BigInt::from(10)]); let result1_bytes = &BigInt::to_vec(&result1)[..]; let mut array_result: [u8; 64] = [0u8; 64]; array_result.copy_from_slice(result1_bytes); - assert!(HMacSha512::verify(&key, &vec![&BigInt::from(10)], array_result).is_ok()); + assert!(HMacSha512::verify(&key, &[&BigInt::from(10)], array_result).is_ok()); let key2 = BigInt::sample(512); // same data , different key - let result2 = HMacSha512::create_hmac(&key2, &vec![&BigInt::from(10)]); + let result2 = HMacSha512::create_hmac(&key2, &[&BigInt::from(10)]); assert_ne!(result1, result2); // same key , different data - let result3 = HMacSha512::create_hmac(&key, &vec![&BigInt::from(10), &BigInt::from(11)]); + let result3 = HMacSha512::create_hmac(&key, &[&BigInt::from(10), &BigInt::from(11)]); assert_ne!(result1, result3); // same key, same data - let result4 = HMacSha512::create_hmac(&key, &vec![&BigInt::from(10)]); + let result4 = HMacSha512::create_hmac(&key, &[&BigInt::from(10)]); assert_eq!(result1, result4) } } diff --git a/src/cryptographic_primitives/hashing/merkle_tree.rs b/src/cryptographic_primitives/hashing/merkle_tree.rs index 10f53a24..3456a181 100644 --- a/src/cryptographic_primitives/hashing/merkle_tree.rs +++ b/src/cryptographic_primitives/hashing/merkle_tree.rs @@ -55,6 +55,7 @@ impl MT256

{ MerkleTree::root_hash(&self.tree) } + #[allow(clippy::result_unit_err)] pub fn validate_proof(proof: &Proof<[u8; 32]>, root: &[u8]) -> Result<(), ()> { if Proof::validate::<[u8; 32]>(proof, root) { Ok(()) diff --git a/src/cryptographic_primitives/hashing/traits.rs b/src/cryptographic_primitives/hashing/traits.rs index a29677af..d2d3dd1f 100644 --- a/src/cryptographic_primitives/hashing/traits.rs +++ b/src/cryptographic_primitives/hashing/traits.rs @@ -16,5 +16,6 @@ pub trait Hash { pub trait KeyedHash { fn create_hmac(key: &BigInt, data: &[&BigInt]) -> BigInt; + #[allow(clippy::result_unit_err)] fn verify(key: &BigInt, data: &[&BigInt], code_bytes: [u8; 64]) -> Result<(), ()>; } diff --git a/src/cryptographic_primitives/proofs/sigma_correct_homomorphic_elgamal_enc.rs b/src/cryptographic_primitives/proofs/sigma_correct_homomorphic_elgamal_enc.rs index 542f51f3..e35568ca 100644 --- a/src/cryptographic_primitives/proofs/sigma_correct_homomorphic_elgamal_enc.rs +++ b/src/cryptographic_primitives/proofs/sigma_correct_homomorphic_elgamal_enc.rs @@ -107,7 +107,7 @@ mod tests { let h: P::Scalar = ECScalar::new_random(); let H = G.clone() * h; let y: P::Scalar = ECScalar::new_random(); - let Y = G.clone() * y.clone(); + let Y = G.clone() * y; let D = H.clone() * witness.x.clone() + Y.clone() * witness.r.clone(); let E = G.clone() * witness.r.clone(); let delta = HomoElGamalStatement { G, H, Y, D, E }; diff --git a/src/cryptographic_primitives/proofs/sigma_dlog.rs b/src/cryptographic_primitives/proofs/sigma_dlog.rs index 1f758ebd..e888f8e5 100644 --- a/src/cryptographic_primitives/proofs/sigma_dlog.rs +++ b/src/cryptographic_primitives/proofs/sigma_dlog.rs @@ -96,10 +96,6 @@ mod tests { { let witness: P::Scalar = ECScalar::new_random(); let dlog_proof = DLogProof::

::prove(&witness); - let verified = DLogProof::verify(&dlog_proof); - match verified { - Ok(_t) => assert!(true), - Err(_e) => assert!(false), - } + assert!(DLogProof::verify(&dlog_proof).is_ok()); } } diff --git a/src/cryptographic_primitives/proofs/sigma_ec_ddh.rs b/src/cryptographic_primitives/proofs/sigma_ec_ddh.rs index 0aedc3a9..c29b159e 100644 --- a/src/cryptographic_primitives/proofs/sigma_ec_ddh.rs +++ b/src/cryptographic_primitives/proofs/sigma_ec_ddh.rs @@ -66,7 +66,7 @@ where let z_g1 = delta.g1.clone() * self.z.clone(); let z_g2 = delta.g2.clone() * self.z.clone(); let a1_plus_e_h1 = self.a1.clone() + delta.h1.clone() * e.clone(); - let a2_plus_e_h2 = self.a2.clone() + delta.h2.clone() * e.clone(); + let a2_plus_e_h2 = self.a2.clone() + delta.h2.clone() * e; if z_g1 == a1_plus_e_h1 && z_g2 == a2_plus_e_h2 { Ok(()) } else { @@ -112,7 +112,7 @@ mod tests { let g2: P = ECPoint::base_point2(); let x2: P::Scalar = ECScalar::new_random(); let h1 = g1.clone() * x.clone(); - let h2 = g2.clone() * x2.clone(); + let h2 = g2.clone() * x2; let delta = ECDDHStatement { g1, g2, h1, h2 }; let w = ECDDHWitness { x }; let proof = ECDDHProof::prove(&w, &delta); diff --git a/src/cryptographic_primitives/proofs/sigma_valid_pedersen.rs b/src/cryptographic_primitives/proofs/sigma_valid_pedersen.rs index e5684d18..763bb0d7 100644 --- a/src/cryptographic_primitives/proofs/sigma_valid_pedersen.rs +++ b/src/cryptographic_primitives/proofs/sigma_valid_pedersen.rs @@ -37,6 +37,7 @@ where P: ECPoint + Clone, P::Scalar: Zeroize, { + #[allow(clippy::many_single_char_names)] pub fn prove(m: &P::Scalar, r: &P::Scalar) -> PedersenProof

{ let g: P = ECPoint::generator(); let h: P = ECPoint::base_point2(); diff --git a/src/cryptographic_primitives/proofs/sigma_valid_pedersen_blind.rs b/src/cryptographic_primitives/proofs/sigma_valid_pedersen_blind.rs index 1f55a4fb..a9d3c6fe 100644 --- a/src/cryptographic_primitives/proofs/sigma_valid_pedersen_blind.rs +++ b/src/cryptographic_primitives/proofs/sigma_valid_pedersen_blind.rs @@ -36,6 +36,7 @@ where P: ECPoint + Clone, P::Scalar: Zeroize + Clone, { + #[allow(clippy::many_single_char_names)] //TODO: add self verification to prover proof pub fn prove(m: &P::Scalar, r: &P::Scalar) -> PedersenBlindingProof

{ let h: P = ECPoint::base_point2(); diff --git a/src/cryptographic_primitives/secret_sharing/feldman_vss.rs b/src/cryptographic_primitives/secret_sharing/feldman_vss.rs index 08d729c4..e119c636 100644 --- a/src/cryptographic_primitives/secret_sharing/feldman_vss.rs +++ b/src/cryptographic_primitives/secret_sharing/feldman_vss.rs @@ -58,22 +58,20 @@ where ) } - // takes given VSS and generates a new VSS for the same secret and a secret shares vector to match the new coomitments + // takes given VSS and generates a new VSS for the same secret and a secret shares vector to match the new commitments pub fn reshare(&self) -> (VerifiableSS

, Vec) { let one: P::Scalar = ECScalar::from(&BigInt::one()); - let poly = VerifiableSS::

::sample_polynomial(self.parameters.threshold.clone(), &one); - let index_vec: Vec = (1..=self.parameters.share_count.clone()).collect(); + let poly = VerifiableSS::

::sample_polynomial(self.parameters.threshold, &one); + let index_vec: Vec = (1..=self.parameters.share_count).collect(); let secret_shares_biased = VerifiableSS::

::evaluate_polynomial(&poly, &index_vec); let secret_shares: Vec<_> = (0..secret_shares_biased.len()) .map(|i| secret_shares_biased[i].sub(&one.get_element())) .collect(); let G: P = ECPoint::generator(); - let mut new_commitments = Vec::new(); - new_commitments.push(self.commitments[0].clone()); - for i in 1..poly.len() { - new_commitments.push((G.clone() * poly[i].clone()) + self.commitments[i].clone()) + let mut new_commitments = vec![self.commitments[0].clone()]; + for (poly, commitment) in poly.iter().zip(&self.commitments).skip(1) { + new_commitments.push((G.clone() * poly.clone()) + commitment.clone()) } - ( VerifiableSS { parameters: self.parameters.clone(), @@ -150,7 +148,7 @@ where let points = indices .iter() .map(|i| { - let index_bn = BigInt::from(*i as u32 + 1 as u32); + let index_bn = BigInt::from(*i as u32 + 1); ECScalar::from(&index_bn) }) .collect::>(); @@ -224,10 +222,9 @@ where let mut comm_iterator = self.commitments.iter().rev(); let head = comm_iterator.next().unwrap(); let tail = comm_iterator; - let comm_to_point = tail.fold(head.clone(), |acc, x: &P| { + tail.fold(head.clone(), |acc, x: &P| { x.clone() + acc * index_fe.clone() - }); - comm_to_point.clone() + }) } //compute \lambda_{index,S}, a lagrangian coefficient that change the (t,n) scheme to (|S|,|S|) @@ -242,7 +239,7 @@ where // add one to indices to get points let points: Vec = (0..params.share_count) .map(|i| { - let index_bn = BigInt::from(i as u32 + 1 as u32); + let index_bn = BigInt::from(i as u32 + 1); ECScalar::from(&index_bn) }) .collect(); @@ -287,14 +284,16 @@ mod tests { let (vss_scheme, secret_shares) = VerifiableSS::

::share_at_indices(3, 5, &secret, &parties); - let mut shares_vec = Vec::new(); - shares_vec.push(secret_shares[0].clone()); - shares_vec.push(secret_shares[1].clone()); - shares_vec.push(secret_shares[3].clone()); - shares_vec.push(secret_shares[4].clone()); + let shares_vec = vec![ + secret_shares[0].clone(), + secret_shares[1].clone(), + secret_shares[3].clone(), + secret_shares[4].clone(), + ]; + //test reconstruction - let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 1, 4, 5], &shares_vec); + let secret_reconstructed = vss_scheme.reconstruct(&[0, 1, 4, 5], &shares_vec); assert_eq!(secret, secret_reconstructed); } @@ -309,14 +308,16 @@ mod tests { let (vss_scheme, secret_shares) = VerifiableSS::

::share(3, 5, &secret); - let mut shares_vec = Vec::new(); - shares_vec.push(secret_shares[0].clone()); - shares_vec.push(secret_shares[1].clone()); - shares_vec.push(secret_shares[2].clone()); - shares_vec.push(secret_shares[4].clone()); + let shares_vec = vec![ + secret_shares[0].clone(), + secret_shares[1].clone(), + secret_shares[2].clone(), + secret_shares[4].clone(), + ]; + //test reconstruction - let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 1, 2, 4], &shares_vec); + let secret_reconstructed = vss_scheme.reconstruct(&[0, 1, 2, 4], &shares_vec); assert_eq!(secret, secret_reconstructed); // test secret shares are verifiable @@ -356,14 +357,15 @@ mod tests { let (vss_scheme, secret_shares) = VerifiableSS::

::share(3, 7, &secret); - let mut shares_vec = Vec::new(); - shares_vec.push(secret_shares[0].clone()); - shares_vec.push(secret_shares[6].clone()); - shares_vec.push(secret_shares[2].clone()); - shares_vec.push(secret_shares[4].clone()); + let shares_vec = vec![ + secret_shares[0].clone(), + secret_shares[6].clone(), + secret_shares[2].clone(), + secret_shares[4].clone(), + ]; //test reconstruction - let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 6, 2, 4], &shares_vec); + let secret_reconstructed = vss_scheme.reconstruct(&[0, 6, 2, 4], &shares_vec); assert_eq!(secret, secret_reconstructed); // test secret shares are verifiable @@ -399,12 +401,10 @@ mod tests { let (vss_scheme, secret_shares) = VerifiableSS::

::share(1, 2, &secret); - let mut shares_vec = Vec::new(); - shares_vec.push(secret_shares[0].clone()); - shares_vec.push(secret_shares[1].clone()); + let shares_vec = vec![secret_shares[0].clone(), secret_shares[1].clone()]; //test reconstruction - let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 1], &shares_vec); + let secret_reconstructed = vss_scheme.reconstruct(&[0, 1], &shares_vec); assert_eq!(secret, secret_reconstructed); // test secret shares are verifiable @@ -432,9 +432,7 @@ mod tests { let (vss_scheme, secret_shares) = VerifiableSS::

::share(1, 3, &secret); - let mut shares_vec = Vec::new(); - shares_vec.push(secret_shares[0].clone()); - shares_vec.push(secret_shares[1].clone()); + let shares_vec = vec![secret_shares[0].clone(), secret_shares[1].clone()]; // test commitment to point and sum of commitments let (vss_scheme2, secret_shares2) = VerifiableSS::

::share(1, 3, &secret); @@ -443,14 +441,14 @@ mod tests { let point_comm2 = vss_scheme.get_point_commitment(2); let g: P = ECPoint::generator(); let g_sum = g.clone() * sum; - assert_eq!(g.clone() * secret_shares[0].clone(), point_comm1.clone()); - assert_eq!(g.clone() * secret_shares[1].clone(), point_comm2.clone()); + assert_eq!(g.clone() * secret_shares[0].clone(), point_comm1); + assert_eq!(g * secret_shares[1].clone(), point_comm2); let point1_sum_com = vss_scheme.get_point_commitment(1) + vss_scheme2.get_point_commitment(1); assert_eq!(point1_sum_com, g_sum); //test reconstruction - let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 1], &shares_vec); + let secret_reconstructed = vss_scheme.reconstruct(&[0, 1], &shares_vec); assert_eq!(secret, secret_reconstructed); // test secret shares are verifiable @@ -484,12 +482,10 @@ mod tests { let new_share_party_2 = secret_shares[1].clone() + zero_secret_shares[1].clone(); let new_share_party_3 = secret_shares[2].clone() + zero_secret_shares[2].clone(); - let mut shares_vec = Vec::new(); - shares_vec.push(new_share_party_1.clone()); - shares_vec.push(new_share_party_3.clone()); + let shares_vec = vec![new_share_party_1.clone(), new_share_party_3.clone()]; // reconstruction - let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 2], &shares_vec); + let secret_reconstructed = vss_scheme.reconstruct(&[0, 2], &shares_vec); assert_eq!(secret, secret_reconstructed); // test secret shares are verifiable diff --git a/src/elliptic/curves/bls12_381/g1.rs b/src/elliptic/curves/bls12_381/g1.rs index 849b11a6..49001df1 100644 --- a/src/elliptic/curves/bls12_381/g1.rs +++ b/src/elliptic/curves/bls12_381/g1.rs @@ -86,7 +86,7 @@ impl ECScalar for FieldScalar { } fn get_element(&self) -> SK { - self.fe.clone() + self.fe } fn set_element(&mut self, element: SK) { self.fe = element @@ -117,7 +117,7 @@ impl ECScalar for FieldScalar { fn to_big_int(&self) -> BigInt { let tmp = self.fe.into_repr(); - let scalar_u64 = tmp.as_ref().clone(); + let scalar_u64 = tmp.as_ref(); let to_bn = scalar_u64.iter().rev().fold(BigInt::zero(), |acc, x| { let element_bn = BigInt::from(*x); @@ -143,7 +143,7 @@ impl ECScalar for FieldScalar { fn add(&self, other: &SK) -> FieldScalar { let mut add_fe = FieldScalar { purpose: "other add", - fe: other.clone(), + fe: *other, }; add_fe.fe.add_assign(&self.fe); FieldScalar { @@ -155,7 +155,7 @@ impl ECScalar for FieldScalar { fn mul(&self, other: &SK) -> FieldScalar { let mut mul_fe = FieldScalar { purpose: "other mul", - fe: other.clone(), + fe: *other, }; mul_fe.fe.mul_assign(&self.fe); FieldScalar { @@ -165,7 +165,7 @@ impl ECScalar for FieldScalar { } fn sub(&self, other: &SK) -> FieldScalar { - let mut other_neg = other.clone(); + let mut other_neg = *other; other_neg.negate(); let sub_fe = FieldScalar { purpose: "other sub", @@ -175,13 +175,13 @@ impl ECScalar for FieldScalar { } fn invert(&self) -> FieldScalar { - let sc = self.fe.clone(); + let sc = self.fe; let inv_sc = sc.inverse().unwrap(); //TODO - let inv_fe = FieldScalar { + + FieldScalar { purpose: "inverse", fe: inv_sc, - }; - inv_fe + } } } @@ -317,11 +317,11 @@ impl ECPoint for G1Point { } fn get_element(&self) -> PK { - self.ge.clone() + self.ge } fn x_coor(&self) -> Option { - let tmp = G1Uncompressed::from_affine(self.ge.clone()); + let tmp = G1Uncompressed::from_affine(self.ge); let bytes = tmp.as_ref(); let x_coor = &bytes[0..COMPRESSED_SIZE]; let bn = BigInt::from(x_coor); @@ -329,7 +329,7 @@ impl ECPoint for G1Point { } fn y_coor(&self) -> Option { - let tmp = G1Uncompressed::from_affine(self.ge.clone()); + let tmp = G1Uncompressed::from_affine(self.ge); let bytes = tmp.as_ref(); let y_coor = &bytes[COMPRESSED_SIZE..COMPRESSED_SIZE * 2]; let bn = BigInt::from(y_coor); @@ -337,10 +337,9 @@ impl ECPoint for G1Point { } fn bytes_compressed_to_big_int(&self) -> BigInt { - let tmp = G1Compressed::from_affine(self.ge.clone()); + let tmp = G1Compressed::from_affine(self.ge); let bytes = tmp.as_ref(); - let bn = BigInt::from(&bytes[..]); - bn + BigInt::from(&bytes[..]) } fn from_bytes(bytes: &[u8]) -> Result { @@ -359,7 +358,8 @@ impl ECPoint for G1Point { purpose: "from_bytes", ge: g1_comp.into_affine(), //TODO: handle error }; - return Ok(pk); + + Ok(pk) } // in this case the opposite of from_bytes: takes compressed pk to COMPRESSED_SIZE bytes. @@ -552,9 +552,6 @@ impl G1Point { #[cfg(test)] mod tests { - use bincode; - use serde_json; - use pairing_plus::bls12_381::{G1Uncompressed, G1}; use pairing_plus::hash_to_curve::HashToCurve; use pairing_plus::hash_to_field::ExpandMsgXmd; @@ -591,6 +588,7 @@ mod tests { #[test] #[should_panic] + #[allow(clippy::op_ref)] // Enables type inference. fn test_serdes_bad_pk() { let pk = GE::generator(); let s = serde_json::to_string(&pk).expect("Failed in serialization"); @@ -616,9 +614,9 @@ mod tests { let a_minus_b_fe: FE = a.sub(&b.get_element()); let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_minus_b_fe; - let point_a = &base * &a; - let point_b = &base * &b; + let point_ab1 = base * a_minus_b_fe; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.sub_point(&point_b.get_element()); println!( "point ab1: {:?}", @@ -636,11 +634,11 @@ mod tests { fn test_add_point() { let a: FE = ECScalar::new_random(); let b: FE = ECScalar::new_random(); - let a_plus_b_fe = a.clone() + &b; + let a_plus_b_fe = a + b; let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_plus_b_fe; - let point_a = &base * &a; - let point_b = &base * &b; + let point_ab1 = base * a_plus_b_fe; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.add_point(&point_b.get_element()); assert_eq!(point_ab1, point_ab2); @@ -650,7 +648,7 @@ mod tests { fn test_add_scalar() { let a: FE = ECScalar::new_random(); let zero: FE = FE::zero(); - let a_plus_zero: FE = a.clone() + zero; + let a_plus_zero: FE = a + zero; assert_eq!(a_plus_zero, a); } @@ -679,10 +677,10 @@ mod tests { fn test_mul_point() { let a: FE = ECScalar::new_random(); let b: FE = ECScalar::new_random(); - let a_mul_b_fe = a.clone() * &b; + let a_mul_b_fe = a * b; let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_mul_b_fe; - let point_a = &base * &a; + let point_ab1 = base * a_mul_b_fe; + let point_a = base * a; let point_ab2 = point_a.scalar_mul(&b.get_element()); assert_eq!(point_ab1, point_ab2); @@ -706,7 +704,7 @@ mod tests { let g: GE = ECPoint::generator(); let fe: FE = ECScalar::from(&BigInt::from(1)); - let b_tag = &g * &fe; + let b_tag = g * fe; assert_eq!(b_tag, g); } diff --git a/src/elliptic/curves/bls12_381/g2.rs b/src/elliptic/curves/bls12_381/g2.rs index 842a255f..36103d41 100644 --- a/src/elliptic/curves/bls12_381/g2.rs +++ b/src/elliptic/curves/bls12_381/g2.rs @@ -89,7 +89,7 @@ impl ECScalar for FieldScalar { } fn get_element(&self) -> SK { - self.fe.clone() + self.fe } fn set_element(&mut self, element: SK) { self.fe = element @@ -120,7 +120,7 @@ impl ECScalar for FieldScalar { fn to_big_int(&self) -> BigInt { let tmp = self.fe.into_repr(); - let scalar_u64 = tmp.as_ref().clone(); + let scalar_u64 = tmp.as_ref(); let to_bn = scalar_u64.iter().rev().fold(BigInt::zero(), |acc, x| { let element_bn = BigInt::from(*x); @@ -146,7 +146,7 @@ impl ECScalar for FieldScalar { fn add(&self, other: &SK) -> FieldScalar { let mut add_fe = FieldScalar { purpose: "other add", - fe: other.clone(), + fe: *other, }; add_fe.fe.add_assign(&self.fe); FieldScalar { @@ -158,7 +158,7 @@ impl ECScalar for FieldScalar { fn mul(&self, other: &SK) -> FieldScalar { let mut mul_fe = FieldScalar { purpose: "other mul", - fe: other.clone(), + fe: *other, }; mul_fe.fe.mul_assign(&self.fe); FieldScalar { @@ -168,7 +168,7 @@ impl ECScalar for FieldScalar { } fn sub(&self, other: &SK) -> FieldScalar { - let mut other_neg = other.clone(); + let mut other_neg = *other; other_neg.negate(); let sub_fe = FieldScalar { purpose: "other sub", @@ -178,13 +178,12 @@ impl ECScalar for FieldScalar { } fn invert(&self) -> FieldScalar { - let sc = self.fe.clone(); + let sc = self.fe; let inv_sc = sc.inverse().unwrap(); //TODO - let inv_fe = FieldScalar { + FieldScalar { purpose: "inverse", fe: inv_sc, - }; - inv_fe + } } } @@ -326,11 +325,11 @@ impl ECPoint for G2Point { } fn get_element(&self) -> PK { - self.ge.clone() + self.ge } fn x_coor(&self) -> Option { - let tmp = G2Uncompressed::from_affine(self.ge.clone()); + let tmp = G2Uncompressed::from_affine(self.ge); let bytes = tmp.as_ref(); let x_coor = &bytes[0..COMPRESSED_SIZE]; let bn = BigInt::from(x_coor); @@ -338,7 +337,7 @@ impl ECPoint for G2Point { } fn y_coor(&self) -> Option { - let tmp = G2Uncompressed::from_affine(self.ge.clone()); + let tmp = G2Uncompressed::from_affine(self.ge); let bytes = tmp.as_ref(); let y_coor = &bytes[COMPRESSED_SIZE..2 * COMPRESSED_SIZE]; let bn = BigInt::from(y_coor); @@ -346,10 +345,9 @@ impl ECPoint for G2Point { } fn bytes_compressed_to_big_int(&self) -> BigInt { - let tmp = G2Compressed::from_affine(self.ge.clone()); + let tmp = G2Compressed::from_affine(self.ge); let bytes = tmp.as_ref(); - let bn = BigInt::from(&bytes[..]); - bn + BigInt::from(&bytes[..]) } fn from_bytes(bytes: &[u8]) -> Result { @@ -369,7 +367,8 @@ impl ECPoint for G2Point { purpose: "from_bytes", ge: g2_comp.into_affine(), //TODO: handle error }; - return Ok(pk); + + Ok(pk) } // in this case the opposite of from_bytes: takes compressed pk to COMPRESSED_SIZE bytes. @@ -562,9 +561,6 @@ impl G2Point { #[cfg(test)] mod tests { - use bincode; - use serde_json; - use pairing_plus::bls12_381::{G2Uncompressed, G2}; use pairing_plus::hash_to_curve::HashToCurve; use pairing_plus::hash_to_field::ExpandMsgXmd; @@ -601,6 +597,7 @@ mod tests { #[test] #[should_panic] + #[allow(clippy::op_ref)] // Enables type inference. fn test_serdes_bad_pk() { let pk = GE::generator(); let s = serde_json::to_string(&pk).expect("Failed in serialization"); @@ -626,9 +623,9 @@ mod tests { let a_minus_b_fe: FE = a.sub(&b.get_element()); let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_minus_b_fe; - let point_a = &base * &a; - let point_b = &base * &b; + let point_ab1 = base * a_minus_b_fe; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.sub_point(&point_b.get_element()); println!( "point ab1: {:?}", @@ -646,11 +643,11 @@ mod tests { fn test_add_point() { let a: FE = ECScalar::new_random(); let b: FE = ECScalar::new_random(); - let a_plus_b_fe = a.clone() + &b; + let a_plus_b_fe = a + b; let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_plus_b_fe; - let point_a = &base * &a; - let point_b = &base * &b; + let point_ab1 = base * a_plus_b_fe; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.add_point(&point_b.get_element()); assert_eq!(point_ab1, point_ab2); @@ -660,7 +657,7 @@ mod tests { fn test_add_scalar() { let a: FE = ECScalar::new_random(); let zero: FE = FE::zero(); - let a_plus_zero: FE = a.clone() + zero; + let a_plus_zero: FE = a + zero; assert_eq!(a_plus_zero, a); } @@ -689,10 +686,10 @@ mod tests { fn test_mul_point() { let a: FE = ECScalar::new_random(); let b: FE = ECScalar::new_random(); - let a_mul_b_fe = a.clone() * &b; + let a_mul_b_fe = a * b; let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_mul_b_fe; - let point_a = &base * &a; + let point_ab1 = base * a_mul_b_fe; + let point_a = base * a; let point_ab2 = point_a.scalar_mul(&b.get_element()); assert_eq!(point_ab1, point_ab2); @@ -715,7 +712,7 @@ mod tests { let g: GE = ECPoint::generator(); let fe: FE = ECScalar::from(&BigInt::from(1)); - let b_tag = &g * &fe; + let b_tag = g * fe; assert_eq!(b_tag, g); } diff --git a/src/elliptic/curves/bls12_381/mod.rs b/src/elliptic/curves/bls12_381/mod.rs index a7523315..b410c7f3 100644 --- a/src/elliptic/curves/bls12_381/mod.rs +++ b/src/elliptic/curves/bls12_381/mod.rs @@ -41,7 +41,7 @@ impl Pair { } pub fn add_pair(&self, other: &Pair) -> Self { - let mut res = self.clone(); + let mut res = *self; res.e.mul_assign(&other.e); Pair { e: res.e } } @@ -74,7 +74,7 @@ mod tests { let p: GE1 = ECPoint::generator(); let q: GE2 = ECPoint::generator(); let r: GE2 = ECPoint::base_point2(); - let q_plus_r = &q + &r; + let q_plus_r = q + r; let e_p_q = Pair::compute_pairing(&p, &q); let e_p_r = Pair::compute_pairing(&p, &r); let e_p_q_r = Pair::compute_pairing(&p, &q_plus_r); diff --git a/src/elliptic/curves/curve_ristretto.rs b/src/elliptic/curves/curve_ristretto.rs index 20ea4bf5..7ffb8be8 100644 --- a/src/elliptic/curves/curve_ristretto.rs +++ b/src/elliptic/curves/curve_ristretto.rs @@ -494,7 +494,6 @@ mod tests { use crate::elliptic::curves::traits::ECPoint; use crate::elliptic::curves::traits::ECScalar; use crate::BigInt; - use serde_json; type GE = RistrettoCurvPoint; type FE = RistrettoScalar; diff --git a/src/elliptic/curves/ed25519.rs b/src/elliptic/curves/ed25519.rs index 6a8759d7..7c51aeeb 100644 --- a/src/elliptic/curves/ed25519.rs +++ b/src/elliptic/curves/ed25519.rs @@ -550,6 +550,7 @@ impl<'de> Visitor<'de> for Ed25519PointVisitor { } } +#[allow(clippy::many_single_char_names)] //helper function, based on https://ed25519.cr.yp.to/python/ed25519.py pub fn xrecover(y_coor: BigInt) -> BigInt { // let d = "37095705934669439343138083508754565189542113879843219016388785533085940283555"; @@ -560,9 +561,9 @@ pub fn xrecover(y_coor: BigInt) -> BigInt { let d_d = expmod(&BigInt::from(121_666), &(q.clone() - BigInt::from(2)), &q); let d_bn = d_n * d_d; - let y_sqr = y_coor.clone() * y_coor.clone(); + let y_sqr = y_coor.clone() * y_coor; let u = y_sqr.clone() - one.clone(); - let v = y_sqr * d_bn.clone() + one.clone(); + let v = y_sqr * d_bn + one; let v_inv = expmod(&v, &(q.clone() - BigInt::from(2)), &q); let x_sqr = u * v_inv; @@ -575,7 +576,7 @@ pub fn xrecover(y_coor: BigInt) -> BigInt { x = BigInt::mod_mul(&x, &i, &q); } if x.modulus(&BigInt::from(2i32)) != BigInt::zero() { - x = q.clone() - x.clone(); + x = q - x.clone(); } x @@ -585,7 +586,7 @@ pub fn xrecover(y_coor: BigInt) -> BigInt { pub fn expmod(b: &BigInt, e: &BigInt, m: &BigInt) -> BigInt { let one = BigInt::one(); if e.clone() == BigInt::zero() { - return one.clone(); + return one; }; let t_temp = expmod(b, &(e.clone() / BigInt::from(2u32)), m); let mut t = BigInt::mod_pow(&t_temp, &BigInt::from(2u32), m); @@ -603,12 +604,12 @@ mod tests { use crate::elliptic::curves::traits::ECPoint; use crate::elliptic::curves::traits::ECScalar; use crate::BigInt; - use serde_json; type GE = Ed25519Point; type FE = Ed25519Scalar; #[test] + #[allow(clippy::op_ref)] // Enables type inference. fn test_serdes_pk() { let mut pk = GE::generator(); let mut s = serde_json::to_string(&pk).expect("Failed in serialization"); @@ -645,6 +646,7 @@ mod tests { } #[test] + #[allow(clippy::op_ref)] // Enables type inference. fn bincode_pk() { let pk = GE::generator(); let encoded = bincode::serialize(&pk).unwrap(); @@ -655,6 +657,7 @@ mod tests { #[test] #[should_panic] + #[allow(clippy::op_ref)] // Enables type inference. fn test_serdes_bad_pk() { let pk = GE::generator(); let s = serde_json::to_string(&pk).expect("Failed in serialization"); @@ -679,9 +682,9 @@ mod tests { let b: FE = ECScalar::new_random(); let a_minus_b_fe: FE = a.sub(&b.get_element()); let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_minus_b_fe; - let point_a = &base * &a; - let point_b = &base * &b; + let point_ab1 = base * a_minus_b_fe; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.sub_point(&point_b.get_element()); assert_eq!(point_ab1, point_ab2); } @@ -690,11 +693,11 @@ mod tests { fn test_add_point() { let a: FE = ECScalar::new_random(); let b: FE = ECScalar::new_random(); - let a_plus_b_fe = a.clone() + &b; + let a_plus_b_fe = a + b; let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_plus_b_fe; - let point_a = &base * &a; - let point_b = &base * &b; + let point_ab1 = base * a_plus_b_fe; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.add_point(&point_b.get_element()); assert_eq!(point_ab1, point_ab2); @@ -704,7 +707,7 @@ mod tests { fn test_add_scalar() { let a: FE = ECScalar::new_random(); let zero: FE = FE::zero(); - let a_plus_zero: FE = a.clone() + zero; + let a_plus_zero: FE = a + zero; assert_eq!(a_plus_zero, a); } @@ -731,10 +734,10 @@ mod tests { fn test_mul_point() { let a: FE = ECScalar::new_random(); let b: FE = ECScalar::new_random(); - let a_mul_b_fe = a.clone() * &b; + let a_mul_b_fe = a * b; let base: GE = ECPoint::generator(); - let point_ab1 = &base * &a_mul_b_fe; - let point_a = &base * &a; + let point_ab1 = base * a_mul_b_fe; + let point_a = base * a; let point_ab2 = point_a.scalar_mul(&b.get_element()); assert_eq!(point_ab1, point_ab2); @@ -776,7 +779,7 @@ mod tests { let g: GE = ECPoint::generator(); let fe: FE = ECScalar::from(&BigInt::from(1)); - let b_tag = &g * &fe; + let b_tag = g * fe; assert_eq!(b_tag, g); } @@ -790,7 +793,7 @@ mod tests { ]; let tv_bn = BigInt::from(&test_vec[..]); let test_fe: FE = ECScalar::from(&tv_bn); - let test_ge = g * &test_fe; + let test_ge = g * test_fe; let test_ge_bytes = test_ge.get_element().to_bytes(); let test_ge2: GE = ECPoint::from_bytes(&test_ge_bytes[..]).unwrap(); let eight: FE = ECScalar::from(&BigInt::from(8)); diff --git a/src/elliptic/curves/p256.rs b/src/elliptic/curves/p256.rs index 75ea2445..a7992d24 100644 --- a/src/elliptic/curves/p256.rs +++ b/src/elliptic/curves/p256.rs @@ -79,7 +79,7 @@ impl ECScalar for Secp256r1Scalar { } fn get_element(&self) -> SK { - self.fe.clone() + self.fe } fn set_element(&mut self, element: SK) { @@ -242,7 +242,7 @@ impl ECPoint for Secp256r1Point { type Scalar = Secp256r1Scalar; fn base_point2() -> Secp256r1Point { - let mut v = vec![4 as u8]; + let mut v = vec![4_u8]; v.extend(BASE_POINT2_X.as_ref()); v.extend(BASE_POINT2_Y.as_ref()); Secp256r1Point::from_bytes(&v).unwrap() @@ -257,7 +257,7 @@ impl ECPoint for Secp256r1Point { } fn get_element(&self) -> PK { - self.ge.clone() + self.ge } fn bytes_compressed_to_big_int(&self) -> BigInt { @@ -652,9 +652,9 @@ mod tests { let a_minus_b = BigInt::mod_add(&a.to_big_int(), &minus_b, &q); let a_minus_b_fe: Secp256r1Scalar = ECScalar::from(&a_minus_b); let base: Secp256r1Point = ECPoint::generator(); - let point_ab1 = base.clone() * a_minus_b_fe; - let point_a = base.clone() * a; - let point_b = base.clone() * b; + let point_ab1 = base * a_minus_b_fe; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.sub_point(&point_b.get_element()); assert_eq!(point_ab1.get_element(), point_ab2.get_element()); } @@ -750,10 +750,10 @@ mod tests { fn test_pk_to_key_slice() { for _ in 1..200 { let r = Secp256r1Scalar::new_random(); - let rg = Secp256r1Point::generator() * &r; + let rg = Secp256r1Point::generator() * r; let key_slice = rg.pk_to_key_slice(); assert!(key_slice.len() == 65); - assert!(key_slice[0].clone() == 4); + assert!(key_slice[0] == 4); let rg_prime: Secp256r1Point = ECPoint::from_bytes(&key_slice).unwrap(); assert_eq!(rg_prime.get_element(), rg.get_element()); } @@ -842,50 +842,32 @@ mod tests { fn add_sub_point() { let g = Secp256r1Point::generator(); let i: Secp256r1Scalar = ECScalar::from(&BigInt::from(3)); - assert_eq!( - (g.clone() + g.clone() + g.clone()).get_element(), - (g.clone() * i).get_element() - ); - assert_eq!( - (g.clone() + g.clone()).get_element(), - (g.clone() + g.clone() - g.clone() + g.clone()).get_element() - ); + assert_eq!((g + g + g).get_element(), (g * i).get_element()); + assert_eq!((g + g).get_element(), (g + g - g + g).get_element()); } #[test] fn add_scalar() { let i: Secp256r1Scalar = ECScalar::from(&BigInt::from(1)); let j: Secp256r1Scalar = ECScalar::from(&BigInt::from(2)); - assert_eq!((i.clone() + i.clone()).to_big_int(), j.to_big_int()); - assert_eq!( - (i.clone() + i.clone() + i.clone() + i.clone()).to_big_int(), - (j.clone() + j.clone()).to_big_int() - ); + assert_eq!((i + i).to_big_int(), j.to_big_int()); + assert_eq!((i + i + i + i).to_big_int(), (j + j).to_big_int()); } #[test] fn sub_scalar() { let i: Secp256r1Scalar = ECScalar::from(&BigInt::from(1)); - assert_eq!( - (i.clone() + i.clone() - i.clone()).to_big_int(), - i.to_big_int() - ); + assert_eq!((i + i - i).to_big_int(), i.to_big_int()); let j: Secp256r1Scalar = ECScalar::from(&BigInt::from(2)); - assert_eq!( - (j.clone() + j.clone() - j.clone()).to_big_int(), - j.to_big_int() - ); + assert_eq!((j + j - j).to_big_int(), j.to_big_int()); let k = Secp256r1Scalar::new_random(); - assert_eq!( - (k.clone() + k.clone() - k.clone()).to_big_int(), - k.to_big_int() - ); + assert_eq!((k + k - k).to_big_int(), k.to_big_int()); } #[test] fn mul_scalar() { let i: Secp256r1Scalar = ECScalar::from(&BigInt::from(1)); let j: Secp256r1Scalar = ECScalar::from(&BigInt::from(2)); - assert_eq!((j.clone() * i.clone()).to_big_int(), j.to_big_int()); + assert_eq!((j * i).to_big_int(), j.to_big_int()); } } diff --git a/src/elliptic/curves/secp256_k1.rs b/src/elliptic/curves/secp256_k1.rs index 3e688d1e..f5abc665 100644 --- a/src/elliptic/curves/secp256_k1.rs +++ b/src/elliptic/curves/secp256_k1.rs @@ -147,7 +147,7 @@ impl ECScalar for Secp256k1Scalar { fn add(&self, other: &SK) -> Secp256k1Scalar { let mut other_scalar: FE = ECScalar::new_random(); - other_scalar.set_element(other.clone()); + other_scalar.set_element(*other); let res: FE = ECScalar::from(&BigInt::mod_add( &self.to_big_int(), &other_scalar.to_big_int(), @@ -161,7 +161,7 @@ impl ECScalar for Secp256k1Scalar { fn mul(&self, other: &SK) -> Secp256k1Scalar { let mut other_scalar: FE = ECScalar::new_random(); - other_scalar.set_element(other.clone()); + other_scalar.set_element(*other); let res: FE = ECScalar::from(&BigInt::mod_mul( &self.to_big_int(), &other_scalar.to_big_int(), @@ -175,7 +175,7 @@ impl ECScalar for Secp256k1Scalar { fn sub(&self, other: &SK) -> Secp256k1Scalar { let mut other_scalar: FE = ECScalar::new_random(); - other_scalar.set_element(other.clone()); + other_scalar.set_element(*other); let res: FE = ECScalar::from(&BigInt::mod_sub( &self.to_big_int(), &other_scalar.to_big_int(), @@ -280,7 +280,7 @@ impl ECPoint for Secp256k1Point { type Scalar = Secp256k1Scalar; fn base_point2() -> Secp256k1Point { - let mut v = vec![4 as u8]; + let mut v = vec![4_u8]; v.extend(BASE_POINT2_X.as_ref()); v.extend(BASE_POINT2_Y.as_ref()); Secp256k1Point { @@ -290,7 +290,7 @@ impl ECPoint for Secp256k1Point { } fn generator() -> Secp256k1Point { - let mut v = vec![4 as u8]; + let mut v = vec![4_u8]; v.extend(GENERATOR_X.as_ref()); v.extend(GENERATOR_Y.as_ref()); Secp256k1Point { @@ -336,9 +336,9 @@ impl ECPoint for Secp256k1Point { 33..=63 => { let mut template = vec![0; 64 - bytes_vec.len()]; template.extend_from_slice(&bytes); - let bytes_vec = template; + let mut bytes_vec = template; let mut template: Vec = vec![4]; - template.append(&mut bytes_vec.clone()); + template.append(&mut bytes_vec); let bytes_slice = &template[..]; bytes_array_65.copy_from_slice(&bytes_slice[0..65]); @@ -353,9 +353,9 @@ impl ECPoint for Secp256k1Point { 0..=32 => { let mut template = vec![0; 32 - bytes_vec.len()]; template.extend_from_slice(&bytes); - let bytes_vec = template; + let mut bytes_vec = template; let mut template: Vec = vec![2]; - template.append(&mut bytes_vec.clone()); + template.append(&mut bytes_vec); let bytes_slice = &template[..]; bytes_array_33.copy_from_slice(&bytes_slice[0..33]); @@ -368,9 +368,9 @@ impl ECPoint for Secp256k1Point { } _ => { let bytes_slice = &bytes_vec[0..64]; - let bytes_vec = bytes_slice.to_vec(); + let mut bytes_vec = bytes_slice.to_vec(); let mut template: Vec = vec![4]; - template.append(&mut bytes_vec.clone()); + template.append(&mut bytes_vec); let bytes_slice = &template[..]; bytes_array_65.copy_from_slice(&bytes_slice[0..65]); @@ -384,7 +384,7 @@ impl ECPoint for Secp256k1Point { } } fn pk_to_key_slice(&self) -> Vec { - let mut v = vec![4 as u8]; + let mut v = vec![4_u8]; let x_vec = BigInt::to_vec(&self.x_coor().unwrap()); let y_vec = BigInt::to_vec(&self.y_coor().unwrap()); @@ -471,7 +471,7 @@ impl ECPoint for Secp256k1Point { assert_eq!(x, &BigInt::from(vec_x.as_ref())); assert_eq!(y, &BigInt::from(vec_y.as_ref())); - let mut v = vec![4 as u8]; + let mut v = vec![4_u8]; v.extend(vec_x); v.extend(vec_y); @@ -622,8 +622,6 @@ mod tests { use crate::cryptographic_primitives::hashing::traits::Hash; use crate::elliptic::curves::traits::ECPoint; use crate::elliptic::curves::traits::ECScalar; - use bincode; - use serde_json; #[test] fn serialize_sk() { @@ -723,7 +721,7 @@ mod tests { #[test] fn test_from_bytes() { let g = Secp256k1Point::generator(); - let hash = HSha256::create_hash(&vec![&g.bytes_compressed_to_big_int()]); + let hash = HSha256::create_hash(&[&g.bytes_compressed_to_big_int()]); let hash_vec = BigInt::to_vec(&hash); let result = Secp256k1Point::from_bytes(&hash_vec); assert_eq!(result.unwrap_err(), ErrorKey::InvalidPublicKey) @@ -770,10 +768,10 @@ mod tests { let a_minus_b = BigInt::mod_add(&a.to_big_int(), &minus_b, &order); let a_minus_b_fe: FE = ECScalar::from(&a_minus_b); let base: GE = ECPoint::generator(); - let point_ab1 = base.clone() * a_minus_b_fe; + let point_ab1 = base * a_minus_b_fe; - let point_a = base.clone() * a; - let point_b = base.clone() * b; + let point_a = base * a; + let point_b = base * b; let point_ab2 = point_a.sub_point(&point_b.get_element()); assert_eq!(point_ab1.get_element(), point_ab2.get_element()); } @@ -801,11 +799,11 @@ mod tests { fn test_pk_to_key_slice() { for _ in 1..200 { let r = FE::new_random(); - let rg = GE::generator() * &r; + let rg = GE::generator() * r; let key_slice = rg.pk_to_key_slice(); assert!(key_slice.len() == 65); - assert!(key_slice[0].clone() == 4); + assert!(key_slice[0] == 4); let rg_prime: GE = ECPoint::from_bytes(&key_slice[1..65]).unwrap(); assert_eq!(rg_prime.get_element(), rg.get_element());