Integer type name change.
[silc.git] / lib / silcmath / silcprimegen.c
index decb9538edbe46c2ea00675b5377f98359a2bdde..515f2b400fc8a0dc6d5c724f43e2fd9ddd260a25 100644 (file)
@@ -53,7 +53,7 @@
 
 */
 
-static unsigned int primetable[] =
+static SilcUInt32 primetable[] =
 {
   2, 3, 5, 7, 11, 13, 17, 19,
   23, 29, 31, 37, 41, 43, 47, 53,
@@ -195,28 +195,37 @@ static unsigned int primetable[] =
    If argument verbose is TRUE this will display some status information
    about the progress of generation. */
 
-int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose)
+bool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, bool verbose)
 {
-  unsigned char *numbuf;
-  unsigned int i, b, k;
-  unsigned int *spmods;
-  SilcInt r, base, tmp, tmp2, oprime;
+  unsigned char *numbuf = NULL;
+  SilcUInt32 i, b, k;
+  SilcUInt32 *spmods;
+  SilcMPInt r, base, tmp, tmp2, oprime;
 
   silc_mp_init(&r);
-  silc_mp_init_set_ui(&base, 2);
+  silc_mp_init(&base);
   silc_mp_init(&tmp);
   silc_mp_init(&tmp2);
   silc_mp_init(&oprime);
 
+  silc_mp_set_ui(&base, 2);
+
   SILC_LOG_DEBUG(("Generating new prime"));
 
-  /* Get random number */
-  numbuf = silc_rng_global_get_rn_string((bits / 8));
-  if (!numbuf)
-    return FALSE;
+  /* Get random number and assure that the first digit is not zero since
+     our conversion routines does not like the first digit being zero. */
+  do {
+    if (numbuf) {
+      memset(numbuf, 0, (bits / 8));
+      silc_free(numbuf);
+    }
+    numbuf = silc_rng_global_get_rn_string((bits / 8));
+    if (!numbuf)
+      return FALSE;
+  } while (numbuf[0] == '0');
 
   /* Convert into MP and set the size */
-  silc_mp_set_str(prime, numbuf, 16);
+  silc_mp_set_str(prime, numbuf, 16);  
   silc_mp_mod_2exp(prime, prime, bits);
 
   /* Empty buffer */
@@ -225,11 +234,11 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose)
 
   /* Number could be even number, so we'll make it odd. */
   silc_mp_set_ui(&tmp, 1);
-  silc_mp_ior(prime, prime, &tmp);             /* OR operator */
+  silc_mp_or(prime, prime, &tmp);              /* OR operator */
 
   /* Init modulo table with the prime candidate and the primes
      in the primetable. */
-  spmods = silc_calloc(1, sizeof(primetable) * sizeof(unsigned int));
+  spmods = silc_calloc(1, sizeof(primetable) * sizeof(SilcUInt32));
   for (i = 0; primetable[i] != 0; i++) {
     silc_mp_mod_ui(&tmp, prime, primetable[i]);
     spmods[i] = silc_mp_get_ui(&tmp);
@@ -263,7 +272,7 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose)
     /* Does the prime pass the Fermat's prime test.
      * r = 2 ^ p mod p, if r == 2, then p is probably a prime. 
      */
-    silc_mp_powm(&r, &base, &oprime, &oprime);
+    silc_mp_pow_mod(&r, &base, &oprime, &oprime);
     if (silc_mp_cmp_ui(&r, 2) != 0) {
       if (verbose) {
        printf(".");
@@ -289,11 +298,11 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose)
   }
 
   silc_free(spmods);
-  silc_mp_clear(&r);
-  silc_mp_clear(&base);
-  silc_mp_clear(&tmp);
-  silc_mp_clear(&tmp2);
-  silc_mp_clear(&oprime);
+  silc_mp_uninit(&r);
+  silc_mp_uninit(&base);
+  silc_mp_uninit(&tmp);
+  silc_mp_uninit(&tmp2);
+  silc_mp_uninit(&oprime);
 
   return TRUE;
 }
@@ -301,14 +310,15 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose)
 /* Performs primality testings for given number. Returns TRUE if the 
    number is probably a prime. */
 
-int silc_math_prime_test(SilcInt *p)
+bool silc_math_prime_test(SilcMPInt *p)
 {
-  SilcInt r, base, tmp;
+  SilcMPInt r, base, tmp;
   int i, ret = 0;
   
   silc_mp_init(&r);
   silc_mp_init(&tmp);
-  silc_mp_init_set_ui(&base, 2);
+  silc_mp_init(&base);
+  silc_mp_set_ui(&base, 2);
   
   SILC_LOG_DEBUG(("Testing probability of prime"));
 
@@ -325,13 +335,13 @@ int silc_math_prime_test(SilcInt *p)
   /* Does the prime pass the Fermat's prime test.
    * r = 2 ^ p mod p, if r == 2, then p is probably a prime.
    */
-  silc_mp_powm(&r, &base, p, p);
+  silc_mp_pow_mod(&r, &base, p, p);
   if (silc_mp_cmp_ui(&r, 2) != 0)
     ret = -1;
 
-  silc_mp_clear(&r);
-  silc_mp_clear(&tmp);
-  silc_mp_clear(&base);
+  silc_mp_uninit(&r);
+  silc_mp_uninit(&tmp);
+  silc_mp_uninit(&base);
   
   if (ret)
     return FALSE;