~mdw
/
catacomb
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
math/, pub/: Take a more consistent approach to prime-generation failures.
[catacomb]
/
math
/
strongprime.c
diff --git
a/math/strongprime.c
b/math/strongprime.c
index
a2dd438
..
a82bfad
100644
(file)
--- a/
math/strongprime.c
+++ b/
math/strongprime.c
@@
-63,6
+63,7
@@
mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
{
mp *s, *t, *q;
dstr dn = DSTR_INIT;
{
mp *s, *t, *q;
dstr dn = DSTR_INIT;
+ size_t nb;
mp *rr = d;
pgen_filterctx c;
mp *rr = d;
pgen_filterctx c;
@@
-81,20
+82,20
@@
mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
/* --- Choose two primes %$s$% and %$t$% of half the required size --- */
/* --- Choose two primes %$s$% and %$t$% of half the required size --- */
-
assert(((void)"nbits too small in strongprime_setup", nbits/2 > BITSLOP)
);
- nb
its
= nbits/2 - BITSLOP;
+
if (nbits/2 <= BITSLOP) return (0
);
+ nb = nbits/2 - BITSLOP;
c.step = 1;
c.step = 1;
- rr = mprand(rr, nb
its
, r, 1);
+ 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,
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
its
), pgen_test, &rb)) == 0)
+ rabin_iters(nb), pgen_test, &rb)) == 0)
goto fail_s;
goto fail_s;
- rr = mprand(rr, nb
its
, r, 1);
+ 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,
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
its
), pgen_test, &rb)) == 0)
+ rabin_iters(nb), pgen_test, &rb)) == 0)
goto fail_t;
/* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
goto fail_t;
/* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
@@
-105,9
+106,8
@@
mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
rr = mp_add(rr, rr, MP_ONE);
DRESET(&dn); dstr_putf(&dn, "%s [r]", name);
j.j = &c.f;
rr = mp_add(rr, rr, MP_ONE);
DRESET(&dn); dstr_putf(&dn, "%s [r]", name);
j.j = &c.f;
- nbits += BITSLOP;
q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j,
q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j,
- rabin_iters(nb
its
), pgen_test, &rb);
+ rabin_iters(nb
+ BITSLOP
), pgen_test, &rb);
pfilt_destroy(&c.f);
if (!q)
goto fail_r;
pfilt_destroy(&c.f);
if (!q)
goto fail_r;
@@
-132,13
+132,13
@@
mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
/* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
{
/* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
{
- mp *x;
+ mp *x
, *y
;
x = mp_mul(MP_NEW, q, s);
x = mp_lsl(x, x, 1);
pfilt_create(f, x);
x = mp_mul(MP_NEW, q, s);
x = mp_lsl(x, x, 1);
pfilt_create(f, x);
-
x = mp_lsl(x, x, BITSLOP
- 1);
- rr = mp_
add(rr
, rr, x);
- mp_drop(x);
+
y = mp_lsl(MP_NEW, MP_ONE, nbits
- 1);
+ rr = mp_
leastcongruent(rr, y
, rr, x);
+ mp_drop(x);
mp_drop(y);
}
/* --- Return the result --- */
}
/* --- Return the result --- */
@@
-188,16
+188,20
@@
fail_s:
mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
unsigned n, pgen_proc *event, void *ectx)
{
mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
unsigned n, pgen_proc *event, void *ectx)
{
+ mp *p;
pfilt f;
pgen_jumpctx j;
rabin rb;
pfilt f;
pgen_jumpctx j;
rabin rb;
- d = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
+ 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;
j.j = &f;
-
d = pgen(name, d, d
, event, ectx, n, pgen_jump, &j,
+
p = pgen(name, p, p
, event, ectx, n, pgen_jump, &j,
rabin_iters(nbits), pgen_test, &rb);
pfilt_destroy(&f);
rabin_iters(nbits), pgen_test, &rb);
pfilt_destroy(&f);
- return (d);
+ mp_drop(d);
+ return (p);
}
/*----- That's all, folks -------------------------------------------------*/
}
/*----- That's all, folks -------------------------------------------------*/