| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #include <tommath.h>
 | 
					
						
							|  |  |  | #ifdef BN_MP_EXPTMOD_C
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +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-02-28 16:08:34 +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-02-28 16:08:34 +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-02-28 16:08:34 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* this is a shell function that calls either the normal or Montgomery
 | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |  * exptmod functions.  Originally the call to the montgomery code was | 
					
						
							| 
									
										
										
										
											2003-02-28 16:09:08 +00:00
										 |  |  |  * embedded in the normal function but that wasted alot of stack space | 
					
						
							|  |  |  |  * for nothing (since 99% of the time the Montgomery code would be called) | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2003-12-24 18:59:22 +00:00
										 |  |  | int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   int dr; | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* modulus P must be positive */ | 
					
						
							|  |  |  |   if (P->sign == MP_NEG) { | 
					
						
							|  |  |  |      return MP_VAL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* if exponent X is negative we have to recurse */ | 
					
						
							|  |  |  |   if (X->sign == MP_NEG) { | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #ifdef BN_MP_INVMOD_C
 | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |      mp_int tmpG, tmpX; | 
					
						
							|  |  |  |      int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      /* first compute 1/G mod P */ | 
					
						
							|  |  |  |      if ((err = mp_init(&tmpG)) != MP_OKAY) { | 
					
						
							|  |  |  |         return err; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |      if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) { | 
					
						
							|  |  |  |         mp_clear(&tmpG); | 
					
						
							|  |  |  |         return err; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      /* now get |X| */ | 
					
						
							|  |  |  |      if ((err = mp_init(&tmpX)) != MP_OKAY) { | 
					
						
							|  |  |  |         mp_clear(&tmpG); | 
					
						
							|  |  |  |         return err; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |      if ((err = mp_abs(X, &tmpX)) != MP_OKAY) { | 
					
						
							|  |  |  |         mp_clear_multi(&tmpG, &tmpX, NULL); | 
					
						
							|  |  |  |         return err; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-29 13:35:26 +00:00
										 |  |  |      /* and now compute (1/G)**|X| instead of G**X [X < 0] */ | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |      err = mp_exptmod(&tmpG, &tmpX, P, Y); | 
					
						
							|  |  |  |      mp_clear_multi(&tmpG, &tmpX, NULL); | 
					
						
							|  |  |  |      return err; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #else 
 | 
					
						
							|  |  |  |      /* no invmod */ | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |      return MP_VAL; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-12 08:40:15 +00:00
										 |  |  | /* modified diminished radix reduction */ | 
					
						
							| 
									
										
										
										
											2005-08-01 16:37:28 +00:00
										 |  |  | #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
 | 
					
						
							| 
									
										
										
										
											2005-02-12 08:40:15 +00:00
										 |  |  |   if (mp_reduce_is_2k_l(P) == MP_YES) { | 
					
						
							|  |  |  |      return s_mp_exptmod(G, X, P, Y, 1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #ifdef BN_MP_DR_IS_MODULUS_C
 | 
					
						
							| 
									
										
										
										
											2003-08-05 01:24:44 +00:00
										 |  |  |   /* is it a DR modulus? */ | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |   dr = mp_dr_is_modulus(P); | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2005-02-12 08:40:15 +00:00
										 |  |  |   /* default to no */ | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |   dr = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-08-05 01:24:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #ifdef BN_MP_REDUCE_IS_2K_C
 | 
					
						
							| 
									
										
										
										
											2005-02-12 08:40:15 +00:00
										 |  |  |   /* if not, is it a unrestricted DR modulus? */ | 
					
						
							| 
									
										
										
										
											2003-05-29 13:35:26 +00:00
										 |  |  |   if (dr == 0) { | 
					
						
							|  |  |  |      dr = mp_reduce_is_2k(P) << 1; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-06-06 19:35:48 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2005-02-12 08:40:15 +00:00
										 |  |  |   /* if the modulus is odd or dr != 0 use the montgomery method */ | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #ifdef BN_MP_EXPTMOD_FAST_C
 | 
					
						
							| 
									
										
										
										
											2003-06-06 19:35:48 +00:00
										 |  |  |   if (mp_isodd (P) == 1 || dr !=  0) { | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |     return mp_exptmod_fast (G, X, P, Y, dr); | 
					
						
							| 
									
										
										
										
											2003-02-28 16:09:08 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef BN_S_MP_EXPTMOD_C
 | 
					
						
							| 
									
										
										
										
											2003-08-05 01:24:44 +00:00
										 |  |  |     /* otherwise use the generic Barrett reduction technique */ | 
					
						
							| 
									
										
										
										
											2005-02-12 08:40:15 +00:00
										 |  |  |     return s_mp_exptmod (G, X, P, Y, 0); | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     /* no exptmod for evens */ | 
					
						
							|  |  |  |     return MP_VAL; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef BN_MP_EXPTMOD_FAST_C
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:09:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-08-01 16:37:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* $Source$ */ | 
					
						
							|  |  |  | /* $Revision$ */ | 
					
						
							|  |  |  | /* $Date$ */ |