3 * Chinese Remainder Theorem computations (Gauss's algorithm)
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 #include "mpbarrett.h"
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @mpcrt_create@ --- *
39 * Arguments: @mpcrt *c@ = pointer to CRT context
40 * @mpcrt_mod *v@ = pointer to vector of moduli
41 * @size_t k@ = number of moduli
42 * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
46 * Use: Initializes a context for solving Chinese Remainder Theorem
47 * problems. The vector of moduli can be incomplete. Omitted
48 * items must be left as null pointers. Not all combinations of
49 * missing things can be coped with, even if there is
50 * technically enough information to cope. For example, if @n@
51 * is unspecified, all the @m@ values must be present, even if
52 * there is one modulus with both @m@ and @n@ (from which the
53 * product of all moduli could clearly be calculated).
56 void mpcrt_create(mpcrt
*c
, mpcrt_mod
*v
, size_t k
, mp
*n
)
60 /* --- Simple initialization things --- */
65 /* --- Work out @n@ if I don't have it already --- */
72 for (i
= 0; i
< k
; i
++)
73 mpmul_add(&mm
, v
[i
].m
);
77 /* --- A quick hack if %$k = 2$% --- */
81 /* --- The %$n / n_i$% values are trivial in this case --- */
84 v
[0].n
= MP_COPY(v
[1].m
);
86 v
[1].n
= MP_COPY(v
[0].m
);
88 /* --- Now sort out the inverses --- *
90 * @mp_gcd@ will ensure that the first argument is negative.
93 if (!v
[0].ni
&& !v
[1].ni
) {
95 mp_gcd(&g
, &v
[0].ni
, &v
[1].ni
, v
[0].n
, v
[1].n
);
96 assert(MP_EQ(g
, MP_ONE
));
98 v
[0].ni
= mp_add(v
[0].ni
, v
[0].ni
, v
[1].n
);
108 x
= mp_mul(MP_NEW
, v
[j
].n
, v
[j
].ni
);
109 x
= mp_sub(x
, x
, MP_ONE
);
110 mp_div(&x
, 0, x
, v
[i
].n
);
115 /* --- Set up the Barrett context --- */
117 mpbarrett_create(&c
->mb
, n
);
119 /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */
121 for (i
= 0; i
< k
; i
++) {
123 mp_div(&v
[i
].n
, 0, n
, v
[i
].m
);
125 v
[i
].ni
= mp_modinv(MP_NEW
, v
[i
].n
, v
[i
].m
);
127 v
[i
].nni
= mp_mul(MP_NEW
, v
[i
].n
, v
[i
].ni
);
135 /* --- @mpcrt_destroy@ --- *
137 * Arguments: @mpcrt *c@ - pointer to CRT context
141 * Use: Destroys a CRT context, releasing all the resources it holds.
144 void mpcrt_destroy(mpcrt
*c
)
148 for (i
= 0; i
< c
->k
; i
++) {
149 if (c
->v
[i
].m
) mp_drop(c
->v
[i
].m
);
150 if (c
->v
[i
].n
) mp_drop(c
->v
[i
].n
);
151 if (c
->v
[i
].ni
) mp_drop(c
->v
[i
].ni
);
152 if (c
->v
[i
].nni
) mp_drop(c
->v
[i
].nni
);
154 mpbarrett_destroy(&c
->mb
);
157 /* --- @mpcrt_solve@ --- *
159 * Arguments: @mpcrt *c@ = pointer to CRT context
160 * @mp *d@ = fake destination
161 * @mp **v@ = array of residues
163 * Returns: The unique solution modulo the product of the individual
164 * moduli, which leaves the given residues.
166 * Use: Constructs a result given its residue modulo an array of
167 * coprime integers. This can be used to improve performance of
168 * RSA encryption or Blum-Blum-Shub generation if the factors
169 * of the modulus are known, since results can be computed mod
170 * each of the individual factors and then combined at the end.
171 * This is rather faster than doing the full-scale modular
175 mp
*mpcrt_solve(mpcrt
*c
, mp
*d
, mp
**v
)
181 for (i
= 0; i
< c
->k
; i
++) {
182 x
= mp_mul(x
, c
->v
[i
].nni
, v
[i
]);
183 x
= mpbarrett_reduce(&c
->mb
, x
, x
);
188 a
= mpbarrett_reduce(&c
->mb
, a
, a
);
194 /*----- Test rig ----------------------------------------------------------*/
198 static int verify(size_t n
, dstr
*v
)
200 mpcrt_mod
*m
= xmalloc(n
* sizeof(mpcrt_mod
));
201 mp
**r
= xmalloc(n
* sizeof(mp
*));
207 for (i
= 0; i
< n
; i
++) {
208 r
[i
] = *(mp
**)v
[2 * i
].buf
;
209 m
[i
].m
= *(mp
**)v
[2 * i
+ 1].buf
;
214 a
= *(mp
**)v
[2 * n
].buf
;
216 mpcrt_create(&c
, m
, n
, 0);
217 b
= mpcrt_solve(&c
, MP_NEW
, r
);
220 fputs("\n*** failed\n", stderr
);
221 fputs("n = ", stderr
);
222 mp_writefile(c
.mb
.m
, stderr
, 10);
223 for (i
= 0; i
< n
; i
++) {
224 fprintf(stderr
, "\nr[%lu] = ", (unsigned long)i
);
225 mp_writefile(r
[i
], stderr
, 10);
226 fprintf(stderr
, "\nm[%lu] = ", (unsigned long)i
);
227 mp_writefile(m
[i
].m
, stderr
, 10);
228 fprintf(stderr
, "\nN[%lu] = ", (unsigned long)i
);
229 mp_writefile(m
[i
].n
, stderr
, 10);
230 fprintf(stderr
, "\nM[%lu] = ", (unsigned long)i
);
231 mp_writefile(m
[i
].ni
, stderr
, 10);
233 fputs("\nresult = ", stderr
);
234 mp_writefile(b
, stderr
, 10);
235 fputs("\nexpect = ", stderr
);
236 mp_writefile(a
, stderr
, 10);
241 for (i
= 0; i
< n
; i
++)
248 assert(mparena_count(MPARENA_GLOBAL
) == 0);
252 static int crt1(dstr
*v
) { return verify(1, v
); }
253 static int crt2(dstr
*v
) { return verify(2, v
); }
254 static int crt3(dstr
*v
) { return verify(3, v
); }
255 static int crt4(dstr
*v
) { return verify(4, v
); }
256 static int crt5(dstr
*v
) { return verify(5, v
); }
258 static test_chunk tests
[] = {
259 { "crt-1", crt1
, { &type_mp
, &type_mp
,
261 { "crt-2", crt2
, { &type_mp
, &type_mp
,
264 { "crt-3", crt3
, { &type_mp
, &type_mp
,
268 { "crt-4", crt4
, { &type_mp
, &type_mp
,
273 { "crt-5", crt5
, { &type_mp
, &type_mp
,
282 int main(int argc
, char *argv
[])
285 test_run(argc
, argv
, tests
, SRCDIR
"/t/mpcrt");
291 /*----- That's all, folks -------------------------------------------------*/