3 * $Id: limlee.c,v 1.1 2000/07/09 21:30:58 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.1 2000/07/09 21:30:58 mdw
34 * Lim-Lee prime generation.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/alloc.h>
41 #include <mLib/dstr.h>
47 #include "primorial.h"
50 /*----- Main code ---------------------------------------------------------*/
54 * Arguments: @const char *name@ = pointer to name root
55 * @mp *d@ = pointer to destination integer
56 * @mp *newp@ = how to generate factor primes
57 * @unsigned ql@ = size of individual factors
58 * @unsigned pl@ = size of large prime
59 * @grand *r@ = a random number source
60 * @unsigned on@ = number of outer attempts to make
61 * @pgen_proc *oev@ = outer event handler function
62 * @void *oec@ = argument for the outer event handler
63 * @pgen_proc *iev@ = inner event handler function
64 * @void *iec@ = argument for the inner event handler
65 * @size_t *nf@, @mp ***f@ = output array for factors
67 * Returns: A Lim-Lee prime, or null if generation failed.
69 * Use: Generates Lim-Lee primes. A Lim-Lee prime %$p$% is one which
70 * satisfies %$p = 2 \prod_i q_i + 1$%, where all of the %$q_i$%
71 * are large enough to resist square-root discrete log
74 * If we succeed, and @f@ is non-null, we write the array of
75 * factors chosen to @f@ for the benefit of the caller.
78 static void comb_init(octet
*c
, unsigned n
, unsigned r
)
81 memset(c
+ (n
- r
), 1, r
);
84 static int comb_next(octet
*c
, unsigned n
, unsigned r
)
88 /* --- How the algorithm works --- *
90 * Set bits start at the end and work their way towards the start.
91 * Excepting bits already at the start, we scan for the lowest set bit, and
92 * move it one place nearer the start. A group of bits at the start are
93 * counted and reset just below the `moved' bit. If there is no moved bit
97 /* --- Count the group at the start --- */
106 /* --- Move the next bit down one --- *
108 * There must be one, because otherwise we'd have counted %$r$% bits
121 mp
*limlee(const char *name
, mp
*d
, mp
*newp
,
122 unsigned ql
, unsigned pl
, grand
*r
,
123 unsigned on
, pgen_proc
*oev
, void *oec
,
124 pgen_proc
*iev
, void *iec
,
135 unsigned long seq
= 0;
141 /* --- First of all, decide on a number of factors to make --- */
147 else if (qql
&& nn
> 1) {
152 /* --- Now decide on how many primes I'll actually generate --- *
154 * The formula %$m = \max(3 n + 5, 25)$% comes from GPG's prime generation
162 /* --- Now allocate the working memory --- */
165 v
= xmalloc(mm
* sizeof(mp
*));
168 /* --- Initialize everything and try to find a prime --- */
173 ev
.tests
= ntest
= rabin_iters(pl
);
176 if (oev
&& oev(PGEN_BEGIN
, &ev
, oec
) == PGEN_ABORT
)
180 dstr_putf(&dn
, "%s [+]", name
);
181 qq
= mprand(d
, qql
, r
, 1);
183 qq
= pgen(dn
.buf
, qq
, qq
, iev
, iec
,
184 0, pgen_filter
, &pf
, rabin_iters(qql
), pgen_test
, &rb
);
188 comb_init(c
, mm
, nn
);
189 for (i
= 0; i
< mm
; i
++)
192 /* --- The main combinations loop --- */
195 mpmul mmul
= MPMUL_INIT
;
197 /* --- Multiply a bunch of primes together --- */
200 mpmul_add(&mmul
, qq
);
202 for (i
= 0; i
< mm
; i
++) {
209 dstr_putf(&dn
, "%s [%lu] = ", name
, seq
++);
210 z
= mprand(newp
, ql
, ev
.r
, 1);
211 z
= pgen(dn
.buf
, z
, z
, iev
, iec
,
212 0, pgen_filter
, &pf
, rabin_iters(ql
), pgen_test
, &rb
);
215 mpmul_add(&mmul
, v
[i
]);
218 /* --- Now do some testing --- */
221 mp
*p
= mpmul_done(&mmul
);
225 /* --- Check for small factors --- */
228 p
= mp_add(p
, p
, MP_ONE
);
229 mp_gcd(&g
, 0, 0, p
, primorial
);
230 if (MP_CMP(g
, !=, MP_ONE
)) {
237 /* --- Send an event out --- */
240 if (oev
&& oev(PGEN_TRY
, &ev
, oec
) == PGEN_ABORT
) {
245 /* --- Do the Rabin testing --- */
247 rabin_create(&rb
, p
);
250 g
= mprand_range(g
, p
, ev
.r
, 1);
251 rc
= rabin_test(&rb
, g
);
252 if (rc
== PGEN_PASS
) {
257 if (oev
&&oev(rc
, &ev
, oec
) == PGEN_ABORT
)
259 } while (rc
== PGEN_PASS
);
267 if (rc
== PGEN_ABORT
)
274 } while (comb_next(c
, mm
, nn
));
276 /* --- That failed --- */
282 oev(PGEN_ABORT
, &ev
, &oec
);
287 for (i
= 0; i
< mm
; i
++)
291 /* --- We did it! --- */
299 *f
= vv
= xmalloc(nn
* sizeof(mp
*));
302 for (i
= 0; i
< mm
; i
++) {
320 /* --- We blew it --- */
323 for (i
= 0; i
< mm
; i
++)