3 * $Id: mpcrt.c,v 1.5 2001/04/29 17:39:33 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.5 2001/04/29 17:39:33 mdw
36 * Revision 1.4 2001/04/19 18:25:38 mdw
37 * Use mpmul for the multiplication.
39 * Revision 1.3 2000/10/08 12:11:22 mdw
40 * Use @MP_EQ@ instead of @MP_CMP@.
42 * Revision 1.2 1999/12/10 23:22:32 mdw
43 * Interface changes for suggested destinations. Use Barrett reduction.
45 * Revision 1.1 1999/11/22 20:50:57 mdw
46 * Add support for solving Chinese Remainder Theorem problems.
50 /*----- Header files ------------------------------------------------------*/
55 #include "mpbarrett.h"
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @mpcrt_create@ --- *
61 * Arguments: @mpcrt *c@ = pointer to CRT context
62 * @mpcrt_mod *v@ = pointer to vector of moduli
63 * @size_t k@ = number of moduli
64 * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
68 * Use: Initializes a context for solving Chinese Remainder Theorem
69 * problems. The vector of moduli can be incomplete. Omitted
70 * items must be left as null pointers. Not all combinations of
71 * missing things can be coped with, even if there is
72 * technically enough information to cope. For example, if @n@
73 * is unspecified, all the @m@ values must be present, even if
74 * there is one modulus with both @m@ and @n@ (from which the
75 * product of all moduli could clearly be calculated).
78 void mpcrt_create(mpcrt
*c
, mpcrt_mod
*v
, size_t k
, mp
*n
)
82 /* --- Simple initialization things --- */
87 /* --- Work out @n@ if I don't have it already --- */
94 for (i
= 0; i
< k
; i
++)
95 mpmul_add(&mm
, v
[i
].m
);
99 /* --- A quick hack if %$k = 2$% --- */
103 /* --- The %$n / n_i$% values are trivial in this case --- */
106 v
[0].n
= MP_COPY(v
[1].m
);
108 v
[1].n
= MP_COPY(v
[0].m
);
110 /* --- Now sort out the inverses --- *
112 * @mp_gcd@ will ensure that the first argument is negative.
115 if (!v
[0].ni
&& !v
[1].ni
) {
116 mp_gcd(0, &v
[0].ni
, &v
[1].ni
, v
[0].n
, v
[1].n
);
117 v
[0].ni
= mp_add(v
[0].ni
, v
[0].ni
, v
[1].n
);
127 x
= mp_mul(MP_NEW
, v
[j
].n
, v
[j
].ni
);
128 x
= mp_sub(x
, x
, MP_ONE
);
129 mp_div(&x
, 0, x
, v
[i
].n
);
134 /* --- Set up the Barrett context --- */
136 mpbarrett_create(&c
->mb
, n
);
138 /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */
140 for (i
= 0; i
< k
; i
++) {
142 mp_div(&v
[i
].n
, 0, n
, v
[i
].m
);
144 mp_gcd(0, &v
[i
].ni
, 0, v
[i
].n
, v
[i
].m
);
146 v
[i
].nni
= mp_mul(MP_NEW
, v
[i
].n
, v
[i
].ni
);
154 /* --- @mpcrt_destroy@ --- *
156 * Arguments: @mpcrt *c@ - pointer to CRT context
160 * Use: Destroys a CRT context, releasing all the resources it holds.
163 void mpcrt_destroy(mpcrt
*c
)
167 for (i
= 0; i
< c
->k
; i
++) {
168 if (c
->v
[i
].m
) mp_drop(c
->v
[i
].m
);
169 if (c
->v
[i
].n
) mp_drop(c
->v
[i
].n
);
170 if (c
->v
[i
].ni
) mp_drop(c
->v
[i
].ni
);
171 if (c
->v
[i
].nni
) mp_drop(c
->v
[i
].nni
);
173 mpbarrett_destroy(&c
->mb
);
176 /* --- @mpcrt_solve@ --- *
178 * Arguments: @mpcrt *c@ = pointer to CRT context
179 * @mp *d@ = fake destination
180 * @mp **v@ = array of residues
182 * Returns: The unique solution modulo the product of the individual
183 * moduli, which leaves the given residues.
185 * Use: Constructs a result given its residue modulo an array of
186 * coprime integers. This can be used to improve performance of
187 * RSA encryption or Blum-Blum-Shub generation if the factors
188 * of the modulus are known, since results can be computed mod
189 * each of the individual factors and then combined at the end.
190 * This is rather faster than doing the full-scale modular
194 mp
*mpcrt_solve(mpcrt
*c
, mp
*d
, mp
**v
)
200 for (i
= 0; i
< c
->k
; i
++) {
201 x
= mp_mul(x
, c
->v
[i
].nni
, v
[i
]);
202 x
= mpbarrett_reduce(&c
->mb
, x
, x
);
207 a
= mpbarrett_reduce(&c
->mb
, a
, a
);
213 /*----- Test rig ----------------------------------------------------------*/
217 static int verify(size_t n
, dstr
*v
)
219 mpcrt_mod
*m
= xmalloc(n
* sizeof(mpcrt_mod
));
220 mp
**r
= xmalloc(n
* sizeof(mp
*));
226 for (i
= 0; i
< n
; i
++) {
227 r
[i
] = *(mp
**)v
[2 * i
].buf
;
228 m
[i
].m
= *(mp
**)v
[2 * i
+ 1].buf
;
233 a
= *(mp
**)v
[2 * n
].buf
;
235 mpcrt_create(&c
, m
, n
, 0);
236 b
= mpcrt_solve(&c
, MP_NEW
, r
);
239 fputs("\n*** failed\n", stderr
);
240 fputs("n = ", stderr
);
241 mp_writefile(c
.mb
.m
, stderr
, 10);
242 for (i
= 0; i
< n
; i
++) {
243 fprintf(stderr
, "\nr[%u] = ", i
);
244 mp_writefile(r
[i
], stderr
, 10);
245 fprintf(stderr
, "\nm[%u] = ", i
);
246 mp_writefile(m
[i
].m
, stderr
, 10);
247 fprintf(stderr
, "\nN[%u] = ", i
);
248 mp_writefile(m
[i
].n
, stderr
, 10);
249 fprintf(stderr
, "\nM[%u] = ", i
);
250 mp_writefile(m
[i
].ni
, stderr
, 10);
252 fputs("\nresult = ", stderr
);
253 mp_writefile(b
, stderr
, 10);
254 fputs("\nexpect = ", stderr
);
255 mp_writefile(a
, stderr
, 10);
260 for (i
= 0; i
< n
; i
++)
267 assert(mparena_count(MPARENA_GLOBAL
) == 0);
271 static int crt1(dstr
*v
) { return verify(1, v
); }
272 static int crt2(dstr
*v
) { return verify(2, v
); }
273 static int crt3(dstr
*v
) { return verify(3, v
); }
274 static int crt4(dstr
*v
) { return verify(4, v
); }
275 static int crt5(dstr
*v
) { return verify(5, v
); }
277 static test_chunk tests
[] = {
278 { "crt-1", crt1
, { &type_mp
, &type_mp
,
280 { "crt-2", crt2
, { &type_mp
, &type_mp
,
283 { "crt-3", crt3
, { &type_mp
, &type_mp
,
287 { "crt-4", crt4
, { &type_mp
, &type_mp
,
292 { "crt-5", crt5
, { &type_mp
, &type_mp
,
301 int main(int argc
, char *argv
[])
304 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpcrt");
310 /*----- That's all, folks -------------------------------------------------*/