chiark / gitweb /
Version bump.
[catacomb] / strongprime.c
1 /* -*-c-*-
2  *
3  * $Id: strongprime.c,v 1.4 2000/07/01 11:24:52 mdw Exp $
4  *
5  * Generate `strong' prime numbers
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: strongprime.c,v $
33  * Revision 1.4  2000/07/01 11:24:52  mdw
34  * Remove old debugging code.
35  *
36  * Revision 1.3  2000/06/17 12:10:09  mdw
37  * Add some argument checking.  Use MP secure memory interface.
38  *
39  * Revision 1.2  2000/02/12 18:21:03  mdw
40  * Overhaul of key management (again).
41  *
42  * Revision 1.1  1999/12/22 15:51:22  mdw
43  * Find `strong' RSA primes using Gordon's algorithm.
44  *
45  */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <mLib/dstr.h>
50
51 #include "grand.h"
52 #include "rand.h"
53 #include "mp.h"
54 #include "mpmont.h"
55 #include "mprand.h"
56 #include "pgen.h"
57 #include "pfilt.h"
58 #include "rabin.h"
59
60 /*----- Main code ---------------------------------------------------------*/
61
62 /* --- @strongprime_setup@ --- *
63  *
64  * Arguments:   @const char *name@ = pointer to name root
65  *              @mp *d@ = destination for search start point
66  *              @pfilt *f@ = where to store filter jump context
67  *              @unsigned nbits@ = number of bits wanted
68  *              @grand *r@ = random number source
69  *              @unsigned n@ = number of attempts to make
70  *              @pgen_proc *event@ = event handler function
71  *              @void *ectx@ = argument for the event handler
72  *
73  * Returns:     A starting point for a `strong' prime search, or zero.
74  *
75  * Use:         Sets up for a strong prime search, so that primes with
76  *              particular properties can be found.  It's probably important
77  *              to note that the number left in the filter context @f@ is
78  *              congruent to 2 (mod 4).
79  */
80
81 mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
82                       grand *r, unsigned n, pgen_proc *event, void *ectx)
83 {
84   mp *s, *t, *q;
85   dstr dn = DSTR_INIT;
86
87   mp *rr = d;
88   pgen_filterctx c;
89   pgen_jumpctx j;
90   rabin rb;
91
92   /* --- The bitslop parameter --- *
93    *
94    * There's quite a lot of prime searching to be done.  The constant
95    * @BITSLOP@ is a (low) approximation to the base-2 log of the expected
96    * number of steps to find a prime number.  Experimentation shows that
97    * numbers around 10 seem to be good.
98    */
99
100 #define BITSLOP 12
101
102   /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
103
104   assert(((void)"nbits too small in strongprime_setup", nbits/2 > BITSLOP));
105   nbits = nbits/2 - BITSLOP;
106   c.step = 1;
107
108   rr = mprand(rr, nbits, r, 1);
109   DRESET(&dn); dstr_putf(&dn, "%s [s]", name);
110   if ((s = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
111            rabin_iters(nbits), pgen_test, &rb)) == 0)
112     goto fail_s;
113
114   rr = mprand(rr, nbits, r, 1);
115   DRESET(&dn); dstr_putf(&dn, "%s [t]", name);
116   if ((t = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
117                 rabin_iters(nbits), pgen_test, &rb)) == 0)
118     goto fail_t;
119
120   /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
121
122   rr = mp_lsl(rr, t, 1);
123   pfilt_create(&c.f, rr);
124   rr = mp_lsl(rr, rr, BITSLOP - 1);
125   rr = mp_add(rr, rr, MP_ONE);
126   DRESET(&dn); dstr_putf(&dn, "%s [r]", name);
127   j.j = &c.f;
128   nbits += BITSLOP;
129   q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j,
130            rabin_iters(nbits), pgen_test, &rb);
131   pfilt_destroy(&c.f);
132   if (!q)
133     goto fail_r;
134
135   /* --- Select a suitable starting-point for finding %$p$% --- *
136    *
137    * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
138    */
139
140   {
141     mpmont mm;
142
143     mpmont_create(&mm, q);
144     rr = mp_sub(rr, q, MP_TWO);
145     rr = mpmont_exp(&mm, rr, s, rr);
146     mpmont_destroy(&mm);
147     rr = mp_mul(rr, rr, s);
148     rr = mp_lsl(rr, rr, 1);
149     rr = mp_sub(rr, rr, MP_ONE);
150   }
151
152   /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
153
154   {
155     mp *x;
156     x = mp_mul(MP_NEW, q, s);
157     x = mp_lsl(x, x, 1);
158     pfilt_create(f, x);
159     x = mp_lsl(x, x, BITSLOP - 1);
160     rr = mp_add(rr, rr, x);
161     mp_drop(x);
162   }
163
164   /* --- Return the result --- */
165
166   mp_drop(q);
167   mp_drop(t);
168   mp_drop(s);
169   dstr_destroy(&dn);
170   return (rr);
171
172   /* --- Tidy up if something failed --- */
173
174 fail_r:
175   mp_drop(t);
176 fail_t:
177   mp_drop(s);
178 fail_s:
179   mp_drop(rr);
180   dstr_destroy(&dn);
181   return (0);
182
183 #undef BITSLOP
184 }
185
186 /* --- @strongprime@ --- *
187  *
188  * Arguments:   @const char *name@ = pointer to name root
189  *              @mp *d@ = destination integer
190  *              @unsigned nbits@ = number of bits wanted
191  *              @grand *r@ = random number source
192  *              @unsigned n@ = number of attempts to make
193  *              @pgen_proc *event@ = event handler function
194  *              @void *ectx@ = argument for the event handler
195  *
196  * Returns:     A `strong' prime, or zero.
197  *
198  * Use:         Finds `strong' primes.  A strong prime %$p$% is such that
199  *
200  *                * %$p - 1$% has a large prime factor %$r$%,
201  *                * %$p + 1$% has a large prime factor %$s$%, and
202  *                * %$r - 1$% has a large prime factor %$t$%.
203  *
204  *              The numbers produced may be slightly larger than requested,
205  *              by a few bits.
206  */
207
208 mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
209                 unsigned n, pgen_proc *event, void *ectx)
210 {
211   pfilt f;
212   pgen_jumpctx j;
213   rabin rb;
214   
215   d = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
216   j.j = &f;
217   d = pgen(name, d, d, event, ectx, n, pgen_jump, &j,
218            rabin_iters(nbits), pgen_test, &rb);
219   pfilt_destroy(&f);
220   return (d);
221 }
222
223 /*----- That's all, folks -------------------------------------------------*/