3 * $Id: mpcrt.c,v 1.1 1999/11/22 20:50:57 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.1 1999/11/22 20:50:57 mdw
34 * Add support for solving Chinese Remainder Theorem problems.
38 /*----- Header files ------------------------------------------------------*/
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @mpcrt_create@ --- *
48 * Arguments: @mpcrt *c@ = pointer to CRT context
49 * @mpcrt_mod *v@ = pointer to vector of moduli
50 * @size_t k@ = number of moduli
51 * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
55 * Use: Initializes a context for solving Chinese Remainder Theorem
56 * problems. The vector of moduli can be incomplete. Omitted
57 * items must be left as null pointers. Not all combinations of
58 * missing things can be coped with, even if there is
59 * technically enough information to cope. For example, if @n@
60 * is unspecified, all the @m@ values must be present, even if
61 * there is one modulus with both @m@ and @n@ (from which the
62 * product of all moduli could clearly be calculated).
65 void mpcrt_create(mpcrt
*c
, mpcrt_mod
*v
, size_t k
, mp
*n
)
67 mp
*x
= MP_NEW
, *y
= MP_NEW
;
70 /* --- Simple initialization things --- */
75 /* --- Work out @n@ if I don't have it already --- */
79 for (i
= 1; i
< k
; i
++) {
80 mp
*d
= mp_mul(x
, n
, v
[i
].m
);
86 /* --- Set up the Montgomery context --- */
88 mpmont_create(&c
->mm
, n
);
90 /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */
92 for (i
= 0; i
< k
; i
++) {
94 mp_div(&v
[i
].n
, 0, n
, v
[i
].m
);
96 mp_gcd(0, &v
[i
].ni
, 0, v
[i
].n
, v
[i
].m
);
98 x
= mpmont_mul(&c
->mm
, x
, v
[i
].n
, c
->mm
.r2
);
99 y
= mpmont_mul(&c
->mm
, y
, v
[i
].ni
, c
->mm
.r2
);
100 v
[i
].nnir
= mpmont_mul(&c
->mm
, MP_NEW
, x
, y
);
112 /* --- @mpcrt_destroy@ --- *
114 * Arguments: @mpcrt *c@ - pointer to CRT context
118 * Use: Destroys a CRT context, releasing all the resources it holds.
121 void mpcrt_destroy(mpcrt
*c
)
125 for (i
= 0; i
< c
->k
; i
++) {
126 if (c
->v
[i
].m
) mp_drop(c
->v
[i
].m
);
127 if (c
->v
[i
].n
) mp_drop(c
->v
[i
].n
);
128 if (c
->v
[i
].ni
) mp_drop(c
->v
[i
].ni
);
129 if (c
->v
[i
].nnir
) mp_drop(c
->v
[i
].nnir
);
131 mpmont_destroy(&c
->mm
);
134 /* --- @mpcrt_solve@ --- *
136 * Arguments: @mpcrt *c@ = pointer to CRT context
137 * @mp **v@ = array of residues
139 * Returns: The unique solution modulo the product of the individual
140 * moduli, which leaves the given residues.
142 * Use: Constructs a result given its residue modulo an array of
143 * coprime integers. This can be used to improve performance of
144 * RSA encryption or Blum-Blum-Shub generation if the factors
145 * of the modulus are known, since results can be computed mod
146 * each of the individual factors and then combined at the end.
147 * This is rather faster than doing the full-scale modular
151 mp
*mpcrt_solve(mpcrt
*c
, mp
**v
)
157 for (i
= 0; i
< c
->k
; i
++) {
158 x
= mpmont_mul(&c
->mm
, x
, c
->v
[i
].nnir
, v
[i
]);
163 if (MP_CMP(a
, >=, c
->mm
.m
))
164 mp_div(0, &a
, a
, c
->mm
.m
);
168 /*----- Test rig ----------------------------------------------------------*/
172 static int verify(size_t n
, dstr
*v
)
174 mpcrt_mod
*m
= xmalloc(n
* sizeof(mpcrt_mod
));
175 mp
**r
= xmalloc(n
* sizeof(mp
*));
181 for (i
= 0; i
< n
; i
++) {
182 r
[i
] = *(mp
**)v
[2 * i
].buf
;
183 m
[i
].m
= *(mp
**)v
[2 * i
+ 1].buf
;
188 a
= *(mp
**)v
[2 * n
].buf
;
190 mpcrt_create(&c
, m
, n
, 0);
191 b
= mpcrt_solve(&c
, r
);
193 if (MP_CMP(a
, !=, b
)) {
194 fputs("\n*** failed\n", stderr
);
195 fputs("n = ", stderr
);
196 mp_writefile(c
.mm
.m
, stderr
, 10);
197 for (i
= 0; i
< n
; i
++) {
198 fprintf(stderr
, "\nr[%u] = ", i
);
199 mp_writefile(r
[i
], stderr
, 10);
200 fprintf(stderr
, "\nm[%u] = ", i
);
201 mp_writefile(m
[i
].m
, stderr
, 10);
202 fprintf(stderr
, "\nN[%u] = ", i
);
203 mp_writefile(m
[i
].n
, stderr
, 10);
204 fprintf(stderr
, "\nM[%u] = ", i
);
205 mp_writefile(m
[i
].ni
, stderr
, 10);
207 fputs("\nresult = ", stderr
);
208 mp_writefile(b
, stderr
, 10);
209 fputs("\nexpect = ", stderr
);
210 mp_writefile(a
, stderr
, 10);
223 static int crt1(dstr
*v
) { return verify(1, v
); }
224 static int crt2(dstr
*v
) { return verify(2, v
); }
225 static int crt3(dstr
*v
) { return verify(3, v
); }
226 static int crt4(dstr
*v
) { return verify(4, v
); }
227 static int crt5(dstr
*v
) { return verify(5, v
); }
229 static test_chunk tests
[] = {
230 { "crt-1", crt1
, { &type_mp
, &type_mp
,
232 { "crt-2", crt2
, { &type_mp
, &type_mp
,
235 { "crt-3", crt3
, { &type_mp
, &type_mp
,
239 { "crt-4", crt4
, { &type_mp
, &type_mp
,
244 { "crt-5", crt5
, { &type_mp
, &type_mp
,
253 int main(int argc
, char *argv
[])
256 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpcrt");
262 /*----- That's all, folks -------------------------------------------------*/