prime generation: Deploy the new Baillie--PSW testers.
[catacomb] / math / strongprime.c
index 9eab8b2..958cd28 100644 (file)
@@ -28,6 +28,7 @@
 /*----- Header files ------------------------------------------------------*/
 
 #include <mLib/dstr.h>
+#include <mLib/macros.h>
 
 #include "grand.h"
 #include "mp.h"
@@ -39,6 +40,9 @@
 
 /*----- Main code ---------------------------------------------------------*/
 
+/* Oh, just shut up. */
+CLANG_WARNING("-Wempty-body")
+
 /* --- @strongprime_setup@ --- *
  *
  * Arguments:  @const char *name@ = pointer to name root
@@ -75,7 +79,6 @@ mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
   mp *rr = d;
   pgen_filterctx c;
   pgen_jumpctx j;
-  rabin rb;
 
   /* --- Figure out how large the smaller primes should be --- *
    *
@@ -112,16 +115,19 @@ mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
   rr = mprand(rr, nb, r, 1);
   DRESET(&dn); dstr_putf(&dn, "%s [s]", name);
   if ((s = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
-               rabin_iters(nb), pgen_test, &rb)) == 0)
+               PGEN_BAILLIEPSWNTESTS, pgen_bailliepswtest, 0)) == 0)
     goto fail_s;
 
   rr = mprand(rr, nb, r, 1);
   DRESET(&dn); dstr_putf(&dn, "%s [t]", name);
   if ((t = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
-               rabin_iters(nb), pgen_test, &rb)) == 0)
+               PGEN_BAILLIEPSWNTESTS, pgen_bailliepswtest, 0)) == 0)
     goto fail_t;
 
-  /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
+  /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- *
+   *
+   * Then %$r \equiv 1 \pmod{t}$%, i.e., %$r - 1$% is a multiple of %$t$%.
+   */
 
   rr = mp_lsl(rr, t, 1);
   pfilt_create(&c.f, rr);
@@ -130,27 +136,25 @@ mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
   DRESET(&dn); dstr_putf(&dn, "%s [r]", name);
   j.j = &c.f;
   q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j,
-          rabin_iters(nb + slop), pgen_test, &rb);
+          PGEN_BAILLIEPSWNTESTS, pgen_bailliepswtest, 0);
   pfilt_destroy(&c.f);
   if (!q)
     goto fail_r;
 
   /* --- Select a suitable congruence class for %$p$% --- *
    *
-   * This computes %$p_0 = 2 s (s^{r - 2} \bmod r) - 1$%.
+   * This computes %$p_0 = 2 s (s^{-1} \bmod r) - 1$%.  Then %$p_0 + 1$% is
+   * clearly a multiple of %$s$%, and
+   *
+   *   %$p_0 - 1 \equiv 2 s s^{-1} - 2 \equiv 0 \pmod{r}$%
+   *
+   * is a multiple of %$r$%.
    */
 
-  {
-    mpmont mm;
-
-    mpmont_create(&mm, q);
-    rr = mp_sub(rr, q, MP_TWO);
-    rr = mpmont_exp(&mm, rr, s, rr);
-    mpmont_destroy(&mm);
-    rr = mp_mul(rr, rr, s);
-    rr = mp_lsl(rr, rr, 1);
-    rr = mp_sub(rr, rr, MP_ONE);
-  }
+  rr = mp_modinv(rr, s, q);
+  rr = mp_mul(rr, rr, s);
+  rr = mp_lsl(rr, rr, 1);
+  rr = mp_sub(rr, rr, MP_ONE);
 
   /* --- Pick a starting point for the search --- *
    *
@@ -214,14 +218,13 @@ mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
   mp *p;
   pfilt f;
   pgen_jumpctx j;
-  rabin rb;
 
   if (d) mp_copy(d);
   p = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
   if (!p) { mp_drop(d); return (0); }
   j.j = &f;
   p = pgen(name, p, p, event, ectx, n, pgen_jump, &j,
-          rabin_iters(nbits), pgen_test, &rb);
+          PGEN_BAILLIEPSWNTESTS, pgen_bailliepswtest, 0);
   if (mp_bits(p) != nbits) { mp_drop(p); return (0); }
   pfilt_destroy(&f);
   mp_drop(d);