Skip to content

Commit 6675a21

Browse files
committed
Use checked_add() instead of wrapping_add()
1 parent 4222476 commit 6675a21

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/hazmat/lucas.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Lucas primality test.
22
use crypto_bigint::{
33
modular::runtime_mod::{DynResidue, DynResidueParams},
4-
Integer, Limb, Uint, Word,
4+
CheckedAdd, Integer, Limb, Uint, Word,
55
};
66

77
use super::{
@@ -187,7 +187,7 @@ fn decompose<const L: usize>(n: &Uint<L>) -> (u32, Uint<L>) {
187187
}
188188

189189
// This won't overflow since the original `n` was odd, so we right-shifted at least once.
190-
(s, n.wrapping_add(&Uint::<L>::ONE))
190+
(s, n.checked_add(&Uint::<L>::ONE).expect("Integer overflow"))
191191
}
192192

193193
/// The checks to perform in the Lucas test.

src/hazmat/miller_rabin.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rand_core::CryptoRngCore;
44

55
use crypto_bigint::{
66
modular::runtime_mod::{DynResidue, DynResidueParams},
7-
Integer, NonZero, RandomMod, Uint,
7+
CheckedAdd, Integer, NonZero, RandomMod, Uint,
88
};
99

1010
use super::Primality;
@@ -104,8 +104,11 @@ impl<const L: usize> MillerRabin<L> {
104104

105105
let range = self.candidate.wrapping_sub(&Uint::<L>::from(4u32));
106106
let range_nonzero = NonZero::new(range).unwrap();
107-
let random =
108-
Uint::<L>::random_mod(rng, &range_nonzero).wrapping_add(&Uint::<L>::from(3u32));
107+
// This should not overflow as long as `random_mod()` behaves according to the contract
108+
// (that is, returns a number within the given range).
109+
let random = Uint::<L>::random_mod(rng, &range_nonzero)
110+
.checked_add(&Uint::<L>::from(3u32))
111+
.expect("Integer overflow");
109112
self.test(&random)
110113
}
111114
}

src/hazmat/sieve.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use alloc::{vec, vec::Vec};
55

6-
use crypto_bigint::{Random, Uint};
6+
use crypto_bigint::{CheckedAdd, Random, Uint};
77
use rand_core::CryptoRngCore;
88

99
use crate::hazmat::precomputed::{SmallPrime, RECIPROCALS, SMALL_PRIMES};
@@ -152,7 +152,13 @@ impl<const L: usize> Sieve<L> {
152152
}
153153

154154
// Set the new base.
155-
self.base = self.base.wrapping_add(&self.incr.into());
155+
// Should not overflow since `incr` is never greater than `incr_limit`,
156+
// and the latter is chosen such that it doesn't overflow when added to `base`
157+
// (see the rest of this method).
158+
self.base = self
159+
.base
160+
.checked_add(&self.incr.into())
161+
.expect("Integer overflow");
156162

157163
self.incr = 0;
158164

@@ -210,7 +216,13 @@ impl<const L: usize> Sieve<L> {
210216
let result = if self.current_is_composite() {
211217
None
212218
} else {
213-
let mut num = self.base.wrapping_add(&self.incr.into());
219+
// The overflow should never happen here since `incr`
220+
// is never greater than `incr_limit`, and the latter is chosen such that
221+
// it does not overflow when added to `base` (see `update_residues()`).
222+
let mut num = self
223+
.base
224+
.checked_add(&self.incr.into())
225+
.expect("Integer overflow");
214226
if self.safe_primes {
215227
num = (num << 1) | Uint::<L>::ONE;
216228
}

0 commit comments

Comments
 (0)