| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpcrt.c,v 1.1 1999/11/22 20:50:57 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.1 1999/11/22 20:50:57 mdw |
| 34 | * Add support for solving Chinese Remainder Theorem problems. |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | /*----- Header files ------------------------------------------------------*/ |
| 39 | |
| 40 | #include "mp.h" |
| 41 | #include "mpcrt.h" |
| 42 | #include "mpmont.h" |
| 43 | |
| 44 | /*----- Main code ---------------------------------------------------------*/ |
| 45 | |
| 46 | /* --- @mpcrt_create@ --- * |
| 47 | * |
| 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) |
| 52 | * |
| 53 | * Returns: --- |
| 54 | * |
| 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). |
| 63 | */ |
| 64 | |
| 65 | void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n) |
| 66 | { |
| 67 | mp *x = MP_NEW, *y = MP_NEW; |
| 68 | size_t i; |
| 69 | |
| 70 | /* --- Simple initialization things --- */ |
| 71 | |
| 72 | c->k = k; |
| 73 | c->v = v; |
| 74 | |
| 75 | /* --- Work out @n@ if I don't have it already --- */ |
| 76 | |
| 77 | if (n == MP_NEW) { |
| 78 | n = MP_COPY(v[0].m); |
| 79 | for (i = 1; i < k; i++) { |
| 80 | mp *d = mp_mul(x, n, v[i].m); |
| 81 | x = n; |
| 82 | n = d; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* --- Set up the Montgomery context --- */ |
| 87 | |
| 88 | mpmont_create(&c->mm, n); |
| 89 | |
| 90 | /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */ |
| 91 | |
| 92 | for (i = 0; i < k; i++) { |
| 93 | if (!v[i].n) |
| 94 | mp_div(&v[i].n, 0, n, v[i].m); |
| 95 | if (!v[i].ni) |
| 96 | mp_gcd(0, &v[i].ni, 0, v[i].n, v[i].m); |
| 97 | if (!v[i].nnir) { |
| 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); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* --- Done --- */ |
| 105 | |
| 106 | if (x) |
| 107 | mp_drop(x); |
| 108 | if (y) |
| 109 | mp_drop(y); |
| 110 | } |
| 111 | |
| 112 | /* --- @mpcrt_destroy@ --- * |
| 113 | * |
| 114 | * Arguments: @mpcrt *c@ - pointer to CRT context |
| 115 | * |
| 116 | * Returns: --- |
| 117 | * |
| 118 | * Use: Destroys a CRT context, releasing all the resources it holds. |
| 119 | */ |
| 120 | |
| 121 | void mpcrt_destroy(mpcrt *c) |
| 122 | { |
| 123 | size_t i; |
| 124 | |
| 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); |
| 130 | } |
| 131 | mpmont_destroy(&c->mm); |
| 132 | } |
| 133 | |
| 134 | /* --- @mpcrt_solve@ --- * |
| 135 | * |
| 136 | * Arguments: @mpcrt *c@ = pointer to CRT context |
| 137 | * @mp **v@ = array of residues |
| 138 | * |
| 139 | * Returns: The unique solution modulo the product of the individual |
| 140 | * moduli, which leaves the given residues. |
| 141 | * |
| 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 |
| 148 | * exponentiation. |
| 149 | */ |
| 150 | |
| 151 | mp *mpcrt_solve(mpcrt *c, mp **v) |
| 152 | { |
| 153 | mp *a = MP_ZERO; |
| 154 | mp *x = MP_NEW; |
| 155 | size_t i; |
| 156 | |
| 157 | for (i = 0; i < c->k; i++) { |
| 158 | x = mpmont_mul(&c->mm, x, c->v[i].nnir, v[i]); |
| 159 | a = mp_add(a, a, x); |
| 160 | } |
| 161 | if (x) |
| 162 | mp_drop(x); |
| 163 | if (MP_CMP(a, >=, c->mm.m)) |
| 164 | mp_div(0, &a, a, c->mm.m); |
| 165 | return (a); |
| 166 | } |
| 167 | |
| 168 | /*----- Test rig ----------------------------------------------------------*/ |
| 169 | |
| 170 | #ifdef TEST_RIG |
| 171 | |
| 172 | static int verify(size_t n, dstr *v) |
| 173 | { |
| 174 | mpcrt_mod *m = xmalloc(n * sizeof(mpcrt_mod)); |
| 175 | mp **r = xmalloc(n * sizeof(mp *)); |
| 176 | mpcrt c; |
| 177 | mp *a, *b; |
| 178 | size_t i; |
| 179 | int ok = 1; |
| 180 | |
| 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; |
| 184 | m[i].n = 0; |
| 185 | m[i].ni = 0; |
| 186 | m[i].nnir = 0; |
| 187 | } |
| 188 | a = *(mp **)v[2 * n].buf; |
| 189 | |
| 190 | mpcrt_create(&c, m, n, 0); |
| 191 | b = mpcrt_solve(&c, r); |
| 192 | |
| 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); |
| 206 | } |
| 207 | fputs("\nresult = ", stderr); |
| 208 | mp_writefile(b, stderr, 10); |
| 209 | fputs("\nexpect = ", stderr); |
| 210 | mp_writefile(a, stderr, 10); |
| 211 | fputc('\n', stderr); |
| 212 | ok = 0; |
| 213 | } |
| 214 | |
| 215 | mp_drop(a); |
| 216 | mp_drop(b); |
| 217 | mpcrt_destroy(&c); |
| 218 | free(m); |
| 219 | free(r); |
| 220 | return (ok); |
| 221 | } |
| 222 | |
| 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); } |
| 228 | |
| 229 | static test_chunk tests[] = { |
| 230 | { "crt-1", crt1, { &type_mp, &type_mp, |
| 231 | &type_mp, 0 } }, |
| 232 | { "crt-2", crt2, { &type_mp, &type_mp, |
| 233 | &type_mp, &type_mp, |
| 234 | &type_mp, 0 } }, |
| 235 | { "crt-3", crt3, { &type_mp, &type_mp, |
| 236 | &type_mp, &type_mp, |
| 237 | &type_mp, &type_mp, |
| 238 | &type_mp, 0 } }, |
| 239 | { "crt-4", crt4, { &type_mp, &type_mp, |
| 240 | &type_mp, &type_mp, |
| 241 | &type_mp, &type_mp, |
| 242 | &type_mp, &type_mp, |
| 243 | &type_mp, 0 } }, |
| 244 | { "crt-5", crt5, { &type_mp, &type_mp, |
| 245 | &type_mp, &type_mp, |
| 246 | &type_mp, &type_mp, |
| 247 | &type_mp, &type_mp, |
| 248 | &type_mp, &type_mp, |
| 249 | &type_mp, 0 } }, |
| 250 | { 0, 0, { 0 } } |
| 251 | }; |
| 252 | |
| 253 | int main(int argc, char *argv[]) |
| 254 | { |
| 255 | sub_init(); |
| 256 | test_run(argc, argv, tests, SRCDIR "/tests/mpcrt"); |
| 257 | return (0); |
| 258 | } |
| 259 | |
| 260 | #endif |
| 261 | |
| 262 | /*----- That's all, folks -------------------------------------------------*/ |