3 * $Id: limlee.c,v 1.5 2000/08/18 19:16:51 mdw Exp $
5 * Generate Lim-Lee primes
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.5 2000/08/18 19:16:51 mdw
34 * New stepper interface for constructing Lim-Lee primes.
36 * Revision 1.4 2000/08/15 21:45:05 mdw
37 * Use the new trial division equipment in pfilt. This gives a 10%
38 * performance improvement in dsa-gen.t.
40 * Revision 1.3 2000/07/29 09:58:32 mdw
41 * (limlee): Bug fix. Old versions didn't set the filter step if @ql@ was
42 * an exact divisor of @pl@.
44 * Revision 1.2 2000/07/26 18:00:00 mdw
47 * Revision 1.1 2000/07/09 21:30:58 mdw
48 * Lim-Lee prime generation.
52 /*----- Header files ------------------------------------------------------*/
54 #include <mLib/alloc.h>
55 #include <mLib/dstr.h>
63 /*----- Stepping through combinations -------------------------------------*/
65 /* --- @comb_init@ --- *
67 * Arguments: @octet *c@ = pointer to byte-flag array
68 * @unsigned n@ = number of items in the array
69 * @unsigned r@ = number of desired items
73 * Use: Initializes a byte-flag array which, under the control of
74 * @comb_next@, will step through all combinations of @r@ chosen
78 static void comb_init(octet
*c
, unsigned n
, unsigned r
)
81 memset(c
+ (n
- r
), 1, r
);
84 /* --- @comb_next@ --- *
86 * Arguments: @octet *c@ = pointer to byte-flag array
87 * @unsigned n@ = number of items in the array
88 * @unsigned r@ = number of desired items
90 * Returns: Nonzero if another combination was returned, zero if we've
93 * Use: Steps on to the next combination in sequence.
96 static int comb_next(octet
*c
, unsigned n
, unsigned r
)
100 /* --- How the algorithm works --- *
102 * Set bits start at the end and work their way towards the start.
103 * Excepting bits already at the start, we scan for the lowest set bit, and
104 * move it one place nearer the start. A group of bits at the start are
105 * counted and reset just below the `moved' bit. If there is no moved bit
109 /* --- Count the group at the start --- */
118 /* --- Move the next bit down one --- *
120 * There must be one, because otherwise we'd have counted %$r$% bits
133 /*----- Default prime generator -------------------------------------------*/
135 static void llgen(limlee_factor
*f
, unsigned pl
, limlee_stepctx
*l
)
142 p
= mprand(l
->newp
, pl
, l
->r
, 1);
144 p
= pgen(l
->d
.buf
, p
, p
, l
->iev
, l
->iec
, 0, pgen_filter
, &pf
,
145 rabin_iters(pl
), pgen_test
, &r
);
151 static void llfree(limlee_factor
*f
, limlee_stepctx
*l
)
157 static const limlee_primeops primeops_simple
= { llgen
, llfree
};
159 /*----- Lim-Lee stepper ---------------------------------------------------*/
163 * Arguments: @pgen_event *ev@ = pointer to event block
164 * @limlee_stepctx *l@ = pointer to Lim-Lee context
166 * Returns: A @PGEN@ result code.
168 * Use: Initializes the stepper.
171 static int init(pgen_event
*ev
, limlee_stepctx
*l
)
176 /* --- First of all, decide on a number of factors to make --- */
178 l
->nf
= l
->pl
/ l
->ql
;
182 else if (qql
&& l
->nf
> 1) {
187 /* --- Now decide on how many primes I'll actually generate --- *
189 * The formula %$m = \max(3 n + 5, 25)$% comes from GPG's prime generation
193 l
->poolsz
= l
->nf
* 3 + 5;
197 /* --- Allocate and initialize the various tables --- */
199 l
->c
= xmalloc(l
->poolsz
);
200 l
->v
= xmalloc(l
->poolsz
* sizeof(limlee_factor
));
201 comb_init(l
->c
, l
->poolsz
, l
->nf
);
202 for (i
= 0; i
< l
->poolsz
; i
++)
205 /* --- Other bits of initialization --- */
211 l
->pops
= &primeops_simple
;
215 /* --- Find a big prime --- */
220 dstr_putf(&l
->d
, "%s*", ev
->name
);
221 l
->pops
->pgen(&l
->qq
, qql
, l
);
229 * Arguments: @int rq@ = request which triggered this call
230 * @pgen_event *ev@ = pointer to event block
231 * @limlee_stepctx *l@ = pointer to Lim-Lee context
233 * Returns: A @PGEN@ result code.
235 * Use: Initializes the stepper.
238 static int next(int rq
, pgen_event
*ev
, limlee_stepctx
*l
)
249 mpmul mm
= MPMUL_INIT
;
251 /* --- Step on to next combination --- */
253 if (rq
== PGEN_TRY
&& !comb_next(l
->c
, l
->poolsz
, l
->nf
)) {
254 for (i
= 0; i
< l
->poolsz
; i
++) {
255 l
->pops
->pfree(&l
->v
[i
], l
);
259 rq
= PGEN_TRY
; /* For next time through */
261 /* --- Gather up some factors --- */
264 mpmul_add(&mm
, l
->qq
.p
);
265 for (i
= 0; i
< l
->poolsz
; i
++) {
270 dstr_putf(&l
->d
, "%s_%lu", ev
->name
, l
->seq
++);
271 l
->pops
->pgen(&l
->v
[i
], l
->ql
, l
);
273 mpmul_add(&mm
, l
->v
[i
].p
);
276 /* --- Check it for small factors --- */
281 if ((rc
= pfilt_smallfactor(p
)) != PGEN_FAIL
)
292 * Arguments: @pgen_event *ev@ = pointer to event block
293 * @limlee_stepctx *l@ = pointer to Lim-Lee context
295 * Returns: A @PGEN@ result code.
297 * Use: Finalizes the stepper. The output values in the context
298 * take on their final results; other resources are discarded.
301 static int done(pgen_event
*ev
, limlee_stepctx
*l
)
306 /* --- If an output vector of factors is wanted, produce one --- */
308 if (!(l
->f
& LIMLEE_KEEPFACTORS
))
313 v
= xmalloc(l
->nf
* sizeof(limlee_factor
));
316 for (i
= 0, j
= 0; i
< l
->poolsz
; i
++) {
320 l
->pops
->pfree(&l
->v
[i
], l
);
327 l
->pops
->pfree(&l
->qq
, l
);
333 /* --- Free other resources --- */
343 /* --- @limlee_step@ --- */
345 int limlee_step(int rq
, pgen_event
*ev
, void *p
)
347 limlee_stepctx
*l
= p
;
352 if ((rc
= init(ev
, l
)) != PGEN_TRY
)
355 return (next(rq
, ev
, l
));
357 return (done(ev
, l
));
362 /*----- Main code ---------------------------------------------------------*/
364 /* --- @limlee@ --- *
366 * Arguments: @const char *name@ = pointer to name root
367 * @mp *d@ = pointer to destination integer
368 * @mp *newp@ = how to generate factor primes
369 * @unsigned ql@ = size of individual factors
370 * @unsigned pl@ = size of large prime
371 * @grand *r@ = a random number source
372 * @unsigned on@ = number of outer attempts to make
373 * @pgen_proc *oev@ = outer event handler function
374 * @void *oec@ = argument for the outer event handler
375 * @pgen_proc *iev@ = inner event handler function
376 * @void *iec@ = argument for the inner event handler
377 * @size_t *nf@, @mp ***f@ = output array for factors
379 * Returns: A Lim-Lee prime, or null if generation failed.
381 * Use: Generates Lim-Lee primes. A Lim-Lee prime %$p$% is one which
382 * satisfies %$p = 2 \prod_i q_i + 1$%, where all of the %$q_i$%
383 * are large enough to resist square-root discrete log
386 * If we succeed, and @f@ is non-null, we write the array of
387 * factors chosen to @f@ for the benefit of the caller.
390 mp
*limlee(const char *name
, mp
*d
, mp
*newp
,
391 unsigned ql
, unsigned pl
, grand
*r
,
392 unsigned on
, pgen_proc
*oev
, void *oec
,
393 pgen_proc
*iev
, void *iec
,
405 unsigned long seq
= 0;
411 /* --- First of all, decide on a number of factors to make --- */
417 else if (qql
&& nn
> 1) {
422 /* --- Now decide on how many primes I'll actually generate --- *
424 * The formula %$m = \max(3 n + 5, 25)$% comes from GPG's prime generation
432 /* --- Now allocate the working memory --- */
434 v
= xmalloc(mm
* sizeof(mp
*));
437 /* --- Initialize everything and try to find a prime --- */
442 ev
.tests
= ntest
= rabin_iters(pl
);
445 if (oev
&& oev(PGEN_BEGIN
, &ev
, oec
) == PGEN_ABORT
)
450 dstr_putf(&dn
, "%s*", name
);
451 qq
= mprand(d
, qql
, r
, 1);
452 qq
= pgen(dn
.buf
, qq
, qq
, iev
, iec
,
453 0, pgen_filter
, &pf
, rabin_iters(qql
), pgen_test
, &rb
);
457 comb_init(c
, mm
, nn
);
458 for (i
= 0; i
< mm
; i
++)
461 /* --- The main combinations loop --- */
464 mpmul mmul
= MPMUL_INIT
;
466 /* --- Multiply a bunch of primes together --- */
469 mpmul_add(&mmul
, qq
);
471 for (i
= 0; i
< mm
; i
++) {
478 dstr_putf(&dn
, "%s_%lu] = ", name
, seq
++);
479 z
= mprand(newp
, ql
, ev
.r
, 1);
480 z
= pgen(dn
.buf
, z
, z
, iev
, iec
,
481 0, pgen_filter
, &pf
, rabin_iters(ql
), pgen_test
, &rb
);
484 mpmul_add(&mmul
, v
[i
]);
487 /* --- Now do some testing --- */
490 mp
*p
= mpmul_done(&mmul
);
494 /* --- Check for small factors --- */
497 p
= mp_add(p
, p
, MP_ONE
);
498 rc
= pfilt_smallfactor(p
);
499 if (rc
== PGEN_FAIL
) {
504 /* --- Send an event out --- */
507 if (oev
&& oev(PGEN_TRY
, &ev
, oec
) == PGEN_ABORT
) {
512 /* --- Do the Rabin testing --- */
514 rabin_create(&rb
, p
);
517 g
= mprand_range(g
, p
, ev
.r
, 1);
518 rc
= rabin_test(&rb
, g
);
519 if (rc
== PGEN_PASS
) {
524 if (oev
&&oev(rc
, &ev
, oec
) == PGEN_ABORT
)
526 } while (rc
== PGEN_PASS
);
534 if (rc
== PGEN_ABORT
)
541 } while (comb_next(c
, mm
, nn
));
543 /* --- That failed --- */
549 oev(PGEN_ABORT
, &ev
, &oec
);
554 for (i
= 0; i
< mm
; i
++)
558 /* --- We did it! --- */
566 *f
= vv
= xmalloc(nn
* sizeof(mp
*));
569 for (i
= 0; i
< mm
; i
++) {
587 /* --- We blew it --- */
590 for (i
= 0; i
< mm
; i
++)
602 l
.f
= 0; if (f
) l
.f
|= LIMLEE_KEEPFACTORS
;
604 l
.pl
= pl
; l
.ql
= ql
;
609 d
= pgen(name
, d
, 0, oev
, oec
, on
, limlee_step
, &l
,
610 rabin_iters(pl
), pgen_test
, &rr
);
615 v
= xmalloc(l
.nf
* sizeof(mp
*));
616 for (i
= 0; i
< l
.nf
; i
++)
627 /*----- That's all, folks -------------------------------------------------*/