Karatsuba squaring algorithm.
[u/mdw/catacomb] / mpx-ksqr.c
diff --git a/mpx-ksqr.c b/mpx-ksqr.c
new file mode 100644 (file)
index 0000000..45c49c9
--- /dev/null
@@ -0,0 +1,259 @@
+/* -*-c-*-
+ *
+ * $Id: mpx-ksqr.c,v 1.1 1999/12/11 10:57:43 mdw Exp $
+ *
+ * Karatsuba-based squaring 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: mpx-ksqr.c,v $
+ * Revision 1.1  1999/12/11 10:57:43  mdw
+ * Karatsuba squaring algorithm.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdio.h>
+
+#include "mpx.h"
+
+/*----- Tweakables --------------------------------------------------------*/
+
+#ifdef TEST_RIG
+#  undef KARATSUBA_CUTOFF
+#  define KARATSUBA_CUTOFF 2
+#endif
+
+/*----- Addition macros ---------------------------------------------------*/
+
+#define ULSL1(dv, av, avl) do {                                                \
+  mpw *_dv = (dv);                                                     \
+  const mpw *_av = (av), *_avl = (avl);                                        \
+  mpw _c = 0;                                                          \
+                                                                       \
+  while (_av < _avl) {                                                 \
+    mpw _x = *_av++;                                                   \
+    *_dv++ = MPW(_c | (_x << 1));                                      \
+    _c = MPW(_x >> (MPW_BITS - 1));                                    \
+  }                                                                    \
+  *_dv++ = _c;                                                         \
+} while (0)
+
+#define UADD(dv, av, avl) do {                                         \
+  mpw *_dv = (dv);                                                     \
+  const mpw *_av = (av), *_avl = (avl);                                        \
+  mpw _c = 0;                                                          \
+                                                                       \
+  while (_av < _avl) {                                                 \
+    mpw _a, _b;                                                                \
+    mpd _x;                                                            \
+    _a = *_av++;                                                       \
+    _b = *_dv;                                                         \
+    _x = (mpd)_a + (mpd)_b + _c;                                       \
+    *_dv++ = MPW(_x);                                                  \
+    _c = _x >> MPW_BITS;                                               \
+  }                                                                    \
+  while (_c) {                                                         \
+    mpd _x = (mpd)*_dv + (mpd)_c;                                      \
+    *_dv++ = MPW(_x);                                                  \
+    _c = _x >> MPW_BITS;                                               \
+  }                                                                    \
+} while (0)
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @mpx_ksqr@ --- *
+ *
+ * Arguments:  @mpw *dv, *dvl@ = pointer to destination buffer
+ *             @const mpw *av, *avl@ = pointer to first argument
+ *             @mpw *sv, *svl@ = pointer to scratch workspace
+ *
+ * Returns:    ---
+ *
+ * Use:                Squares a multiprecision integers using something similar to
+ *             Karatsuba's multiplication algorithm.  This is rather faster
+ *             than traditional long multiplication (e.g., @mpx_umul@) on
+ *             large numbers, although more expensive on small ones, and
+ *             rather simpler than full-blown Karatsuba multiplication.
+ *
+ *             The destination must be twice as large as the argument.  The
+ *             scratch space must be twice as large as the argument, plus
+ *             the magic number @KARATSUBA_SLOP@.
+ */
+
+void mpx_ksqr(mpw *dv, mpw *dvl,
+             const mpw *av, const mpw *avl,
+             mpw *sv, mpw *svl)
+{
+  const mpw *avm;
+  size_t m;
+
+  /* --- Dispose of easy cases to @mpx_usqr@ --- *
+   *
+   * Karatsuba is only a win on large numbers, because of all the
+   * recursiveness and bookkeeping.  The recursive calls make a quick check
+   * to see whether to bottom out to @mpx_usqr@ which should help quite a
+   * lot, but sometimes the only way to know is to make sure...
+   */
+
+  MPX_SHRINK(av, avl);
+
+  if (avl - av <= KARATSUBA_CUTOFF) {
+    mpx_usqr(dv, dvl, av, avl);
+    return;
+  }
+
+  /* --- How the algorithm works --- *
+   *
+   * Unlike Karatsuba's identity for multiplication which isn't particularly
+   * obvious, the identity for multiplication is known to all schoolchildren.
+   * Let %$A = xb + y$%.  Then %$A^2 = x^2 b^x + 2 x y b + y^2$%.  So now I
+   * have three multiplications, each four times easier, and that's a win.
+   */
+
+  /* --- First things --- *
+   *
+   * Sort out where to break the factor in half.
+   */
+
+  m = (avl - av + 1) >> 1;
+  avm = av + m;
+
+  /* --- Sort out everything --- */
+
+  {
+    mpw *ssv = sv + 2 * m;
+    mpw *tdv = dv + m;
+    mpw *rdv = tdv + m;
+
+    /* --- The cross term in the middle needs a multiply --- *
+     *
+     * This isn't actually true, since %$x y = ((x + y)^2 - (x - y)^2)/4%.
+     * But that's two squarings, versus one multiplication.
+     */
+
+    if (m > KARATSUBA_CUTOFF)
+      mpx_kmul(sv, ssv, av, avm, avm, avl, ssv, svl);
+    else
+      mpx_umul(sv, ssv, av, avm, avm, avl);
+    ULSL1(tdv, sv, ssv);
+    MPX_ZERO(dv, tdv);
+    MPX_ZERO(rdv + m + 1, dvl);
+
+    if (m > KARATSUBA_CUTOFF)
+      mpx_ksqr(sv, ssv, avm, avl, ssv, svl);
+    else
+      mpx_usqr(sv, ssv, avm, avl);
+    UADD(rdv, sv, ssv);
+    
+    if (m > KARATSUBA_CUTOFF)
+      mpx_ksqr(sv, ssv, av, avm, ssv, svl);
+    else
+      mpx_usqr(sv, ssv, av, avm);
+    UADD(dv, sv, ssv);
+  }
+}
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+#include <mLib/alloc.h>
+#include <mLib/testrig.h>
+
+#include "mpscan.h"
+
+#define ALLOC(v, vl, sz) do {                                           \
+  size_t _sz = (sz);                                                    \
+  mpw *_vv = xmalloc(MPWS(_sz));                                        \
+  mpw *_vvl = _vv + _sz;                                                \
+  (v) = _vv;                                                            \
+  (vl) = _vvl;                                                          \
+} while (0)
+
+#define LOAD(v, vl, d) do {                                             \
+  const dstr *_d = (d);                                                 \
+  mpw *_v, *_vl;                                                        \
+  ALLOC(_v, _vl, MPW_RQ(_d->len));                                      \
+  mpx_loadb(_v, _vl, _d->buf, _d->len);                                 \
+  (v) = _v;                                                             \
+  (vl) = _vl;                                                           \
+} while (0)
+
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+
+static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
+{
+  fputs(msg, stderr);
+  MPX_SHRINK(v, vl);
+  while (v < vl)
+    fprintf(stderr, " %08lx", (unsigned long)*--vl);
+  fputc('\n', stderr);
+}
+
+static int usqr(dstr *v)
+{
+  mpw *a, *al;
+  mpw *c, *cl;
+  mpw *d, *dl;
+  mpw *s, *sl;
+  size_t m;
+  int ok = 1;
+
+  LOAD(a, al, &v[0]);
+  LOAD(c, cl, &v[1]);
+  m = al - a + 1;
+  ALLOC(d, dl, 2 * m);
+  ALLOC(s, sl, 2 * m + 32);
+
+  mpx_ksqr(d, dl, a, al, s, sl);
+  if (MPX_UCMP(d, dl, !=, c, cl)) {
+    fprintf(stderr, "\n*** usqr failed\n");
+    dumpmp("       a", a, al);
+    dumpmp("expected", c, cl);
+    dumpmp("  result", d, dl);
+    ok = 0;
+  }
+
+  free(a); free(c); free(d); free(s);
+  return (ok);
+}
+
+static test_chunk defs[] = {
+  { "usqr", usqr, { &type_hex, &type_hex, 0 } },
+  { 0, 0, { 0 } }
+};
+
+int main(int argc, char *argv[])
+{
+  test_run(argc, argv, defs, SRCDIR"/tests/mpx");
+  return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/