@@ -3,10 +3,8 @@ use crypto_bigint::{Limb, Reciprocal, Word};
33/// The type that fits any small prime from the table. 
44pub ( crate )  type  SmallPrime  = u16 ; 
55
6- const  SMALL_PRIMES_SIZE :  usize  = 2047 ; 
7- 
86/// The list of 2nd to 2048th primes (The 1st one, 2, is not included). 
9- pub ( crate )  const  SMALL_PRIMES :  [ SmallPrime ;  SMALL_PRIMES_SIZE ]  = [ 
7+ pub ( crate )  const  SMALL_PRIMES :  [ SmallPrime ;  2047 ]  = [ 
108    3 ,  5 ,  7 ,  11 ,  13 ,  17 ,  19 ,  23 ,  29 ,  31 ,  37 ,  41 ,  43 ,  47 ,  53 ,  59 ,  61 ,  67 ,  71 ,  73 ,  79 ,  83 ,  89 ,  97 , 
119    101 ,  103 ,  107 ,  109 ,  113 ,  127 ,  131 ,  137 ,  139 ,  149 ,  151 ,  157 ,  163 ,  167 ,  173 ,  179 ,  181 ,  191 ,  193 , 
1210    197 ,  199 ,  211 ,  223 ,  227 ,  229 ,  233 ,  239 ,  241 ,  251 ,  257 ,  263 ,  269 ,  271 ,  277 ,  281 ,  283 ,  293 ,  307 , 
@@ -147,10 +145,10 @@ pub(crate) const SMALL_PRIMES: [SmallPrime; SMALL_PRIMES_SIZE] = [
147145    17747 ,  17749 ,  17761 ,  17783 ,  17789 ,  17791 ,  17807 ,  17827 ,  17837 ,  17839 ,  17851 ,  17863 , 
148146] ; 
149147
150- const  fn  create_reciprocals ( )  -> [ Reciprocal ;  SMALL_PRIMES_SIZE ]  { 
151-     let  mut  arr = [ Reciprocal :: default ( ) ;  SMALL_PRIMES_SIZE ] ; 
148+ const  fn  create_reciprocals ( )  -> [ Reciprocal ;  SMALL_PRIMES . len ( ) ]  { 
149+     let  mut  arr = [ Reciprocal :: default ( ) ;  SMALL_PRIMES . len ( ) ] ; 
152150    let  mut  i = 0 ; 
153-     while  i < SMALL_PRIMES_SIZE  { 
151+     while  i < SMALL_PRIMES . len ( )  { 
154152        arr[ i]  = Reciprocal :: new ( 
155153            Limb ( SMALL_PRIMES [ i]  as  Word ) 
156154                . to_nz ( ) 
@@ -161,4 +159,28 @@ const fn create_reciprocals() -> [Reciprocal; SMALL_PRIMES_SIZE] {
161159    arr
162160} 
163161
164- pub ( crate )  const  RECIPROCALS :  [ Reciprocal ;  SMALL_PRIMES_SIZE ]  = create_reciprocals ( ) ; 
162+ pub ( crate )  const  RECIPROCALS :  [ Reciprocal ;  SMALL_PRIMES . len ( ) ]  = create_reciprocals ( ) ; 
163+ 
164+ #[ cfg( test) ]  
165+ mod  tests { 
166+     use  crypto_bigint:: { NonZero ,  Random ,  U256 } ; 
167+     use  rand_core:: OsRng ; 
168+ 
169+     use  super :: { create_reciprocals,  SMALL_PRIMES } ; 
170+ 
171+     #[ test]  
172+     fn  correctness ( )  { 
173+         let  reciprocals = create_reciprocals ( ) ; 
174+ 
175+         assert_eq ! ( reciprocals. len( ) ,  SMALL_PRIMES . len( ) ) ; 
176+ 
177+         for  ( reciprocal,  prime)  in  reciprocals. iter ( ) . zip ( SMALL_PRIMES . iter ( ) )  { 
178+             for  _ in  0 ..10  { 
179+                 let  x = U256 :: random ( & mut  OsRng ) ; 
180+                 let  r_ref = ( x % NonZero :: new ( U256 :: from ( * prime) ) . unwrap ( ) ) . as_limbs ( ) [ 0 ] ; 
181+                 let  r_test = x. rem_limb_with_reciprocal ( reciprocal) ; 
182+                 assert_eq ! ( r_ref,  r_test) ; 
183+             } 
184+         } 
185+     } 
186+ } 
0 commit comments