3 * $Id: mpcrt.c,v 1.2 1999/12/10 23:22:32 mdw Exp $
5 * Chinese Remainder Theorem computations (Gauss's algorithm)
7 * (c) 1999 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.2 1999/12/10 23:22:32 mdw
34 * Interface changes for suggested destinations. Use Barrett reduction.
36 * Revision 1.1 1999/11/22 20:50:57 mdw
37 * Add support for solving Chinese Remainder Theorem problems.
41 /*----- Header files ------------------------------------------------------*/
45 #include "mpbarrett.h"
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @mpcrt_create@ --- *
51 * Arguments: @mpcrt *c@ = pointer to CRT context
52 * @mpcrt_mod *v@ = pointer to vector of moduli
53 * @size_t k@ = number of moduli
54 * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
58 * Use: Initializes a context for solving Chinese Remainder Theorem
59 * problems. The vector of moduli can be incomplete. Omitted
60 * items must be left as null pointers. Not all combinations of
61 * missing things can be coped with, even if there is
62 * technically enough information to cope. For example, if @n@
63 * is unspecified, all the @m@ values must be present, even if
64 * there is one modulus with both @m@ and @n@ (from which the
65 * product of all moduli could clearly be calculated).
68 void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n)
72 /* --- Simple initialization things --- */
77 /* --- Work out @n@ if I don't have it already --- */
83 for (i = 1; i < k; i++)
84 n = mp_mul(n, n, v[i].m);
87 /* --- A quick hack if %$k = 2$% --- */
91 /* --- The %$n / n_i$% values are trivial in this case --- */
94 v[0].n = MP_COPY(v[1].m);
96 v[1].n = MP_COPY(v[0].m);
98 /* --- Now sort out the inverses --- *
100 * @mp_gcd@ will ensure that the first argument is negative.
103 if (!v[0].ni && !v[1].ni) {
104 mp_gcd(0, &v[0].ni, &v[1].ni, v[0].n, v[1].n);
105 v[0].ni = mp_add(v[0].ni, v[0].ni, v[1].n);
115 x = mp_mul(MP_NEW, v[j].n, v[j].ni);
116 x = mp_sub(x, x, MP_ONE);
117 mp_div(&x, 0, x, v[i].n);
122 /* --- Set up the Barrett context --- */
124 mpbarrett_create(&c->mb, n);
126 /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */
128 for (i = 0; i < k; i++) {
130 mp_div(&v[i].n, 0, n, v[i].m);
132 mp_gcd(0, &v[i].ni, 0, v[i].n, v[i].m);
134 v[i].nni = mp_mul(MP_NEW, v[i].n, v[i].ni);
142 /* --- @mpcrt_destroy@ --- *
144 * Arguments: @mpcrt *c@ - pointer to CRT context
148 * Use: Destroys a CRT context, releasing all the resources it holds.
151 void mpcrt_destroy(mpcrt *c)
155 for (i = 0; i < c->k; i++) {
156 if (c->v[i].m) mp_drop(c->v[i].m);
157 if (c->v[i].n) mp_drop(c->v[i].n);
158 if (c->v[i].ni) mp_drop(c->v[i].ni);
159 if (c->v[i].nni) mp_drop(c->v[i].nni);
161 mpbarrett_destroy(&c->mb);
164 /* --- @mpcrt_solve@ --- *
166 * Arguments: @mpcrt *c@ = pointer to CRT context
167 * @mp *d@ = fake destination
168 * @mp **v@ = array of residues
170 * Returns: The unique solution modulo the product of the individual
171 * moduli, which leaves the given residues.
173 * Use: Constructs a result given its residue modulo an array of
174 * coprime integers. This can be used to improve performance of
175 * RSA encryption or Blum-Blum-Shub generation if the factors
176 * of the modulus are known, since results can be computed mod
177 * each of the individual factors and then combined at the end.
178 * This is rather faster than doing the full-scale modular
182 mp *mpcrt_solve(mpcrt *c, mp *d, mp **v)
188 for (i = 0; i < c->k; i++) {
189 x = mp_mul(x, c->v[i].nni, v[i]);
190 x = mpbarrett_reduce(&c->mb, x, x);
195 a = mpbarrett_reduce(&c->mb, a, a);
201 /*----- Test rig ----------------------------------------------------------*/
205 static int verify(size_t n, dstr *v)
207 mpcrt_mod *m = xmalloc(n * sizeof(mpcrt_mod));
208 mp **r = xmalloc(n * sizeof(mp *));
214 for (i = 0; i < n; i++) {
215 r[i] = *(mp **)v[2 * i].buf;
216 m[i].m = *(mp **)v[2 * i + 1].buf;
221 a = *(mp **)v[2 * n].buf;
223 mpcrt_create(&c, m, n, 0);
224 b = mpcrt_solve(&c, MP_NEW, r);
226 if (MP_CMP(a, !=, b)) {
227 fputs("\n*** failed\n", stderr);
228 fputs("n = ", stderr);
229 mp_writefile(c.mb.m, stderr, 10);
230 for (i = 0; i < n; i++) {
231 fprintf(stderr, "\nr[%u] = ", i);
232 mp_writefile(r[i], stderr, 10);
233 fprintf(stderr, "\nm[%u] = ", i);
234 mp_writefile(m[i].m, stderr, 10);
235 fprintf(stderr, "\nN[%u] = ", i);
236 mp_writefile(m[i].n, stderr, 10);
237 fprintf(stderr, "\nM[%u] = ", i);
238 mp_writefile(m[i].ni, stderr, 10);
240 fputs("\nresult = ", stderr);
241 mp_writefile(b, stderr, 10);
242 fputs("\nexpect = ", stderr);
243 mp_writefile(a, stderr, 10);
248 for (i = 0; i < n; i++)
255 assert(mparena_count(MPARENA_GLOBAL) == 0);
259 static int crt1(dstr *v) { return verify(1, v); }
260 static int crt2(dstr *v) { return verify(2, v); }
261 static int crt3(dstr *v) { return verify(3, v); }
262 static int crt4(dstr *v) { return verify(4, v); }
263 static int crt5(dstr *v) { return verify(5, v); }
265 static test_chunk tests[] = {
266 { "crt-1", crt1, { &type_mp, &type_mp,
268 { "crt-2", crt2, { &type_mp, &type_mp,
271 { "crt-3", crt3, { &type_mp, &type_mp,
275 { "crt-4", crt4, { &type_mp, &type_mp,
280 { "crt-5", crt5, { &type_mp, &type_mp,
289 int main(int argc, char *argv[])
292 test_run(argc, argv, tests, SRCDIR "/tests/mpcrt");
298 /*----- That's all, folks -------------------------------------------------*/