catcrypt.c, catsign.c: Shorten chunk sizes.
[u/mdw/catacomb] / mpmont.c
index 39f51ed..65b3fcc 100644 (file)
--- a/mpmont.c
+++ b/mpmont.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: mpmont.c,v 1.19 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
  *
  * Montgomery reduction
  *
  * (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,
@@ -49,7 +49,7 @@
  * Arguments:  @mpmont *mm@ = pointer to Montgomery reduction context
  *             @mp *m@ = modulus to use
  *
- * Returns:    ---
+ * Returns:    Zero on success, nonzero on error.
  *
  * Use:                Initializes a Montgomery reduction context ready for use.
  *             The argument @m@ must be a positive odd integer.
 
 #ifdef MPMONT_DISABLE
 
-void mpmont_create(mpmont *mm, mp *m)
+int mpmont_create(mpmont *mm, mp *m)
 {
   mp_shrink(m);
   mm->m = MP_COPY(m);
   mm->r = MP_ONE;
   mm->r2 = MP_ONE;
   mm->mi = MP_ONE;
+  return (0);
 }
 
 #else
 
-void mpmont_create(mpmont *mm, mp *m)
+int mpmont_create(mpmont *mm, mp *m)
 {
   size_t n = MP_LEN(m);
   mp *r2 = mp_new(2 * n + 1, 0);
@@ -76,7 +77,8 @@ void mpmont_create(mpmont *mm, mp *m)
 
   /* --- Take a copy of the modulus --- */
 
-  assert(MP_ISPOS(m) && MP_ISODD(m));
+ if (!MP_POSP(m) || !MP_ODDP(m))
+   return (-1);
   mm->m = MP_COPY(m);
 
   /* --- Determine %$R^2$% --- */
@@ -97,6 +99,7 @@ void mpmont_create(mpmont *mm, mp *m)
   mp_div(0, &mm->r2, r2, m);
   mm->r = mpmont_reduce(mm, MP_NEW, mm->r2);
   MP_DROP(r2);
+  return (0);
 }
 
 #endif
@@ -327,7 +330,7 @@ static int tcreate(dstr *v)
     fputs("\n*** bad r", stderr);
     fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
     fputs("\nexpected ", stderr); mp_writefile(r, stderr, 10);
-    fputs("\n   found ", stderr); mp_writefile(mm.r, stderr, 10);
+    fputs("\n  found ", stderr); mp_writefile(mm.r, stderr, 10);
     fputc('\n', stderr);
     ok = 0;
   }
@@ -336,7 +339,7 @@ static int tcreate(dstr *v)
     fputs("\n*** bad r2", stderr);
     fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
     fputs("\nexpected ", stderr); mp_writefile(r2, stderr, 10);
-    fputs("\n   found ", stderr); mp_writefile(mm.r2, stderr, 10);
+    fputs("\n  found ", stderr); mp_writefile(mm.r2, stderr, 10);
     fputc('\n', stderr);
     ok = 0;
   }