| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpcrt.c,v 1.6 2004/04/08 01:36:15 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 | /*----- Header files ------------------------------------------------------*/ |
| 31 | |
| 32 | #include "mp.h" |
| 33 | #include "mpcrt.h" |
| 34 | #include "mpmul.h" |
| 35 | #include "mpbarrett.h" |
| 36 | |
| 37 | /*----- Main code ---------------------------------------------------------*/ |
| 38 | |
| 39 | /* --- @mpcrt_create@ --- * |
| 40 | * |
| 41 | * Arguments: @mpcrt *c@ = pointer to CRT context |
| 42 | * @mpcrt_mod *v@ = pointer to vector of moduli |
| 43 | * @size_t k@ = number of moduli |
| 44 | * @mp *n@ = product of all moduli (@MP_NEW@ if unknown) |
| 45 | * |
| 46 | * Returns: --- |
| 47 | * |
| 48 | * Use: Initializes a context for solving Chinese Remainder Theorem |
| 49 | * problems. The vector of moduli can be incomplete. Omitted |
| 50 | * items must be left as null pointers. Not all combinations of |
| 51 | * missing things can be coped with, even if there is |
| 52 | * technically enough information to cope. For example, if @n@ |
| 53 | * is unspecified, all the @m@ values must be present, even if |
| 54 | * there is one modulus with both @m@ and @n@ (from which the |
| 55 | * product of all moduli could clearly be calculated). |
| 56 | */ |
| 57 | |
| 58 | void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n) |
| 59 | { |
| 60 | size_t i; |
| 61 | |
| 62 | /* --- Simple initialization things --- */ |
| 63 | |
| 64 | c->k = k; |
| 65 | c->v = v; |
| 66 | |
| 67 | /* --- Work out @n@ if I don't have it already --- */ |
| 68 | |
| 69 | if (n != MP_NEW) |
| 70 | n = MP_COPY(n); |
| 71 | else { |
| 72 | mpmul mm; |
| 73 | mpmul_init(&mm); |
| 74 | for (i = 0; i < k; i++) |
| 75 | mpmul_add(&mm, v[i].m); |
| 76 | n = mpmul_done(&mm); |
| 77 | } |
| 78 | |
| 79 | /* --- A quick hack if %$k = 2$% --- */ |
| 80 | |
| 81 | if (k == 2) { |
| 82 | |
| 83 | /* --- The %$n / n_i$% values are trivial in this case --- */ |
| 84 | |
| 85 | if (!v[0].n) |
| 86 | v[0].n = MP_COPY(v[1].m); |
| 87 | if (!v[1].n) |
| 88 | v[1].n = MP_COPY(v[0].m); |
| 89 | |
| 90 | /* --- Now sort out the inverses --- * |
| 91 | * |
| 92 | * @mp_gcd@ will ensure that the first argument is negative. |
| 93 | */ |
| 94 | |
| 95 | if (!v[0].ni && !v[1].ni) { |
| 96 | mp *g = MP_NEW; |
| 97 | mp_gcd(&g, &v[0].ni, &v[1].ni, v[0].n, v[1].n); |
| 98 | assert(MP_EQ(g, MP_ONE)); |
| 99 | mp_drop(g); |
| 100 | v[0].ni = mp_add(v[0].ni, v[0].ni, v[1].n); |
| 101 | } else { |
| 102 | int i, j; |
| 103 | mp *x; |
| 104 | |
| 105 | if (!v[0].ni) |
| 106 | i = 0, j = 1; |
| 107 | else |
| 108 | i = 1, j = 0; |
| 109 | |
| 110 | x = mp_mul(MP_NEW, v[j].n, v[j].ni); |
| 111 | x = mp_sub(x, x, MP_ONE); |
| 112 | mp_div(&x, 0, x, v[i].n); |
| 113 | v[i].ni = x; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* --- Set up the Barrett context --- */ |
| 118 | |
| 119 | mpbarrett_create(&c->mb, n); |
| 120 | |
| 121 | /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */ |
| 122 | |
| 123 | for (i = 0; i < k; i++) { |
| 124 | if (!v[i].n) |
| 125 | mp_div(&v[i].n, 0, n, v[i].m); |
| 126 | if (!v[i].ni) |
| 127 | v[i].ni = mp_modinv(MP_NEW, v[i].n, v[i].m); |
| 128 | if (!v[i].nni) |
| 129 | v[i].nni = mp_mul(MP_NEW, v[i].n, v[i].ni); |
| 130 | } |
| 131 | |
| 132 | /* --- Done --- */ |
| 133 | |
| 134 | mp_drop(n); |
| 135 | } |
| 136 | |
| 137 | /* --- @mpcrt_destroy@ --- * |
| 138 | * |
| 139 | * Arguments: @mpcrt *c@ - pointer to CRT context |
| 140 | * |
| 141 | * Returns: --- |
| 142 | * |
| 143 | * Use: Destroys a CRT context, releasing all the resources it holds. |
| 144 | */ |
| 145 | |
| 146 | void mpcrt_destroy(mpcrt *c) |
| 147 | { |
| 148 | size_t i; |
| 149 | |
| 150 | for (i = 0; i < c->k; i++) { |
| 151 | if (c->v[i].m) mp_drop(c->v[i].m); |
| 152 | if (c->v[i].n) mp_drop(c->v[i].n); |
| 153 | if (c->v[i].ni) mp_drop(c->v[i].ni); |
| 154 | if (c->v[i].nni) mp_drop(c->v[i].nni); |
| 155 | } |
| 156 | mpbarrett_destroy(&c->mb); |
| 157 | } |
| 158 | |
| 159 | /* --- @mpcrt_solve@ --- * |
| 160 | * |
| 161 | * Arguments: @mpcrt *c@ = pointer to CRT context |
| 162 | * @mp *d@ = fake destination |
| 163 | * @mp **v@ = array of residues |
| 164 | * |
| 165 | * Returns: The unique solution modulo the product of the individual |
| 166 | * moduli, which leaves the given residues. |
| 167 | * |
| 168 | * Use: Constructs a result given its residue modulo an array of |
| 169 | * coprime integers. This can be used to improve performance of |
| 170 | * RSA encryption or Blum-Blum-Shub generation if the factors |
| 171 | * of the modulus are known, since results can be computed mod |
| 172 | * each of the individual factors and then combined at the end. |
| 173 | * This is rather faster than doing the full-scale modular |
| 174 | * exponentiation. |
| 175 | */ |
| 176 | |
| 177 | mp *mpcrt_solve(mpcrt *c, mp *d, mp **v) |
| 178 | { |
| 179 | mp *a = MP_ZERO; |
| 180 | mp *x = MP_NEW; |
| 181 | size_t i; |
| 182 | |
| 183 | for (i = 0; i < c->k; i++) { |
| 184 | x = mp_mul(x, c->v[i].nni, v[i]); |
| 185 | x = mpbarrett_reduce(&c->mb, x, x); |
| 186 | a = mp_add(a, a, x); |
| 187 | } |
| 188 | if (x) |
| 189 | MP_DROP(x); |
| 190 | a = mpbarrett_reduce(&c->mb, a, a); |
| 191 | if (d != MP_NEW) |
| 192 | MP_DROP(d); |
| 193 | return (a); |
| 194 | } |
| 195 | |
| 196 | /*----- Test rig ----------------------------------------------------------*/ |
| 197 | |
| 198 | #ifdef TEST_RIG |
| 199 | |
| 200 | static int verify(size_t n, dstr *v) |
| 201 | { |
| 202 | mpcrt_mod *m = xmalloc(n * sizeof(mpcrt_mod)); |
| 203 | mp **r = xmalloc(n * sizeof(mp *)); |
| 204 | mpcrt c; |
| 205 | mp *a, *b; |
| 206 | size_t i; |
| 207 | int ok = 1; |
| 208 | |
| 209 | for (i = 0; i < n; i++) { |
| 210 | r[i] = *(mp **)v[2 * i].buf; |
| 211 | m[i].m = *(mp **)v[2 * i + 1].buf; |
| 212 | m[i].n = 0; |
| 213 | m[i].ni = 0; |
| 214 | m[i].nni = 0; |
| 215 | } |
| 216 | a = *(mp **)v[2 * n].buf; |
| 217 | |
| 218 | mpcrt_create(&c, m, n, 0); |
| 219 | b = mpcrt_solve(&c, MP_NEW, r); |
| 220 | |
| 221 | if (!MP_EQ(a, b)) { |
| 222 | fputs("\n*** failed\n", stderr); |
| 223 | fputs("n = ", stderr); |
| 224 | mp_writefile(c.mb.m, stderr, 10); |
| 225 | for (i = 0; i < n; i++) { |
| 226 | fprintf(stderr, "\nr[%u] = ", i); |
| 227 | mp_writefile(r[i], stderr, 10); |
| 228 | fprintf(stderr, "\nm[%u] = ", i); |
| 229 | mp_writefile(m[i].m, stderr, 10); |
| 230 | fprintf(stderr, "\nN[%u] = ", i); |
| 231 | mp_writefile(m[i].n, stderr, 10); |
| 232 | fprintf(stderr, "\nM[%u] = ", i); |
| 233 | mp_writefile(m[i].ni, stderr, 10); |
| 234 | } |
| 235 | fputs("\nresult = ", stderr); |
| 236 | mp_writefile(b, stderr, 10); |
| 237 | fputs("\nexpect = ", stderr); |
| 238 | mp_writefile(a, stderr, 10); |
| 239 | fputc('\n', stderr); |
| 240 | ok = 0; |
| 241 | } |
| 242 | |
| 243 | for (i = 0; i < n; i++) |
| 244 | mp_drop(r[i]); |
| 245 | mp_drop(a); |
| 246 | mp_drop(b); |
| 247 | mpcrt_destroy(&c); |
| 248 | free(m); |
| 249 | free(r); |
| 250 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 251 | return (ok); |
| 252 | } |
| 253 | |
| 254 | static int crt1(dstr *v) { return verify(1, v); } |
| 255 | static int crt2(dstr *v) { return verify(2, v); } |
| 256 | static int crt3(dstr *v) { return verify(3, v); } |
| 257 | static int crt4(dstr *v) { return verify(4, v); } |
| 258 | static int crt5(dstr *v) { return verify(5, v); } |
| 259 | |
| 260 | static test_chunk tests[] = { |
| 261 | { "crt-1", crt1, { &type_mp, &type_mp, |
| 262 | &type_mp, 0 } }, |
| 263 | { "crt-2", crt2, { &type_mp, &type_mp, |
| 264 | &type_mp, &type_mp, |
| 265 | &type_mp, 0 } }, |
| 266 | { "crt-3", crt3, { &type_mp, &type_mp, |
| 267 | &type_mp, &type_mp, |
| 268 | &type_mp, &type_mp, |
| 269 | &type_mp, 0 } }, |
| 270 | { "crt-4", crt4, { &type_mp, &type_mp, |
| 271 | &type_mp, &type_mp, |
| 272 | &type_mp, &type_mp, |
| 273 | &type_mp, &type_mp, |
| 274 | &type_mp, 0 } }, |
| 275 | { "crt-5", crt5, { &type_mp, &type_mp, |
| 276 | &type_mp, &type_mp, |
| 277 | &type_mp, &type_mp, |
| 278 | &type_mp, &type_mp, |
| 279 | &type_mp, &type_mp, |
| 280 | &type_mp, 0 } }, |
| 281 | { 0, 0, { 0 } } |
| 282 | }; |
| 283 | |
| 284 | int main(int argc, char *argv[]) |
| 285 | { |
| 286 | sub_init(); |
| 287 | test_run(argc, argv, tests, SRCDIR "/tests/mpcrt"); |
| 288 | return (0); |
| 289 | } |
| 290 | |
| 291 | #endif |
| 292 | |
| 293 | /*----- That's all, folks -------------------------------------------------*/ |