bn_mp_mul_d.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_MUL_D_C
00003 /* LibTomMath, multiple-precision integer library -- Tom St Denis
00004  *
00005  * LibTomMath is a library that provides multiple-precision
00006  * integer arithmetic as well as number theoretic functionality.
00007  *
00008  * The library was designed directly after the MPI library by
00009  * Michael Fromberger but has been written from scratch with
00010  * additional optimizations in place.
00011  *
00012  * The library is free for all purposes without any express
00013  * guarantee it works.
00014  *
00015  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
00016  */
00017 
00018 /* multiply by a digit */
00019 int
00020 mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
00021 {
00022   mp_digit u, *tmpa, *tmpc;
00023   mp_word  r;
00024   int      ix, res, olduse;
00025 
00026   /* make sure c is big enough to hold a*b */
00027   if (c->alloc < a->used + 1) {
00028     if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
00029       return res;
00030     }
00031   }
00032 
00033   /* get the original destinations used count */
00034   olduse = c->used;
00035 
00036   /* set the sign */
00037   c->sign = a->sign;
00038 
00039   /* alias for a->dp [source] */
00040   tmpa = a->dp;
00041 
00042   /* alias for c->dp [dest] */
00043   tmpc = c->dp;
00044 
00045   /* zero carry */
00046   u = 0;
00047 
00048   /* compute columns */
00049   for (ix = 0; ix < a->used; ix++) {
00050     /* compute product and carry sum for this term */
00051     r       = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
00052 
00053     /* mask off higher bits to get a single digit */
00054     *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
00055 
00056     /* send carry into next iteration */
00057     u       = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
00058   }
00059 
00060   /* store final carry [if any] and increment ix offset  */
00061   *tmpc++ = u;
00062   ++ix;
00063 
00064   /* now zero digits above the top */
00065   while (ix++ < olduse) {
00066      *tmpc++ = 0;
00067   }
00068 
00069   /* set used count */
00070   c->used = a->used + 1;
00071   mp_clamp(c);
00072 
00073   return MP_OKAY;
00074 }
00075 #endif
00076 
00077 /* $Source: /cvsroot/tcl/libtommath/bn_mp_mul_d.c,v $ */
00078 /* $Revision: 1.1.1.4 $ */
00079 /* $Date: 2006/12/01 00:08:11 $ */



Generated on Wed Mar 12 12:18:24 2008 by  doxygen 1.5.1