trim trailing spaces
This commit is contained in:
		
							parent
							
								
									a0a86c696a
								
							
						
					
					
						commit
						15681f9a12
					
				| @ -15,10 +15,10 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes the modular inverse via binary extended euclidean algorithm, 
 | /* computes the modular inverse via binary extended euclidean algorithm,
 | ||||||
|  * that is c = 1/a mod b  |  * that is c = 1/a mod b | ||||||
|  * |  * | ||||||
|  * Based on slow invmod except this is optimized for the case where b is  |  * Based on slow invmod except this is optimized for the case where b is | ||||||
|  * odd as per HAC Note 14.64 on pp. 610 |  * odd as per HAC Note 14.64 on pp. 610 | ||||||
|  */ |  */ | ||||||
| int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) | int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) | ||||||
|  | |||||||
| @ -17,15 +17,15 @@ | |||||||
| 
 | 
 | ||||||
| /* Fast (comba) multiplier
 | /* Fast (comba) multiplier
 | ||||||
|  * |  * | ||||||
|  * This is the fast column-array [comba] multiplier.  It is  |  * This is the fast column-array [comba] multiplier.  It is | ||||||
|  * designed to compute the columns of the product first  |  * designed to compute the columns of the product first | ||||||
|  * then handle the carries afterwards.  This has the effect  |  * then handle the carries afterwards.  This has the effect | ||||||
|  * of making the nested loops that compute the columns very |  * of making the nested loops that compute the columns very | ||||||
|  * simple and schedulable on super-scalar processors. |  * simple and schedulable on super-scalar processors. | ||||||
|  * |  * | ||||||
|  * This has been modified to produce a variable number of  |  * This has been modified to produce a variable number of | ||||||
|  * digits of output so if say only a half-product is required  |  * digits of output so if say only a half-product is required | ||||||
|  * you don't have to compute the upper half (a feature  |  * you don't have to compute the upper half (a feature | ||||||
|  * required for fast Barrett reduction). |  * required for fast Barrett reduction). | ||||||
|  * |  * | ||||||
|  * Based on Algorithm 14.12 on pp.595 of HAC. |  * Based on Algorithm 14.12 on pp.595 of HAC. | ||||||
| @ -49,7 +49,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | |||||||
| 
 | 
 | ||||||
|   /* clear the carry */ |   /* clear the carry */ | ||||||
|   _W = 0; |   _W = 0; | ||||||
|   for (ix = 0; ix < pa; ix++) {  |   for (ix = 0; ix < pa; ix++) { | ||||||
|       int      tx, ty; |       int      tx, ty; | ||||||
|       int      iy; |       int      iy; | ||||||
|       mp_digit *tmpx, *tmpy; |       mp_digit *tmpx, *tmpy; | ||||||
| @ -62,7 +62,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | |||||||
|       tmpx = a->dp + tx; |       tmpx = a->dp + tx; | ||||||
|       tmpy = b->dp + ty; |       tmpy = b->dp + ty; | ||||||
| 
 | 
 | ||||||
|       /* this is the number of times the loop will iterrate, essentially 
 |       /* this is the number of times the loop will iterrate, essentially
 | ||||||
|          while (tx++ < a->used && ty-- >= 0) { ... } |          while (tx++ < a->used && ty-- >= 0) { ... } | ||||||
|        */ |        */ | ||||||
|       iy = MIN(a->used-tx, ty+1); |       iy = MIN(a->used-tx, ty+1); | ||||||
|  | |||||||
| @ -41,7 +41,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | |||||||
|   /* number of output digits to produce */ |   /* number of output digits to produce */ | ||||||
|   pa = a->used + b->used; |   pa = a->used + b->used; | ||||||
|   _W = 0; |   _W = 0; | ||||||
|   for (ix = digs; ix < pa; ix++) {  |   for (ix = digs; ix < pa; ix++) { | ||||||
|       int      tx, ty, iy; |       int      tx, ty, iy; | ||||||
|       mp_digit *tmpx, *tmpy; |       mp_digit *tmpx, *tmpy; | ||||||
| 
 | 
 | ||||||
| @ -53,7 +53,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | |||||||
|       tmpx = a->dp + tx; |       tmpx = a->dp + tx; | ||||||
|       tmpy = b->dp + ty; |       tmpy = b->dp + ty; | ||||||
| 
 | 
 | ||||||
|       /* this is the number of times the loop will iterrate, essentially its 
 |       /* this is the number of times the loop will iterrate, essentially its
 | ||||||
|          while (tx++ < a->used && ty-- >= 0) { ... } |          while (tx++ < a->used && ty-- >= 0) { ... } | ||||||
|        */ |        */ | ||||||
|       iy = MIN(a->used-tx, ty+1); |       iy = MIN(a->used-tx, ty+1); | ||||||
| @ -69,7 +69,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | |||||||
|       /* make next carry */ |       /* make next carry */ | ||||||
|       _W = _W >> ((mp_word)DIGIT_BIT); |       _W = _W >> ((mp_word)DIGIT_BIT); | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   /* setup dest */ |   /* setup dest */ | ||||||
|   olduse  = c->used; |   olduse  = c->used; | ||||||
|   c->used = pa; |   c->used = pa; | ||||||
|  | |||||||
| @ -16,10 +16,10 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* the jist of squaring...
 | /* the jist of squaring...
 | ||||||
|  * you do like mult except the offset of the tmpx [one that  |  * you do like mult except the offset of the tmpx [one that | ||||||
|  * starts closer to zero] can't equal the offset of tmpy.   |  * starts closer to zero] can't equal the offset of tmpy. | ||||||
|  * So basically you set up iy like before then you min it with |  * So basically you set up iy like before then you min it with | ||||||
|  * (ty-tx) so that it never happens.  You double all those  |  * (ty-tx) so that it never happens.  You double all those | ||||||
|  * you add in the inner loop |  * you add in the inner loop | ||||||
| 
 | 
 | ||||||
| After that loop you do the squares and add them in. | After that loop you do the squares and add them in. | ||||||
| @ -41,7 +41,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b) | |||||||
| 
 | 
 | ||||||
|   /* number of output digits to produce */ |   /* number of output digits to produce */ | ||||||
|   W1 = 0; |   W1 = 0; | ||||||
|   for (ix = 0; ix < pa; ix++) {  |   for (ix = 0; ix < pa; ix++) { | ||||||
|       int      tx, ty, iy; |       int      tx, ty, iy; | ||||||
|       mp_word  _W; |       mp_word  _W; | ||||||
|       mp_digit *tmpy; |       mp_digit *tmpy; | ||||||
| @ -62,7 +62,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b) | |||||||
|        */ |        */ | ||||||
|       iy = MIN(a->used-tx, ty+1); |       iy = MIN(a->used-tx, ty+1); | ||||||
| 
 | 
 | ||||||
|       /* now for squaring tx can never equal ty 
 |       /* now for squaring tx can never equal ty
 | ||||||
|        * we halve the distance since they approach at a rate of 2x |        * we halve the distance since they approach at a rate of 2x | ||||||
|        * and we have to round because odd cases need to be executed |        * and we have to round because odd cases need to be executed | ||||||
|        */ |        */ | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes a = 2**b 
 | /* computes a = 2**b
 | ||||||
|  * |  * | ||||||
|  * Simple algorithm which zeroes the int, grows it then just sets one bit |  * Simple algorithm which zeroes the int, grows it then just sets one bit | ||||||
|  * as required. |  * as required. | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* b = |a| 
 | /* b = |a|
 | ||||||
|  * |  * | ||||||
|  * Simple function copies the input and fixes the sign to positive |  * Simple function copies the input and fixes the sign to positive | ||||||
|  */ |  */ | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* trim unused digits 
 | /* trim unused digits
 | ||||||
|  * |  * | ||||||
|  * This is used to ensure that leading zero digits are |  * This is used to ensure that leading zero digits are | ||||||
|  * trimed and the leading "used" digit will be non-zero |  * trimed and the leading "used" digit will be non-zero | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ | |||||||
|  */ |  */ | ||||||
| #include <stdarg.h> | #include <stdarg.h> | ||||||
| 
 | 
 | ||||||
| void mp_clear_multi(mp_int *mp, ...)  | void mp_clear_multi(mp_int *mp, ...) | ||||||
| { | { | ||||||
|     mp_int* next_mp = mp; |     mp_int* next_mp = mp; | ||||||
|     va_list args; |     va_list args; | ||||||
|  | |||||||
| @ -27,7 +27,7 @@ mp_cmp (mp_int * a, mp_int * b) | |||||||
|         return MP_GT; |         return MP_GT; | ||||||
|      } |      } | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   /* compare digits */ |   /* compare digits */ | ||||||
|   if (a->sign == MP_NEG) { |   if (a->sign == MP_NEG) { | ||||||
|      /* if negative compare opposite direction */ |      /* if negative compare opposite direction */ | ||||||
|  | |||||||
| @ -25,7 +25,7 @@ int mp_cmp_mag (mp_int * a, mp_int * b) | |||||||
|   if (a->used > b->used) { |   if (a->used > b->used) { | ||||||
|     return MP_GT; |     return MP_GT; | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   if (a->used < b->used) { |   if (a->used < b->used) { | ||||||
|     return MP_LT; |     return MP_LT; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| static const int lnz[16] = {  | static const int lnz[16] = { | ||||||
|    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 |    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -29,7 +29,7 @@ mp_count_bits (mp_int * a) | |||||||
| 
 | 
 | ||||||
|   /* get number of digits and add that */ |   /* get number of digits and add that */ | ||||||
|   r = (a->used - 1) * DIGIT_BIT; |   r = (a->used - 1) * DIGIT_BIT; | ||||||
|    | 
 | ||||||
|   /* take the last digit and count the bits in it */ |   /* take the last digit and count the bits in it */ | ||||||
|   q = a->dp[a->used - 1]; |   q = a->dp[a->used - 1]; | ||||||
|   while (q > ((mp_digit) 0)) { |   while (q > ((mp_digit) 0)) { | ||||||
|  | |||||||
| @ -23,14 +23,14 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) | |||||||
|   mp_word  w, t; |   mp_word  w, t; | ||||||
|   mp_digit b; |   mp_digit b; | ||||||
|   int      res, ix; |   int      res, ix; | ||||||
|    | 
 | ||||||
|   /* b = 2**DIGIT_BIT / 3 */ |   /* b = 2**DIGIT_BIT / 3 */ | ||||||
|   b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); |   b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); | ||||||
| 
 | 
 | ||||||
|   if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { |   if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { | ||||||
|      return res; |      return res; | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   q.used = a->used; |   q.used = a->used; | ||||||
|   q.sign = a->sign; |   q.sign = a->sign; | ||||||
|   w = 0; |   w = 0; | ||||||
| @ -68,7 +68,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) | |||||||
|      mp_exch(&q, c); |      mp_exch(&q, c); | ||||||
|   } |   } | ||||||
|   mp_clear(&q); |   mp_clear(&q); | ||||||
|    | 
 | ||||||
|   return res; |   return res; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -79,13 +79,13 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) | |||||||
|   if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { |   if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { | ||||||
|      return res; |      return res; | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   q.used = a->used; |   q.used = a->used; | ||||||
|   q.sign = a->sign; |   q.sign = a->sign; | ||||||
|   w = 0; |   w = 0; | ||||||
|   for (ix = a->used - 1; ix >= 0; ix--) { |   for (ix = a->used - 1; ix >= 0; ix--) { | ||||||
|      w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); |      w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); | ||||||
|       | 
 | ||||||
|      if (w >= b) { |      if (w >= b) { | ||||||
|         t = (mp_digit)(w / b); |         t = (mp_digit)(w / b); | ||||||
|         w -= ((mp_word)t) * ((mp_word)b); |         w -= ((mp_word)t) * ((mp_word)b); | ||||||
| @ -94,17 +94,17 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) | |||||||
|       } |       } | ||||||
|       q.dp[ix] = (mp_digit)t; |       q.dp[ix] = (mp_digit)t; | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   if (d != NULL) { |   if (d != NULL) { | ||||||
|      *d = (mp_digit)w; |      *d = (mp_digit)w; | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   if (c != NULL) { |   if (c != NULL) { | ||||||
|      mp_clamp(&q); |      mp_clamp(&q); | ||||||
|      mp_exch(&q, c); |      mp_exch(&q, c); | ||||||
|   } |   } | ||||||
|   mp_clear(&q); |   mp_clear(&q); | ||||||
|    | 
 | ||||||
|   return res; |   return res; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -21,7 +21,7 @@ void mp_dr_setup(mp_int *a, mp_digit *d) | |||||||
|    /* the casts are required if DIGIT_BIT is one less than
 |    /* the casts are required if DIGIT_BIT is one less than
 | ||||||
|     * the number of bits in a mp_digit [e.g. DIGIT_BIT==31] |     * the number of bits in a mp_digit [e.g. DIGIT_BIT==31] | ||||||
|     */ |     */ | ||||||
|    *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -  |    *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - | ||||||
|         ((mp_word)a->dp[0])); |         ((mp_word)a->dp[0])); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* swap the elements of two integers, for cases where you can't simply swap the 
 | /* swap the elements of two integers, for cases where you can't simply swap the
 | ||||||
|  * mp_int pointers around |  * mp_int pointers around | ||||||
|  */ |  */ | ||||||
| void | void | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ | |||||||
| /* based on gmp's mpz_export.
 | /* based on gmp's mpz_export.
 | ||||||
|  * see http://gmplib.org/manual/Integer-Import-and-Export.html
 |  * see http://gmplib.org/manual/Integer-Import-and-Export.html
 | ||||||
|  */ |  */ | ||||||
| int mp_export(void* rop, size_t* countp, int order, size_t size,  | int mp_export(void* rop, size_t* countp, int order, size_t size, | ||||||
|                                 int endian, size_t nails, mp_int* op) { |                                 int endian, size_t nails, mp_int* op) { | ||||||
| 	int result; | 	int result; | ||||||
| 	size_t odd_nails, nail_bytes, i, j, bits, count; | 	size_t odd_nails, nail_bytes, i, j, bits, count; | ||||||
| @ -31,12 +31,12 @@ int mp_export(void* rop, size_t* countp, int order, size_t size, | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (endian == 0) { | 	if (endian == 0) { | ||||||
| 		union { | 		union { | ||||||
| 			unsigned int i; | 			unsigned int i; | ||||||
| 			char c[4]; | 			char c[4]; | ||||||
| 		} lint; | 		} lint; | ||||||
| 		lint.i = 0x01020304; | 		lint.i = 0x01020304; | ||||||
| 
 | 
 | ||||||
| 		endian = (lint.c[0] == 4) ? -1 : 1; | 		endian = (lint.c[0] == 4) ? -1 : 1; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -53,7 +53,7 @@ int mp_export(void* rop, size_t* countp, int order, size_t size, | |||||||
| 	for (i = 0; i < count; ++i) { | 	for (i = 0; i < count; ++i) { | ||||||
| 		for (j = 0; j < size; ++j) { | 		for (j = 0; j < size; ++j) { | ||||||
| 			unsigned char* byte = ( | 			unsigned char* byte = ( | ||||||
| 				(unsigned char*)rop +  | 				(unsigned char*)rop + | ||||||
| 				(((order == -1) ? i : ((count - 1) - i)) * size) + | 				(((order == -1) ? i : ((count - 1) - i)) * size) + | ||||||
| 				((endian == -1) ? j : ((size - 1) - j)) | 				((endian == -1) ? j : ((size - 1) - j)) | ||||||
| 			); | 			); | ||||||
|  | |||||||
| @ -59,7 +59,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) | |||||||
|      err = mp_exptmod(&tmpG, &tmpX, P, Y); |      err = mp_exptmod(&tmpG, &tmpX, P, Y); | ||||||
|      mp_clear_multi(&tmpG, &tmpX, NULL); |      mp_clear_multi(&tmpG, &tmpX, NULL); | ||||||
|      return err; |      return err; | ||||||
| #else  | #else | ||||||
|      /* no invmod */ |      /* no invmod */ | ||||||
|      return MP_VAL; |      return MP_VAL; | ||||||
| #endif | #endif | ||||||
| @ -86,7 +86,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) | |||||||
|      dr = mp_reduce_is_2k(P) << 1; |      dr = mp_reduce_is_2k(P) << 1; | ||||||
|   } |   } | ||||||
| #endif | #endif | ||||||
|      | 
 | ||||||
|   /* if the modulus is odd or dr != 0 use the montgomery method */ |   /* if the modulus is odd or dr != 0 use the montgomery method */ | ||||||
| #ifdef BN_MP_EXPTMOD_FAST_C | #ifdef BN_MP_EXPTMOD_FAST_C | ||||||
|   if ((mp_isodd (P) == MP_YES) || (dr !=  0)) { |   if ((mp_isodd (P) == MP_YES) || (dr !=  0)) { | ||||||
|  | |||||||
| @ -84,7 +84,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode | |||||||
| 
 | 
 | ||||||
|   /* determine and setup reduction code */ |   /* determine and setup reduction code */ | ||||||
|   if (redmode == 0) { |   if (redmode == 0) { | ||||||
| #ifdef BN_MP_MONTGOMERY_SETUP_C      | #ifdef BN_MP_MONTGOMERY_SETUP_C | ||||||
|      /* now setup montgomery  */ |      /* now setup montgomery  */ | ||||||
|      if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) { |      if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) { | ||||||
|         goto LBL_M; |         goto LBL_M; | ||||||
| @ -99,7 +99,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode | |||||||
|      if ((((P->used * 2) + 1) < MP_WARRAY) && |      if ((((P->used * 2) + 1) < MP_WARRAY) && | ||||||
|           (P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { |           (P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { | ||||||
|         redux = fast_mp_montgomery_reduce; |         redux = fast_mp_montgomery_reduce; | ||||||
|      } else  |      } else | ||||||
| #endif | #endif | ||||||
|      { |      { | ||||||
| #ifdef BN_MP_MONTGOMERY_REDUCE_C | #ifdef BN_MP_MONTGOMERY_REDUCE_C | ||||||
|  | |||||||
| @ -20,10 +20,10 @@ | |||||||
| int mp_fread(mp_int *a, int radix, FILE *stream) | int mp_fread(mp_int *a, int radix, FILE *stream) | ||||||
| { | { | ||||||
|    int err, ch, neg, y; |    int err, ch, neg, y; | ||||||
|     | 
 | ||||||
|    /* clear a */ |    /* clear a */ | ||||||
|    mp_zero(a); |    mp_zero(a); | ||||||
|     | 
 | ||||||
|    /* if first digit is - then set negative */ |    /* if first digit is - then set negative */ | ||||||
|    ch = fgetc(stream); |    ch = fgetc(stream); | ||||||
|    if (ch == '-') { |    if (ch == '-') { | ||||||
| @ -32,7 +32,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream) | |||||||
|    } else { |    } else { | ||||||
|       neg = MP_ZPOS; |       neg = MP_ZPOS; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    for (;;) { |    for (;;) { | ||||||
|       /* find y in the radix map */ |       /* find y in the radix map */ | ||||||
|       for (y = 0; y < radix; y++) { |       for (y = 0; y < radix; y++) { | ||||||
| @ -43,7 +43,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream) | |||||||
|       if (y == radix) { |       if (y == radix) { | ||||||
|          break; |          break; | ||||||
|       } |       } | ||||||
|        | 
 | ||||||
|       /* shift up and add */ |       /* shift up and add */ | ||||||
|       if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) { |       if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) { | ||||||
|          return err; |          return err; | ||||||
| @ -51,13 +51,13 @@ int mp_fread(mp_int *a, int radix, FILE *stream) | |||||||
|       if ((err = mp_add_d(a, y, a)) != MP_OKAY) { |       if ((err = mp_add_d(a, y, a)) != MP_OKAY) { | ||||||
|          return err; |          return err; | ||||||
|       } |       } | ||||||
|        | 
 | ||||||
|       ch = fgetc(stream); |       ch = fgetc(stream); | ||||||
|    } |    } | ||||||
|    if (mp_cmp_d(a, 0) != MP_EQ) { |    if (mp_cmp_d(a, 0) != MP_EQ) { | ||||||
|       a->sign = neg; |       a->sign = neg; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    return MP_OKAY; |    return MP_OKAY; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -20,7 +20,7 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream) | |||||||
| { | { | ||||||
|    char *buf; |    char *buf; | ||||||
|    int err, len, x; |    int err, len, x; | ||||||
|     | 
 | ||||||
|    if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { |    if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { | ||||||
|       return err; |       return err; | ||||||
|    } |    } | ||||||
| @ -29,19 +29,19 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream) | |||||||
|    if (buf == NULL) { |    if (buf == NULL) { | ||||||
|       return MP_MEM; |       return MP_MEM; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { |    if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { | ||||||
|       XFREE (buf); |       XFREE (buf); | ||||||
|       return err; |       return err; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    for (x = 0; x < len; x++) { |    for (x = 0; x < len; x++) { | ||||||
|        if (fputc(buf[x], stream) == EOF) { |        if (fputc(buf[x], stream) == EOF) { | ||||||
|           XFREE (buf); |           XFREE (buf); | ||||||
|           return MP_VAL; |           return MP_VAL; | ||||||
|        } |        } | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    XFREE (buf); |    XFREE (buf); | ||||||
|    return MP_OKAY; |    return MP_OKAY; | ||||||
| } | } | ||||||
|  | |||||||
| @ -76,17 +76,17 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c) | |||||||
|         /* swap u and v to make sure v is >= u */ |         /* swap u and v to make sure v is >= u */ | ||||||
|         mp_exch(&u, &v); |         mp_exch(&u, &v); | ||||||
|      } |      } | ||||||
|       | 
 | ||||||
|      /* subtract smallest from largest */ |      /* subtract smallest from largest */ | ||||||
|      if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { |      if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { | ||||||
|         goto LBL_V; |         goto LBL_V; | ||||||
|      } |      } | ||||||
|       | 
 | ||||||
|      /* Divide out all factors of two */ |      /* Divide out all factors of two */ | ||||||
|      if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { |      if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { | ||||||
|         goto LBL_V; |         goto LBL_V; | ||||||
|      }  |      } | ||||||
|   }  |   } | ||||||
| 
 | 
 | ||||||
|   /* multiply by 2**k which we divided out at the beginning */ |   /* multiply by 2**k which we divided out at the beginning */ | ||||||
|   if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) { |   if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) { | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ | |||||||
| /* based on gmp's mpz_import.
 | /* based on gmp's mpz_import.
 | ||||||
|  * see http://gmplib.org/manual/Integer-Import-and-Export.html
 |  * see http://gmplib.org/manual/Integer-Import-and-Export.html
 | ||||||
|  */ |  */ | ||||||
| int mp_import(mp_int* rop, size_t count, int order, size_t size,  | int mp_import(mp_int* rop, size_t count, int order, size_t size, | ||||||
|                             int endian, size_t nails, const void* op) { |                             int endian, size_t nails, const void* op) { | ||||||
| 	int result; | 	int result; | ||||||
| 	size_t odd_nails, nail_bytes, i, j; | 	size_t odd_nails, nail_bytes, i, j; | ||||||
| @ -27,12 +27,12 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size, | |||||||
| 	mp_zero(rop); | 	mp_zero(rop); | ||||||
| 
 | 
 | ||||||
| 	if (endian == 0) { | 	if (endian == 0) { | ||||||
| 		union { | 		union { | ||||||
| 			unsigned int i; | 			unsigned int i; | ||||||
| 			char c[4]; | 			char c[4]; | ||||||
| 		} lint; | 		} lint; | ||||||
| 		lint.i = 0x01020304; | 		lint.i = 0x01020304; | ||||||
| 
 | 
 | ||||||
| 		endian = (lint.c[0] == 4) ? -1 : 1; | 		endian = (lint.c[0] == 4) ? -1 : 1; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -46,7 +46,7 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size, | |||||||
| 	for (i = 0; i < count; ++i) { | 	for (i = 0; i < count; ++i) { | ||||||
| 		for (j = 0; j < (size - nail_bytes); ++j) { | 		for (j = 0; j < (size - nail_bytes); ++j) { | ||||||
| 			unsigned char byte = *( | 			unsigned char byte = *( | ||||||
| 					(unsigned char*)op +  | 					(unsigned char*)op + | ||||||
| 					(((order == 1) ? i : ((count - 1) - i)) * size) + | 					(((order == 1) ? i : ((count - 1) - i)) * size) + | ||||||
| 					((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)) | 					((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)) | ||||||
| 				); | 				); | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ | |||||||
|  */ |  */ | ||||||
| #include <stdarg.h> | #include <stdarg.h> | ||||||
| 
 | 
 | ||||||
| int mp_init_multi(mp_int *mp, ...)  | int mp_init_multi(mp_int *mp, ...) | ||||||
| { | { | ||||||
|     mp_err res = MP_OKAY;      /* Assume ok until proven otherwise */ |     mp_err res = MP_OKAY;      /* Assume ok until proven otherwise */ | ||||||
|     int n = 0;                 /* Number of ok inits */ |     int n = 0;                 /* Number of ok inits */ | ||||||
| @ -30,8 +30,8 @@ int mp_init_multi(mp_int *mp, ...) | |||||||
|                succeeded in init-ing, then return error. |                succeeded in init-ing, then return error. | ||||||
|             */ |             */ | ||||||
|             va_list clean_args; |             va_list clean_args; | ||||||
|              | 
 | ||||||
|             /* now start cleaning up */             |             /* now start cleaning up */ | ||||||
|             cur_arg = mp; |             cur_arg = mp; | ||||||
|             va_start(clean_args, mp); |             va_start(clean_args, mp); | ||||||
|             while (n-- != 0) { |             while (n-- != 0) { | ||||||
|  | |||||||
| @ -21,8 +21,8 @@ int mp_init_size (mp_int * a, int size) | |||||||
|   int x; |   int x; | ||||||
| 
 | 
 | ||||||
|   /* pad size so there are always extra digits */ |   /* pad size so there are always extra digits */ | ||||||
|   size += (MP_PREC * 2) - (size % MP_PREC);	 |   size += (MP_PREC * 2) - (size % MP_PREC); | ||||||
|    | 
 | ||||||
|   /* alloc mem */ |   /* alloc mem */ | ||||||
|   a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size); |   a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size); | ||||||
|   if (a->dp == NULL) { |   if (a->dp == NULL) { | ||||||
|  | |||||||
| @ -27,7 +27,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* init temps */ |   /* init temps */ | ||||||
|   if ((res = mp_init_multi(&x, &y, &u, &v,  |   if ((res = mp_init_multi(&x, &y, &u, &v, | ||||||
|                            &A, &B, &C, &D, NULL)) != MP_OKAY) { |                            &A, &B, &C, &D, NULL)) != MP_OKAY) { | ||||||
|      return res; |      return res; | ||||||
|   } |   } | ||||||
| @ -154,14 +154,14 @@ top: | |||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   /* too big */ |   /* too big */ | ||||||
|   while (mp_cmp_mag(&C, b) != MP_LT) { |   while (mp_cmp_mag(&C, b) != MP_LT) { | ||||||
|       if ((res = mp_sub(&C, b, &C)) != MP_OKAY) { |       if ((res = mp_sub(&C, b, &C)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   /* C is now the inverse */ |   /* C is now the inverse */ | ||||||
|   mp_exch (&C, c); |   mp_exch (&C, c); | ||||||
|   res = MP_OKAY; |   res = MP_OKAY; | ||||||
|  | |||||||
| @ -38,7 +38,7 @@ static const char rem_105[105] = { | |||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| /* Store non-zero to ret if arg is square, and zero if not */ | /* Store non-zero to ret if arg is square, and zero if not */ | ||||||
| int mp_is_square(mp_int *arg,int *ret)  | int mp_is_square(mp_int *arg,int *ret) | ||||||
| { | { | ||||||
|   int           res; |   int           res; | ||||||
|   mp_digit      c; |   mp_digit      c; | ||||||
| @ -46,7 +46,7 @@ int mp_is_square(mp_int *arg,int *ret) | |||||||
|   unsigned long r; |   unsigned long r; | ||||||
| 
 | 
 | ||||||
|   /* Default to Non-square :) */ |   /* Default to Non-square :) */ | ||||||
|   *ret = MP_NO;  |   *ret = MP_NO; | ||||||
| 
 | 
 | ||||||
|   if (arg->sign == MP_NEG) { |   if (arg->sign == MP_NEG) { | ||||||
|     return MP_VAL; |     return MP_VAL; | ||||||
| @ -80,8 +80,8 @@ int mp_is_square(mp_int *arg,int *ret) | |||||||
|   r = mp_get_int(&t); |   r = mp_get_int(&t); | ||||||
|   /* Check for other prime modules, note it's not an ERROR but we must
 |   /* Check for other prime modules, note it's not an ERROR but we must
 | ||||||
|    * free "t" so the easiest way is to goto ERR.  We know that res |    * free "t" so the easiest way is to goto ERR.  We know that res | ||||||
|    * is already equal to MP_OKAY from the mp_mod call  |    * is already equal to MP_OKAY from the mp_mod call | ||||||
|    */  |    */ | ||||||
|   if (((1L<<(r%11)) & 0x5C4L) != 0L)       goto ERR; |   if (((1L<<(r%11)) & 0x5C4L) != 0L)       goto ERR; | ||||||
|   if (((1L<<(r%13)) & 0x9E4L) != 0L)       goto ERR; |   if (((1L<<(r%13)) & 0x9E4L) != 0L)       goto ERR; | ||||||
|   if (((1L<<(r%17)) & 0x5CE8L) != 0L)      goto ERR; |   if (((1L<<(r%17)) & 0x5CE8L) != 0L)      goto ERR; | ||||||
|  | |||||||
| @ -15,33 +15,33 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* c = |a| * |b| using Karatsuba Multiplication using 
 | /* c = |a| * |b| using Karatsuba Multiplication using
 | ||||||
|  * three half size multiplications |  * three half size multiplications | ||||||
|  * |  * | ||||||
|  * Let B represent the radix [e.g. 2**DIGIT_BIT] and  |  * Let B represent the radix [e.g. 2**DIGIT_BIT] and | ||||||
|  * let n represent half of the number of digits in  |  * let n represent half of the number of digits in | ||||||
|  * the min(a,b) |  * the min(a,b) | ||||||
|  * |  * | ||||||
|  * a = a1 * B**n + a0 |  * a = a1 * B**n + a0 | ||||||
|  * b = b1 * B**n + b0 |  * b = b1 * B**n + b0 | ||||||
|  * |  * | ||||||
|  * Then, a * b =>  |  * Then, a * b => | ||||||
|    a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0 |    a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0 | ||||||
|  * |  * | ||||||
|  * Note that a1b1 and a0b0 are used twice and only need to be  |  * Note that a1b1 and a0b0 are used twice and only need to be | ||||||
|  * computed once.  So in total three half size (half # of  |  * computed once.  So in total three half size (half # of | ||||||
|  * digit) multiplications are performed, a0b0, a1b1 and  |  * digit) multiplications are performed, a0b0, a1b1 and | ||||||
|  * (a1+b1)(a0+b0) |  * (a1+b1)(a0+b0) | ||||||
|  * |  * | ||||||
|  * Note that a multiplication of half the digits requires |  * Note that a multiplication of half the digits requires | ||||||
|  * 1/4th the number of single precision multiplications so in  |  * 1/4th the number of single precision multiplications so in | ||||||
|  * total after one call 25% of the single precision multiplications  |  * total after one call 25% of the single precision multiplications | ||||||
|  * are saved.  Note also that the call to mp_mul can end up back  |  * are saved.  Note also that the call to mp_mul can end up back | ||||||
|  * in this function if the a0, a1, b0, or b1 are above the threshold.   |  * in this function if the a0, a1, b0, or b1 are above the threshold. | ||||||
|  * This is known as divide-and-conquer and leads to the famous  |  * This is known as divide-and-conquer and leads to the famous | ||||||
|  * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than  |  * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than | ||||||
|  * the standard O(N**2) that the baseline/comba methods use.   |  * the standard O(N**2) that the baseline/comba methods use. | ||||||
|  * Generally though the overhead of this method doesn't pay off  |  * Generally though the overhead of this method doesn't pay off | ||||||
|  * until a certain size (N ~ 80) is reached. |  * until a certain size (N ~ 80) is reached. | ||||||
|  */ |  */ | ||||||
| int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) | int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) | ||||||
| @ -109,7 +109,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* only need to clamp the lower words since by definition the 
 |   /* only need to clamp the lower words since by definition the
 | ||||||
|    * upper words x1/y1 must have a known number of digits |    * upper words x1/y1 must have a known number of digits | ||||||
|    */ |    */ | ||||||
|   mp_clamp (&x0); |   mp_clamp (&x0); | ||||||
| @ -117,7 +117,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) | |||||||
| 
 | 
 | ||||||
|   /* now calc the products x0y0 and x1y1 */ |   /* now calc the products x0y0 and x1y1 */ | ||||||
|   /* after this x0 is no longer required, free temp [x0==t2]! */ |   /* after this x0 is no longer required, free temp [x0==t2]! */ | ||||||
|   if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)   |   if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY) | ||||||
|     goto X1Y1;          /* x0y0 = x0*y0 */ |     goto X1Y1;          /* x0y0 = x0*y0 */ | ||||||
|   if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY) |   if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY) | ||||||
|     goto X1Y1;          /* x1y1 = x1*y1 */ |     goto X1Y1;          /* x1y1 = x1*y1 */ | ||||||
|  | |||||||
| @ -15,11 +15,11 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Karatsuba squaring, computes b = a*a using three 
 | /* Karatsuba squaring, computes b = a*a using three
 | ||||||
|  * half size squarings |  * half size squarings | ||||||
|  * |  * | ||||||
|  * See comments of karatsuba_mul for details.  It  |  * See comments of karatsuba_mul for details.  It | ||||||
|  * is essentially the same algorithm but merely  |  * is essentially the same algorithm but merely | ||||||
|  * tuned to perform recursive squarings. |  * tuned to perform recursive squarings. | ||||||
|  */ |  */ | ||||||
| int mp_karatsuba_sqr (mp_int * a, mp_int * b) | int mp_karatsuba_sqr (mp_int * a, mp_int * b) | ||||||
|  | |||||||
							
								
								
									
										12
									
								
								bn_mp_mul.c
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								bn_mp_mul.c
									
									
									
									
									
								
							| @ -25,29 +25,29 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c) | |||||||
| #ifdef BN_MP_TOOM_MUL_C | #ifdef BN_MP_TOOM_MUL_C | ||||||
|   if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { |   if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { | ||||||
|     res = mp_toom_mul(a, b, c); |     res = mp_toom_mul(a, b, c); | ||||||
|   } else  |   } else | ||||||
| #endif | #endif | ||||||
| #ifdef BN_MP_KARATSUBA_MUL_C | #ifdef BN_MP_KARATSUBA_MUL_C | ||||||
|   /* use Karatsuba? */ |   /* use Karatsuba? */ | ||||||
|   if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { |   if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { | ||||||
|     res = mp_karatsuba_mul (a, b, c); |     res = mp_karatsuba_mul (a, b, c); | ||||||
|   } else  |   } else | ||||||
| #endif | #endif | ||||||
|   { |   { | ||||||
|     /* can we use the fast multiplier?
 |     /* can we use the fast multiplier?
 | ||||||
|      * |      * | ||||||
|      * The fast multiplier can be used if the output will  |      * The fast multiplier can be used if the output will | ||||||
|      * have less than MP_WARRAY digits and the number of  |      * have less than MP_WARRAY digits and the number of | ||||||
|      * digits won't affect carry propagation |      * digits won't affect carry propagation | ||||||
|      */ |      */ | ||||||
|     int     digs = a->used + b->used + 1; |     int     digs = a->used + b->used + 1; | ||||||
| 
 | 
 | ||||||
| #ifdef BN_FAST_S_MP_MUL_DIGS_C | #ifdef BN_FAST_S_MP_MUL_DIGS_C | ||||||
|     if ((digs < MP_WARRAY) && |     if ((digs < MP_WARRAY) && | ||||||
|         (MIN(a->used, b->used) <=  |         (MIN(a->used, b->used) <= | ||||||
|          (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { |          (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { | ||||||
|       res = fast_s_mp_mul_digs (a, b, c, digs); |       res = fast_s_mp_mul_digs (a, b, c, digs); | ||||||
|     } else  |     } else | ||||||
| #endif | #endif | ||||||
|     { |     { | ||||||
| #ifdef BN_S_MP_MUL_DIGS_C | #ifdef BN_S_MP_MUL_DIGS_C | ||||||
|  | |||||||
| @ -35,24 +35,24 @@ int mp_mul_2(mp_int * a, mp_int * b) | |||||||
| 
 | 
 | ||||||
|     /* alias for source */ |     /* alias for source */ | ||||||
|     tmpa = a->dp; |     tmpa = a->dp; | ||||||
|      | 
 | ||||||
|     /* alias for dest */ |     /* alias for dest */ | ||||||
|     tmpb = b->dp; |     tmpb = b->dp; | ||||||
| 
 | 
 | ||||||
|     /* carry */ |     /* carry */ | ||||||
|     r = 0; |     r = 0; | ||||||
|     for (x = 0; x < a->used; x++) { |     for (x = 0; x < a->used; x++) { | ||||||
|      | 
 | ||||||
|       /* get what will be the *next* carry bit from the 
 |       /* get what will be the *next* carry bit from the
 | ||||||
|        * MSB of the current digit  |        * MSB of the current digit | ||||||
|        */ |        */ | ||||||
|       rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); |       rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); | ||||||
|        | 
 | ||||||
|       /* now shift up this digit, add in the carry [from the previous] */ |       /* now shift up this digit, add in the carry [from the previous] */ | ||||||
|       *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; |       *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; | ||||||
|        | 
 | ||||||
|       /* copy the carry that would be from the source 
 |       /* copy the carry that would be from the source
 | ||||||
|        * digit into the next iteration  |        * digit into the next iteration | ||||||
|        */ |        */ | ||||||
|       r = rr; |       r = rr; | ||||||
|     } |     } | ||||||
| @ -64,8 +64,8 @@ int mp_mul_2(mp_int * a, mp_int * b) | |||||||
|       ++(b->used); |       ++(b->used); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /* now zero any excess digits on the destination 
 |     /* now zero any excess digits on the destination
 | ||||||
|      * that we didn't write to  |      * that we didn't write to | ||||||
|      */ |      */ | ||||||
|     tmpb = b->dp + b->used; |     tmpb = b->dp + b->used; | ||||||
|     for (x = b->used; x < oldused; x++) { |     for (x = b->used; x < oldused; x++) { | ||||||
|  | |||||||
| @ -69,7 +69,7 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c) | |||||||
|       /* set the carry to the carry bits of the current word */ |       /* set the carry to the carry bits of the current word */ | ||||||
|       r = rr; |       r = rr; | ||||||
|     } |     } | ||||||
|      | 
 | ||||||
|     /* set final carry */ |     /* set final carry */ | ||||||
|     if (r != 0) { |     if (r != 0) { | ||||||
|        c->dp[(c->used)++] = r; |        c->dp[(c->used)++] = r; | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* performs one Fermat test.
 | /* performs one Fermat test.
 | ||||||
|  *  |  * | ||||||
|  * If "a" were prime then b**a == b (mod a) since the order of |  * If "a" were prime then b**a == b (mod a) since the order of | ||||||
|  * the multiplicative sub-group would be phi(a) = a-1.  That means |  * the multiplicative sub-group would be phi(a) = a-1.  That means | ||||||
|  * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). |  * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines if an integers is divisible by one 
 | /* determines if an integers is divisible by one
 | ||||||
|  * of the first PRIME_SIZE primes or not |  * of the first PRIME_SIZE primes or not | ||||||
|  * |  * | ||||||
|  * sets result to 0 if not, 1 if yes |  * sets result to 0 if not, 1 if yes | ||||||
|  | |||||||
| @ -15,11 +15,11 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Miller-Rabin test of "a" to the base of "b" as described in 
 | /* Miller-Rabin test of "a" to the base of "b" as described in
 | ||||||
|  * HAC pp. 139 Algorithm 4.24 |  * HAC pp. 139 Algorithm 4.24 | ||||||
|  * |  * | ||||||
|  * Sets result to 0 if definitely composite or 1 if probably prime. |  * 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  |  * Randomly the chance of error is no more than 1/4 and often | ||||||
|  * very much lower. |  * very much lower. | ||||||
|  */ |  */ | ||||||
| int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) | int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) | ||||||
| @ -33,7 +33,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) | |||||||
|   /* ensure b > 1 */ |   /* ensure b > 1 */ | ||||||
|   if (mp_cmp_d(b, 1) != MP_GT) { |   if (mp_cmp_d(b, 1) != MP_GT) { | ||||||
|      return MP_VAL; |      return MP_VAL; | ||||||
|   }      |   } | ||||||
| 
 | 
 | ||||||
|   /* get n1 = a - 1 */ |   /* get n1 = a - 1 */ | ||||||
|   if ((err = mp_init_copy (&n1, a)) != MP_OKAY) { |   if ((err = mp_init_copy (&n1, a)) != MP_OKAY) { | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ | |||||||
| /* makes a truly random prime of a given size (bits),
 | /* makes a truly random prime of a given size (bits),
 | ||||||
|  * |  * | ||||||
|  * Flags are as follows: |  * Flags are as follows: | ||||||
|  *  |  * | ||||||
|  *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4 |  *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4 | ||||||
|  *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) |  *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) | ||||||
|  *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one |  *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one | ||||||
| @ -62,7 +62,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
|    maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0; |    maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0; | ||||||
|    if ((flags & LTM_PRIME_2MSB_ON) != 0) { |    if ((flags & LTM_PRIME_2MSB_ON) != 0) { | ||||||
|       maskOR_msb       |= 0x80 >> ((9 - size) & 7); |       maskOR_msb       |= 0x80 >> ((9 - size) & 7); | ||||||
|    }   |    } | ||||||
| 
 | 
 | ||||||
|    /* get the maskOR_lsb */ |    /* get the maskOR_lsb */ | ||||||
|    maskOR_lsb         = 1; |    maskOR_lsb         = 1; | ||||||
| @ -76,7 +76,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
|          err = MP_VAL; |          err = MP_VAL; | ||||||
|          goto error; |          goto error; | ||||||
|       } |       } | ||||||
|   | 
 | ||||||
|       /* work over the MSbyte */ |       /* work over the MSbyte */ | ||||||
|       tmp[0]    &= maskAND; |       tmp[0]    &= maskAND; | ||||||
|       tmp[0]    |= 1 << ((size - 1) & 7); |       tmp[0]    |= 1 << ((size - 1) & 7); | ||||||
| @ -90,7 +90,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
| 
 | 
 | ||||||
|       /* is it prime? */ |       /* is it prime? */ | ||||||
|       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY)           { goto error; } |       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY)           { goto error; } | ||||||
|       if (res == MP_NO) {   |       if (res == MP_NO) { | ||||||
|          continue; |          continue; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
| @ -98,7 +98,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
|          /* see if (a-1)/2 is prime */ |          /* see if (a-1)/2 is prime */ | ||||||
|          if ((err = mp_sub_d(a, 1, a)) != MP_OKAY)                    { goto error; } |          if ((err = mp_sub_d(a, 1, a)) != MP_OKAY)                    { goto error; } | ||||||
|          if ((err = mp_div_2(a, a)) != MP_OKAY)                       { goto error; } |          if ((err = mp_div_2(a, a)) != MP_OKAY)                       { goto error; } | ||||||
|   | 
 | ||||||
|          /* is it prime? */ |          /* is it prime? */ | ||||||
|          if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY)        { goto error; } |          if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY)        { goto error; } | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -54,7 +54,7 @@ int mp_radix_size (mp_int * a, int radix, int *size) | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* force temp to positive */ |   /* force temp to positive */ | ||||||
|   t.sign = MP_ZPOS;  |   t.sign = MP_ZPOS; | ||||||
| 
 | 
 | ||||||
|   /* fetch out all of the digits */ |   /* fetch out all of the digits */ | ||||||
|   while (mp_iszero (&t) == MP_NO) { |   while (mp_iszero (&t) == MP_NO) { | ||||||
|  | |||||||
| @ -29,8 +29,8 @@ int mp_read_radix (mp_int * a, const char *str, int radix) | |||||||
|     return MP_VAL; |     return MP_VAL; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* if the leading digit is a 
 |   /* if the leading digit is a
 | ||||||
|    * minus set the sign to negative.  |    * minus set the sign to negative. | ||||||
|    */ |    */ | ||||||
|   if (*str == '-') { |   if (*str == '-') { | ||||||
|     ++str; |     ++str; | ||||||
| @ -41,7 +41,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix) | |||||||
| 
 | 
 | ||||||
|   /* set the integer to the default of zero */ |   /* set the integer to the default of zero */ | ||||||
|   mp_zero (a); |   mp_zero (a); | ||||||
|    | 
 | ||||||
|   /* process each digit of the string */ |   /* process each digit of the string */ | ||||||
|   while (*str != '\0') { |   while (*str != '\0') { | ||||||
|     /* if the radix <= 36 the conversion is case insensitive
 |     /* if the radix <= 36 the conversion is case insensitive
 | ||||||
| @ -55,9 +55,9 @@ int mp_read_radix (mp_int * a, const char *str, int radix) | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /* if the char was found in the map 
 |     /* if the char was found in the map
 | ||||||
|      * and is less than the given radix add it |      * and is less than the given radix add it | ||||||
|      * to the number, otherwise exit the loop.  |      * to the number, otherwise exit the loop. | ||||||
|      */ |      */ | ||||||
|     if (y < radix) { |     if (y < radix) { | ||||||
|       if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { |       if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { | ||||||
|  | |||||||
| @ -20,22 +20,22 @@ int mp_reduce_2k_setup(mp_int *a, mp_digit *d) | |||||||
| { | { | ||||||
|    int res, p; |    int res, p; | ||||||
|    mp_int tmp; |    mp_int tmp; | ||||||
|     | 
 | ||||||
|    if ((res = mp_init(&tmp)) != MP_OKAY) { |    if ((res = mp_init(&tmp)) != MP_OKAY) { | ||||||
|       return res; |       return res; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    p = mp_count_bits(a); |    p = mp_count_bits(a); | ||||||
|    if ((res = mp_2expt(&tmp, p)) != MP_OKAY) { |    if ((res = mp_2expt(&tmp, p)) != MP_OKAY) { | ||||||
|       mp_clear(&tmp); |       mp_clear(&tmp); | ||||||
|       return res; |       return res; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) { |    if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) { | ||||||
|       mp_clear(&tmp); |       mp_clear(&tmp); | ||||||
|       return res; |       return res; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    *d = tmp.dp[0]; |    *d = tmp.dp[0]; | ||||||
|    mp_clear(&tmp); |    mp_clear(&tmp); | ||||||
|    return MP_OKAY; |    return MP_OKAY; | ||||||
|  | |||||||
| @ -20,19 +20,19 @@ int mp_reduce_2k_setup_l(mp_int *a, mp_int *d) | |||||||
| { | { | ||||||
|    int    res; |    int    res; | ||||||
|    mp_int tmp; |    mp_int tmp; | ||||||
|     | 
 | ||||||
|    if ((res = mp_init(&tmp)) != MP_OKAY) { |    if ((res = mp_init(&tmp)) != MP_OKAY) { | ||||||
|       return res; |       return res; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) { |    if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) { | ||||||
|       goto ERR; |       goto ERR; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
|    if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) { |    if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto ERR; | ||||||
|    } |    } | ||||||
|     | 
 | ||||||
| ERR: | ERR: | ||||||
|    mp_clear(&tmp); |    mp_clear(&tmp); | ||||||
|    return res; |    return res; | ||||||
|  | |||||||
| @ -20,7 +20,7 @@ int mp_reduce_is_2k(mp_int *a) | |||||||
| { | { | ||||||
|    int ix, iy, iw; |    int ix, iy, iw; | ||||||
|    mp_digit iz; |    mp_digit iz; | ||||||
|     | 
 | ||||||
|    if (a->used == 0) { |    if (a->used == 0) { | ||||||
|       return MP_NO; |       return MP_NO; | ||||||
|    } else if (a->used == 1) { |    } else if (a->used == 1) { | ||||||
| @ -29,7 +29,7 @@ int mp_reduce_is_2k(mp_int *a) | |||||||
|       iy = mp_count_bits(a); |       iy = mp_count_bits(a); | ||||||
|       iz = 1; |       iz = 1; | ||||||
|       iw = 1; |       iw = 1; | ||||||
|      | 
 | ||||||
|       /* Test every bit from the second digit up, must be 1 */ |       /* Test every bit from the second digit up, must be 1 */ | ||||||
|       for (ix = DIGIT_BIT; ix < iy; ix++) { |       for (ix = DIGIT_BIT; ix < iy; ix++) { | ||||||
|           if ((a->dp[iw] & iz) == 0) { |           if ((a->dp[iw] & iz) == 0) { | ||||||
|  | |||||||
| @ -19,7 +19,7 @@ | |||||||
| int mp_reduce_is_2k_l(mp_int *a) | int mp_reduce_is_2k_l(mp_int *a) | ||||||
| { | { | ||||||
|    int ix, iy; |    int ix, iy; | ||||||
|     | 
 | ||||||
|    if (a->used == 0) { |    if (a->used == 0) { | ||||||
|       return MP_NO; |       return MP_NO; | ||||||
|    } else if (a->used == 1) { |    } else if (a->used == 1) { | ||||||
| @ -32,7 +32,7 @@ int mp_reduce_is_2k_l(mp_int *a) | |||||||
|           } |           } | ||||||
|       } |       } | ||||||
|       return (iy >= (a->used/2)) ? MP_YES : MP_NO; |       return (iy >= (a->used/2)) ? MP_YES : MP_NO; | ||||||
|        | 
 | ||||||
|    } |    } | ||||||
|    return MP_NO; |    return MP_NO; | ||||||
| } | } | ||||||
|  | |||||||
| @ -21,7 +21,7 @@ | |||||||
| int mp_reduce_setup (mp_int * a, mp_int * b) | int mp_reduce_setup (mp_int * a, mp_int * b) | ||||||
| { | { | ||||||
|   int     res; |   int     res; | ||||||
|    | 
 | ||||||
|   if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { |   if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { | ||||||
|     return res; |     return res; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -42,8 +42,8 @@ void mp_rshd (mp_int * a, int b) | |||||||
|     /* top [offset into digits] */ |     /* top [offset into digits] */ | ||||||
|     top = a->dp + b; |     top = a->dp + b; | ||||||
| 
 | 
 | ||||||
|     /* this is implemented as a sliding window where 
 |     /* this is implemented as a sliding window where
 | ||||||
|      * the window is b-digits long and digits from  |      * the window is b-digits long and digits from | ||||||
|      * the top of the window are copied to the bottom |      * the top of the window are copied to the bottom | ||||||
|      * |      * | ||||||
|      * e.g. |      * e.g. | ||||||
| @ -61,7 +61,7 @@ void mp_rshd (mp_int * a, int b) | |||||||
|       *bottom++ = 0; |       *bottom++ = 0; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   /* remove excess digits */ |   /* remove excess digits */ | ||||||
|   a->used -= b; |   a->used -= b; | ||||||
| } | } | ||||||
|  | |||||||
| @ -21,7 +21,7 @@ int mp_set_int (mp_int * a, unsigned long b) | |||||||
|   int     x, res; |   int     x, res; | ||||||
| 
 | 
 | ||||||
|   mp_zero (a); |   mp_zero (a); | ||||||
|    | 
 | ||||||
|   /* set four bits at a time */ |   /* set four bits at a time */ | ||||||
|   for (x = 0; x < 8; x++) { |   for (x = 0; x < 8; x++) { | ||||||
|     /* shift the number up four bits */ |     /* shift the number up four bits */ | ||||||
|  | |||||||
| @ -20,11 +20,11 @@ int mp_shrink (mp_int * a) | |||||||
| { | { | ||||||
|   mp_digit *tmp; |   mp_digit *tmp; | ||||||
|   int used = 1; |   int used = 1; | ||||||
|    | 
 | ||||||
|   if(a->used > 0) { |   if(a->used > 0) { | ||||||
|     used = a->used; |     used = a->used; | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   if (a->alloc != used) { |   if (a->alloc != used) { | ||||||
|     if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * used)) == NULL) { |     if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * used)) == NULL) { | ||||||
|       return MP_MEM; |       return MP_MEM; | ||||||
|  | |||||||
| @ -26,12 +26,12 @@ mp_sqr (mp_int * a, mp_int * b) | |||||||
|   if (a->used >= TOOM_SQR_CUTOFF) { |   if (a->used >= TOOM_SQR_CUTOFF) { | ||||||
|     res = mp_toom_sqr(a, b); |     res = mp_toom_sqr(a, b); | ||||||
|   /* Karatsuba? */ |   /* Karatsuba? */ | ||||||
|   } else  |   } else | ||||||
| #endif | #endif | ||||||
| #ifdef BN_MP_KARATSUBA_SQR_C | #ifdef BN_MP_KARATSUBA_SQR_C | ||||||
|   if (a->used >= KARATSUBA_SQR_CUTOFF) { |   if (a->used >= KARATSUBA_SQR_CUTOFF) { | ||||||
|     res = mp_karatsuba_sqr (a, b); |     res = mp_karatsuba_sqr (a, b); | ||||||
|   } else  |   } else | ||||||
| #endif | #endif | ||||||
|   { |   { | ||||||
| #ifdef BN_FAST_S_MP_SQR_C | #ifdef BN_FAST_S_MP_SQR_C | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* this function is less generic than mp_n_root, simpler and faster */ | /* this function is less generic than mp_n_root, simpler and faster */ | ||||||
| int mp_sqrt(mp_int *arg, mp_int *ret)  | int mp_sqrt(mp_int *arg, mp_int *ret) | ||||||
| { | { | ||||||
|   int res; |   int res; | ||||||
|   mp_int t1,t2; |   mp_int t1,t2; | ||||||
| @ -43,7 +43,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret) | |||||||
|   /* First approx. (not very bad for large arg) */ |   /* First approx. (not very bad for large arg) */ | ||||||
|   mp_rshd (&t1,t1.used/2); |   mp_rshd (&t1,t1.used/2); | ||||||
| 
 | 
 | ||||||
|   /* t1 > 0  */  |   /* t1 > 0  */ | ||||||
|   if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { |   if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { | ||||||
|     goto E1; |     goto E1; | ||||||
|   } |   } | ||||||
| @ -54,7 +54,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret) | |||||||
|     goto E1; |     goto E1; | ||||||
|   } |   } | ||||||
|   /* And now t1 > sqrt(arg) */ |   /* And now t1 > sqrt(arg) */ | ||||||
|   do {  |   do { | ||||||
|     if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { |     if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { | ||||||
|       goto E1; |       goto E1; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -15,9 +15,9 @@ | |||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* stores a bignum as a ASCII string in a given radix (2..64) 
 | /* stores a bignum as a ASCII string in a given radix (2..64)
 | ||||||
|  * |  * | ||||||
|  * Stores upto maxlen-1 chars and always a NULL byte  |  * Stores upto maxlen-1 chars and always a NULL byte | ||||||
|  */ |  */ | ||||||
| int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) | int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) | ||||||
| { | { | ||||||
| @ -50,7 +50,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) | |||||||
|     /* store the flag and mark the number as positive */ |     /* store the flag and mark the number as positive */ | ||||||
|     *str++ = '-'; |     *str++ = '-'; | ||||||
|     t.sign = MP_ZPOS; |     t.sign = MP_ZPOS; | ||||||
|   | 
 | ||||||
|     /* subtract a char */ |     /* subtract a char */ | ||||||
|     --maxlen; |     --maxlen; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -74,8 +74,8 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c) | |||||||
|       *tmpc++ &= MP_MASK; |       *tmpc++ &= MP_MASK; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /* now copy higher words if any, that is in A+B 
 |     /* now copy higher words if any, that is in A+B
 | ||||||
|      * if A or B has more digits add those in  |      * if A or B has more digits add those in | ||||||
|      */ |      */ | ||||||
|     if (min != max) { |     if (min != max) { | ||||||
|       for (; i < max; i++) { |       for (; i < max; i++) { | ||||||
|  | |||||||
| @ -54,7 +54,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) | |||||||
|   /* init M array */ |   /* init M array */ | ||||||
|   /* init first cell */ |   /* init first cell */ | ||||||
|   if ((err = mp_init(&M[1])) != MP_OKAY) { |   if ((err = mp_init(&M[1])) != MP_OKAY) { | ||||||
|      return err;  |      return err; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* now init the second half of the array */ |   /* now init the second half of the array */ | ||||||
| @ -72,7 +72,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) | |||||||
|   if ((err = mp_init (&mu)) != MP_OKAY) { |   if ((err = mp_init (&mu)) != MP_OKAY) { | ||||||
|     goto LBL_M; |     goto LBL_M; | ||||||
|   } |   } | ||||||
|    | 
 | ||||||
|   if (redmode == 0) { |   if (redmode == 0) { | ||||||
|      if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) { |      if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) { | ||||||
|         goto LBL_MU; |         goto LBL_MU; | ||||||
| @ -83,22 +83,22 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) | |||||||
|         goto LBL_MU; |         goto LBL_MU; | ||||||
|      } |      } | ||||||
|      redux = mp_reduce_2k_l; |      redux = mp_reduce_2k_l; | ||||||
|   }     |   } | ||||||
| 
 | 
 | ||||||
|   /* create M table
 |   /* create M table
 | ||||||
|    * |    * | ||||||
|    * The M table contains powers of the base,  |    * The M table contains powers of the base, | ||||||
|    * e.g. M[x] = G**x mod P |    * e.g. M[x] = G**x mod P | ||||||
|    * |    * | ||||||
|    * The first half of the table is not  |    * The first half of the table is not | ||||||
|    * computed though accept for M[0] and M[1] |    * computed though accept for M[0] and M[1] | ||||||
|    */ |    */ | ||||||
|   if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) { |   if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) { | ||||||
|     goto LBL_MU; |     goto LBL_MU; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* compute the value at M[1<<(winsize-1)] by squaring 
 |   /* compute the value at M[1<<(winsize-1)] by squaring
 | ||||||
|    * M[1] (winsize-1) times  |    * M[1] (winsize-1) times | ||||||
|    */ |    */ | ||||||
|   if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { |   if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { | ||||||
|     goto LBL_MU; |     goto LBL_MU; | ||||||
| @ -106,7 +106,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) | |||||||
| 
 | 
 | ||||||
|   for (x = 0; x < (winsize - 1); x++) { |   for (x = 0; x < (winsize - 1); x++) { | ||||||
|     /* square it */ |     /* square it */ | ||||||
|     if ((err = mp_sqr (&M[1 << (winsize - 1)],  |     if ((err = mp_sqr (&M[1 << (winsize - 1)], | ||||||
|                        &M[1 << (winsize - 1)])) != MP_OKAY) { |                        &M[1 << (winsize - 1)])) != MP_OKAY) { | ||||||
|       goto LBL_MU; |       goto LBL_MU; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* multiplies |a| * |b| and only computes upto digs digits of result
 | /* multiplies |a| * |b| and only computes upto digs digits of result
 | ||||||
|  * HAC pp. 595, Algorithm 14.12  Modified so you can control how  |  * HAC pp. 595, Algorithm 14.12  Modified so you can control how | ||||||
|  * many digits of output are created. |  * many digits of output are created. | ||||||
|  */ |  */ | ||||||
| int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | ||||||
| @ -29,7 +29,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | |||||||
| 
 | 
 | ||||||
|   /* can we use the fast multiplier? */ |   /* can we use the fast multiplier? */ | ||||||
|   if (((digs) < MP_WARRAY) && |   if (((digs) < MP_WARRAY) && | ||||||
|       (MIN (a->used, b->used) <  |       (MIN (a->used, b->used) < | ||||||
|           (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { |           (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { | ||||||
|     return fast_s_mp_mul_digs (a, b, c, digs); |     return fast_s_mp_mul_digs (a, b, c, digs); | ||||||
|   } |   } | ||||||
| @ -51,10 +51,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) | |||||||
|     /* setup some aliases */ |     /* setup some aliases */ | ||||||
|     /* copy of the digit from a used within the nested loop */ |     /* copy of the digit from a used within the nested loop */ | ||||||
|     tmpx = a->dp[ix]; |     tmpx = a->dp[ix]; | ||||||
|      | 
 | ||||||
|     /* an alias for the destination shifted ix places */ |     /* an alias for the destination shifted ix places */ | ||||||
|     tmpt = t.dp + ix; |     tmpt = t.dp + ix; | ||||||
|      | 
 | ||||||
|     /* an alias for the digits of b */ |     /* an alias for the digits of b */ | ||||||
|     tmpy = b->dp; |     tmpy = b->dp; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -48,7 +48,7 @@ int s_mp_sqr (mp_int * a, mp_int * b) | |||||||
| 
 | 
 | ||||||
|     /* alias for where to store the results */ |     /* alias for where to store the results */ | ||||||
|     tmpt        = t.dp + ((2 * ix) + 1); |     tmpt        = t.dp + ((2 * ix) + 1); | ||||||
|      | 
 | ||||||
|     for (iy = ix + 1; iy < pa; iy++) { |     for (iy = ix + 1; iy < pa; iy++) { | ||||||
|       /* first calculate the product */ |       /* first calculate the product */ | ||||||
|       r       = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); |       r       = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								bncore.c
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								bncore.c
									
									
									
									
									
								
							| @ -21,14 +21,14 @@ | |||||||
| ------------------------------------------------------------- | ------------------------------------------------------------- | ||||||
|  Intel P4 Northwood     /GCC v3.4.1   /        88/       128/LTM 0.32 ;-) |  Intel P4 Northwood     /GCC v3.4.1   /        88/       128/LTM 0.32 ;-) | ||||||
|  AMD Athlon64           /GCC v3.4.4   /        80/       120/LTM 0.35 |  AMD Athlon64           /GCC v3.4.4   /        80/       120/LTM 0.35 | ||||||
|   | 
 | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| int     KARATSUBA_MUL_CUTOFF = 80,      /* Min. number of digits before Karatsuba multiplication is used. */ | int     KARATSUBA_MUL_CUTOFF = 80,      /* Min. number of digits before Karatsuba multiplication is used. */ | ||||||
|         KARATSUBA_SQR_CUTOFF = 120,     /* Min. number of digits before Karatsuba squaring is used. */ |         KARATSUBA_SQR_CUTOFF = 120,     /* Min. number of digits before Karatsuba squaring is used. */ | ||||||
|          | 
 | ||||||
|         TOOM_MUL_CUTOFF      = 350,      /* no optimal values of these are known yet so set em high */ |         TOOM_MUL_CUTOFF      = 350,      /* no optimal values of these are known yet so set em high */ | ||||||
|         TOOM_SQR_CUTOFF      = 400;  |         TOOM_SQR_CUTOFF      = 400; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| /* ref:         $Format:%D$ */ | /* ref:         $Format:%D$ */ | ||||||
|  | |||||||
| @ -60,9 +60,9 @@ | |||||||
|    #undef  BN_FAST_MP_INVMOD_C |    #undef  BN_FAST_MP_INVMOD_C | ||||||
| 
 | 
 | ||||||
|    /* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold
 |    /* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold
 | ||||||
|     * which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines]  |     * which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines] | ||||||
|     * which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without |     * which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without | ||||||
|     * trouble.   |     * trouble. | ||||||
|     */ |     */ | ||||||
|    #undef  BN_S_MP_MUL_DIGS_C |    #undef  BN_S_MP_MUL_DIGS_C | ||||||
|    #undef  BN_S_MP_SQR_C |    #undef  BN_S_MP_SQR_C | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user