Use mpmul for the multiplication.
[u/mdw/catacomb] / mpcrt.c
1 /* -*-c-*-
2 *
3 * $Id: mpcrt.c,v 1.4 2001/04/19 18:25:38 mdw Exp $
4 *
5 * Chinese Remainder Theorem computations (Gauss's algorithm)
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: mpcrt.c,v $
33 * Revision 1.4 2001/04/19 18:25:38 mdw
34 * Use mpmul for the multiplication.
35 *
36 * Revision 1.3 2000/10/08 12:11:22 mdw
37 * Use @MP_EQ@ instead of @MP_CMP@.
38 *
39 * Revision 1.2 1999/12/10 23:22:32 mdw
40 * Interface changes for suggested destinations. Use Barrett reduction.
41 *
42 * Revision 1.1 1999/11/22 20:50:57 mdw
43 * Add support for solving Chinese Remainder Theorem problems.
44 *
45 */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include "mp.h"
50 #include "mpcrt.h"
51 #include "mpmul.h"
52 #include "mpbarrett.h"
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 /* --- @mpcrt_create@ --- *
57 *
58 * Arguments: @mpcrt *c@ = pointer to CRT context
59 * @mpcrt_mod *v@ = pointer to vector of moduli
60 * @size_t k@ = number of moduli
61 * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
62 *
63 * Returns: ---
64 *
65 * Use: Initializes a context for solving Chinese Remainder Theorem
66 * problems. The vector of moduli can be incomplete. Omitted
67 * items must be left as null pointers. Not all combinations of
68 * missing things can be coped with, even if there is
69 * technically enough information to cope. For example, if @n@
70 * is unspecified, all the @m@ values must be present, even if
71 * there is one modulus with both @m@ and @n@ (from which the
72 * product of all moduli could clearly be calculated).
73 */
74
75 void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n)
76 {
77 size_t i;
78
79 /* --- Simple initialization things --- */
80
81 c->k = k;
82 c->v = v;
83
84 /* --- Work out @n@ if I don't have it already --- */
85
86 if (n != MP_NEW)
87 n = MP_COPY(n);
88 else {
89 mpmul mm;
90 mpmul_init(&mm);
91 n = MP_COPY(v[0].m);
92 for (i = 0; i < k; i++)
93 mpmul_add(&mm, v[i].m);
94 n = mpmul_done(&mm);
95 }
96
97 /* --- A quick hack if %$k = 2$% --- */
98
99 if (k == 2) {
100
101 /* --- The %$n / n_i$% values are trivial in this case --- */
102
103 if (!v[0].n)
104 v[0].n = MP_COPY(v[1].m);
105 if (!v[1].n)
106 v[1].n = MP_COPY(v[0].m);
107
108 /* --- Now sort out the inverses --- *
109 *
110 * @mp_gcd@ will ensure that the first argument is negative.
111 */
112
113 if (!v[0].ni && !v[1].ni) {
114 mp_gcd(0, &v[0].ni, &v[1].ni, v[0].n, v[1].n);
115 v[0].ni = mp_add(v[0].ni, v[0].ni, v[1].n);
116 } else {
117 int i, j;
118 mp *x;
119
120 if (!v[0].ni)
121 i = 0, j = 1;
122 else
123 i = 1, j = 0;
124
125 x = mp_mul(MP_NEW, v[j].n, v[j].ni);
126 x = mp_sub(x, x, MP_ONE);
127 mp_div(&x, 0, x, v[i].n);
128 v[i].ni = x;
129 }
130 }
131
132 /* --- Set up the Barrett context --- */
133
134 mpbarrett_create(&c->mb, n);
135
136 /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */
137
138 for (i = 0; i < k; i++) {
139 if (!v[i].n)
140 mp_div(&v[i].n, 0, n, v[i].m);
141 if (!v[i].ni)
142 mp_gcd(0, &v[i].ni, 0, v[i].n, v[i].m);
143 if (!v[i].nni)
144 v[i].nni = mp_mul(MP_NEW, v[i].n, v[i].ni);
145 }
146
147 /* --- Done --- */
148
149 mp_drop(n);
150 }
151
152 /* --- @mpcrt_destroy@ --- *
153 *
154 * Arguments: @mpcrt *c@ - pointer to CRT context
155 *
156 * Returns: ---
157 *
158 * Use: Destroys a CRT context, releasing all the resources it holds.
159 */
160
161 void mpcrt_destroy(mpcrt *c)
162 {
163 size_t i;
164
165 for (i = 0; i < c->k; i++) {
166 if (c->v[i].m) mp_drop(c->v[i].m);
167 if (c->v[i].n) mp_drop(c->v[i].n);
168 if (c->v[i].ni) mp_drop(c->v[i].ni);
169 if (c->v[i].nni) mp_drop(c->v[i].nni);
170 }
171 mpbarrett_destroy(&c->mb);
172 }
173
174 /* --- @mpcrt_solve@ --- *
175 *
176 * Arguments: @mpcrt *c@ = pointer to CRT context
177 * @mp *d@ = fake destination
178 * @mp **v@ = array of residues
179 *
180 * Returns: The unique solution modulo the product of the individual
181 * moduli, which leaves the given residues.
182 *
183 * Use: Constructs a result given its residue modulo an array of
184 * coprime integers. This can be used to improve performance of
185 * RSA encryption or Blum-Blum-Shub generation if the factors
186 * of the modulus are known, since results can be computed mod
187 * each of the individual factors and then combined at the end.
188 * This is rather faster than doing the full-scale modular
189 * exponentiation.
190 */
191
192 mp *mpcrt_solve(mpcrt *c, mp *d, mp **v)
193 {
194 mp *a = MP_ZERO;
195 mp *x = MP_NEW;
196 size_t i;
197
198 for (i = 0; i < c->k; i++) {
199 x = mp_mul(x, c->v[i].nni, v[i]);
200 x = mpbarrett_reduce(&c->mb, x, x);
201 a = mp_add(a, a, x);
202 }
203 if (x)
204 MP_DROP(x);
205 a = mpbarrett_reduce(&c->mb, a, a);
206 if (d != MP_NEW)
207 MP_DROP(d);
208 return (a);
209 }
210
211 /*----- Test rig ----------------------------------------------------------*/
212
213 #ifdef TEST_RIG
214
215 static int verify(size_t n, dstr *v)
216 {
217 mpcrt_mod *m = xmalloc(n * sizeof(mpcrt_mod));
218 mp **r = xmalloc(n * sizeof(mp *));
219 mpcrt c;
220 mp *a, *b;
221 size_t i;
222 int ok = 1;
223
224 for (i = 0; i < n; i++) {
225 r[i] = *(mp **)v[2 * i].buf;
226 m[i].m = *(mp **)v[2 * i + 1].buf;
227 m[i].n = 0;
228 m[i].ni = 0;
229 m[i].nni = 0;
230 }
231 a = *(mp **)v[2 * n].buf;
232
233 mpcrt_create(&c, m, n, 0);
234 b = mpcrt_solve(&c, MP_NEW, r);
235
236 if (!MP_EQ(a, b)) {
237 fputs("\n*** failed\n", stderr);
238 fputs("n = ", stderr);
239 mp_writefile(c.mb.m, stderr, 10);
240 for (i = 0; i < n; i++) {
241 fprintf(stderr, "\nr[%u] = ", i);
242 mp_writefile(r[i], stderr, 10);
243 fprintf(stderr, "\nm[%u] = ", i);
244 mp_writefile(m[i].m, stderr, 10);
245 fprintf(stderr, "\nN[%u] = ", i);
246 mp_writefile(m[i].n, stderr, 10);
247 fprintf(stderr, "\nM[%u] = ", i);
248 mp_writefile(m[i].ni, stderr, 10);
249 }
250 fputs("\nresult = ", stderr);
251 mp_writefile(b, stderr, 10);
252 fputs("\nexpect = ", stderr);
253 mp_writefile(a, stderr, 10);
254 fputc('\n', stderr);
255 ok = 0;
256 }
257
258 for (i = 0; i < n; i++)
259 mp_drop(r[i]);
260 mp_drop(a);
261 mp_drop(b);
262 mpcrt_destroy(&c);
263 free(m);
264 free(r);
265 assert(mparena_count(MPARENA_GLOBAL) == 0);
266 return (ok);
267 }
268
269 static int crt1(dstr *v) { return verify(1, v); }
270 static int crt2(dstr *v) { return verify(2, v); }
271 static int crt3(dstr *v) { return verify(3, v); }
272 static int crt4(dstr *v) { return verify(4, v); }
273 static int crt5(dstr *v) { return verify(5, v); }
274
275 static test_chunk tests[] = {
276 { "crt-1", crt1, { &type_mp, &type_mp,
277 &type_mp, 0 } },
278 { "crt-2", crt2, { &type_mp, &type_mp,
279 &type_mp, &type_mp,
280 &type_mp, 0 } },
281 { "crt-3", crt3, { &type_mp, &type_mp,
282 &type_mp, &type_mp,
283 &type_mp, &type_mp,
284 &type_mp, 0 } },
285 { "crt-4", crt4, { &type_mp, &type_mp,
286 &type_mp, &type_mp,
287 &type_mp, &type_mp,
288 &type_mp, &type_mp,
289 &type_mp, 0 } },
290 { "crt-5", crt5, { &type_mp, &type_mp,
291 &type_mp, &type_mp,
292 &type_mp, &type_mp,
293 &type_mp, &type_mp,
294 &type_mp, &type_mp,
295 &type_mp, 0 } },
296 { 0, 0, { 0 } }
297 };
298
299 int main(int argc, char *argv[])
300 {
301 sub_init();
302 test_run(argc, argv, tests, SRCDIR "/tests/mpcrt");
303 return (0);
304 }
305
306 #endif
307
308 /*----- That's all, folks -------------------------------------------------*/