Skip to content

Commit 7c9379d

Browse files
committed
Fix size mismatches
1 parent 442f6e1 commit 7c9379d

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

src/hazmat/jacobi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(crate) fn jacobi_symbol_vartime<T: Integer>(
8484
};
8585

8686
// A degenerate case.
87-
if abs_a == 1 || p_long.as_ref() == &T::one() {
87+
if abs_a == 1 || p_long.as_ref() == &T::one_like(p_long) {
8888
return result;
8989
}
9090

src/hazmat/lucas.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Lucas primality test.
2-
use crypto_bigint::{Integer, Monty, Odd, Square, Word};
2+
use crypto_bigint::{Integer, Limb, Monty, Odd, Square, Word};
33

44
use super::{
55
gcd::gcd_vartime,
@@ -161,7 +161,7 @@ impl LucasBase for BruteForceBase {
161161
// Since the loop proceeds in increasing P and starts with P - 2 == 1,
162162
// the shared prime factor must be P + 2.
163163
// If P + 2 == n, then n is prime; otherwise P + 2 is a proper factor of n.
164-
let primality = if n.as_ref() == &T::from(p + 2) {
164+
let primality = if n.as_ref() == &T::from_limb_like(Limb::from(p + 2), n.as_ref()) {
165165
Primality::Prime
166166
} else {
167167
Primality::Composite
@@ -182,6 +182,7 @@ fn decompose<T: Integer>(n: &Odd<T>) -> (u32, Odd<T>) {
182182
// Need to be careful here since `n + 1` can overflow.
183183
// Instead of adding 1 and counting trailing 0s, we count trailing ones on the original `n`.
184184

185+
let one = T::one_like(n);
185186
let s = n.trailing_ones_vartime();
186187
let d = if s < n.bits_precision() {
187188
// The shift won't overflow because of the check above.
@@ -190,10 +191,10 @@ fn decompose<T: Integer>(n: &Odd<T>) -> (u32, Odd<T>) {
190191
n.as_ref()
191192
.overflowing_shr_vartime(s)
192193
.expect("shift should be within range by construction")
193-
.checked_add(&T::one())
194+
.checked_add(&one)
194195
.expect("addition should not overflow by construction")
195196
} else {
196-
T::one()
197+
one
197198
};
198199

199200
(s, Odd::new(d).expect("`d` should be odd by construction"))
@@ -293,6 +294,9 @@ pub fn lucas_test<T: Integer>(
293294
// R. Crandall, C. Pomerance, "Prime numbers: a computational perspective",
294295
// 2nd ed., Springer (2005) (ISBN: 0-387-25282-7, 978-0387-25282-7)
295296

297+
// A word-to-big integer conversion helper
298+
let to_integer = |x: Word| T::from_limb_like(Limb::from(x), candidate.as_ref());
299+
296300
// Find the base for the Lucas sequence.
297301
let (p, abs_q, q_is_negative) = match base.generate(candidate) {
298302
Ok(pq) => pq,
@@ -328,7 +332,7 @@ pub fn lucas_test<T: Integer>(
328332
// it does not noticeably affect the performance.
329333
if abs_q != 1
330334
&& gcd_vartime(candidate.as_ref(), abs_q) != 1
331-
&& candidate.as_ref() > &T::from(abs_q)
335+
&& candidate.as_ref() > &to_integer(abs_q)
332336
{
333337
return Primality::Composite;
334338
}
@@ -351,7 +355,7 @@ pub fn lucas_test<T: Integer>(
351355
let q = if q_is_one {
352356
one.clone()
353357
} else {
354-
let abs_q = <T as Integer>::Monty::new(T::from(abs_q), params.clone());
358+
let abs_q = <T as Integer>::Monty::new(to_integer(abs_q), params.clone());
355359
if q_is_negative {
356360
-abs_q
357361
} else {
@@ -364,7 +368,7 @@ pub fn lucas_test<T: Integer>(
364368
let p = if p_is_one {
365369
one.clone()
366370
} else {
367-
<T as Integer>::Monty::new(T::from(p), params.clone())
371+
<T as Integer>::Monty::new(to_integer(p), params.clone())
368372
};
369373

370374
// Compute d-th element of Lucas sequence (U_d(P, Q), V_d(P, Q)), where:
@@ -387,7 +391,7 @@ pub fn lucas_test<T: Integer>(
387391
let mut qk = one.clone(); // keeps Q^k
388392

389393
// D in Montgomery representation - note that it can be negative.
390-
let abs_d = <T as Integer>::Monty::new(T::from(abs_d), params);
394+
let abs_d = <T as Integer>::Monty::new(to_integer(abs_d), params);
391395
let d_m = if d_is_negative { -abs_d } else { abs_d };
392396

393397
for i in (0..d.bits_vartime()).rev() {

src/hazmat/miller_rabin.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Miller-Rabin primality test.
22
3-
use crypto_bigint::{Integer, Monty, NonZero, Odd, PowBoundedExp, RandomMod, Square};
3+
use crypto_bigint::{Integer, Limb, Monty, NonZero, Odd, PowBoundedExp, RandomMod, Square};
44
use rand_core::CryptoRngCore;
55

66
use super::Primality;
@@ -28,14 +28,16 @@ impl<T: Integer + RandomMod> MillerRabin<T> {
2828
/// Initializes a Miller-Rabin test for `candidate`.
2929
pub fn new(candidate: &Odd<T>) -> Self {
3030
let params = <T as Integer>::Monty::new_params_vartime(candidate.clone());
31-
let one = <T as Integer>::Monty::one(params.clone());
32-
let minus_one = -one.clone();
31+
let m_one = <T as Integer>::Monty::one(params.clone());
32+
let m_minus_one = -m_one.clone();
33+
34+
let one = T::one_like(candidate.as_ref());
3335

3436
// Find `s` and odd `d` such that `candidate - 1 == 2^s * d`.
35-
let (s, d) = if candidate.as_ref() == &T::one() {
36-
(0, T::one())
37+
let (s, d) = if candidate.as_ref() == &one {
38+
(0, one)
3739
} else {
38-
let candidate_minus_one = candidate.wrapping_sub(&T::one());
40+
let candidate_minus_one = candidate.wrapping_sub(&one);
3941
let s = candidate_minus_one.trailing_zeros_vartime();
4042
// Will not overflow because `candidate` is odd and greater than 1.
4143
let d = candidate_minus_one
@@ -48,8 +50,8 @@ impl<T: Integer + RandomMod> MillerRabin<T> {
4850
candidate: candidate.as_ref().clone(),
4951
bit_length: candidate.bits_vartime(),
5052
montgomery_params: params,
51-
one,
52-
minus_one,
53+
one: m_one,
54+
minus_one: m_minus_one,
5355
s,
5456
d,
5557
}
@@ -85,7 +87,7 @@ impl<T: Integer + RandomMod> MillerRabin<T> {
8587

8688
/// Perform a Miller-Rabin check with base 2.
8789
pub fn test_base_two(&self) -> Primality {
88-
self.test(&T::from(2u32))
90+
self.test(&T::from_limb_like(Limb::from(2u32), &self.candidate))
8991
}
9092

9193
/// Perform a Miller-Rabin check with a random base (in the range `[3, candidate-2]`)

src/hazmat/sieve.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn random_odd_integer<T: Integer + RandomBits>(
1919
let bit_length = bit_length.get();
2020

2121
let mut random = T::random_bits_with_precision(rng, bit_length, bits_precision);
22-
22+
assert!(random.bits_precision() == bits_precision);
2323
// Make it odd
2424
random.set_bit_vartime(0, true);
2525

@@ -100,7 +100,7 @@ impl<T: Integer> Sieve<T> {
100100
base = T::from(3u32);
101101
} else {
102102
// Adjust the base so that we hit odd numbers when incrementing it by 2.
103-
base |= T::one();
103+
base |= T::one_like(start);
104104
}
105105

106106
// Only calculate residues by primes up to and not including `base`,
@@ -158,9 +158,12 @@ impl<T: Integer> Sieve<T> {
158158
}
159159

160160
// Find the increment limit.
161-
let max_value = match T::one().overflowing_shl_vartime(self.max_bit_length).into() {
161+
let max_value = match T::one_like(&self.base)
162+
.overflowing_shl_vartime(self.max_bit_length)
163+
.into()
164+
{
162165
Some(val) => val,
163-
None => T::one(),
166+
None => T::one_like(&self.base),
164167
};
165168
let incr_limit = max_value.wrapping_sub(&self.base);
166169
self.incr_limit = if incr_limit > T::from(INCR_LIMIT) {
@@ -219,7 +222,7 @@ impl<T: Integer> Sieve<T> {
219222
.checked_add(&self.incr.into())
220223
.expect("addition should not overflow by construction");
221224
if self.safe_primes {
222-
num = num.wrapping_shl_vartime(1) | T::one();
225+
num = num.wrapping_shl_vartime(1) | T::one_like(&self.base);
223226
}
224227
Some(num)
225228
};

src/presets.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::num::NonZeroU32;
22

3-
use crypto_bigint::{Integer, Odd, RandomBits, RandomMod};
3+
use crypto_bigint::{Integer, Limb, Odd, RandomBits, RandomMod};
44
use rand_core::CryptoRngCore;
55

66
#[cfg(feature = "default-rng")]
@@ -132,7 +132,7 @@ pub fn generate_safe_prime_with_rng<T: Integer + RandomBits + RandomMod>(
132132
/// Math. Comp. 90 1931-1955 (2021),
133133
/// DOI: [10.1090/mcom/3616](https://doi.org/10.1090/mcom/3616)
134134
pub fn is_prime_with_rng<T: Integer + RandomMod>(rng: &mut impl CryptoRngCore, num: &T) -> bool {
135-
if num == &T::from(2u32) {
135+
if num == &T::from_limb_like(Limb::from(2u32), num) {
136136
return true;
137137
}
138138

@@ -154,7 +154,7 @@ pub fn is_safe_prime_with_rng<T: Integer + RandomMod>(
154154
// Since, by the definition of safe prime, `(num - 1) / 2` must also be prime,
155155
// and therefore odd, `num` has to be equal to 3 modulo 4.
156156
// 5 is the only exception, so we check for it.
157-
if num == &T::from(5u32) {
157+
if num == &T::from_limb_like(Limb::from(5u32), num) {
158158
return true;
159159
}
160160
if num.as_ref()[0].0 & 3 != 3 {

0 commit comments

Comments
 (0)