@@ -84,12 +84,12 @@ pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
8484 // m1 = c^dP mod p
8585 let p_wide = p_params. modulus ( ) . resize_unchecked ( c. bits_precision ( ) ) ;
8686 let c_mod_dp = ( & c % p_wide. as_nz_ref ( ) ) . resize_unchecked ( dp. bits_precision ( ) ) ;
87- let cp = BoxedMontyForm :: new ( c_mod_dp, p_params. clone ( ) ) ;
87+ let cp = BoxedMontyForm :: new ( c_mod_dp, p_params) ;
8888 let mut m1 = cp. pow ( dp) ;
8989 // m2 = c^dQ mod q
9090 let q_wide = q_params. modulus ( ) . resize_unchecked ( c. bits_precision ( ) ) ;
9191 let c_mod_dq = ( & c % q_wide. as_nz_ref ( ) ) . resize_unchecked ( dq. bits_precision ( ) ) ;
92- let cq = BoxedMontyForm :: new ( c_mod_dq, q_params. clone ( ) ) ;
92+ let cq = BoxedMontyForm :: new ( c_mod_dq, q_params) ;
9393 let m2 = cq. pow ( dq) . retrieve ( ) ;
9494
9595 // Note that since `p` and `q` may have different `bits_precision`,
@@ -106,7 +106,7 @@ pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
106106 Ordering :: Greater => ( & m2) . resize_unchecked ( p_params. bits_precision ( ) ) ,
107107 Ordering :: Equal => m2. clone ( ) ,
108108 } ;
109- let m2r = BoxedMontyForm :: new ( m2_mod_p, p_params. clone ( ) ) ;
109+ let m2r = BoxedMontyForm :: new ( m2_mod_p, p_params) ;
110110 m1 -= & m2r;
111111
112112 // precomputed: qInv = (1/q) mod p
@@ -197,7 +197,7 @@ fn blind<R: TryCryptoRng + ?Sized, K: PublicKeyParts>(
197197 // r^e (mod n)
198198 let mut rpowe = pow_mod_params ( & r, key. e ( ) , n_params) ;
199199 // c * r^e (mod n)
200- let c = mul_mod_params ( c , & rpowe, n_params) ;
200+ let c = c . mul_mod ( & rpowe, n_params. modulus ( ) . as_nz_ref ( ) ) ;
201201 rpowe. zeroize ( ) ;
202202
203203 c
@@ -225,7 +225,7 @@ fn unblind(m: &BoxedUint, unblinder: &BoxedUint, n_params: &BoxedMontyParams) ->
225225 "invalid n_params"
226226 ) ;
227227
228- mul_mod_params ( m , unblinder, n_params)
228+ m . mul_mod ( unblinder, n_params. modulus ( ) . as_nz_ref ( ) )
229229}
230230
231231/// Computes `base.pow_mod(exp, n)` with precomputed `n_params`.
@@ -237,15 +237,7 @@ fn pow_mod_params(base: &BoxedUint, exp: &BoxedUint, n_params: &BoxedMontyParams
237237fn reduce_vartime ( n : & BoxedUint , p : & BoxedMontyParams ) -> BoxedMontyForm {
238238 let modulus = p. modulus ( ) . as_nz_ref ( ) . clone ( ) ;
239239 let n_reduced = n. rem_vartime ( & modulus) . resize_unchecked ( p. bits_precision ( ) ) ;
240- BoxedMontyForm :: new ( n_reduced, p. clone ( ) )
241- }
242-
243- /// Computes `lhs.mul_mod(rhs, n)` with precomputed `n_params`.
244- fn mul_mod_params ( lhs : & BoxedUint , rhs : & BoxedUint , n_params : & BoxedMontyParams ) -> BoxedUint {
245- // TODO: nicer api in crypto-bigint?
246- let lhs = BoxedMontyForm :: new ( lhs. clone ( ) , n_params. clone ( ) ) ;
247- let rhs = BoxedMontyForm :: new ( rhs. clone ( ) , n_params. clone ( ) ) ;
248- ( lhs * rhs) . retrieve ( )
240+ BoxedMontyForm :: new ( n_reduced, p)
249241}
250242
251243/// The following (deterministic) algorithm also recovers the prime factors `p` and `q` of a modulus `n`, given the
@@ -300,7 +292,7 @@ pub fn recover_primes(
300292
301293 // 4. Let ϒ be the positive square root of b^2 – 4n; if ϒ is not an integer,
302294 // then output an error indicator, and exit without further processing.
303- let y = b_squared_minus_four_n. sqrt ( ) ;
295+ let y = b_squared_minus_four_n. floor_sqrt ( ) ;
304296
305297 let y_squared = y. square ( ) ;
306298 let sqrt_is_whole_number = y_squared == b_squared_minus_four_n;
0 commit comments