Remove a division from the Montgomery setup by calculating
[u/mdw/catacomb] / mpmont.c
index 66b7657..35bffc2 100644 (file)
--- a/mpmont.c
+++ b/mpmont.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mpmont.c,v 1.3 1999/11/21 11:35:10 mdw Exp $
+ * $Id: mpmont.c,v 1.4 1999/11/21 12:27:06 mdw Exp $
  *
  * Montgomery reduction
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mpmont.c,v $
+ * Revision 1.4  1999/11/21 12:27:06  mdw
+ * Remove a division from the Montgomery setup by calculating
+ * %$R^2 \bmod m$% first and then %$R \bmod m$% by Montgomery reduction of
+ * %$R^2$%.
+ *
  * Revision 1.3  1999/11/21 11:35:10  mdw
  * Performance improvement: use @mp_sqr@ and @mpmont_reduce@ instead of
  * @mpmont_mul@ for squaring in exponentiation.
@@ -92,15 +97,14 @@ void mpmont_create(mpmont *mm, mp *m)
 
   {
     size_t l = MP_LEN(m);
-    mp *r = mp_create(l + 1);
+    mp *r = mp_create(2 * l + 1);
 
     mm->shift = l * MPW_BITS;
     MPX_ZERO(r->v, r->vl - 1);
     r->vl[-1] = 1;
-    mm->r = mm->r2 = MP_NEW;
-    mp_div(0, &mm->r, r, m);
-    r = mp_sqr(r, mm->r);
+    mm->r2 = MP_NEW;
     mp_div(0, &mm->r2, r, m);
+    mm->r = mpmont_reduce(mm, MP_NEW, mm->r2);
     MP_DROP(r);
   }
 }