| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #include <tommath.h>
 | 
					
						
							|  |  |  | #ifdef BN_MP_PRIME_MILLER_RABIN_C
 | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-08-05 01:24:44 +00:00
										 |  |  |  * LibTomMath is a library that provides multiple-precision | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  * integer arithmetic as well as number theoretic functionality. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-08-05 01:24:44 +00:00
										 |  |  |  * The library was designed directly after the MPI library by | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  * Michael Fromberger but has been written from scratch with | 
					
						
							|  |  |  |  * additional optimizations in place. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The library is free for all purposes without any express | 
					
						
							|  |  |  |  * guarantee it works. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2007-04-18 09:58:18 +00:00
										 |  |  |  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
 | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Miller-Rabin test of "a" to the base of "b" as described in 
 | 
					
						
							|  |  |  |  * HAC pp. 139 Algorithm 4.24 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Sets result to 0 if definitely composite or 1 if probably prime. | 
					
						
							|  |  |  |  * Randomly the chance of error is no more than 1/4 and often  | 
					
						
							|  |  |  |  * very much lower. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2003-12-24 18:59:22 +00:00
										 |  |  | int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   mp_int  n1, y, r; | 
					
						
							|  |  |  |   int     s, j, err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* default */ | 
					
						
							| 
									
										
										
										
											2003-12-24 18:59:22 +00:00
										 |  |  |   *result = MP_NO; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-12 14:31:43 +00:00
										 |  |  |   /* ensure b > 1 */ | 
					
						
							|  |  |  |   if (mp_cmp_d(b, 1) != MP_GT) { | 
					
						
							|  |  |  |      return MP_VAL; | 
					
						
							|  |  |  |   }      | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   /* get n1 = a - 1 */ | 
					
						
							|  |  |  |   if ((err = mp_init_copy (&n1, a)) != MP_OKAY) { | 
					
						
							|  |  |  |     return err; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_N1; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-02 15:39:39 +00:00
										 |  |  |   /* set 2**s * r = n1 */ | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_N1; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2003-07-12 14:31:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* count the number of least significant bits
 | 
					
						
							|  |  |  |    * which are zero | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2003-07-02 15:39:39 +00:00
										 |  |  |   s = mp_cnt_lsb(&r); | 
					
						
							| 
									
										
										
										
											2003-07-12 14:31:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-16 00:26:58 +00:00
										 |  |  |   /* now divide n - 1 by 2**s */ | 
					
						
							| 
									
										
										
										
											2003-07-02 15:39:39 +00:00
										 |  |  |   if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_R; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-02 15:39:39 +00:00
										 |  |  |   /* compute y = b**r mod a */ | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   if ((err = mp_init (&y)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_R; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* if y != 1 and y != n1 do */ | 
					
						
							|  |  |  |   if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) { | 
					
						
							|  |  |  |     j = 1; | 
					
						
							|  |  |  |     /* while j <= s-1 and y != n1 */ | 
					
						
							|  |  |  |     while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) { | 
					
						
							|  |  |  |       if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |          goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* if y == 1 then composite */ | 
					
						
							|  |  |  |       if (mp_cmp_d (&y, 1) == MP_EQ) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |          goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ++j; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* if y != n1 then composite */ | 
					
						
							|  |  |  |     if (mp_cmp (&y, &n1) != MP_EQ) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |       goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* probably prime now */ | 
					
						
							| 
									
										
										
										
											2003-12-24 18:59:22 +00:00
										 |  |  |   *result = MP_YES; | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  | LBL_Y:mp_clear (&y); | 
					
						
							|  |  |  | LBL_R:mp_clear (&r); | 
					
						
							|  |  |  | LBL_N1:mp_clear (&n1); | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   return err; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-08-01 16:37:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* $Source$ */ | 
					
						
							|  |  |  | /* $Revision$ */ | 
					
						
							|  |  |  | /* $Date$ */ |