/* -*-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,
* 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);
/* --- 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$% --- */
mp_div(0, &mm->r2, r2, m);
mm->r = mpmont_reduce(mm, MP_NEW, mm->r2);
MP_DROP(r2);
+ return (0);
}
#endif
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;
}
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;
}