format with astyle (step 4)
This commit is contained in:
		
							parent
							
								
									4439fae168
								
							
						
					
					
						commit
						a20d9b102c
					
				| @ -25,36 +25,36 @@ | ||||
|  */ | ||||
| int mp_prime_fermat(mp_int *a, mp_int *b, int *result) | ||||
| { | ||||
|   mp_int  t; | ||||
|   int     err; | ||||
|    mp_int  t; | ||||
|    int     err; | ||||
| 
 | ||||
|   /* default to composite  */ | ||||
|   *result = MP_NO; | ||||
|    /* default to composite  */ | ||||
|    *result = MP_NO; | ||||
| 
 | ||||
|   /* ensure b > 1 */ | ||||
|   if (mp_cmp_d(b, 1) != MP_GT) { | ||||
|      return MP_VAL; | ||||
|   } | ||||
|    /* ensure b > 1 */ | ||||
|    if (mp_cmp_d(b, 1) != MP_GT) { | ||||
|       return MP_VAL; | ||||
|    } | ||||
| 
 | ||||
|   /* init t */ | ||||
|   if ((err = mp_init(&t)) != MP_OKAY) { | ||||
|     return err; | ||||
|   } | ||||
|    /* init t */ | ||||
|    if ((err = mp_init(&t)) != MP_OKAY) { | ||||
|       return err; | ||||
|    } | ||||
| 
 | ||||
|   /* compute t = b**a mod a */ | ||||
|   if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) { | ||||
|     goto LBL_T; | ||||
|   } | ||||
|    /* compute t = b**a mod a */ | ||||
|    if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) { | ||||
|       goto LBL_T; | ||||
|    } | ||||
| 
 | ||||
|   /* is it equal to b? */ | ||||
|   if (mp_cmp(&t, b) == MP_EQ) { | ||||
|     *result = MP_YES; | ||||
|   } | ||||
|    /* is it equal to b? */ | ||||
|    if (mp_cmp(&t, b) == MP_EQ) { | ||||
|       *result = MP_YES; | ||||
|    } | ||||
| 
 | ||||
|   err = MP_OKAY; | ||||
|    err = MP_OKAY; | ||||
| LBL_T: | ||||
|   mp_clear(&t); | ||||
|   return err; | ||||
|    mp_clear(&t); | ||||
|    return err; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -22,26 +22,26 @@ | ||||
|  */ | ||||
| int mp_prime_is_divisible(mp_int *a, int *result) | ||||
| { | ||||
|   int     err, ix; | ||||
|   mp_digit res; | ||||
|    int     err, ix; | ||||
|    mp_digit res; | ||||
| 
 | ||||
|   /* default to not */ | ||||
|   *result = MP_NO; | ||||
|    /* default to not */ | ||||
|    *result = MP_NO; | ||||
| 
 | ||||
|   for (ix = 0; ix < PRIME_SIZE; ix++) { | ||||
|     /* what is a mod LBL_prime_tab[ix] */ | ||||
|     if ((err = mp_mod_d(a, ltm_prime_tab[ix], &res)) != MP_OKAY) { | ||||
|       return err; | ||||
|     } | ||||
|    for (ix = 0; ix < PRIME_SIZE; ix++) { | ||||
|       /* what is a mod LBL_prime_tab[ix] */ | ||||
|       if ((err = mp_mod_d(a, ltm_prime_tab[ix], &res)) != MP_OKAY) { | ||||
|          return err; | ||||
|       } | ||||
| 
 | ||||
|     /* is the residue zero? */ | ||||
|     if (res == 0) { | ||||
|       *result = MP_YES; | ||||
|       return MP_OKAY; | ||||
|     } | ||||
|   } | ||||
|       /* is the residue zero? */ | ||||
|       if (res == 0) { | ||||
|          *result = MP_YES; | ||||
|          return MP_OKAY; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|   return MP_OKAY; | ||||
|    return MP_OKAY; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -24,58 +24,58 @@ | ||||
|  */ | ||||
| int mp_prime_is_prime(mp_int *a, int t, int *result) | ||||
| { | ||||
|   mp_int  b; | ||||
|   int     ix, err, res; | ||||
|    mp_int  b; | ||||
|    int     ix, err, res; | ||||
| 
 | ||||
|   /* default to no */ | ||||
|   *result = MP_NO; | ||||
|    /* default to no */ | ||||
|    *result = MP_NO; | ||||
| 
 | ||||
|   /* valid value of t? */ | ||||
|   if ((t <= 0) || (t > PRIME_SIZE)) { | ||||
|     return MP_VAL; | ||||
|   } | ||||
|    /* valid value of t? */ | ||||
|    if ((t <= 0) || (t > PRIME_SIZE)) { | ||||
|       return MP_VAL; | ||||
|    } | ||||
| 
 | ||||
|   /* is the input equal to one of the primes in the table? */ | ||||
|   for (ix = 0; ix < PRIME_SIZE; ix++) { | ||||
|    /* is the input equal to one of the primes in the table? */ | ||||
|    for (ix = 0; ix < PRIME_SIZE; ix++) { | ||||
|       if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { | ||||
|          *result = 1; | ||||
|          return MP_OKAY; | ||||
|       } | ||||
|   } | ||||
|    } | ||||
| 
 | ||||
|   /* first perform trial division */ | ||||
|   if ((err = mp_prime_is_divisible(a, &res)) != MP_OKAY) { | ||||
|     return err; | ||||
|   } | ||||
|    /* first perform trial division */ | ||||
|    if ((err = mp_prime_is_divisible(a, &res)) != MP_OKAY) { | ||||
|       return err; | ||||
|    } | ||||
| 
 | ||||
|   /* return if it was trivially divisible */ | ||||
|   if (res == MP_YES) { | ||||
|     return MP_OKAY; | ||||
|   } | ||||
|    /* return if it was trivially divisible */ | ||||
|    if (res == MP_YES) { | ||||
|       return MP_OKAY; | ||||
|    } | ||||
| 
 | ||||
|   /* now perform the miller-rabin rounds */ | ||||
|   if ((err = mp_init(&b)) != MP_OKAY) { | ||||
|     return err; | ||||
|   } | ||||
|    /* now perform the miller-rabin rounds */ | ||||
|    if ((err = mp_init(&b)) != MP_OKAY) { | ||||
|       return err; | ||||
|    } | ||||
| 
 | ||||
|   for (ix = 0; ix < t; ix++) { | ||||
|     /* set the prime */ | ||||
|     mp_set(&b, ltm_prime_tab[ix]); | ||||
|    for (ix = 0; ix < t; ix++) { | ||||
|       /* set the prime */ | ||||
|       mp_set(&b, ltm_prime_tab[ix]); | ||||
| 
 | ||||
|     if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||
|       goto LBL_B; | ||||
|     } | ||||
|       if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||
|          goto LBL_B; | ||||
|       } | ||||
| 
 | ||||
|     if (res == MP_NO) { | ||||
|       goto LBL_B; | ||||
|     } | ||||
|   } | ||||
|       if (res == MP_NO) { | ||||
|          goto LBL_B; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|   /* passed the test */ | ||||
|   *result = MP_YES; | ||||
|    /* passed the test */ | ||||
|    *result = MP_YES; | ||||
| LBL_B: | ||||
|   mp_clear(&b); | ||||
|   return err; | ||||
|    mp_clear(&b); | ||||
|    return err; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -38,28 +38,28 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | ||||
|    if (mp_cmp_d(a, ltm_prime_tab[PRIME_SIZE-1]) == MP_LT) { | ||||
|       /* find which prime it is bigger than */ | ||||
|       for (x = PRIME_SIZE - 2; x >= 0; x--) { | ||||
|           if (mp_cmp_d(a, ltm_prime_tab[x]) != MP_LT) { | ||||
|              if (bbs_style == 1) { | ||||
|                 /* ok we found a prime smaller or
 | ||||
|                  * equal [so the next is larger] | ||||
|                  * | ||||
|                  * however, the prime must be | ||||
|                  * congruent to 3 mod 4 | ||||
|                  */ | ||||
|                 if ((ltm_prime_tab[x + 1] & 3) != 3) { | ||||
|                    /* scan upwards for a prime congruent to 3 mod 4 */ | ||||
|                    for (y = x + 1; y < PRIME_SIZE; y++) { | ||||
|                        if ((ltm_prime_tab[y] & 3) == 3) { | ||||
|                           mp_set(a, ltm_prime_tab[y]); | ||||
|                           return MP_OKAY; | ||||
|                        } | ||||
|                    } | ||||
|                 } | ||||
|              } else { | ||||
|                 mp_set(a, ltm_prime_tab[x + 1]); | ||||
|                 return MP_OKAY; | ||||
|              } | ||||
|           } | ||||
|          if (mp_cmp_d(a, ltm_prime_tab[x]) != MP_LT) { | ||||
|             if (bbs_style == 1) { | ||||
|                /* ok we found a prime smaller or
 | ||||
|                 * equal [so the next is larger] | ||||
|                 * | ||||
|                 * however, the prime must be | ||||
|                 * congruent to 3 mod 4 | ||||
|                 */ | ||||
|                if ((ltm_prime_tab[x + 1] & 3) != 3) { | ||||
|                   /* scan upwards for a prime congruent to 3 mod 4 */ | ||||
|                   for (y = x + 1; y < PRIME_SIZE; y++) { | ||||
|                      if ((ltm_prime_tab[y] & 3) == 3) { | ||||
|                         mp_set(a, ltm_prime_tab[y]); | ||||
|                         return MP_OKAY; | ||||
|                      } | ||||
|                   } | ||||
|                } | ||||
|             } else { | ||||
|                mp_set(a, ltm_prime_tab[x + 1]); | ||||
|                return MP_OKAY; | ||||
|             } | ||||
|          } | ||||
|       } | ||||
|       /* at this point a maybe 1 */ | ||||
|       if (mp_cmp_d(a, 1) == MP_EQ) { | ||||
| @ -118,18 +118,18 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | ||||
| 
 | ||||
|          /* compute the new residue without using division */ | ||||
|          for (x = 1; x < PRIME_SIZE; x++) { | ||||
|              /* add the step to each residue */ | ||||
|              res_tab[x] += kstep; | ||||
|             /* add the step to each residue */ | ||||
|             res_tab[x] += kstep; | ||||
| 
 | ||||
|              /* subtract the modulus [instead of using division] */ | ||||
|              if (res_tab[x] >= ltm_prime_tab[x]) { | ||||
|                 res_tab[x]  -= ltm_prime_tab[x]; | ||||
|              } | ||||
|             /* subtract the modulus [instead of using division] */ | ||||
|             if (res_tab[x] >= ltm_prime_tab[x]) { | ||||
|                res_tab[x]  -= ltm_prime_tab[x]; | ||||
|             } | ||||
| 
 | ||||
|              /* set flag if zero */ | ||||
|              if (res_tab[x] == 0) { | ||||
|                 y = 1; | ||||
|              } | ||||
|             /* set flag if zero */ | ||||
|             if (res_tab[x] == 0) { | ||||
|                y = 1; | ||||
|             } | ||||
|          } | ||||
|       } while ((y == 1) && (step < ((((mp_digit)1) << DIGIT_BIT) - kstep))); | ||||
| 
 | ||||
| @ -145,13 +145,13 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | ||||
| 
 | ||||
|       /* is this prime? */ | ||||
|       for (x = 0; x < t; x++) { | ||||
|           mp_set(&b, ltm_prime_tab[x]); | ||||
|           if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||
|              goto LBL_ERR; | ||||
|           } | ||||
|           if (res == MP_NO) { | ||||
|              break; | ||||
|           } | ||||
|          mp_set(&b, ltm_prime_tab[x]); | ||||
|          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||
|             goto LBL_ERR; | ||||
|          } | ||||
|          if (res == MP_NO) { | ||||
|             break; | ||||
|          } | ||||
|       } | ||||
| 
 | ||||
|       if (res == MP_YES) { | ||||
|  | ||||
| @ -19,14 +19,14 @@ | ||||
| static const struct { | ||||
|    int k, t; | ||||
| } sizes[] = { | ||||
| {   128,    28 }, | ||||
| {   256,    16 }, | ||||
| {   384,    10 }, | ||||
| {   512,     7 }, | ||||
| {   640,     6 }, | ||||
| {   768,     5 }, | ||||
| {   896,     4 }, | ||||
| {  1024,     4 } | ||||
|    {   128,    28 }, | ||||
|    {   256,    16 }, | ||||
|    {   384,    10 }, | ||||
|    {   512,     7 }, | ||||
|    {   640,     6 }, | ||||
|    {   768,     5 }, | ||||
|    {   896,     4 }, | ||||
|    {  1024,     4 } | ||||
| }; | ||||
| 
 | ||||
| /* returns # of RM trials required for a given bit size */ | ||||
| @ -35,11 +35,11 @@ int mp_prime_rabin_miller_trials(int size) | ||||
|    int x; | ||||
| 
 | ||||
|    for (x = 0; x < (int)(sizeof(sizes)/(sizeof(sizes[0]))); x++) { | ||||
|        if (sizes[x].k == size) { | ||||
|           return sizes[x].t; | ||||
|        } else if (sizes[x].k > size) { | ||||
|           return (x == 0) ? sizes[0].t : sizes[x - 1].t; | ||||
|        } | ||||
|       if (sizes[x].k == size) { | ||||
|          return sizes[x].t; | ||||
|       } else if (sizes[x].k > size) { | ||||
|          return (x == 0) ? sizes[0].t : sizes[x - 1].t; | ||||
|       } | ||||
|    } | ||||
|    return sizes[x-1].t + 1; | ||||
| } | ||||
|  | ||||
| @ -18,57 +18,57 @@ | ||||
| /* returns size of ASCII reprensentation */ | ||||
| int mp_radix_size(mp_int *a, int radix, int *size) | ||||
| { | ||||
|   int     res, digs; | ||||
|   mp_int  t; | ||||
|   mp_digit d; | ||||
|    int     res, digs; | ||||
|    mp_int  t; | ||||
|    mp_digit d; | ||||
| 
 | ||||
|   *size = 0; | ||||
|    *size = 0; | ||||
| 
 | ||||
|   /* make sure the radix is in range */ | ||||
|   if ((radix < 2) || (radix > 64)) { | ||||
|     return MP_VAL; | ||||
|   } | ||||
|    /* make sure the radix is in range */ | ||||
|    if ((radix < 2) || (radix > 64)) { | ||||
|       return MP_VAL; | ||||
|    } | ||||
| 
 | ||||
|   if (mp_iszero(a) == MP_YES) { | ||||
|     *size = 2; | ||||
|     return MP_OKAY; | ||||
|   } | ||||
|    if (mp_iszero(a) == MP_YES) { | ||||
|       *size = 2; | ||||
|       return MP_OKAY; | ||||
|    } | ||||
| 
 | ||||
|   /* special case for binary */ | ||||
|   if (radix == 2) { | ||||
|     *size = mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1; | ||||
|     return MP_OKAY; | ||||
|   } | ||||
|    /* special case for binary */ | ||||
|    if (radix == 2) { | ||||
|       *size = mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1; | ||||
|       return MP_OKAY; | ||||
|    } | ||||
| 
 | ||||
|   /* digs is the digit count */ | ||||
|   digs = 0; | ||||
|    /* digs is the digit count */ | ||||
|    digs = 0; | ||||
| 
 | ||||
|   /* if it's negative add one for the sign */ | ||||
|   if (a->sign == MP_NEG) { | ||||
|     ++digs; | ||||
|   } | ||||
|    /* if it's negative add one for the sign */ | ||||
|    if (a->sign == MP_NEG) { | ||||
|       ++digs; | ||||
|    } | ||||
| 
 | ||||
|   /* init a copy of the input */ | ||||
|   if ((res = mp_init_copy(&t, a)) != MP_OKAY) { | ||||
|     return res; | ||||
|   } | ||||
| 
 | ||||
|   /* force temp to positive */ | ||||
|   t.sign = MP_ZPOS; | ||||
| 
 | ||||
|   /* fetch out all of the digits */ | ||||
|   while (mp_iszero(&t) == MP_NO) { | ||||
|     if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { | ||||
|       mp_clear(&t); | ||||
|    /* init a copy of the input */ | ||||
|    if ((res = mp_init_copy(&t, a)) != MP_OKAY) { | ||||
|       return res; | ||||
|     } | ||||
|     ++digs; | ||||
|   } | ||||
|   mp_clear(&t); | ||||
|    } | ||||
| 
 | ||||
|   /* return digs + 1, the 1 is for the NULL byte that would be required. */ | ||||
|   *size = digs + 1; | ||||
|   return MP_OKAY; | ||||
|    /* force temp to positive */ | ||||
|    t.sign = MP_ZPOS; | ||||
| 
 | ||||
|    /* fetch out all of the digits */ | ||||
|    while (mp_iszero(&t) == MP_NO) { | ||||
|       if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { | ||||
|          mp_clear(&t); | ||||
|          return res; | ||||
|       } | ||||
|       ++digs; | ||||
|    } | ||||
|    mp_clear(&t); | ||||
| 
 | ||||
|    /* return digs + 1, the 1 is for the NULL byte that would be required. */ | ||||
|    *size = digs + 1; | ||||
|    return MP_OKAY; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
| @ -18,21 +18,21 @@ | ||||
| /* read signed bin, big endian, first byte is 0==positive or 1==negative */ | ||||
| int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) | ||||
| { | ||||
|   int     res; | ||||
|    int     res; | ||||
| 
 | ||||
|   /* read magnitude */ | ||||
|   if ((res = mp_read_unsigned_bin(a, b + 1, c - 1)) != MP_OKAY) { | ||||
|     return res; | ||||
|   } | ||||
|    /* read magnitude */ | ||||
|    if ((res = mp_read_unsigned_bin(a, b + 1, c - 1)) != MP_OKAY) { | ||||
|       return res; | ||||
|    } | ||||
| 
 | ||||
|   /* first byte is 0 for positive, non-zero for negative */ | ||||
|   if (b[0] == 0) { | ||||
|      a->sign = MP_ZPOS; | ||||
|   } else { | ||||
|      a->sign = MP_NEG; | ||||
|   } | ||||
|    /* first byte is 0 for positive, non-zero for negative */ | ||||
|    if (b[0] == 0) { | ||||
|       a->sign = MP_ZPOS; | ||||
|    } else { | ||||
|       a->sign = MP_NEG; | ||||
|    } | ||||
| 
 | ||||
|   return MP_OKAY; | ||||
|    return MP_OKAY; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -18,35 +18,35 @@ | ||||
| /* reads a unsigned char array, assumes the msb is stored first [big endian] */ | ||||
| int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) | ||||
| { | ||||
|   int     res; | ||||
|    int     res; | ||||
| 
 | ||||
|   /* make sure there are at least two digits */ | ||||
|   if (a->alloc < 2) { | ||||
|      if ((res = mp_grow(a, 2)) != MP_OKAY) { | ||||
|         return res; | ||||
|      } | ||||
|   } | ||||
|    /* make sure there are at least two digits */ | ||||
|    if (a->alloc < 2) { | ||||
|       if ((res = mp_grow(a, 2)) != MP_OKAY) { | ||||
|          return res; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|   /* zero the int */ | ||||
|   mp_zero(a); | ||||
|    /* zero the int */ | ||||
|    mp_zero(a); | ||||
| 
 | ||||
|   /* read the bytes in */ | ||||
|   while (c-- > 0) { | ||||
|     if ((res = mp_mul_2d(a, 8, a)) != MP_OKAY) { | ||||
|       return res; | ||||
|     } | ||||
|    /* read the bytes in */ | ||||
|    while (c-- > 0) { | ||||
|       if ((res = mp_mul_2d(a, 8, a)) != MP_OKAY) { | ||||
|          return res; | ||||
|       } | ||||
| 
 | ||||
| #ifndef MP_8BIT | ||||
|     a->dp[0] |= *b++; | ||||
|     a->used += 1; | ||||
|       a->dp[0] |= *b++; | ||||
|       a->used += 1; | ||||
| #else | ||||
|     a->dp[0] = (*b & MP_MASK); | ||||
|     a->dp[1] |= ((*b++ >> 7U) & 1); | ||||
|     a->used += 2; | ||||
|       a->dp[0] = (*b & MP_MASK); | ||||
|       a->dp[1] |= ((*b++ >> 7U) & 1); | ||||
|       a->used += 2; | ||||
| #endif | ||||
|   } | ||||
|   mp_clamp(a); | ||||
|   return MP_OKAY; | ||||
|    } | ||||
|    mp_clamp(a); | ||||
|    return MP_OKAY; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -32,14 +32,14 @@ int mp_reduce_is_2k(mp_int *a) | ||||
| 
 | ||||
|       /* Test every bit from the second digit up, must be 1 */ | ||||
|       for (ix = DIGIT_BIT; ix < iy; ix++) { | ||||
|           if ((a->dp[iw] & iz) == 0) { | ||||
|              return MP_NO; | ||||
|           } | ||||
|           iz <<= 1; | ||||
|           if (iz > (mp_digit)MP_MASK) { | ||||
|              ++iw; | ||||
|              iz = 1; | ||||
|           } | ||||
|          if ((a->dp[iw] & iz) == 0) { | ||||
|             return MP_NO; | ||||
|          } | ||||
|          iz <<= 1; | ||||
|          if (iz > (mp_digit)MP_MASK) { | ||||
|             ++iw; | ||||
|             iz = 1; | ||||
|          } | ||||
|       } | ||||
|    } | ||||
|    return MP_YES; | ||||
|  | ||||
| @ -27,9 +27,9 @@ int mp_reduce_is_2k_l(mp_int *a) | ||||
|    } else if (a->used > 1) { | ||||
|       /* if more than half of the digits are -1 we're sold */ | ||||
|       for (iy = ix = 0; ix < a->used; ix++) { | ||||
|           if (a->dp[ix] == MP_MASK) { | ||||
|               ++iy; | ||||
|           } | ||||
|          if (a->dp[ix] == MP_MASK) { | ||||
|             ++iy; | ||||
|          } | ||||
|       } | ||||
|       return (iy >= (a->used/2)) ? MP_YES : MP_NO; | ||||
| 
 | ||||
|  | ||||
| @ -20,12 +20,12 @@ | ||||
|  */ | ||||
| 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) { | ||||
|     return res; | ||||
|   } | ||||
|   return mp_div(a, b, a, NULL); | ||||
|    if ((res = mp_2expt(a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { | ||||
|       return res; | ||||
|    } | ||||
|    return mp_div(a, b, a, NULL); | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										72
									
								
								bn_mp_rshd.c
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								bn_mp_rshd.c
									
									
									
									
									
								
							| @ -18,52 +18,52 @@ | ||||
| /* shift right a certain amount of digits */ | ||||
| void mp_rshd(mp_int *a, int b) | ||||
| { | ||||
|   int     x; | ||||
|    int     x; | ||||
| 
 | ||||
|   /* if b <= 0 then ignore it */ | ||||
|   if (b <= 0) { | ||||
|     return; | ||||
|   } | ||||
|    /* if b <= 0 then ignore it */ | ||||
|    if (b <= 0) { | ||||
|       return; | ||||
|    } | ||||
| 
 | ||||
|   /* if b > used then simply zero it and return */ | ||||
|   if (a->used <= b) { | ||||
|     mp_zero(a); | ||||
|     return; | ||||
|   } | ||||
|    /* if b > used then simply zero it and return */ | ||||
|    if (a->used <= b) { | ||||
|       mp_zero(a); | ||||
|       return; | ||||
|    } | ||||
| 
 | ||||
|   { | ||||
|     mp_digit *bottom, *top; | ||||
|    { | ||||
|       mp_digit *bottom, *top; | ||||
| 
 | ||||
|     /* shift the digits down */ | ||||
|       /* shift the digits down */ | ||||
| 
 | ||||
|     /* bottom */ | ||||
|     bottom = a->dp; | ||||
|       /* bottom */ | ||||
|       bottom = a->dp; | ||||
| 
 | ||||
|     /* top [offset into digits] */ | ||||
|     top = a->dp + b; | ||||
|       /* top [offset into digits] */ | ||||
|       top = a->dp + b; | ||||
| 
 | ||||
|     /* this is implemented as a sliding window where
 | ||||
|      * the window is b-digits long and digits from | ||||
|      * the top of the window are copied to the bottom | ||||
|      * | ||||
|      * e.g. | ||||
|       /* this is implemented as a sliding window where
 | ||||
|        * the window is b-digits long and digits from | ||||
|        * the top of the window are copied to the bottom | ||||
|        * | ||||
|        * e.g. | ||||
| 
 | ||||
|      b-2 | b-1 | b0 | b1 | b2 | ... | bb |   ----> | ||||
|                  /\                   |      ----> | ||||
|                   \-------------------/      ----> | ||||
|      */ | ||||
|     for (x = 0; x < (a->used - b); x++) { | ||||
|       *bottom++ = *top++; | ||||
|     } | ||||
|        b-2 | b-1 | b0 | b1 | b2 | ... | bb |   ----> | ||||
|                    /\                   |      ----> | ||||
|                     \-------------------/      ----> | ||||
|        */ | ||||
|       for (x = 0; x < (a->used - b); x++) { | ||||
|          *bottom++ = *top++; | ||||
|       } | ||||
| 
 | ||||
|     /* zero the top digits */ | ||||
|     for (; x < a->used; x++) { | ||||
|       *bottom++ = 0; | ||||
|     } | ||||
|   } | ||||
|       /* zero the top digits */ | ||||
|       for (; x < a->used; x++) { | ||||
|          *bottom++ = 0; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|   /* remove excess digits */ | ||||
|   a->used -= b; | ||||
|    /* remove excess digits */ | ||||
|    a->used -= b; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -18,9 +18,9 @@ | ||||
| /* set to a digit */ | ||||
| void mp_set(mp_int *a, mp_digit b) | ||||
| { | ||||
|   mp_zero(a); | ||||
|   a->dp[0] = b & MP_MASK; | ||||
|   a->used  = (a->dp[0] != 0) ? 1 : 0; | ||||
|    mp_zero(a); | ||||
|    a->dp[0] = b & MP_MASK; | ||||
|    a->used  = (a->dp[0] != 0) ? 1 : 0; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -18,28 +18,28 @@ | ||||
| /* set a 32-bit const */ | ||||
| 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 */ | ||||
|   for (x = 0; x < 8; x++) { | ||||
|     /* shift the number up four bits */ | ||||
|     if ((res = mp_mul_2d(a, 4, a)) != MP_OKAY) { | ||||
|       return res; | ||||
|     } | ||||
|    /* set four bits at a time */ | ||||
|    for (x = 0; x < 8; x++) { | ||||
|       /* shift the number up four bits */ | ||||
|       if ((res = mp_mul_2d(a, 4, a)) != MP_OKAY) { | ||||
|          return res; | ||||
|       } | ||||
| 
 | ||||
|     /* OR in the top four bits of the source */ | ||||
|     a->dp[0] |= (b >> 28) & 15; | ||||
|       /* OR in the top four bits of the source */ | ||||
|       a->dp[0] |= (b >> 28) & 15; | ||||
| 
 | ||||
|     /* shift the source up to the next four bits */ | ||||
|     b <<= 4; | ||||
|       /* shift the source up to the next four bits */ | ||||
|       b <<= 4; | ||||
| 
 | ||||
|     /* ensure that digits are not clamped off */ | ||||
|     a->used += 1; | ||||
|   } | ||||
|   mp_clamp(a); | ||||
|   return MP_OKAY; | ||||
|       /* ensure that digits are not clamped off */ | ||||
|       a->used += 1; | ||||
|    } | ||||
|    mp_clamp(a); | ||||
|    return MP_OKAY; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -18,21 +18,21 @@ | ||||
| /* shrink a bignum */ | ||||
| int mp_shrink(mp_int *a) | ||||
| { | ||||
|   mp_digit *tmp; | ||||
|   int used = 1; | ||||
|    mp_digit *tmp; | ||||
|    int used = 1; | ||||
| 
 | ||||
|   if (a->used > 0) { | ||||
|     used = a->used; | ||||
|   } | ||||
|    if (a->used > 0) { | ||||
|       used = a->used; | ||||
|    } | ||||
| 
 | ||||
|   if (a->alloc != used) { | ||||
|     if ((tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof (mp_digit) * used)) == NULL) { | ||||
|       return MP_MEM; | ||||
|     } | ||||
|     a->dp    = tmp; | ||||
|     a->alloc = used; | ||||
|   } | ||||
|   return MP_OKAY; | ||||
|    if (a->alloc != used) { | ||||
|       if ((tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * used)) == NULL) { | ||||
|          return MP_MEM; | ||||
|       } | ||||
|       a->dp    = tmp; | ||||
|       a->alloc = used; | ||||
|    } | ||||
|    return MP_OKAY; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -18,7 +18,7 @@ | ||||
| /* get the size for an signed equivalent */ | ||||
| int mp_signed_bin_size(mp_int *a) | ||||
| { | ||||
|   return 1 + mp_unsigned_bin_size(a); | ||||
|    return 1 + mp_unsigned_bin_size(a); | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										46
									
								
								bn_mp_sqr.c
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								bn_mp_sqr.c
									
									
									
									
									
								
							| @ -18,39 +18,39 @@ | ||||
| /* computes b = a*a */ | ||||
| int mp_sqr(mp_int *a, mp_int *b) | ||||
| { | ||||
|   int     res; | ||||
|    int     res; | ||||
| 
 | ||||
| #ifdef BN_MP_TOOM_SQR_C | ||||
|   /* use Toom-Cook? */ | ||||
|   if (a->used >= TOOM_SQR_CUTOFF) { | ||||
|     res = mp_toom_sqr(a, b); | ||||
|   /* Karatsuba? */ | ||||
|   } else | ||||
|    /* use Toom-Cook? */ | ||||
|    if (a->used >= TOOM_SQR_CUTOFF) { | ||||
|       res = mp_toom_sqr(a, b); | ||||
|       /* Karatsuba? */ | ||||
|    } else | ||||
| #endif | ||||
| #ifdef BN_MP_KARATSUBA_SQR_C | ||||
|   if (a->used >= KARATSUBA_SQR_CUTOFF) { | ||||
|     res = mp_karatsuba_sqr(a, b); | ||||
|   } else | ||||
|       if (a->used >= KARATSUBA_SQR_CUTOFF) { | ||||
|          res = mp_karatsuba_sqr(a, b); | ||||
|       } else | ||||
| #endif | ||||
|   { | ||||
|       { | ||||
| #ifdef BN_FAST_S_MP_SQR_C | ||||
|     /* can we use the fast comba multiplier? */ | ||||
|     if ((((a->used * 2) + 1) < MP_WARRAY) && | ||||
|          (a->used < | ||||
|          (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) { | ||||
|       res = fast_s_mp_sqr(a, b); | ||||
|     } else | ||||
|          /* can we use the fast comba multiplier? */ | ||||
|          if ((((a->used * 2) + 1) < MP_WARRAY) && | ||||
|              (a->used < | ||||
|               (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) { | ||||
|             res = fast_s_mp_sqr(a, b); | ||||
|          } else | ||||
| #endif | ||||
|     { | ||||
|          { | ||||
| #ifdef BN_S_MP_SQR_C | ||||
|       res = s_mp_sqr(a, b); | ||||
|             res = s_mp_sqr(a, b); | ||||
| #else | ||||
|       res = MP_VAL; | ||||
|             res = MP_VAL; | ||||
| #endif | ||||
|     } | ||||
|   } | ||||
|   b->sign = MP_ZPOS; | ||||
|   return res; | ||||
|          } | ||||
|       } | ||||
|    b->sign = MP_ZPOS; | ||||
|    return res; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -18,20 +18,20 @@ | ||||
| /* c = a * a (mod b) */ | ||||
| int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c) | ||||
| { | ||||
|   int     res; | ||||
|   mp_int  t; | ||||
|    int     res; | ||||
|    mp_int  t; | ||||
| 
 | ||||
|   if ((res = mp_init(&t)) != MP_OKAY) { | ||||
|     return res; | ||||
|   } | ||||
|    if ((res = mp_init(&t)) != MP_OKAY) { | ||||
|       return res; | ||||
|    } | ||||
| 
 | ||||
|   if ((res = mp_sqr(a, &t)) != MP_OKAY) { | ||||
|     mp_clear(&t); | ||||
|     return res; | ||||
|   } | ||||
|   res = mp_mod(&t, b, c); | ||||
|   mp_clear(&t); | ||||
|   return res; | ||||
|    if ((res = mp_sqr(a, &t)) != MP_OKAY) { | ||||
|       mp_clear(&t); | ||||
|       return res; | ||||
|    } | ||||
|    res = mp_mod(&t, b, c); | ||||
|    mp_clear(&t); | ||||
|    return res; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
| @ -17,108 +17,108 @@ | ||||
| 
 | ||||
| int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret) | ||||
| { | ||||
|   int res, legendre; | ||||
|   mp_int t1, C, Q, S, Z, M, T, R, two; | ||||
|   mp_digit i; | ||||
|    int res, legendre; | ||||
|    mp_int t1, C, Q, S, Z, M, T, R, two; | ||||
|    mp_digit i; | ||||
| 
 | ||||
|   /* first handle the simple cases */ | ||||
|   if (mp_cmp_d(n, 0) == MP_EQ) { | ||||
|     mp_zero(ret); | ||||
|     return MP_OKAY; | ||||
|   } | ||||
|   if (mp_cmp_d(prime, 2) == MP_EQ)                              return MP_VAL; /* prime must be odd */ | ||||
|   if ((res = mp_jacobi(n, prime, &legendre)) != MP_OKAY)        return res; | ||||
|   if (legendre == -1)                                           return MP_VAL; /* quadratic non-residue mod prime */ | ||||
|    /* first handle the simple cases */ | ||||
|    if (mp_cmp_d(n, 0) == MP_EQ) { | ||||
|       mp_zero(ret); | ||||
|       return MP_OKAY; | ||||
|    } | ||||
|    if (mp_cmp_d(prime, 2) == MP_EQ)                              return MP_VAL; /* prime must be odd */ | ||||
|    if ((res = mp_jacobi(n, prime, &legendre)) != MP_OKAY)        return res; | ||||
|    if (legendre == -1)                                           return MP_VAL; /* quadratic non-residue mod prime */ | ||||
| 
 | ||||
|   if ((res = mp_init_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL)) != MP_OKAY) { | ||||
|     return res; | ||||
|   } | ||||
|    if ((res = mp_init_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL)) != MP_OKAY) { | ||||
|       return res; | ||||
|    } | ||||
| 
 | ||||
|   /* SPECIAL CASE: if prime mod 4 == 3
 | ||||
|    * compute directly: res = n^(prime+1)/4 mod prime | ||||
|    * Handbook of Applied Cryptography algorithm 3.36 | ||||
|    */ | ||||
|   if ((res = mp_mod_d(prime, 4, &i)) != MP_OKAY)                goto cleanup; | ||||
|   if (i == 3) { | ||||
|     if ((res = mp_add_d(prime, 1, &t1)) != MP_OKAY)             goto cleanup; | ||||
|     if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup; | ||||
|     if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup; | ||||
|     if ((res = mp_exptmod(n, &t1, prime, ret)) != MP_OKAY)      goto cleanup; | ||||
|     res = MP_OKAY; | ||||
|     goto cleanup; | ||||
|   } | ||||
| 
 | ||||
|   /* NOW: Tonelli-Shanks algorithm */ | ||||
| 
 | ||||
|   /* factor out powers of 2 from prime-1, defining Q and S as: prime-1 = Q*2^S */ | ||||
|   if ((res = mp_copy(prime, &Q)) != MP_OKAY)                    goto cleanup; | ||||
|   if ((res = mp_sub_d(&Q, 1, &Q)) != MP_OKAY)                   goto cleanup; | ||||
|   /* Q = prime - 1 */ | ||||
|   mp_zero(&S); | ||||
|   /* S = 0 */ | ||||
|   while (mp_iseven(&Q) != MP_NO) { | ||||
|     if ((res = mp_div_2(&Q, &Q)) != MP_OKAY)                    goto cleanup; | ||||
|     /* Q = Q / 2 */ | ||||
|     if ((res = mp_add_d(&S, 1, &S)) != MP_OKAY)                 goto cleanup; | ||||
|     /* S = S + 1 */ | ||||
|   } | ||||
| 
 | ||||
|   /* find a Z such that the Legendre symbol (Z|prime) == -1 */ | ||||
|   if ((res = mp_set_int(&Z, 2)) != MP_OKAY)                     goto cleanup; | ||||
|   /* Z = 2 */ | ||||
|   while (1) { | ||||
|     if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup; | ||||
|     if (legendre == -1) break; | ||||
|     if ((res = mp_add_d(&Z, 1, &Z)) != MP_OKAY)                 goto cleanup; | ||||
|     /* Z = Z + 1 */ | ||||
|   } | ||||
| 
 | ||||
|   if ((res = mp_exptmod(&Z, &Q, prime, &C)) != MP_OKAY)         goto cleanup; | ||||
|   /* C = Z ^ Q mod prime */ | ||||
|   if ((res = mp_add_d(&Q, 1, &t1)) != MP_OKAY)                  goto cleanup; | ||||
|   if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                    goto cleanup; | ||||
|   /* t1 = (Q + 1) / 2 */ | ||||
|   if ((res = mp_exptmod(n, &t1, prime, &R)) != MP_OKAY)         goto cleanup; | ||||
|   /* R = n ^ ((Q + 1) / 2) mod prime */ | ||||
|   if ((res = mp_exptmod(n, &Q, prime, &T)) != MP_OKAY)          goto cleanup; | ||||
|   /* T = n ^ Q mod prime */ | ||||
|   if ((res = mp_copy(&S, &M)) != MP_OKAY)                       goto cleanup; | ||||
|   /* M = S */ | ||||
|   if ((res = mp_set_int(&two, 2)) != MP_OKAY)                   goto cleanup; | ||||
| 
 | ||||
|   res = MP_VAL; | ||||
|   while (1) { | ||||
|     if ((res = mp_copy(&T, &t1)) != MP_OKAY)                    goto cleanup; | ||||
|     i = 0; | ||||
|     while (1) { | ||||
|       if (mp_cmp_d(&t1, 1) == MP_EQ) break; | ||||
|       if ((res = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup; | ||||
|       i++; | ||||
|     } | ||||
|     if (i == 0) { | ||||
|       if ((res = mp_copy(&R, ret)) != MP_OKAY)                  goto cleanup; | ||||
|    /* SPECIAL CASE: if prime mod 4 == 3
 | ||||
|     * compute directly: res = n^(prime+1)/4 mod prime | ||||
|     * Handbook of Applied Cryptography algorithm 3.36 | ||||
|     */ | ||||
|    if ((res = mp_mod_d(prime, 4, &i)) != MP_OKAY)                goto cleanup; | ||||
|    if (i == 3) { | ||||
|       if ((res = mp_add_d(prime, 1, &t1)) != MP_OKAY)             goto cleanup; | ||||
|       if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup; | ||||
|       if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                  goto cleanup; | ||||
|       if ((res = mp_exptmod(n, &t1, prime, ret)) != MP_OKAY)      goto cleanup; | ||||
|       res = MP_OKAY; | ||||
|       goto cleanup; | ||||
|     } | ||||
|     if ((res = mp_sub_d(&M, i, &t1)) != MP_OKAY)                goto cleanup; | ||||
|     if ((res = mp_sub_d(&t1, 1, &t1)) != MP_OKAY)               goto cleanup; | ||||
|     if ((res = mp_exptmod(&two, &t1, prime, &t1)) != MP_OKAY)   goto cleanup; | ||||
|     /* t1 = 2 ^ (M - i - 1) */ | ||||
|     if ((res = mp_exptmod(&C, &t1, prime, &t1)) != MP_OKAY)     goto cleanup; | ||||
|     /* t1 = C ^ (2 ^ (M - i - 1)) mod prime */ | ||||
|     if ((res = mp_sqrmod(&t1, prime, &C)) != MP_OKAY)           goto cleanup; | ||||
|     /* C = (t1 * t1) mod prime */ | ||||
|     if ((res = mp_mulmod(&R, &t1, prime, &R)) != MP_OKAY)       goto cleanup; | ||||
|     /* R = (R * t1) mod prime */ | ||||
|     if ((res = mp_mulmod(&T, &C, prime, &T)) != MP_OKAY)        goto cleanup; | ||||
|     /* T = (T * C) mod prime */ | ||||
|     mp_set(&M, i); | ||||
|     /* M = i */ | ||||
|   } | ||||
|    } | ||||
| 
 | ||||
|    /* NOW: Tonelli-Shanks algorithm */ | ||||
| 
 | ||||
|    /* factor out powers of 2 from prime-1, defining Q and S as: prime-1 = Q*2^S */ | ||||
|    if ((res = mp_copy(prime, &Q)) != MP_OKAY)                    goto cleanup; | ||||
|    if ((res = mp_sub_d(&Q, 1, &Q)) != MP_OKAY)                   goto cleanup; | ||||
|    /* Q = prime - 1 */ | ||||
|    mp_zero(&S); | ||||
|    /* S = 0 */ | ||||
|    while (mp_iseven(&Q) != MP_NO) { | ||||
|       if ((res = mp_div_2(&Q, &Q)) != MP_OKAY)                    goto cleanup; | ||||
|       /* Q = Q / 2 */ | ||||
|       if ((res = mp_add_d(&S, 1, &S)) != MP_OKAY)                 goto cleanup; | ||||
|       /* S = S + 1 */ | ||||
|    } | ||||
| 
 | ||||
|    /* find a Z such that the Legendre symbol (Z|prime) == -1 */ | ||||
|    if ((res = mp_set_int(&Z, 2)) != MP_OKAY)                     goto cleanup; | ||||
|    /* Z = 2 */ | ||||
|    while (1) { | ||||
|       if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup; | ||||
|       if (legendre == -1) break; | ||||
|       if ((res = mp_add_d(&Z, 1, &Z)) != MP_OKAY)                 goto cleanup; | ||||
|       /* Z = Z + 1 */ | ||||
|    } | ||||
| 
 | ||||
|    if ((res = mp_exptmod(&Z, &Q, prime, &C)) != MP_OKAY)         goto cleanup; | ||||
|    /* C = Z ^ Q mod prime */ | ||||
|    if ((res = mp_add_d(&Q, 1, &t1)) != MP_OKAY)                  goto cleanup; | ||||
|    if ((res = mp_div_2(&t1, &t1)) != MP_OKAY)                    goto cleanup; | ||||
|    /* t1 = (Q + 1) / 2 */ | ||||
|    if ((res = mp_exptmod(n, &t1, prime, &R)) != MP_OKAY)         goto cleanup; | ||||
|    /* R = n ^ ((Q + 1) / 2) mod prime */ | ||||
|    if ((res = mp_exptmod(n, &Q, prime, &T)) != MP_OKAY)          goto cleanup; | ||||
|    /* T = n ^ Q mod prime */ | ||||
|    if ((res = mp_copy(&S, &M)) != MP_OKAY)                       goto cleanup; | ||||
|    /* M = S */ | ||||
|    if ((res = mp_set_int(&two, 2)) != MP_OKAY)                   goto cleanup; | ||||
| 
 | ||||
|    res = MP_VAL; | ||||
|    while (1) { | ||||
|       if ((res = mp_copy(&T, &t1)) != MP_OKAY)                    goto cleanup; | ||||
|       i = 0; | ||||
|       while (1) { | ||||
|          if (mp_cmp_d(&t1, 1) == MP_EQ) break; | ||||
|          if ((res = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup; | ||||
|          i++; | ||||
|       } | ||||
|       if (i == 0) { | ||||
|          if ((res = mp_copy(&R, ret)) != MP_OKAY)                  goto cleanup; | ||||
|          res = MP_OKAY; | ||||
|          goto cleanup; | ||||
|       } | ||||
|       if ((res = mp_sub_d(&M, i, &t1)) != MP_OKAY)                goto cleanup; | ||||
|       if ((res = mp_sub_d(&t1, 1, &t1)) != MP_OKAY)               goto cleanup; | ||||
|       if ((res = mp_exptmod(&two, &t1, prime, &t1)) != MP_OKAY)   goto cleanup; | ||||
|       /* t1 = 2 ^ (M - i - 1) */ | ||||
|       if ((res = mp_exptmod(&C, &t1, prime, &t1)) != MP_OKAY)     goto cleanup; | ||||
|       /* t1 = C ^ (2 ^ (M - i - 1)) mod prime */ | ||||
|       if ((res = mp_sqrmod(&t1, prime, &C)) != MP_OKAY)           goto cleanup; | ||||
|       /* C = (t1 * t1) mod prime */ | ||||
|       if ((res = mp_mulmod(&R, &t1, prime, &R)) != MP_OKAY)       goto cleanup; | ||||
|       /* R = (R * t1) mod prime */ | ||||
|       if ((res = mp_mulmod(&T, &C, prime, &T)) != MP_OKAY)        goto cleanup; | ||||
|       /* T = (T * C) mod prime */ | ||||
|       mp_set(&M, i); | ||||
|       /* M = i */ | ||||
|    } | ||||
| 
 | ||||
| cleanup: | ||||
|   mp_clear_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL); | ||||
|   return res; | ||||
|    mp_clear_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL); | ||||
|    return res; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
							
								
								
									
										110
									
								
								bn_mp_sub_d.c
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								bn_mp_sub_d.c
									
									
									
									
									
								
							| @ -18,71 +18,71 @@ | ||||
| /* single digit subtraction */ | ||||
| int mp_sub_d(mp_int *a, mp_digit b, mp_int *c) | ||||
| { | ||||
|   mp_digit *tmpa, *tmpc, mu; | ||||
|   int       res, ix, oldused; | ||||
|    mp_digit *tmpa, *tmpc, mu; | ||||
|    int       res, ix, oldused; | ||||
| 
 | ||||
|   /* grow c as required */ | ||||
|   if (c->alloc < (a->used + 1)) { | ||||
|      if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { | ||||
|         return res; | ||||
|      } | ||||
|   } | ||||
|    /* grow c as required */ | ||||
|    if (c->alloc < (a->used + 1)) { | ||||
|       if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { | ||||
|          return res; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|   /* if a is negative just do an unsigned
 | ||||
|    * addition [with fudged signs] | ||||
|    */ | ||||
|   if (a->sign == MP_NEG) { | ||||
|      a->sign = MP_ZPOS; | ||||
|      res     = mp_add_d(a, b, c); | ||||
|      a->sign = c->sign = MP_NEG; | ||||
|    /* if a is negative just do an unsigned
 | ||||
|     * addition [with fudged signs] | ||||
|     */ | ||||
|    if (a->sign == MP_NEG) { | ||||
|       a->sign = MP_ZPOS; | ||||
|       res     = mp_add_d(a, b, c); | ||||
|       a->sign = c->sign = MP_NEG; | ||||
| 
 | ||||
|      /* clamp */ | ||||
|      mp_clamp(c); | ||||
|       /* clamp */ | ||||
|       mp_clamp(c); | ||||
| 
 | ||||
|      return res; | ||||
|   } | ||||
|       return res; | ||||
|    } | ||||
| 
 | ||||
|   /* setup regs */ | ||||
|   oldused = c->used; | ||||
|   tmpa    = a->dp; | ||||
|   tmpc    = c->dp; | ||||
|    /* setup regs */ | ||||
|    oldused = c->used; | ||||
|    tmpa    = a->dp; | ||||
|    tmpc    = c->dp; | ||||
| 
 | ||||
|   /* if a <= b simply fix the single digit */ | ||||
|   if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) { | ||||
|      if (a->used == 1) { | ||||
|         *tmpc++ = b - *tmpa; | ||||
|      } else { | ||||
|         *tmpc++ = b; | ||||
|      } | ||||
|      ix      = 1; | ||||
|    /* if a <= b simply fix the single digit */ | ||||
|    if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) { | ||||
|       if (a->used == 1) { | ||||
|          *tmpc++ = b - *tmpa; | ||||
|       } else { | ||||
|          *tmpc++ = b; | ||||
|       } | ||||
|       ix      = 1; | ||||
| 
 | ||||
|      /* negative/1digit */ | ||||
|      c->sign = MP_NEG; | ||||
|      c->used = 1; | ||||
|   } else { | ||||
|      /* positive/size */ | ||||
|      c->sign = MP_ZPOS; | ||||
|      c->used = a->used; | ||||
|       /* negative/1digit */ | ||||
|       c->sign = MP_NEG; | ||||
|       c->used = 1; | ||||
|    } else { | ||||
|       /* positive/size */ | ||||
|       c->sign = MP_ZPOS; | ||||
|       c->used = a->used; | ||||
| 
 | ||||
|      /* subtract first digit */ | ||||
|      *tmpc    = *tmpa++ - b; | ||||
|      mu       = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1); | ||||
|      *tmpc++ &= MP_MASK; | ||||
|       /* subtract first digit */ | ||||
|       *tmpc    = *tmpa++ - b; | ||||
|       mu       = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1); | ||||
|       *tmpc++ &= MP_MASK; | ||||
| 
 | ||||
|      /* handle rest of the digits */ | ||||
|      for (ix = 1; ix < a->used; ix++) { | ||||
|         *tmpc    = *tmpa++ - mu; | ||||
|         mu       = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1); | ||||
|         *tmpc++ &= MP_MASK; | ||||
|      } | ||||
|   } | ||||
|       /* handle rest of the digits */ | ||||
|       for (ix = 1; ix < a->used; ix++) { | ||||
|          *tmpc    = *tmpa++ - mu; | ||||
|          mu       = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1); | ||||
|          *tmpc++ &= MP_MASK; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|   /* zero excess digits */ | ||||
|   while (ix++ < oldused) { | ||||
|      *tmpc++ = 0; | ||||
|   } | ||||
|   mp_clamp(c); | ||||
|   return MP_OKAY; | ||||
|    /* zero excess digits */ | ||||
|    while (ix++ < oldused) { | ||||
|       *tmpc++ = 0; | ||||
|    } | ||||
|    mp_clamp(c); | ||||
|    return MP_OKAY; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
| @ -18,21 +18,21 @@ | ||||
| /* d = a - b (mod c) */ | ||||
| int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d) | ||||
| { | ||||
|   int     res; | ||||
|   mp_int  t; | ||||
|    int     res; | ||||
|    mp_int  t; | ||||
| 
 | ||||
| 
 | ||||
|   if ((res = mp_init(&t)) != MP_OKAY) { | ||||
|     return res; | ||||
|   } | ||||
|    if ((res = mp_init(&t)) != MP_OKAY) { | ||||
|       return res; | ||||
|    } | ||||
| 
 | ||||
|   if ((res = mp_sub(a, b, &t)) != MP_OKAY) { | ||||
|     mp_clear(&t); | ||||
|     return res; | ||||
|   } | ||||
|   res = mp_mod(&t, c, d); | ||||
|   mp_clear(&t); | ||||
|   return res; | ||||
|    if ((res = mp_sub(a, b, &t)) != MP_OKAY) { | ||||
|       mp_clear(&t); | ||||
|       return res; | ||||
|    } | ||||
|    res = mp_mod(&t, c, d); | ||||
|    mp_clear(&t); | ||||
|    return res; | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user