bn_mp_mul_2d.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_MUL_2D_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 /* shift left by a certain bit count */
00019 int mp_mul_2d (mp_int * a, int b, mp_int * c)
00020 {
00021   mp_digit d;
00022   int      res;
00023 
00024   /* copy */
00025   if (a != c) {
00026      if ((res = mp_copy (a, c)) != MP_OKAY) {
00027        return res;
00028      }
00029   }
00030 
00031   if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
00032      if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
00033        return res;
00034      }
00035   }
00036 
00037   /* shift by as many digits in the bit count */
00038   if (b >= (int)DIGIT_BIT) {
00039     if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
00040       return res;
00041     }
00042   }
00043 
00044   /* shift any bit count < DIGIT_BIT */
00045   d = (mp_digit) (b % DIGIT_BIT);
00046   if (d != 0) {
00047     register mp_digit *tmpc, shift, mask, r, rr;
00048     register int x;
00049 
00050     /* bitmask for carries */
00051     mask = (((mp_digit)1) << d) - 1;
00052 
00053     /* shift for msbs */
00054     shift = DIGIT_BIT - d;
00055 
00056     /* alias */
00057     tmpc = c->dp;
00058 
00059     /* carry */
00060     r    = 0;
00061     for (x = 0; x < c->used; x++) {
00062       /* get the higher bits of the current word */
00063       rr = (*tmpc >> shift) & mask;
00064 
00065       /* shift the current word and OR in the carry */
00066       *tmpc = ((*tmpc << d) | r) & MP_MASK;
00067       ++tmpc;
00068 
00069       /* set the carry to the carry bits of the current word */
00070       r = rr;
00071     }
00072     
00073     /* set final carry */
00074     if (r != 0) {
00075        c->dp[(c->used)++] = r;
00076     }
00077   }
00078   mp_clamp (c);
00079   return MP_OKAY;
00080 }
00081 #endif
00082 
00083 /* $Source: /cvsroot/tcl/libtommath/bn_mp_mul_2d.c,v $ */
00084 /* $Revision: 1.1.1.3 $ */
00085 /* $Date: 2006/12/01 00:08:11 $ */



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