11//! Lucas primality test.
22use core:: num:: NonZero ;
3- use crypto_bigint:: { Limb , MontyForm , MontyMultiplier , Odd , Square , UnsignedMontyForm , Word } ;
3+ use crypto_bigint:: { Limb , MontyForm , MontyMultiplier , Odd , Square , UnsignedWithMontyForm , Word } ;
44
55use super :: {
66 Primality ,
@@ -28,7 +28,7 @@ pub trait LucasBase {
2828 /// Given an odd integer, returns `Ok((P, abs(Q), is_negative(Q)))` on success,
2929 /// or `Err(Primality)` if the primality for the given integer was discovered
3030 /// during the search for a base.
31- fn generate < T : UnsignedMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > ;
31+ fn generate < T : UnsignedWithMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > ;
3232}
3333
3434/// "Method A" for selecting the base given in Baillie & Wagstaff[^Baillie1980],
@@ -45,7 +45,7 @@ pub trait LucasBase {
4545pub struct SelfridgeBase ;
4646
4747impl LucasBase for SelfridgeBase {
48- fn generate < T : UnsignedMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
48+ fn generate < T : UnsignedWithMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
4949 let mut abs_d = 5 ;
5050 let mut d_is_negative = false ;
5151 let n_is_small = n. bits_vartime ( ) < Word :: BITS ; // if true, `n` fits into one `Word`
@@ -109,7 +109,7 @@ impl LucasBase for SelfridgeBase {
109109pub struct AStarBase ;
110110
111111impl LucasBase for AStarBase {
112- fn generate < T : UnsignedMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
112+ fn generate < T : UnsignedWithMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
113113 SelfridgeBase . generate ( n) . map ( |( p, abs_q, q_is_negative) | {
114114 if abs_q == 1 && q_is_negative {
115115 ( 5 , 5 , false )
@@ -131,7 +131,7 @@ impl LucasBase for AStarBase {
131131pub struct BruteForceBase ;
132132
133133impl LucasBase for BruteForceBase {
134- fn generate < T : UnsignedMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
134+ fn generate < T : UnsignedWithMontyForm > ( & self , n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
135135 let mut p = 3 ;
136136 let mut attempts = 0 ;
137137
@@ -178,7 +178,7 @@ impl LucasBase for BruteForceBase {
178178/// For the given odd `n`, finds `s` and odd `d` such that `n + 1 == 2^s * d`.
179179fn decompose < T > ( n : & Odd < T > ) -> ( u32 , Odd < T > )
180180where
181- T : UnsignedMontyForm ,
181+ T : UnsignedWithMontyForm ,
182182{
183183 // Need to be careful here since `n + 1` can overflow.
184184 // Instead of adding 1 and counting trailing 0s, we count trailing ones on the original `n`.
@@ -320,7 +320,7 @@ pub enum LucasCheck {
320320/// [^FIPS]: FIPS-186.5 standard, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf>
321321pub fn lucas_test < T > ( candidate : Odd < T > , base : impl LucasBase , check : LucasCheck ) -> Primality
322322where
323- T : UnsignedMontyForm ,
323+ T : UnsignedWithMontyForm ,
324324{
325325 // The comments in this function use references in `LucasCheck`, plus this one:
326326 //
@@ -375,10 +375,10 @@ where
375375 let ( s, d) = decompose ( & candidate) ;
376376
377377 // Some constants in Montgomery form
378- let params = <T as UnsignedMontyForm >:: MontyForm :: new_params_vartime ( candidate. clone ( ) ) ;
378+ let params = <T as UnsignedWithMontyForm >:: MontyForm :: new_params_vartime ( candidate. clone ( ) ) ;
379379
380- let zero = <T as UnsignedMontyForm >:: MontyForm :: zero ( & params) ;
381- let one = <T as UnsignedMontyForm >:: MontyForm :: one ( & params) ;
380+ let zero = <T as UnsignedWithMontyForm >:: MontyForm :: zero ( & params) ;
381+ let one = <T as UnsignedWithMontyForm >:: MontyForm :: one ( & params) ;
382382 let two = one. clone ( ) + & one;
383383 let minus_two = -two. clone ( ) ;
384384
@@ -387,7 +387,7 @@ where
387387 let q = if q_is_one {
388388 one. clone ( )
389389 } else {
390- let abs_q = <T as UnsignedMontyForm >:: MontyForm :: new ( to_integer ( abs_q) , & params) ;
390+ let abs_q = <T as UnsignedWithMontyForm >:: MontyForm :: new ( to_integer ( abs_q) , & params) ;
391391 if q_is_negative { -abs_q } else { abs_q }
392392 } ;
393393
@@ -396,7 +396,7 @@ where
396396 let p = if p_is_one {
397397 one. clone ( )
398398 } else {
399- <T as UnsignedMontyForm >:: MontyForm :: new ( to_integer ( p) , & params)
399+ <T as UnsignedWithMontyForm >:: MontyForm :: new ( to_integer ( p) , & params)
400400 } ;
401401
402402 // Compute d-th element of Lucas sequence (U_d(P, Q), V_d(P, Q)), where:
@@ -415,15 +415,15 @@ where
415415
416416 // Starting with k = 0
417417 let mut vk = two. clone ( ) ; // keeps V_k
418- let mut uk = <T as UnsignedMontyForm >:: MontyForm :: zero ( & params) ; // keeps U_k
418+ let mut uk = <T as UnsignedWithMontyForm >:: MontyForm :: zero ( & params) ; // keeps U_k
419419 let mut qk = one. clone ( ) ; // keeps Q^k
420420
421- let mut temp = <T as UnsignedMontyForm >:: MontyForm :: zero ( & params) ;
421+ let mut temp = <T as UnsignedWithMontyForm >:: MontyForm :: zero ( & params) ;
422422
423- let mut mm = <<T as UnsignedMontyForm >:: MontyForm as MontyForm >:: Multiplier :: from ( & params) ;
423+ let mut mm = <<T as UnsignedWithMontyForm >:: MontyForm as MontyForm >:: Multiplier :: from ( & params) ;
424424
425425 // D in Montgomery representation - note that it can be negative.
426- let abs_d = <T as UnsignedMontyForm >:: MontyForm :: new ( to_integer ( abs_d) , & params) ;
426+ let abs_d = <T as UnsignedWithMontyForm >:: MontyForm :: new ( to_integer ( abs_d) , & params) ;
427427 let d_m = if d_is_negative { -abs_d } else { abs_d } ;
428428
429429 for i in ( 0 ..d. bits_vartime ( ) ) . rev ( ) {
@@ -606,7 +606,7 @@ mod tests {
606606
607607 use alloc:: format;
608608
609- use crypto_bigint:: { Odd , U64 , U128 , Uint , UnsignedMontyForm , Word } ;
609+ use crypto_bigint:: { Odd , U64 , U128 , Uint , UnsignedWithMontyForm , Word } ;
610610
611611 #[ cfg( feature = "tests-exhaustive" ) ]
612612 use num_prime:: nt_funcs:: is_prime64;
@@ -657,7 +657,7 @@ mod tests {
657657 struct TestBase ;
658658
659659 impl LucasBase for TestBase {
660- fn generate < T : UnsignedMontyForm > ( & self , _n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
660+ fn generate < T : UnsignedWithMontyForm > ( & self , _n : & Odd < T > ) -> Result < ( Word , Word , bool ) , Primality > {
661661 Ok ( ( 5 , 5 , false ) )
662662 }
663663 }
0 commit comments