2003-02-28 16:08:34 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomMath is library that provides for multiple-precision
|
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
|
|
|
* The library is designed directly after the MPI library by
|
|
|
|
* Michael Fromberger but has been written from scratch with
|
|
|
|
* additional optimizations in place.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
2003-03-13 02:11:11 +00:00
|
|
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
|
|
|
#include <tommath.h>
|
|
|
|
|
|
|
|
/* multiply by a digit */
|
|
|
|
int
|
|
|
|
mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
|
|
|
|
{
|
2003-02-28 16:09:08 +00:00
|
|
|
int res, pa, olduse;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
|
|
|
pa = a->used;
|
2003-02-28 16:09:08 +00:00
|
|
|
if (c->alloc < pa + 1) {
|
|
|
|
if ((res = mp_grow (c, pa + 1)) != MP_OKAY) {
|
|
|
|
return res;
|
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2003-02-28 16:09:08 +00:00
|
|
|
olduse = c->used;
|
|
|
|
c->used = pa + 1;
|
|
|
|
|
|
|
|
{
|
|
|
|
register mp_digit u, *tmpa, *tmpc;
|
|
|
|
register mp_word r;
|
|
|
|
register int ix;
|
|
|
|
|
|
|
|
tmpc = c->dp + c->used;
|
|
|
|
for (ix = c->used; ix < olduse; ix++) {
|
|
|
|
*tmpc++ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpa = a->dp;
|
|
|
|
tmpc = c->dp;
|
|
|
|
|
|
|
|
u = 0;
|
|
|
|
for (ix = 0; ix < pa; ix++) {
|
|
|
|
r = ((mp_word) u) + ((mp_word) * tmpa++) * ((mp_word) b);
|
|
|
|
*tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
|
|
|
|
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
|
|
|
|
}
|
|
|
|
*tmpc = u;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2003-02-28 16:09:08 +00:00
|
|
|
mp_clamp (c);
|
2003-02-28 16:08:34 +00:00
|
|
|
return MP_OKAY;
|
|
|
|
}
|