Optimized prime checking.
authorPekka Riikonen <priikone@silcnet.org>
Sat, 21 Jul 2007 07:26:14 +0000 (07:26 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 21 Jul 2007 07:26:14 +0000 (07:26 +0000)
lib/silcmath/silcmath.h
lib/silcmath/silcprimegen.c

index 08a08c0c00d149e67a88b1c5bc652c7e9b601207..c3e09b470c0c4ac696ff1c6b251fe9a8e50e1868 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2005 Pekka Riikonen
+  Copyright (C) 1997 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -43,8 +43,7 @@
  *
  *    Find appropriate prime. It generates a number by taking random bytes.
  *    It then tests the number that it's not divisible by any of the small
- *    primes and then it performs Fermat's prime test. I thank Rieks Joosten
- *    (r.joosten@pijnenburg.nl) for such a good help with prime tests.
+ *    primes and then it performs Fermat's prime test.
  *
  *    If argument verbose is TRUE this will display some status information
  *    about the progress of generation.  If the `rng' is NULL then global
index 1b56acd8fa62d742b765f5147f9dc7a2e6ab259f..b2fe429db40e9ebcd574c204541365b266d9c1f4 100644 (file)
@@ -341,8 +341,13 @@ SilcBool silc_math_prime_test(SilcMPInt *p)
     silc_mp_mod_ui(&tmp, p, primetable[i]);
 
     /* If mod is 0, the number is composite */
-    if (silc_mp_cmp_ui(&tmp, 0) == 0)
-      ret = -1;
+    if (silc_mp_cmp_ui(&tmp, 0) == 0) {
+      SILC_LOG_DEBUG(("Number is not prime"));
+      silc_mp_uninit(&r);
+      silc_mp_uninit(&tmp);
+      silc_mp_uninit(&base);
+      return FALSE;
+    }
   }
 
   /* Does the prime pass the Fermat's prime test.
@@ -356,8 +361,10 @@ SilcBool silc_math_prime_test(SilcMPInt *p)
   silc_mp_uninit(&tmp);
   silc_mp_uninit(&base);
 
-  if (ret)
+  if (ret) {
+    SILC_LOG_DEBUG(("Number is not prime"));
     return FALSE;
+  }
 
   /* Number is probably a prime */
   return TRUE;