@@ -4,7 +4,7 @@ use rand_core::CryptoRngCore;
44
55use crypto_bigint:: {
66 modular:: runtime_mod:: { DynResidue , DynResidueParams } ,
7- Integer , NonZero , RandomMod , Uint , Zero ,
7+ Integer , NonZero , RandomMod , Uint ,
88} ;
99
1010use super :: Primality ;
@@ -23,7 +23,7 @@ pub struct MillerRabin<const L: usize> {
2323 montgomery_params : DynResidueParams < L > ,
2424 one : DynResidue < L > ,
2525 minus_one : DynResidue < L > ,
26- s : u32 ,
26+ s : usize ,
2727 d : Uint < L > ,
2828}
2929
@@ -35,7 +35,12 @@ impl<const L: usize> MillerRabin<L> {
3535 let params = DynResidueParams :: < L > :: new ( candidate) ;
3636 let one = DynResidue :: < L > :: one ( params) ;
3737 let minus_one = -one;
38- let ( s, d) = decompose ( candidate) ;
38+
39+ // Find `s` and odd `d` such that `candidate - 1 == 2^s * d`.
40+ let candidate_minus_one = candidate. wrapping_sub ( & Uint :: < L > :: ONE ) ;
41+ let s = candidate_minus_one. trailing_zeros ( ) ;
42+ let d = candidate_minus_one >> s;
43+
3944 Self {
4045 candidate : * candidate,
4146 montgomery_params : params,
@@ -86,24 +91,6 @@ impl<const L: usize> MillerRabin<L> {
8691 }
8792}
8893
89- /// For the given odd `n`, finds `s` and odd `d` such that `n - 1 == 2^s * d`.
90- fn decompose < const L : usize > ( n : & Uint < L > ) -> ( u32 , Uint < L > ) {
91- let mut d = n. wrapping_sub ( & Uint :: < L > :: ONE ) ;
92- let mut s = 0 ;
93-
94- // Corner case, exit early to prevent being stuck in the loop.
95- if d. is_zero ( ) . into ( ) {
96- return ( 0 , Uint :: < L > :: ZERO ) ;
97- }
98-
99- while d. is_even ( ) . into ( ) {
100- d >>= 1 ;
101- s += 1 ;
102- }
103-
104- ( s, d)
105- }
106-
10794#[ cfg( test) ]
10895mod tests {
10996
0 commit comments