3 * $Id: limlee.c,v 1.9 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/alloc.h>
33 #include <mLib/dstr.h>
41 /*----- Stepping through combinations -------------------------------------*/
43 /* --- @comb_init@ --- *
45 * Arguments: @octet *c@ = pointer to byte-flag array
46 * @unsigned n@ = number of items in the array
47 * @unsigned r@ = number of desired items
51 * Use: Initializes a byte-flag array which, under the control of
52 * @comb_next@, will step through all combinations of @r@ chosen
56 static void comb_init(octet
*c
, unsigned n
, unsigned r
)
59 memset(c
+ (n
- r
), 1, r
);
62 /* --- @comb_next@ --- *
64 * Arguments: @octet *c@ = pointer to byte-flag array
65 * @unsigned n@ = number of items in the array
66 * @unsigned r@ = number of desired items
68 * Returns: Nonzero if another combination was returned, zero if we've
71 * Use: Steps on to the next combination in sequence.
74 static int comb_next(octet
*c
, unsigned n
, unsigned r
)
78 /* --- How the algorithm works --- *
80 * Set bits start at the end and work their way towards the start.
81 * Excepting bits already at the start, we scan for the lowest set bit, and
82 * move it one place nearer the start. A group of bits at the start are
83 * counted and reset just below the `moved' bit. If there is no moved bit
87 /* --- Count the group at the start --- */
96 /* --- Move the next bit down one --- *
98 * There must be one, because otherwise we'd have counted %$r$% bits
111 /*----- Default prime generator -------------------------------------------*/
113 static void llgen(limlee_factor
*f
, unsigned pl
, limlee_stepctx
*l
)
120 p
= mprand(l
->newp
, pl
, l
->r
, 1);
122 p
= pgen(l
->d
.buf
, p
, p
, l
->iev
, l
->iec
, 0, pgen_filter
, &pf
,
123 rabin_iters(pl
), pgen_test
, &r
);
129 static void llfree(limlee_factor
*f
, limlee_stepctx
*l
)
134 static const limlee_primeops primeops_simple
= { llgen
, llfree
};
136 /*----- Lim-Lee stepper ---------------------------------------------------*/
140 * Arguments: @pgen_event *ev@ = pointer to event block
141 * @limlee_stepctx *l@ = pointer to Lim-Lee context
143 * Returns: A @PGEN@ result code.
145 * Use: Initializes the stepper.
148 static int init(pgen_event
*ev
, limlee_stepctx
*l
)
153 /* --- First of all, decide on a number of factors to make --- */
155 l
->nf
= l
->pl
/ l
->ql
;
159 else if (qql
&& l
->nf
> 1) {
164 /* --- Now decide on how many primes I'll actually generate --- *
166 * The formula %$m = \max(3 n + 5, 25)$% comes from GPG's prime generation
170 l
->poolsz
= l
->nf
* 3 + 5;
174 /* --- Allocate and initialize the various tables --- */
176 l
->c
= xmalloc(l
->poolsz
);
177 l
->v
= xmalloc(l
->poolsz
* sizeof(limlee_factor
));
178 comb_init(l
->c
, l
->poolsz
, l
->nf
);
179 for (i
= 0; i
< l
->poolsz
; i
++)
182 /* --- Other bits of initialization --- */
187 l
->pops
= &primeops_simple
;
191 /* --- Find a big prime --- */
196 dstr_putf(&l
->d
, "%s*", ev
->name
);
197 l
->pops
->pgen(&l
->qq
, qql
, l
);
205 * Arguments: @int rq@ = request which triggered this call
206 * @pgen_event *ev@ = pointer to event block
207 * @limlee_stepctx *l@ = pointer to Lim-Lee context
209 * Returns: A @PGEN@ result code.
211 * Use: Initializes the stepper.
214 static int next(int rq
, pgen_event
*ev
, limlee_stepctx
*l
)
223 mpmul mm
= MPMUL_INIT
;
225 /* --- Step on to next combination --- */
227 if (rq
== PGEN_TRY
&& !comb_next(l
->c
, l
->poolsz
, l
->nf
)) {
228 for (i
= 0; i
< l
->poolsz
; i
++) {
229 l
->pops
->pfree(&l
->v
[i
], l
);
233 rq
= PGEN_TRY
; /* For next time through */
235 /* --- Gather up some factors --- */
238 mpmul_add(&mm
, l
->qq
.p
);
239 for (i
= 0; i
< l
->poolsz
; i
++) {
244 dstr_putf(&l
->d
, "%s_%lu", ev
->name
, l
->seq
++);
245 l
->pops
->pgen(&l
->v
[i
], l
->ql
, l
);
247 mpmul_add(&mm
, l
->v
[i
].p
);
250 /* --- Check it for small factors --- */
255 if ((rc
= pfilt_smallfactor(p
)) != PGEN_FAIL
)
266 * Arguments: @pgen_event *ev@ = pointer to event block
267 * @limlee_stepctx *l@ = pointer to Lim-Lee context
269 * Returns: A @PGEN@ result code.
271 * Use: Finalizes the stepper. The output values in the context
272 * take on their final results; other resources are discarded.
275 static int done(pgen_event
*ev
, limlee_stepctx
*l
)
280 /* --- If an output vector of factors is wanted, produce one --- */
282 if (!(l
->f
& LIMLEE_KEEPFACTORS
))
287 v
= xmalloc(l
->nf
* sizeof(limlee_factor
));
290 for (i
= 0, j
= 0; i
< l
->poolsz
; i
++) {
294 l
->pops
->pfree(&l
->v
[i
], l
);
301 l
->pops
->pfree(&l
->qq
, l
);
307 /* --- Free other resources --- */
317 /* --- @limlee_step@ --- */
319 int limlee_step(int rq
, pgen_event
*ev
, void *p
)
321 limlee_stepctx
*l
= p
;
326 if ((rc
= init(ev
, l
)) != PGEN_TRY
)
329 return (next(rq
, ev
, l
));
331 return (done(ev
, l
));
336 /*----- Main code ---------------------------------------------------------*/
338 /* --- @limlee@ --- *
340 * Arguments: @const char *name@ = pointer to name root
341 * @mp *d@ = pointer to destination integer
342 * @mp *newp@ = how to generate factor primes
343 * @unsigned ql@ = size of individual factors
344 * @unsigned pl@ = size of large prime
345 * @grand *r@ = a random number source
346 * @unsigned on@ = number of outer attempts to make
347 * @pgen_proc *oev@ = outer event handler function
348 * @void *oec@ = argument for the outer event handler
349 * @pgen_proc *iev@ = inner event handler function
350 * @void *iec@ = argument for the inner event handler
351 * @size_t *nf@, @mp ***f@ = output array for factors
353 * Returns: A Lim-Lee prime, or null if generation failed.
355 * Use: Generates Lim-Lee primes. A Lim-Lee prime %$p$% is one which
356 * satisfies %$p = 2 \prod_i q_i + 1$%, where all of the %$q_i$%
357 * are large enough to resist square-root discrete log
360 * If we succeed, and @f@ is non-null, we write the array of
361 * factors chosen to @f@ for the benefit of the caller.
364 mp
*limlee(const char *name
, mp
*d
, mp
*newp
,
365 unsigned ql
, unsigned pl
, grand
*r
,
366 unsigned on
, pgen_proc
*oev
, void *oec
,
367 pgen_proc
*iev
, void *iec
,
373 l
.f
= 0; if (f
) l
.f
|= LIMLEE_KEEPFACTORS
;
375 l
.pl
= pl
; l
.ql
= ql
;
381 d
= pgen(name
, d
, 0, oev
, oec
, on
, limlee_step
, &l
,
382 rabin_iters(pl
), pgen_test
, &rr
);
387 v
= xmalloc(l
.nf
* sizeof(mp
*));
388 for (i
= 0; i
< l
.nf
; i
++)
398 /*----- That's all, folks -------------------------------------------------*/