| 
									
										
										
										
											2018-05-02 21:43:17 +02:00
										 |  |  | #include "tommath_private.h"
 | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #ifdef BN_MP_DR_REDUCE_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. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-29 17:56:20 +01:00
										 |  |  |  * SPDX-License-Identifier: Unlicense | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-29 13:35:26 +00:00
										 |  |  | /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
 | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |  * Based on algorithm from the paper | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * "Generating Efficient Primes for Discrete Log Cryptosystems" | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |  *                 Chae Hoon Lim, Pil Joong Lee, | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  *          POSTECH Information Research Laboratories | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The modulus must be of a special format [see manual] | 
					
						
							| 
									
										
										
										
											2003-05-29 13:35:26 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Has been modified to use algorithm 7.10 from the LTM book instead | 
					
						
							| 
									
										
										
										
											2003-08-29 14:06:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-09-19 22:43:07 +00:00
										 |  |  |  * Input x must be in the range 0 <= x <= (n-1)**2 | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-09-20 16:59:43 +02:00
										 |  |  | int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k) | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    int      err, i, m; | 
					
						
							|  |  |  |    mp_word  r; | 
					
						
							|  |  |  |    mp_digit mu, *tmpx1, *tmpx2; | 
					
						
							| 
									
										
										
										
											2003-09-19 22:43:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* m = digits in modulus */ | 
					
						
							|  |  |  |    m = n->used; | 
					
						
							| 
									
										
										
										
											2003-09-19 22:43:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* ensure that "x" has at least 2m digits */ | 
					
						
							|  |  |  |    if (x->alloc < (m + m)) { | 
					
						
							|  |  |  |       if ((err = mp_grow(x, m + m)) != MP_OKAY) { | 
					
						
							|  |  |  |          return err; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |    } | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* top of loop, this is where the code resumes if
 | 
					
						
							|  |  |  |     * another reduction pass is required. | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2003-05-29 13:35:26 +00:00
										 |  |  | top: | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* aliases for digits */ | 
					
						
							|  |  |  |    /* alias for lower half of x */ | 
					
						
							|  |  |  |    tmpx1 = x->dp; | 
					
						
							| 
									
										
										
										
											2003-09-19 22:43:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* alias for upper half of x, or x/B**m */ | 
					
						
							|  |  |  |    tmpx2 = x->dp + m; | 
					
						
							| 
									
										
										
										
											2003-09-19 22:43:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* set carry to zero */ | 
					
						
							|  |  |  |    mu = 0; | 
					
						
							| 
									
										
										
										
											2003-09-19 22:43:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* compute (x mod B**m) + k * [x/B**m] inline and inplace */ | 
					
						
							|  |  |  |    for (i = 0; i < m; i++) { | 
					
						
							| 
									
										
										
										
											2017-10-15 19:58:35 +02:00
										 |  |  |       r         = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu; | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |       *tmpx1++  = (mp_digit)(r & MP_MASK); | 
					
						
							|  |  |  |       mu        = (mp_digit)(r >> ((mp_word)DIGIT_BIT)); | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    } | 
					
						
							| 
									
										
										
										
											2003-08-29 14:06:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* set final carry */ | 
					
						
							|  |  |  |    *tmpx1++ = mu; | 
					
						
							| 
									
										
										
										
											2003-08-29 14:06:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* zero words above m */ | 
					
						
							|  |  |  |    for (i = m + 1; i < x->used; i++) { | 
					
						
							| 
									
										
										
										
											2003-05-29 13:35:26 +00:00
										 |  |  |       *tmpx1++ = 0; | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    } | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* clamp, sub and return */ | 
					
						
							|  |  |  |    mp_clamp(x); | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:23:48 +02:00
										 |  |  |    /* if x >= n then subtract and reduce again
 | 
					
						
							|  |  |  |     * Each successive "recursion" makes the input smaller and smaller. | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |    if (mp_cmp_mag(x, n) != MP_LT) { | 
					
						
							|  |  |  |       if ((err = s_mp_sub(x, n, x)) != MP_OKAY) { | 
					
						
							|  |  |  |          return err; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       goto top; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  |    return MP_OKAY; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-08-01 16:37:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-28 16:27:26 +02:00
										 |  |  | /* ref:         $Format:%D$ */ | 
					
						
							|  |  |  | /* git commit:  $Format:%H$ */ | 
					
						
							|  |  |  | /* commit time: $Format:%ai$ */ |