--- /dev/null
+/* -*-c-*-
+ *
+ * $Id: mpcrt.c,v 1.1 1999/11/22 20:50:57 mdw Exp $
+ *
+ * Chinese Remainder Theorem computations (Gauss's algorithm)
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: mpcrt.c,v $
+ * Revision 1.1 1999/11/22 20:50:57 mdw
+ * Add support for solving Chinese Remainder Theorem problems.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "mp.h"
+#include "mpcrt.h"
+#include "mpmont.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @mpcrt_create@ --- *
+ *
+ * Arguments: @mpcrt *c@ = pointer to CRT context
+ * @mpcrt_mod *v@ = pointer to vector of moduli
+ * @size_t k@ = number of moduli
+ * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
+ *
+ * Returns: ---
+ *
+ * Use: Initializes a context for solving Chinese Remainder Theorem
+ * problems. The vector of moduli can be incomplete. Omitted
+ * items must be left as null pointers. Not all combinations of
+ * missing things can be coped with, even if there is
+ * technically enough information to cope. For example, if @n@
+ * is unspecified, all the @m@ values must be present, even if
+ * there is one modulus with both @m@ and @n@ (from which the
+ * product of all moduli could clearly be calculated).
+ */
+
+void mpcrt_create(mpcrt *c, mpcrt_mod *v, size_t k, mp *n)
+{
+ mp *x = MP_NEW, *y = MP_NEW;
+ size_t i;
+
+ /* --- Simple initialization things --- */
+
+ c->k = k;
+ c->v = v;
+
+ /* --- Work out @n@ if I don't have it already --- */
+
+ if (n == MP_NEW) {
+ n = MP_COPY(v[0].m);
+ for (i = 1; i < k; i++) {
+ mp *d = mp_mul(x, n, v[i].m);
+ x = n;
+ n = d;
+ }
+ }
+
+ /* --- Set up the Montgomery context --- */
+
+ mpmont_create(&c->mm, n);
+
+ /* --- Walk through filling in @n@, @ni@ and @nnir@ --- */
+
+ for (i = 0; i < k; i++) {
+ if (!v[i].n)
+ mp_div(&v[i].n, 0, n, v[i].m);
+ if (!v[i].ni)
+ mp_gcd(0, &v[i].ni, 0, v[i].n, v[i].m);
+ if (!v[i].nnir) {
+ x = mpmont_mul(&c->mm, x, v[i].n, c->mm.r2);
+ y = mpmont_mul(&c->mm, y, v[i].ni, c->mm.r2);
+ v[i].nnir = mpmont_mul(&c->mm, MP_NEW, x, y);
+ }
+ }
+
+ /* --- Done --- */
+
+ if (x)
+ mp_drop(x);
+ if (y)
+ mp_drop(y);
+}
+
+/* --- @mpcrt_destroy@ --- *
+ *
+ * Arguments: @mpcrt *c@ - pointer to CRT context
+ *
+ * Returns: ---
+ *
+ * Use: Destroys a CRT context, releasing all the resources it holds.
+ */
+
+void mpcrt_destroy(mpcrt *c)
+{
+ size_t i;
+
+ for (i = 0; i < c->k; i++) {
+ if (c->v[i].m) mp_drop(c->v[i].m);
+ if (c->v[i].n) mp_drop(c->v[i].n);
+ if (c->v[i].ni) mp_drop(c->v[i].ni);
+ if (c->v[i].nnir) mp_drop(c->v[i].nnir);
+ }
+ mpmont_destroy(&c->mm);
+}
+
+/* --- @mpcrt_solve@ --- *
+ *
+ * Arguments: @mpcrt *c@ = pointer to CRT context
+ * @mp **v@ = array of residues
+ *
+ * Returns: The unique solution modulo the product of the individual
+ * moduli, which leaves the given residues.
+ *
+ * Use: Constructs a result given its residue modulo an array of
+ * coprime integers. This can be used to improve performance of
+ * RSA encryption or Blum-Blum-Shub generation if the factors
+ * of the modulus are known, since results can be computed mod
+ * each of the individual factors and then combined at the end.
+ * This is rather faster than doing the full-scale modular
+ * exponentiation.
+ */
+
+mp *mpcrt_solve(mpcrt *c, mp **v)
+{
+ mp *a = MP_ZERO;
+ mp *x = MP_NEW;
+ size_t i;
+
+ for (i = 0; i < c->k; i++) {
+ x = mpmont_mul(&c->mm, x, c->v[i].nnir, v[i]);
+ a = mp_add(a, a, x);
+ }
+ if (x)
+ mp_drop(x);
+ if (MP_CMP(a, >=, c->mm.m))
+ mp_div(0, &a, a, c->mm.m);
+ return (a);
+}
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+static int verify(size_t n, dstr *v)
+{
+ mpcrt_mod *m = xmalloc(n * sizeof(mpcrt_mod));
+ mp **r = xmalloc(n * sizeof(mp *));
+ mpcrt c;
+ mp *a, *b;
+ size_t i;
+ int ok = 1;
+
+ for (i = 0; i < n; i++) {
+ r[i] = *(mp **)v[2 * i].buf;
+ m[i].m = *(mp **)v[2 * i + 1].buf;
+ m[i].n = 0;
+ m[i].ni = 0;
+ m[i].nnir = 0;
+ }
+ a = *(mp **)v[2 * n].buf;
+
+ mpcrt_create(&c, m, n, 0);
+ b = mpcrt_solve(&c, r);
+
+ if (MP_CMP(a, !=, b)) {
+ fputs("\n*** failed\n", stderr);
+ fputs("n = ", stderr);
+ mp_writefile(c.mm.m, stderr, 10);
+ for (i = 0; i < n; i++) {
+ fprintf(stderr, "\nr[%u] = ", i);
+ mp_writefile(r[i], stderr, 10);
+ fprintf(stderr, "\nm[%u] = ", i);
+ mp_writefile(m[i].m, stderr, 10);
+ fprintf(stderr, "\nN[%u] = ", i);
+ mp_writefile(m[i].n, stderr, 10);
+ fprintf(stderr, "\nM[%u] = ", i);
+ mp_writefile(m[i].ni, stderr, 10);
+ }
+ fputs("\nresult = ", stderr);
+ mp_writefile(b, stderr, 10);
+ fputs("\nexpect = ", stderr);
+ mp_writefile(a, stderr, 10);
+ fputc('\n', stderr);
+ ok = 0;
+ }
+
+ mp_drop(a);
+ mp_drop(b);
+ mpcrt_destroy(&c);
+ free(m);
+ free(r);
+ return (ok);
+}
+
+static int crt1(dstr *v) { return verify(1, v); }
+static int crt2(dstr *v) { return verify(2, v); }
+static int crt3(dstr *v) { return verify(3, v); }
+static int crt4(dstr *v) { return verify(4, v); }
+static int crt5(dstr *v) { return verify(5, v); }
+
+static test_chunk tests[] = {
+ { "crt-1", crt1, { &type_mp, &type_mp,
+ &type_mp, 0 } },
+ { "crt-2", crt2, { &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, 0 } },
+ { "crt-3", crt3, { &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, 0 } },
+ { "crt-4", crt4, { &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, 0 } },
+ { "crt-5", crt5, { &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, &type_mp,
+ &type_mp, 0 } },
+ { 0, 0, { 0 } }
+};
+
+int main(int argc, char *argv[])
+{
+ sub_init();
+ test_run(argc, argv, tests, SRCDIR "/tests/mpcrt");
+ return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/
--- /dev/null
+/* -*-c-*-
+ *
+ * $Id: mpcrt.h,v 1.1 1999/11/22 20:50:57 mdw Exp $
+ *
+ * Chinese Remainder Theorem computations (Gauss's algorithm)
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: mpcrt.h,v $
+ * Revision 1.1 1999/11/22 20:50:57 mdw
+ * Add support for solving Chinese Remainder Theorem problems.
+ *
+ */
+
+#ifndef MPCRT_H
+#define MPCRT_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+#ifndef MP_H
+# include "mp.h"
+#endif
+
+#ifndef MPMONT_H
+# include "mpmont.h"
+#endif
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct mpcrt_mod {
+ mp *m; /* %$n_i$% -- the modulus */
+ mp *n; /* %$N_i = n / n_i$% */
+ mp *ni; /* %$M_i = N_i^{-1} \bmod n_i$% */
+ mp *nnir; /* %$N_i M_i R \bmod m$% */
+} mpcrt_mod;
+
+typedef struct mpcrt {
+ size_t k; /* Number of distinct moduli */
+ mpmont mm; /* Montgomery context for product */
+ mpcrt_mod *v; /* Vector of information for each */
+} mpcrt;
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @mpcrt_create@ --- *
+ *
+ * Arguments: @mpcrt *c@ = pointer to CRT context
+ * @mpcrt_mod *v@ = pointer to vector of moduli
+ * @size_t k@ = number of moduli
+ * @mp *n@ = product of all moduli (@MP_NEW@ if unknown)
+ *
+ * Returns: ---
+ *
+ * Use: Initializes a context for solving Chinese Remainder Theorem
+ * problems. The vector of moduli can be incomplete. Omitted
+ * items must be left as null pointers. Not all combinations of
+ * missing things can be coped with, even if there is
+ * technically enough information to cope. For example, if @n@
+ * is unspecified, all the @m@ values must be present, even if
+ * there is one modulus with both @m@ and @n@ (from which the
+ * product of all moduli could clearly be calculated).
+ */
+
+extern void mpcrt_create(mpcrt */*c*/, mpcrt_mod */*v*/,
+ size_t /*k*/, mp */*n*/);
+
+/* --- @mpcrt_destroy@ --- *
+ *
+ * Arguments: @mpcrt *c@ - pointer to CRT context
+ *
+ * Returns: ---
+ *
+ * Use: Destroys a CRT context, releasing all the resources it holds.
+ */
+
+extern void mpcrt_destroy(mpcrt */*c*/);
+
+/* --- @mpcrt_solve@ --- *
+ *
+ * Arguments: @mpcrt *c@ = pointer to CRT context
+ * @mp **v@ = array of residues
+ *
+ * Returns: The unique solution modulo the product of the individual
+ * moduli, which leaves the given residues.
+ *
+ * Use: Constructs a result given its residue modulo an array of
+ * coprime integers. This can be used to improve performance of
+ * RSA encryption or Blum-Blum-Shub generation if the factors
+ * of the modulus are known, since results can be computed mod
+ * each of the individual factors and then combined at the end.
+ * This is rather faster than doing the full-scale modular
+ * exponentiation.
+ */
+
+extern mp *mpcrt_solve(mpcrt */*c*/, mp **/*v*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif