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