.links: Drop obsolete `lib-config.in' file.
[u/mdw/catacomb] / mpmont-mexp.c
index fabac3e..92f40c3 100644 (file)
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: mpmont-mexp.c,v 1.6 2001/06/16 13:00:20 mdw Exp $
+ * $Id$
  *
  * Multiple simultaneous exponentiations
  *
  * (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: mpmont-mexp.c,v $
- * Revision 1.6  2001/06/16 13:00:20  mdw
- * Use the generic exponentiation functions.
- *
- * Revision 1.5  2000/10/08 12:11:22  mdw
- * Use @MP_EQ@ instead of @MP_CMP@.
- *
- * Revision 1.4  2000/06/17 11:45:09  mdw
- * Major memory management overhaul.  Added arena support.  Use the secure
- * arena for secret integers.  Replace and improve the MP management macros
- * (e.g., replace MP_MODIFY by MP_DEST).
- *
- * Revision 1.3  1999/12/10 23:18:39  mdw
- * Change interface for suggested destinations.
- *
- * Revision 1.2  1999/11/21 11:35:10  mdw
- * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
- * @mpmont_mul@ for squaring in exponentiation.
- *
- * Revision 1.1  1999/11/19 13:19:29  mdw
- * Simultaneous exponentiation support.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "mp.h"
@@ -67,7 +41,7 @@
  *
  * Arguments:  @mpmont *mm@ = pointer to Montgomery reduction context
  *             @mp *d@ = fake destination
- *             @mp_expfactor *f@ = pointer to array of factors
+ *             @const mp_expfactor *f@ = pointer to array of factors
  *             @size_t n@ = number of factors supplied
  *
  * Returns:    If the bases are %$g_0, g_1, \ldots, g_{n-1}$% and the
  *             except that the %$g_i$% and result are in Montgomery form.
  */
 
-mp *mpmont_mexpr(mpmont *mm, mp *d, mp_expfactor *f, size_t n)
+static mp *mexpr(mpmont *mm, mp *d, mp_expfactor *f, size_t n)
 {
-  mp *a = mp_copy(mm->r);
-  mp *spare;
+  mp *a = MP_COPY(mm->r);
+  mp *spare = MP_NEW;
   size_t i;
 
-  spare = MP_NEW;
   for (i = 0; i < n; i++) {
-    if (f[i].exp->f & MP_BURN) {
+    mp *t;
+    if (f[i].exp->f & MP_BURN)
       spare = MP_NEWSEC;
-      break;
+    if (MP_NEGP(f[i].exp)) {
+      t = mpmont_reduce(mm, f[i].base, f[i].base);
+      t = mp_modinv(t, t, mm->m);
+      f[i].base = mpmont_mul(mm, t, t, mm->r2);
     }
   }
-
   EXP_SIMUL(a, f, n);
   mp_drop(d);
   mp_drop(spare);
+  for (i = 0; i < n; i++)
+    MP_DROP(f[i].base);
+  xfree(f);
   return (a);
 }
 
+mp *mpmont_mexpr(mpmont *mm, mp *d, const mp_expfactor *f, size_t n)
+{
+  mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor));
+  size_t i;
+
+  for (i = 0; i < n; i++) {
+    ff[i].base = MP_COPY(f[i].base);
+    ff[i].exp = f[i].exp;
+  }
+  return (mexpr(mm, d, ff, n));
+}
+
 /* --- @mpmont_mexp@ --- *
  *
  * Arguments:  @mpmont *mm@ = pointer to Montgomery reduction context
  *             @mp *d@ = fake destination
- *             @mp_expfactor *f@ = pointer to array of factors
+ *             @const mp_expfactor *f@ = pointer to array of factors
  *             @size_t n@ = number of factors supplied
  *
  * Returns:    Product of bases raised to exponents, all mod @m@.
@@ -111,21 +102,17 @@ mp *mpmont_mexpr(mpmont *mm, mp *d, mp_expfactor *f, size_t n)
  * Use:                Convenient interface over @mpmont_mexpr@.
  */
 
-mp *mpmont_mexp(mpmont *mm, mp *d, mp_expfactor *f, size_t n)
+mp *mpmont_mexp(mpmont *mm, mp *d, const mp_expfactor *f, size_t n)
 {
-  mp_expfactor *v = xmalloc(sizeof(*v) * n);
+  mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor));
   size_t i;
 
   for (i = 0; i < n; i++) {
-    v[i].base = mpmont_mul(mm, MP_NEW, f[i].base, mm->r2);
-    v[i].exp = f[i].exp;
+    ff[i].base = mpmont_mul(mm, MP_NEW, f[i].base, mm->r2);
+    ff[i].exp = f[i].exp;
   }
-  d = mpmont_mexpr(mm, d, v, n);
-  for (i = 0; i < n; i++)
-    MP_DROP(v[i].base);
-  xfree(v);
-  d = mpmont_reduce(mm, d, d);
-  return (d);
+  d = mexpr(mm, d, ff, n);
+  return (mpmont_reduce(mm, d, d));
 }
 
 /*----- Test rig ----------------------------------------------------------*/
@@ -219,7 +206,7 @@ int main(int argc, char *argv[])
   sub_init();
   test_run(argc, argv, tests, SRCDIR "/tests/mpmont");
   return (0);
-}   
+}
 
 #endif