- pgen px, py;
- mp *pp;
- mp *g = MP_NEW;
- grand *gr = fibrand_create(0);
- int rcx, rcy;
- int fail = BBSEV_OK;
- size_t sz;
-
- /* --- Initialize @p@ and @q@ --- *
- *
- * Divide both by two, and make the results odd.
- */
-
- p = mp_lsr(MP_NEW, p, 1); p->v[0] |= 1;
- q = mp_lsr(MP_NEW, q, 1); q->v[0] |= 1;
-
- /* --- Set up the search for @p@ --- *
- *
- * I want a prime %$p$% such that %$(p - 1)/2$% has no small factors.
- */
-
- rcx = pgen_create(&px, p); mp_drop(p);
- rcy = pgen_muladd(&py, &px, 2, 1);
-
- if (proc && (fail = proc(BBSEV_FINDP, 0, arg)) != 0)
- goto fail_0;
-
- sz = mp_bits(py.m);
- for (;;) {
- if (rcx != PGEN_COMPOSITE && rcy != PGEN_COMPOSITE) {
- if (rcy != PGEN_PRIME) {
- rabin r;
- int i;
-
- if (proc && (fail = proc(BBSEV_TRYP, py.m, arg)) != 0)
- break;
- rabin_create(&r, py.m);
- for (i = 0; i < 5; i++) {
- g = mprand(g, sz, gr, 1);
- if ((rcy = rabin_test(&r, g)) == PGEN_COMPOSITE)
- break;
- if (proc && (fail = proc(BBSEV_PASSP, py.m, arg)) != 0)
- break;
- }
- rabin_destroy(&r);
- if (fail)
- goto fail_0;
- if (i < 5) {
- if (proc && (fail = proc(BBSEV_FAILP, py.m, arg)) != 0)
- goto fail_0;
- if (n) {
- n--;
- if (!n) {
- fail = BBSEV_FAILP;
- goto fail_0;
- }
- }
- }
- }
-
- if (rcy != PGEN_COMPOSITE)
- break;
- }
- rcx = pgen_step(&px, 2);
- rcy = pgen_step(&py, 4);
+ rabin rb;
+ pgen_safejumpctx j;
+ pgen_gcdstepctx g;
+ unsigned nb = nbits/2;
+ mp *x = MP_NEW;
+
+ /* --- Generate @p@ --- */
+
+again:
+ if ((x = strongprime_setup("p", x, &j.jq, nb, r, n, event, ectx)) == 0)
+ goto fail_x;
+ bp->p = pgen("p", MP_NEW, x, event, ectx, n, pgen_safejump, &j,
+ rabin_iters(nb), pgen_test, &rb);
+ pfilt_destroy(&j.jq);
+ if (!bp->p) {
+ if (n)
+ goto fail_p;
+ goto again;