Skip to content

Commit d891ab1

Browse files
authored
Bump crypto-bigint to v0.7.0-rc.16 (#628)
This includes changes to `BoxedMontyForm::new` that handle cloning the `Arc` around the `BoxedMontyParams` internally, rather than requiring the caller to clone it. It also renames the unchecked square root to `floor_sqrt`, which is fine for the one usage here (in the prime recovery implementation), because it immediately performs a check on the result.
1 parent c8cb934 commit d891ab1

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ exclude = ["marvin_toolkit/", "thirdparty/"]
1515

1616
[dependencies]
1717
const-oid = { version = "0.10", default-features = false }
18-
crypto-bigint = { version = "0.7.0-rc.13", default-features = false, features = ["zeroize", "alloc"] }
19-
crypto-primes = { version = "0.7.0-pre.5", default-features = false }
18+
crypto-bigint = { version = "0.7.0-rc.16", default-features = false, features = ["zeroize", "alloc"] }
19+
crypto-primes = { version = "0.7.0-pre.6", default-features = false }
2020
digest = { version = "0.11.0-rc.4", default-features = false, features = ["alloc", "oid"] }
2121
rand_core = { version = "0.10.0-rc-2", default-features = false }
2222
signature = { version = "3.0.0-rc.5", default-features = false, features = ["alloc", "digest", "rand_core"] }

src/algorithms/rsa.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
237237
fn 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;

src/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl RsaPrivateKey {
525525
Ordering::Equal => &q % NonZero::new(p.clone()).expect("`p` is non-zero"),
526526
};
527527

528-
let q_mod_p = BoxedMontyForm::new(q_mod_p, p_params.clone());
528+
let q_mod_p = BoxedMontyForm::new(q_mod_p, &p_params);
529529
let qinv = q_mod_p.invert().into_option().ok_or(Error::InvalidPrime)?;
530530

531531
debug_assert_eq!(dp.bits_precision(), p.bits_precision());

0 commit comments

Comments
 (0)