| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpcrt.c,v 1.5 2001/04/29 17:39:33 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.5 2001/04/29 17:39:33 mdw |
| 34 | * Fix memory leak. |
| 35 | * |
| 36 | * Revision 1.4 2001/04/19 18:25:38 mdw |
| 37 | * Use mpmul for the multiplication. |
| 38 | * |
| 39 | * Revision 1.3 2000/10/08 12:11:22 mdw |
| 40 | * Use @MP_EQ@ instead of @MP_CMP@. |
| 41 | * |
| 42 | * Revision 1.2 1999/12/10 23:22:32 mdw |
| 43 | * Interface changes for suggested destinations. Use Barrett reduction. |
| 44 | * |
| 45 | * Revision 1.1 1999/11/22 20:50:57 mdw |
| 46 | * Add support for solving Chinese Remainder Theorem problems. |
| 47 | * |
| 48 | */ |
| 49 | |
| 50 | /*----- Header files ------------------------------------------------------*/ |
| 51 | |
| 52 | #include "mp.h" |
| 53 | #include "mpcrt.h" |
| 54 | #include "mpmul.h" |
| 55 | #include "mpbarrett.h" |
| 56 | |
| 57 | /*----- Main code ---------------------------------------------------------*/ |
| 58 | |
| 59 | /* --- @mpcrt_create@ --- * |
| 60 | * |
| 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) |
| 65 | * |
| 66 | * Returns: --- |
| 67 | * |
| 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). |
| 76 | */ |
| 77 | |
| 78 | void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n) |
| 79 | { |
| 80 | size_t i; |
| 81 | |
| 82 | /* --- Simple initialization things --- */ |
| 83 | |
| 84 | c->k = k; |
| 85 | c->v = v; |
| 86 | |
| 87 | /* --- Work out @n@ if I don't have it already --- */ |
| 88 | |
| 89 | if (n != MP_NEW) |
| 90 | n = MP_COPY(n); |
| 91 | else { |
| 92 | mpmul mm; |
| 93 | mpmul_init(&mm); |
| 94 | for (i = 0; i < k; i++) |
| 95 | mpmul_add(&mm, v[i].m); |
| 96 | n = mpmul_done(&mm); |
| 97 | } |
| 98 | |
| 99 | /* --- A quick hack if %$k = 2$% --- */ |
| 100 | |
| 101 | if (k == 2) { |
| 102 | |
| 103 | /* --- The %$n / n_i$% values are trivial in this case --- */ |
| 104 | |
| 105 | if (!v[0].n) |
| 106 | v[0].n = MP_COPY(v[1].m); |
| 107 | if (!v[1].n) |
| 108 | v[1].n = MP_COPY(v[0].m); |
| 109 | |
| 110 | /* --- Now sort out the inverses --- * |
| 111 | * |
| 112 | * @mp_gcd@ will ensure that the first argument is negative. |
| 113 | */ |
| 114 | |
| 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); |
| 118 | } else { |
| 119 | int i, j; |
| 120 | mp *x; |
| 121 | |
| 122 | if (!v[0].ni) |
| 123 | i = 0, j = 1; |
| 124 | else |
| 125 | i = 1, j = 0; |
| 126 | |
| 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); |
| 130 | v[i].ni = x; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* --- Set up the Barrett context --- */ |
| 135 | |
| 136 | mpbarrett_create(&c->mb, n); |
| 137 | |
| 138 | /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */ |
| 139 | |
| 140 | for (i = 0; i < k; i++) { |
| 141 | if (!v[i].n) |
| 142 | mp_div(&v[i].n, 0, n, v[i].m); |
| 143 | if (!v[i].ni) |
| 144 | mp_gcd(0, &v[i].ni, 0, v[i].n, v[i].m); |
| 145 | if (!v[i].nni) |
| 146 | v[i].nni = mp_mul(MP_NEW, v[i].n, v[i].ni); |
| 147 | } |
| 148 | |
| 149 | /* --- Done --- */ |
| 150 | |
| 151 | mp_drop(n); |
| 152 | } |
| 153 | |
| 154 | /* --- @mpcrt_destroy@ --- * |
| 155 | * |
| 156 | * Arguments: @mpcrt *c@ - pointer to CRT context |
| 157 | * |
| 158 | * Returns: --- |
| 159 | * |
| 160 | * Use: Destroys a CRT context, releasing all the resources it holds. |
| 161 | */ |
| 162 | |
| 163 | void mpcrt_destroy(mpcrt *c) |
| 164 | { |
| 165 | size_t i; |
| 166 | |
| 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); |
| 172 | } |
| 173 | mpbarrett_destroy(&c->mb); |
| 174 | } |
| 175 | |
| 176 | /* --- @mpcrt_solve@ --- * |
| 177 | * |
| 178 | * Arguments: @mpcrt *c@ = pointer to CRT context |
| 179 | * @mp *d@ = fake destination |
| 180 | * @mp **v@ = array of residues |
| 181 | * |
| 182 | * Returns: The unique solution modulo the product of the individual |
| 183 | * moduli, which leaves the given residues. |
| 184 | * |
| 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 |
| 191 | * exponentiation. |
| 192 | */ |
| 193 | |
| 194 | mp *mpcrt_solve(mpcrt *c, mp *d, mp **v) |
| 195 | { |
| 196 | mp *a = MP_ZERO; |
| 197 | mp *x = MP_NEW; |
| 198 | size_t i; |
| 199 | |
| 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); |
| 203 | a = mp_add(a, a, x); |
| 204 | } |
| 205 | if (x) |
| 206 | MP_DROP(x); |
| 207 | a = mpbarrett_reduce(&c->mb, a, a); |
| 208 | if (d != MP_NEW) |
| 209 | MP_DROP(d); |
| 210 | return (a); |
| 211 | } |
| 212 | |
| 213 | /*----- Test rig ----------------------------------------------------------*/ |
| 214 | |
| 215 | #ifdef TEST_RIG |
| 216 | |
| 217 | static int verify(size_t n, dstr *v) |
| 218 | { |
| 219 | mpcrt_mod *m = xmalloc(n * sizeof(mpcrt_mod)); |
| 220 | mp **r = xmalloc(n * sizeof(mp *)); |
| 221 | mpcrt c; |
| 222 | mp *a, *b; |
| 223 | size_t i; |
| 224 | int ok = 1; |
| 225 | |
| 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; |
| 229 | m[i].n = 0; |
| 230 | m[i].ni = 0; |
| 231 | m[i].nni = 0; |
| 232 | } |
| 233 | a = *(mp **)v[2 * n].buf; |
| 234 | |
| 235 | mpcrt_create(&c, m, n, 0); |
| 236 | b = mpcrt_solve(&c, MP_NEW, r); |
| 237 | |
| 238 | if (!MP_EQ(a, b)) { |
| 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); |
| 251 | } |
| 252 | fputs("\nresult = ", stderr); |
| 253 | mp_writefile(b, stderr, 10); |
| 254 | fputs("\nexpect = ", stderr); |
| 255 | mp_writefile(a, stderr, 10); |
| 256 | fputc('\n', stderr); |
| 257 | ok = 0; |
| 258 | } |
| 259 | |
| 260 | for (i = 0; i < n; i++) |
| 261 | mp_drop(r[i]); |
| 262 | mp_drop(a); |
| 263 | mp_drop(b); |
| 264 | mpcrt_destroy(&c); |
| 265 | free(m); |
| 266 | free(r); |
| 267 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 268 | return (ok); |
| 269 | } |
| 270 | |
| 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); } |
| 276 | |
| 277 | static test_chunk tests[] = { |
| 278 | { "crt-1", crt1, { &type_mp, &type_mp, |
| 279 | &type_mp, 0 } }, |
| 280 | { "crt-2", crt2, { &type_mp, &type_mp, |
| 281 | &type_mp, &type_mp, |
| 282 | &type_mp, 0 } }, |
| 283 | { "crt-3", crt3, { &type_mp, &type_mp, |
| 284 | &type_mp, &type_mp, |
| 285 | &type_mp, &type_mp, |
| 286 | &type_mp, 0 } }, |
| 287 | { "crt-4", crt4, { &type_mp, &type_mp, |
| 288 | &type_mp, &type_mp, |
| 289 | &type_mp, &type_mp, |
| 290 | &type_mp, &type_mp, |
| 291 | &type_mp, 0 } }, |
| 292 | { "crt-5", crt5, { &type_mp, &type_mp, |
| 293 | &type_mp, &type_mp, |
| 294 | &type_mp, &type_mp, |
| 295 | &type_mp, &type_mp, |
| 296 | &type_mp, &type_mp, |
| 297 | &type_mp, 0 } }, |
| 298 | { 0, 0, { 0 } } |
| 299 | }; |
| 300 | |
| 301 | int main(int argc, char *argv[]) |
| 302 | { |
| 303 | sub_init(); |
| 304 | test_run(argc, argv, tests, SRCDIR "/tests/mpcrt"); |
| 305 | return (0); |
| 306 | } |
| 307 | |
| 308 | #endif |
| 309 | |
| 310 | /*----- That's all, folks -------------------------------------------------*/ |