@@ -22,20 +22,6 @@ pub fn rsa_encrypt<K: PublicKeyParts>(key: &K, m: &BoxedUint) -> Result<BoxedUin
22
22
Ok ( res)
23
23
}
24
24
25
- fn try_set_precision ( x : BoxedUint , bits_precision : u32 ) -> Result < BoxedUint > {
26
- match x. bits_precision ( ) . cmp ( & bits_precision) {
27
- Ordering :: Greater => {
28
- if x. bits ( ) <= bits_precision {
29
- Ok ( x. shorten ( bits_precision) )
30
- } else {
31
- Err ( Error :: Internal )
32
- }
33
- }
34
- Ordering :: Less => Ok ( x. widen ( bits_precision) ) ,
35
- Ordering :: Equal => Ok ( x) ,
36
- }
37
- }
38
-
39
25
/// ⚠️ Performs raw RSA decryption with no padding or error checking.
40
26
///
41
27
/// Returns a plaintext `BoxedUint`. Performs RSA blinding if an `Rng` is passed.
@@ -69,9 +55,9 @@ pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
69
55
let c = if let Some ( rng) = rng {
70
56
let ( blinded, unblinder) = blind ( rng, priv_key, c, n_params) ?;
71
57
ir = Some ( unblinder) ;
72
- blinded. widen ( bits)
58
+ blinded. try_resize ( bits) . ok_or ( Error :: Internal ) ?
73
59
} else {
74
- c. widen ( bits)
60
+ c. try_resize ( bits) . ok_or ( Error :: Internal ) ?
75
61
} ;
76
62
77
63
let is_multiprime = priv_key. primes ( ) . len ( ) > 2 ;
@@ -96,36 +82,31 @@ pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
96
82
// (modulo `p` and `q`) rather than calculating the remainder directly.
97
83
98
84
// m1 = c^dP mod p
99
- let p_wide = NonZero :: new ( p_params. modulus ( ) . widen ( c. bits_precision ( ) ) )
100
- . expect ( "`p` is non-zero" ) ;
101
- let c_mod_dp = ( & c % p_wide) . shorten ( dp. bits_precision ( ) ) ;
85
+ let p_wide = p_params. modulus ( ) . resize_unchecked ( c. bits_precision ( ) ) ;
86
+ let c_mod_dp = ( & c % p_wide. as_nz_ref ( ) ) . resize_unchecked ( dp. bits_precision ( ) ) ;
102
87
let cp = BoxedMontyForm :: new ( c_mod_dp, p_params. clone ( ) ) ;
103
88
let mut m1 = cp. pow ( dp) ;
104
89
// m2 = c^dQ mod q
105
- let q_wide = NonZero :: new ( q_params. modulus ( ) . widen ( c. bits_precision ( ) ) )
106
- . expect ( "`q` is non-zero" ) ;
107
- let c_mod_dq = ( & c % q_wide) . shorten ( dq. bits_precision ( ) ) ;
90
+ let q_wide = q_params. modulus ( ) . resize_unchecked ( c. bits_precision ( ) ) ;
91
+ let c_mod_dq = ( & c % q_wide. as_nz_ref ( ) ) . resize_unchecked ( dq. bits_precision ( ) ) ;
108
92
let cq = BoxedMontyForm :: new ( c_mod_dq, q_params. clone ( ) ) ;
109
93
let m2 = cq. pow ( dq) . retrieve ( ) ;
110
94
111
95
// Note that since `p` and `q` may have different `bits_precision`,
112
96
// it may be different for `m1` and `m2` as well.
113
97
114
98
// (m1 - m2) mod p = (m1 mod p) - (m2 mod p) mod p
115
- let m2r = match p_params. bits_precision ( ) . cmp ( & q_params. bits_precision ( ) ) {
99
+ let m2_mod_p = match p_params. bits_precision ( ) . cmp ( & q_params. bits_precision ( ) ) {
116
100
Ordering :: Less => {
117
- let p_wide =
118
- NonZero :: new ( p. widen ( q_params. bits_precision ( ) ) ) . expect ( "`p` is non-zero" ) ;
119
- BoxedMontyForm :: new (
120
- ( & m2 % p_wide) . shorten ( p_params. bits_precision ( ) ) ,
121
- p_params. clone ( ) ,
122
- )
123
- }
124
- Ordering :: Greater => {
125
- BoxedMontyForm :: new ( m2. widen ( p_params. bits_precision ( ) ) , p_params. clone ( ) )
101
+ let p_wide = NonZero :: new ( p. clone ( ) )
102
+ . expect ( "`p` is non-zero" )
103
+ . resize_unchecked ( q_params. bits_precision ( ) ) ;
104
+ ( & m2 % p_wide) . resize_unchecked ( p_params. bits_precision ( ) )
126
105
}
127
- Ordering :: Equal => BoxedMontyForm :: new ( m2. clone ( ) , p_params. clone ( ) ) ,
106
+ Ordering :: Greater => ( & m2) . resize_unchecked ( p_params. bits_precision ( ) ) ,
107
+ Ordering :: Equal => m2. clone ( ) ,
128
108
} ;
109
+ let m2r = BoxedMontyForm :: new ( m2_mod_p, p_params. clone ( ) ) ;
129
110
m1 -= & m2r;
130
111
131
112
// precomputed: qInv = (1/q) mod p
@@ -134,8 +115,10 @@ pub fn rsa_decrypt<R: TryCryptoRng + ?Sized>(
134
115
let h = ( qinv * m1) . retrieve ( ) ;
135
116
136
117
// m = m2 + h.q
137
- let m2 = try_set_precision ( m2, n. bits_precision ( ) ) ?;
138
- let hq = try_set_precision ( h * q, n. bits_precision ( ) ) ?;
118
+ let m2 = m2. try_resize ( n. bits_precision ( ) ) . ok_or ( Error :: Internal ) ?;
119
+ let hq = ( h * q)
120
+ . try_resize ( n. bits_precision ( ) )
121
+ . ok_or ( Error :: Internal ) ?;
139
122
m2. wrapping_add ( & hq)
140
123
}
141
124
_ => {
@@ -253,7 +236,7 @@ fn pow_mod_params(base: &BoxedUint, exp: &BoxedUint, n_params: &BoxedMontyParams
253
236
254
237
fn reduce_vartime ( n : & BoxedUint , p : & BoxedMontyParams ) -> BoxedMontyForm {
255
238
let modulus = p. modulus ( ) . as_nz_ref ( ) . clone ( ) ;
256
- let n_reduced = n. rem_vartime ( & modulus) . widen ( p. bits_precision ( ) ) ;
239
+ let n_reduced = n. rem_vartime ( & modulus) . resize_unchecked ( p. bits_precision ( ) ) ;
257
240
BoxedMontyForm :: new ( n_reduced, p. clone ( ) )
258
241
}
259
242
@@ -283,19 +266,20 @@ pub fn recover_primes(
283
266
284
267
// 1. Let a = (de – 1) × GCD(n – 1, de – 1).
285
268
let bits = d. bits_precision ( ) * 2 ;
286
- let one = BoxedUint :: one ( ) . widen ( bits) ;
287
- let e = e. widen ( bits ) ;
288
- let d = d. widen ( bits ) ;
289
- let n = n. as_ref ( ) . widen ( bits) ;
269
+ let one = BoxedUint :: one_with_precision ( bits) ;
270
+ let e = e. resize_unchecked ( d . bits_precision ( ) ) ;
271
+ let d = d. resize_unchecked ( d . bits_precision ( ) ) ;
272
+ let n = n. resize_unchecked ( bits) ;
290
273
291
- let a1 = & d * & e - & one;
292
- let a2 = ( & n - & one) . gcd ( & a1) ;
274
+ let a1 = d * e - & one;
275
+ let a2 = ( n . as_ref ( ) - & one) . gcd ( & a1) ;
293
276
let a = a1 * a2;
294
- let n = n. widen ( a. bits_precision ( ) ) ;
277
+ let n = n. resize_unchecked ( a. bits_precision ( ) ) ;
295
278
296
279
// 2. Let m = floor(a /n) and r = a – m n, so that a = m n + r and 0 ≤ r < n.
297
- let m = & a / NonZero :: new ( n. clone ( ) ) . expect ( "checked" ) ;
298
- let r = a - & m * & n;
280
+ let m = & a / & n;
281
+ let r = a - & m * n. as_ref ( ) ;
282
+ let n = n. get ( ) ;
299
283
300
284
// 3. Let b = ( (n – r)/(m + 1) ) + 1; if b is not an integer or b^2 ≤ 4n, then output an error indicator,
301
285
// and exit without further processing.
@@ -360,7 +344,7 @@ pub(crate) fn compute_private_exponent_euler_totient(
360
344
for prime in primes {
361
345
totient *= prime - & BoxedUint :: one ( ) ;
362
346
}
363
- let exp = exp. widen ( totient. bits_precision ( ) ) ;
347
+ let exp = exp. resize_unchecked ( totient. bits_precision ( ) ) ;
364
348
365
349
// NOTE: `mod_inverse` checks if `exp` evenly divides `totient` and returns `None` if so.
366
350
// This ensures that `exp` is not a factor of any `(prime - 1)`.
@@ -391,7 +375,7 @@ pub(crate) fn compute_private_exponent_carmicheal(
391
375
// LCM inlined
392
376
let gcd = p1. gcd ( & q1) ;
393
377
let lcm = p1 / NonZero :: new ( gcd) . expect ( "gcd is non zero" ) * & q1;
394
- let exp = exp. widen ( lcm. bits_precision ( ) ) ;
378
+ let exp = exp. resize_unchecked ( lcm. bits_precision ( ) ) ;
395
379
if let Some ( d) = exp. inv_mod ( & lcm) . into ( ) {
396
380
Ok ( d)
397
381
} else {
0 commit comments