You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@milagro.apache.org by km...@apache.org on 2020/05/05 06:39:20 UTC

[incubator-milagro-crypto-rust] 34/44: Tidy up compiler warnings and remove clone from FF

This is an automated email from the ASF dual-hosted git repository.

kmccusker pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-milagro-crypto-rust.git

commit e00159c4de34e6a7e589a6784467f59a39df9943
Author: Kirk Baird <ba...@outlook.com>
AuthorDate: Tue Apr 21 10:58:10 2020 +1000

    Tidy up compiler warnings and remove clone from FF
    
    Signed-off-by: Kirk Baird <ba...@outlook.com>
---
 src/big.rs  | 25 +++++++++----------------
 src/dbig.rs | 15 +++++----------
 src/ecp.rs  | 23 +++++++++++------------
 src/ff.rs   | 12 +++---------
 src/rsa.rs  | 10 +++++-----
 5 files changed, 33 insertions(+), 52 deletions(-)

diff --git a/src/big.rs b/src/big.rs
index f0696c7..2c935c6 100644
--- a/src/big.rs
+++ b/src/big.rs
@@ -617,7 +617,6 @@ impl Big {
     pub fn rmod(&mut self, n: &Big) {
         let mut k = 0;
         let mut m = n.clone();
-        let mut r = Big::new();
         self.norm();
         if Big::comp(self, &m) < 0 {
             return;
@@ -633,7 +632,7 @@ impl Big {
         while k > 0 {
             m.fshr(1);
 
-            r = self.clone();
+            let mut r = self.clone();
             r.sub(&m);
             r.norm();
             self.cmove(
@@ -653,7 +652,6 @@ impl Big {
         let mut e = Big::new_int(1);
         let mut b = self.clone();
         let mut m = n.clone();
-        let mut r = Big::new();
         self.zero();
 
         while Big::comp(&b, &m) >= 0 {
@@ -666,7 +664,7 @@ impl Big {
             m.fshr(1);
             e.fshr(1);
 
-            r = b.clone();
+            let mut r = b.clone();
             r.sub(&m);
             r.norm();
             let d = (1 - ((r.w[NLEN - 1] >> (arch::CHUNK - 1)) & 1)) as isize;
@@ -732,22 +730,18 @@ impl Big {
     /// Jacobi Symbol (this/p). Returns 0, 1 or -1
     pub fn jacobi(&mut self, p: &Big) -> isize {
         let mut m: usize = 0;
-        let mut t = Big::new();
-        let mut x = Big::new();
-        let mut n = Big::new();
-        let zilch = Big::new();
         let one = Big::new_int(1);
-        if p.parity() == 0 || Big::comp(self, &zilch) == 0 || Big::comp(p, &one) <= 0 {
+        if p.parity() == 0 || self.iszilch() || Big::comp(p, &one) <= 0 {
             return 0;
         }
         self.norm();
 
-        x = self.clone();
-        n = p.clone();
+        let mut x = self.clone();
+        let mut n = p.clone();
         x.rmod(p);
 
         while Big::comp(&n, &one) > 0 {
-            if Big::comp(&x, &zilch) == 0 {
+            if x.iszilch() {
                 return 0;
             }
             let n8 = n.lastbits(3) as usize;
@@ -760,7 +754,7 @@ impl Big {
                 m += (n8 * n8 - 1) / 8
             }
             m += (n8 - 1) * ((x.lastbits(2) as usize) - 1) / 4;
-            t = n.clone();
+            let mut t = n.clone();
             t.rmod(&x);
             n = x.clone();
             x = t.clone();
@@ -781,7 +775,6 @@ impl Big {
         let mut v = p.clone();
         let mut x1 = Big::new_int(1);
         let mut x2 = Big::new();
-        let mut t = Big::new();
         let one = Big::new_int(1);
 
         while (Big::comp(&u, &one) != 0) && (Big::comp(&v, &one) != 0) {
@@ -807,7 +800,7 @@ impl Big {
                 if Big::comp(&x1, &x2) >= 0 {
                     x1.sub(&x2)
                 } else {
-                    t = p.clone();
+                    let mut t = p.clone();
                     t.sub(&x2);
                     x1.add(&t);
                 }
@@ -818,7 +811,7 @@ impl Big {
                 if Big::comp(&x2, &x1) >= 0 {
                     x2.sub(&x1)
                 } else {
-                    t = p.clone();
+                    let mut t = p.clone();
                     t.sub(&x1);
                     x2.add(&t);
                 }
diff --git a/src/dbig.rs b/src/dbig.rs
index 689aeb8..6201d72 100644
--- a/src/dbig.rs
+++ b/src/dbig.rs
@@ -163,11 +163,9 @@ impl DBig {
         let mut k = 0;
         self.norm();
         let mut m = DBig::new_scopy(c);
-        let mut dr = DBig::new();
 
         if DBig::comp(self, &m) < 0 {
-            let r = Big::new_dcopy(self);
-            return r;
+            return Big::new_dcopy(self);
         }
 
         loop {
@@ -181,7 +179,7 @@ impl DBig {
         while k > 0 {
             m.shr(1);
 
-            dr = self.clone();
+            let mut dr = self.clone();
             dr.sub(&m);
             dr.norm();
             self.cmove(
@@ -191,8 +189,7 @@ impl DBig {
 
             k -= 1;
         }
-        let r = Big::new_dcopy(self);
-        r
+        Big::new_dcopy(self)
     }
 
     /// return self / c
@@ -201,8 +198,6 @@ impl DBig {
         let mut m = DBig::new_scopy(c);
         let mut a = Big::new();
         let mut e = Big::new_int(1);
-        let mut dr = DBig::new();
-        let mut r = Big::new();
         self.norm();
 
         while DBig::comp(self, &m) >= 0 {
@@ -215,12 +210,12 @@ impl DBig {
             m.shr(1);
             e.shr(1);
 
-            dr = self.clone();
+            let mut dr = self.clone();
             dr.sub(&m);
             dr.norm();
             let d = (1 - ((dr.w[big::DNLEN - 1] >> (arch::CHUNK - 1)) & 1)) as isize;
             self.cmove(&dr, d);
-            r = a.clone();
+            let mut r = a.clone();
             r.add(&e);
             r.norm();
             a.cmove(&r, d);
diff --git a/src/ecp.rs b/src/ecp.rs
index 2c9b9db..1f1bed2 100644
--- a/src/ecp.rs
+++ b/src/ecp.rs
@@ -931,22 +931,21 @@ impl ECP {
         if CURVETYPE == CurveType::Montgomery {
             return self.mul(&mut Big::new_int(e as isize));
         } else {
-            let mut P = ECP::new();
             let mut R0 = ECP::new();
             let mut R1 = self.clone();
 
             for i in (0..bts).rev() {
                 let b = ((e >> i) & 1) as isize;
-                P = R1.clone();
+                let mut P = R1.clone();
                 P.add(&R0);
                 R0.cswap(&mut R1, b);
                 R1 = P.clone();
                 R0.dbl();
                 R0.cswap(&mut R1, b);
             }
-            P = R0.clone();
+            let mut P = R0.clone();
             P.affine();
-            return P;
+            P
         }
     }
 
@@ -955,8 +954,7 @@ impl ECP {
         if e.iszilch() || self.is_infinity() {
             return ECP::new();
         }
-        let mut P = ECP::new();
-        if CURVETYPE == CurveType::Montgomery {
+        let mut T = if CURVETYPE == CurveType::Montgomery {
             /* use Ladder */
             let mut R0 = self.clone();
             let mut R1 = self.clone();
@@ -967,14 +965,14 @@ impl ECP {
 
             for i in (0..nb - 1).rev() {
                 let b = e.bit(i);
-                P = R1.clone();
+                let mut P = R1.clone();
                 P.dadd(&mut R0, &D);
                 R0.cswap(&mut R1, b);
                 R1 = P.clone();
                 R0.dbl();
                 R0.cswap(&mut R1, b);
             }
-            P = R0.clone();
+            R0.clone()
         } else {
             let mut W: [ECP; 8] = [
                 ECP::new(),
@@ -1024,7 +1022,7 @@ impl ECP {
             }
             w[nb] = t.lastbits(5) as i8;
 
-            P = W[((w[nb] as usize) - 1) / 2].clone();
+            let mut P = W[((w[nb] as usize) - 1) / 2].clone();
             for i in (0..nb).rev() {
                 Q.selector(&W, w[i] as i32);
                 P.dbl();
@@ -1034,9 +1032,10 @@ impl ECP {
                 P.add(&Q);
             }
             P.sub(&C); /* apply correction */
-        }
-        P.affine();
-        P
+            P
+        };
+        T.affine();
+        T
     }
 
     /* Return e.this+f.Q */
diff --git a/src/ff.rs b/src/ff.rs
index 0f23b87..c2b12cf 100644
--- a/src/ff.rs
+++ b/src/ff.rs
@@ -39,7 +39,6 @@ pub const P_OMASK: Chunk = (-1) << (P_MBITS % big::BASEBITS);
 pub const P_FEXCESS: Chunk = 1 << (big::BASEBITS * big::NLEN - P_MBITS - 1);
 pub const P_TBITS: usize = P_MBITS % big::BASEBITS;
 
-#[derive(Clone)]
 pub struct FF {
     v: Vec<Big>,
     length: usize,
@@ -145,20 +144,16 @@ impl FF {
 
     /* shift right by BIGBITS-bit words */
     pub fn shrw(&mut self, n: usize) {
-        let mut t = Big::new();
         for i in 0..n {
-            t = self.v[i + n].clone();
-            self.v[i] = t.clone();
+            self.v[i] = self.v[i + n].clone();
             self.v[i + n].zero();
         }
     }
 
     /* shift left by BIGBITS-bit words */
     pub fn shlw(&mut self, n: usize) {
-        let mut t = Big::new();
         for i in 0..n {
-            t = self.v[i].clone();
-            self.v[n + i] = t.clone();
+            self.v[n + i] = self.v[i].clone();
             self.v[i].zero();
         }
     }
@@ -205,9 +200,8 @@ impl FF {
     }
 
     pub fn rsinc(&mut self, n: usize) {
-        let mut t = Big::new();
         for i in 0..n {
-            t = self.v[i].clone();
+            let t = self.v[i].clone();
             self.v[n + i].add(&t);
         }
     }
diff --git a/src/rsa.rs b/src/rsa.rs
index 30bdcd0..a3022e0 100644
--- a/src/rsa.rs
+++ b/src/rsa.rs
@@ -121,7 +121,7 @@ pub fn key_pair(rng: &mut RAND, e: isize, prv: &mut RsaPrivateKey, pbc: &mut Rsa
             prv.p.inc(4);
         }
 
-        p1 = prv.p.clone();
+        p1.copy(&prv.p);
         p1.dec(1);
 
         if p1.cfactor(e) {
@@ -139,7 +139,7 @@ pub fn key_pair(rng: &mut RAND, e: isize, prv: &mut RsaPrivateKey, pbc: &mut Rsa
             prv.q.inc(4);
         }
 
-        q1 = prv.q.clone();
+        q1.copy(&prv.q);
         q1.dec(1);
 
         if q1.cfactor(e) {
@@ -152,7 +152,7 @@ pub fn key_pair(rng: &mut RAND, e: isize, prv: &mut RsaPrivateKey, pbc: &mut Rsa
     pbc.n = FF::mul(&prv.p, &prv.q);
     pbc.e = e;
 
-    t = p1.clone();
+    t.copy(&p1);
     t.shr();
     prv.dp.set(e);
     prv.dp.invmodp(&t);
@@ -161,7 +161,7 @@ pub fn key_pair(rng: &mut RAND, e: isize, prv: &mut RsaPrivateKey, pbc: &mut Rsa
     }
     prv.dp.norm();
 
-    t = q1.clone();
+    t.copy(&q1);
     t.shr();
     prv.dq.set(e);
     prv.dq.invmodp(&t);
@@ -170,7 +170,7 @@ pub fn key_pair(rng: &mut RAND, e: isize, prv: &mut RsaPrivateKey, pbc: &mut Rsa
     }
     prv.dq.norm();
 
-    prv.c = prv.p.clone();
+    prv.c.copy(&prv.p);
     prv.c.invmodp(&prv.q);
 }