dsig.c: Allow precomputed hashes to be read from a file.
[u/mdw/catacomb] / mpx.c
diff --git a/mpx.c b/mpx.c
index 4097b3e..29e1ded 100644 (file)
--- a/mpx.c
+++ b/mpx.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: mpx.c,v 1.12 2002/10/06 22:52:50 mdw Exp $
+ * $Id$
  *
  * Low-level multiprecision arithmetic
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * 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.c,v $
- * Revision 1.12  2002/10/06 22:52:50  mdw
- * Pile of changes for supporting two's complement properly.
- *
- * Revision 1.11  2001/04/03 19:36:05  mdw
- * Add some simple bitwise operations so that Perl can use them.
- *
- * Revision 1.10  2000/10/08 12:06:12  mdw
- * Provide @mpx_ueq@ for rapidly testing equality of two integers.
- *
- * Revision 1.9  2000/06/26 07:52:50  mdw
- * Portability fix for the bug fix.
- *
- * Revision 1.8  2000/06/25 12:59:02  mdw
- * (mpx_udiv): Fix bug in quotient digit estimation.
- *
- * Revision 1.7  1999/12/22 15:49:07  mdw
- * New function for division by a small integer.
- *
- * Revision 1.6  1999/11/20 22:43:44  mdw
- * Integrate testing for MPX routines.
- *
- * Revision 1.5  1999/11/20 22:23:27  mdw
- * Add function versions of some low-level macros with wider use.
- *
- * Revision 1.4  1999/11/17 18:04:09  mdw
- * Add two's-complement functionality.  Improve mpx_udiv a little by
- * performing the multiplication of the divisor by q with the subtraction
- * from r.
- *
- * Revision 1.3  1999/11/13 01:57:31  mdw
- * Remove stray debugging code.
- *
- * Revision 1.2  1999/11/13 01:50:59  mdw
- * Multiprecision routines finished and tested.
- *
- * Revision 1.1  1999/09/03 08:41:12  mdw
- * Initial import.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <assert.h>
@@ -81,6 +38,7 @@
 
 #include "mptypes.h"
 #include "mpx.h"
+#include "bitops.h"
 
 /*----- Loading and storing -----------------------------------------------*/
 
@@ -274,12 +232,12 @@ void mpx_storel2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
       bits -= 8;
     }
     b = U8(~b + c);
-    c = !b;
+    c = c && !b;
     *p++ = b;
   }
   while (p < q) {
     b = U8(~b + c);
-    c = !b;
+    c = c && !b;
     *p++ = b;
     b = 0;
   }
@@ -311,7 +269,7 @@ void mpx_loadl2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
     return;
   while (p < q) {
     n = U8(~(*p++) + c);
-    c = !n;
+    c = c && !n;
     w |= n << bits;
     bits += 8;
     if (bits >= MPW_BITS) {
@@ -364,12 +322,12 @@ void mpx_storeb2cn(const mpw *v, const mpw *vl, void *pp, size_t sz)
       bits -= 8;
     }
     b = U8(~b + c);
-    c = !b;
+    c = c && !b;
     *--q = b;
   }
   while (q > p) {
     b = ~b + c;
-    c = !(b & 0xff);
+    c = c && !(b & 0xff);
     *--q = b;
     b = 0;
   }
@@ -401,7 +359,7 @@ void mpx_loadb2cn(mpw *v, mpw *vl, const void *pp, size_t sz)
     return;
   while (q > p) {
     n = U8(~(*--q) + c);
-    c = !n;
+    c = c && !n;
     w |= n << bits;
     bits += 8;
     if (bits >= MPW_BITS) {
@@ -459,15 +417,19 @@ void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
   }
 
   /* --- Break out word and bit shifts for more sophisticated work --- */
-       
+
   nw = n / MPW_BITS;
   nb = n % MPW_BITS;
 
   /* --- Handle a shift by a multiple of the word size --- */
 
   if (nb == 0) {
-    MPX_COPY(dv + nw, dvl, av, avl);
-    memset(dv, 0, MPWS(nw));
+    if (nw >= dvl - dv)
+      MPX_ZERO(dv, dvl);
+    else {
+      MPX_COPY(dv + nw, dvl, av, avl);
+      memset(dv, 0, MPWS(nw));
+    }
   }
 
   /* --- And finally the difficult case --- *
@@ -500,12 +462,110 @@ void mpx_lsl(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
 
     while (avl > av) {
       mpw t = *--avl;
-      *--dvl = (t >> nr) | w;
+      *--dvl = MPW((t >> nr) | w);
       w = t << nb;
     }
 
-    *--dvl = w;
+    *--dvl = MPW(w);
+    MPX_ZERO(dv, dvl);
+  }
+
+done:;
+}
+
+/* --- @mpx_lslc@ --- *
+ *
+ * Arguments:  @mpw *dv, *dvl@ = destination vector base and limit
+ *             @const mpw *av, *avl@ = source vector base and limit
+ *             @size_t n@ = number of bit positions to shift by
+ *
+ * Returns:    ---
+ *
+ * Use:                Performs a logical shift left operation on an integer, only
+ *             it fills in the bits with ones instead of zeroes.
+ */
+
+void mpx_lslc(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
+{
+  size_t nw;
+  unsigned nb;
+
+  /* --- Trivial special case --- */
+
+  if (n == 0)
+    MPX_COPY(dv, dvl, av, avl);
+
+  /* --- Single bit shifting --- */
+
+  else if (n == 1) {
+    mpw w = 1;
+    while (av < avl) {
+      mpw t;
+      if (dv >= dvl)
+       goto done;
+      t = *av++;
+      *dv++ = MPW((t << 1) | w);
+      w = t >> (MPW_BITS - 1);
+    }
+    if (dv >= dvl)
+      goto done;
+    *dv++ = MPW(w);
     MPX_ZERO(dv, dvl);
+    goto done;
+  }
+
+  /* --- Break out word and bit shifts for more sophisticated work --- */
+
+  nw = n / MPW_BITS;
+  nb = n % MPW_BITS;
+
+  /* --- Handle a shift by a multiple of the word size --- */
+
+  if (nb == 0) {
+    if (nw >= dvl - dv)
+      MPX_ONE(dv, dvl);
+    else {
+      MPX_COPY(dv + nw, dvl, av, avl);
+      MPX_ONE(dv, dv + nw);
+    }
+  }
+
+  /* --- And finally the difficult case --- *
+   *
+   * This is a little convoluted, because I have to start from the end and
+   * work backwards to avoid overwriting the source, if they're both the same
+   * block of memory.
+   */
+
+  else {
+    mpw w;
+    size_t nr = MPW_BITS - nb;
+    size_t dvn = dvl - dv;
+    size_t avn = avl - av;
+
+    if (dvn <= nw) {
+      MPX_ONE(dv, dvl);
+      goto done;
+    }
+
+    if (dvn > avn + nw) {
+      size_t off = avn + nw + 1;
+      MPX_ZERO(dv + off, dvl);
+      dvl = dv + off;
+      w = 0;
+    } else {
+      avl = av + dvn - nw;
+      w = *--avl << nb;
+    }
+
+    while (avl > av) {
+      mpw t = *--avl;
+      *--dvl = MPW((t >> nr) | w);
+      w = t << nb;
+    }
+
+    *--dvl = MPW((MPW_MAX >> nr) | w);
+    MPX_ONE(dv, dvl);
   }
 
 done:;
@@ -535,7 +595,7 @@ void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
   /* --- Single bit shifting --- */
 
   else if (n == 1) {
-    mpw w = *av++ >> 1;
+    mpw w = av < avl ? *av++ >> 1 : 0;
     while (av < avl) {
       mpw t;
       if (dv >= dvl)
@@ -558,8 +618,12 @@ void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
 
   /* --- Handle a shift by a multiple of the word size --- */
 
-  if (nb == 0)
-    MPX_COPY(dv, dvl, av + nw, avl);
+  if (nb == 0) {
+    if (nw >= avl - av)
+      MPX_ZERO(dv, dvl);
+    else
+      MPX_COPY(dv, dvl, av + nw, avl);
+  }
 
   /* --- And finally the difficult case --- */
 
@@ -568,7 +632,7 @@ void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n)
     size_t nr = MPW_BITS - nb;
 
     av += nw;
-    w = *av++;
+    w = av < avl ? *av++ : 0;
     while (av < avl) {
       mpw t;
       if (dv >= dvl)
@@ -611,7 +675,7 @@ void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,      \
     mpw a, b;                                                          \
     a = (av < avl) ? *av++ : 0;                                                \
     b = (bv < bvl) ? *bv++ : 0;                                                \
-    *dv++ = MPX_B##string(a, b);                                       \
+    *dv++ = B##string(a, b);                                           \
   }                                                                    \
 }
 
@@ -758,6 +822,30 @@ void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
 
 void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); }
 
+/* --- @mpx_uaddnlsl@ --- *
+ *
+ * Arguments:  @mpw *dv, *dvl@ = destination and first argument vector
+ *             @mpw a@ = second argument
+ *             @unsigned o@ = offset in bits
+ *
+ * Returns:    ---
+ *
+ * Use:                Computes %$d + 2^o a$%.  If the result overflows then
+ *             high-order bits are discarded, as usual.  We must have
+ *             @0 < o < MPW_BITS@.
+ */
+
+void mpx_uaddnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
+{
+  mpd x = (mpd)a << o;
+
+  while (x && dv < dvl) {
+    x += *dv;
+    *dv++ = MPW(x);
+    x >>= MPW_BITS;
+  }
+}
+
 /* --- @mpx_usub@ --- *
  *
  * Arguments:  @mpw *dv, *dvl@ = destination vector base and limit
@@ -812,6 +900,33 @@ void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
 
 void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); }
 
+/* --- @mpx_uaddnlsl@ --- *
+ *
+ * Arguments:  @mpw *dv, *dvl@ = destination and first argument vector
+ *             @mpw a@ = second argument
+ *             @unsigned o@ = offset in bits
+ *
+ * Returns:    ---
+ *
+ * Use:                Computes %$d + 2^o a$%.  If the result overflows then
+ *             high-order bits are discarded, as usual.  We must have
+ *             @0 < o < MPW_BITS@.
+ */
+
+void mpx_usubnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o)
+{
+  mpw b = a >> (MPW_BITS - o);
+  a <<= o;
+
+  if (dv < dvl) {
+    mpd x = (mpd)*dv - MPW(a);
+    *dv++ = MPW(x);
+    if (x >> MPW_BITS)
+      b++;
+    MPX_USUBN(dv, dvl, b);
+  }
+}
+
 /* --- @mpx_umul@ --- *
  *
  * Arguments:  @mpw *dv, *dvl@ = destination vector base and limit
@@ -835,7 +950,7 @@ void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
   MPX_SHRINK(bv, bvl);
 
   /* --- Deal with a multiply by zero --- */
-  
+
   if (bv == bvl) {
     MPX_ZERO(dv, dvl);
     return;
@@ -880,9 +995,7 @@ void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl,
  */
 
 void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
-{
-  MPX_UMULN(dv, dvl, av, avl, m);
-}
+  { MPX_UMULN(dv, dvl, av, avl, m); }
 
 /* --- @mpx_umlan@ --- *
  *
@@ -897,9 +1010,7 @@ void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
  */
 
 void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m)
-{
-  MPX_UMLAN(dv, dvl, av, avl, m);
-}
+  { MPX_UMLAN(dv, dvl, av, avl, m); }
 
 /* --- @mpx_usqr@ --- *
  *
@@ -1005,8 +1116,8 @@ void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl,
     unsigned b;
 
     d = dvl[-1];
-    for (b = MPW_BITS / 2; b; b >>= 1) {
-      if (d < (MPW_MAX >> b)) {
+    for (b = MPW_P2; b; b >>= 1) {
+      if (d <= (MPW_MAX >> b)) {
        d <<= b;
        norm += b;
       }
@@ -1219,7 +1330,7 @@ mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d)
 } while (0)
 
 #define MAX(x, y) ((x) > (y) ? (x) : (y))
-  
+
 static void dumpbits(const char *msg, const void *pp, size_t sz)
 {
   const octet *p = pp;
@@ -1306,7 +1417,7 @@ static int loadstore(dstr *v)
   if (!ok)
     dumpbits("input data", v->buf, v->len);
 
-  free(m);
+  xfree(m);
   dstr_destroy(&d);
   return (ok);
 }
@@ -1344,7 +1455,7 @@ static int twocl(dstr *v)
     dumpbits("neg", v[1].buf, v[1].len);
   }
 
-  free(m);
+  xfree(m);
   dstr_destroy(&d);
 
   return (ok);
@@ -1383,7 +1494,7 @@ static int twocb(dstr *v)
     dumpbits("neg", v[1].buf, v[1].len);
   }
 
-  free(m);
+  xfree(m);
   dstr_destroy(&d);
 
   return (ok);
@@ -1404,13 +1515,38 @@ static int lsl(dstr *v)
   mpx_lsl(d, dl, a, al, n);
   if (!mpx_ueq(d, dl, c, cl)) {
     fprintf(stderr, "\n*** lsl(%i) failed\n", n);
-    dumpmp("       a", a, al);
+    dumpmp("      a", a, al);
+    dumpmp("expected", c, cl);
+    dumpmp("  result", d, dl);
+    ok = 0;
+  }
+
+  xfree(a); xfree(c); xfree(d);
+  return (ok);
+}
+
+static int lslc(dstr *v)
+{
+  mpw *a, *al;
+  int n = *(int *)v[1].buf;
+  mpw *c, *cl;
+  mpw *d, *dl;
+  int ok = 1;
+
+  LOAD(a, al, &v[0]);
+  LOAD(c, cl, &v[2]);
+  ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS);
+
+  mpx_lslc(d, dl, a, al, n);
+  if (!mpx_ueq(d, dl, c, cl)) {
+    fprintf(stderr, "\n*** lslc(%i) failed\n", n);
+    dumpmp("      a", a, al);
     dumpmp("expected", c, cl);
     dumpmp("  result", d, dl);
     ok = 0;
   }
 
-  free(a); free(c); free(d);
+  xfree(a); xfree(c); xfree(d);
   return (ok);
 }
 
@@ -1429,13 +1565,13 @@ static int lsr(dstr *v)
   mpx_lsr(d, dl, a, al, n);
   if (!mpx_ueq(d, dl, c, cl)) {
     fprintf(stderr, "\n*** lsr(%i) failed\n", n);
-    dumpmp("       a", a, al);
+    dumpmp("      a", a, al);
     dumpmp("expected", c, cl);
     dumpmp("  result", d, dl);
     ok = 0;
   }
 
-  free(a); free(c); free(d);
+  xfree(a); xfree(c); xfree(d);
   return (ok);
 }
 
@@ -1455,14 +1591,14 @@ static int uadd(dstr *v)
   mpx_uadd(d, dl, a, al, b, bl);
   if (!mpx_ueq(d, dl, c, cl)) {
     fprintf(stderr, "\n*** uadd failed\n");
-    dumpmp("       a", a, al);
-    dumpmp("       b", b, bl);
+    dumpmp("      a", a, al);
+    dumpmp("      b", b, bl);
     dumpmp("expected", c, cl);
     dumpmp("  result", d, dl);
     ok = 0;
   }
 
-  free(a); free(b); free(c); free(d);
+  xfree(a); xfree(b); xfree(c); xfree(d);
   return (ok);
 }
 
@@ -1482,14 +1618,14 @@ static int usub(dstr *v)
   mpx_usub(d, dl, a, al, b, bl);
   if (!mpx_ueq(d, dl, c, cl)) {
     fprintf(stderr, "\n*** usub failed\n");
-    dumpmp("       a", a, al);
-    dumpmp("       b", b, bl);
+    dumpmp("      a", a, al);
+    dumpmp("      b", b, bl);
     dumpmp("expected", c, cl);
     dumpmp("  result", d, dl);
     ok = 0;
   }
 
-  free(a); free(b); free(c); free(d);
+  xfree(a); xfree(b); xfree(c); xfree(d);
   return (ok);
 }
 
@@ -1509,14 +1645,14 @@ static int umul(dstr *v)
   mpx_umul(d, dl, a, al, b, bl);
   if (!mpx_ueq(d, dl, c, cl)) {
     fprintf(stderr, "\n*** umul failed\n");
-    dumpmp("       a", a, al);
-    dumpmp("       b", b, bl);
+    dumpmp("      a", a, al);
+    dumpmp("      b", b, bl);
     dumpmp("expected", c, cl);
     dumpmp("  result", d, dl);
     ok = 0;
   }
 
-  free(a); free(b); free(c); free(d);
+  xfree(a); xfree(b); xfree(c); xfree(d);
   return (ok);
 }
 
@@ -1534,13 +1670,13 @@ static int usqr(dstr *v)
   mpx_usqr(d, dl, a, al);
   if (!mpx_ueq(d, dl, c, cl)) {
     fprintf(stderr, "\n*** usqr failed\n");
-    dumpmp("       a", a, al);
+    dumpmp("      a", a, al);
     dumpmp("expected", c, cl);
     dumpmp("  result", d, dl);
     ok = 0;
   }
 
-  free(a); free(c); free(d);
+  xfree(a); xfree(c); xfree(d);
   return (ok);
 }
 
@@ -1573,7 +1709,7 @@ static int udiv(dstr *v)
     ok = 0;
   }
 
-  free(a); free(b); free(r); free(q); free(s); free(qq);
+  xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq);
   return (ok);
 }
 
@@ -1582,6 +1718,7 @@ static test_chunk defs[] = {
   { "2cl", twocl, { &type_hex, &type_hex, } },
   { "2cb", twocb, { &type_hex, &type_hex, } },
   { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } },
+  { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } },
   { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } },
   { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } },
   { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } },