A variety of small tweaks and fixes. Make mpmont etc. return errors
[u/mdw/catacomb] / mpmont.c
index 24fc9a3..e62678d 100644 (file)
--- a/mpmont.c
+++ b/mpmont.c
@@ -49,7 +49,7 @@
  * Arguments:  @mpmont *mm@ = pointer to Montgomery reduction context
  *             @mp *m@ = modulus to use
  *
  * 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.
  *
  * Use:                Initializes a Montgomery reduction context ready for use.
  *             The argument @m@ must be a positive odd integer.
 
 #ifdef MPMONT_DISABLE
 
 
 #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;
 {
   mp_shrink(m);
   mm->m = MP_COPY(m);
   mm->r = MP_ONE;
   mm->r2 = MP_ONE;
   mm->mi = MP_ONE;
+  return (0);
 }
 
 #else
 
 }
 
 #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);
 {
   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 --- */
 
 
   /* --- Take a copy of the modulus --- */
 
-  assert(MP_POSP(m) && MP_ODDP(m));
+ if (!MP_POSP(m) || !MP_ODDP(m))
+   return (-1);
   mm->m = MP_COPY(m);
 
   /* --- Determine %$R^2$% --- */
   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);
   mp_div(0, &mm->r2, r2, m);
   mm->r = mpmont_reduce(mm, MP_NEW, mm->r2);
   MP_DROP(r2);
+  return (0);
 }
 
 #endif
 }
 
 #endif