bn_mp_mul_2.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_MUL_2_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 /* b = a*2 */
00019 int mp_mul_2(mp_int * a, mp_int * b)
00020 {
00021   int     x, res, oldused;
00022 
00023   /* grow to accomodate result */
00024   if (b->alloc < a->used + 1) {
00025     if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
00026       return res;
00027     }
00028   }
00029 
00030   oldused = b->used;
00031   b->used = a->used;
00032 
00033   {
00034     register mp_digit r, rr, *tmpa, *tmpb;
00035 
00036     /* alias for source */
00037     tmpa = a->dp;
00038     
00039     /* alias for dest */
00040     tmpb = b->dp;
00041 
00042     /* carry */
00043     r = 0;
00044     for (x = 0; x < a->used; x++) {
00045     
00046       /* get what will be the *next* carry bit from the 
00047        * MSB of the current digit 
00048        */
00049       rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
00050       
00051       /* now shift up this digit, add in the carry [from the previous] */
00052       *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
00053       
00054       /* copy the carry that would be from the source 
00055        * digit into the next iteration 
00056        */
00057       r = rr;
00058     }
00059 
00060     /* new leading digit? */
00061     if (r != 0) {
00062       /* add a MSB which is always 1 at this point */
00063       *tmpb = 1;
00064       ++(b->used);
00065     }
00066 
00067     /* now zero any excess digits on the destination 
00068      * that we didn't write to 
00069      */
00070     tmpb = b->dp + b->used;
00071     for (x = b->used; x < oldused; x++) {
00072       *tmpb++ = 0;
00073     }
00074   }
00075   b->sign = a->sign;
00076   return MP_OKAY;
00077 }
00078 #endif
00079 
00080 /* $Source: /cvsroot/tcl/libtommath/bn_mp_mul_2.c,v $ */
00081 /* $Revision: 1.1.1.3 $ */
00082 /* $Date: 2006/12/01 00:08:11 $ */



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