bn_s_mp_add.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_S_MP_ADD_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 /* low level addition, based on HAC pp.594, Algorithm 14.7 */
00019 int
00020 s_mp_add (mp_int * a, mp_int * b, mp_int * c)
00021 {
00022   mp_int *x;
00023   int     olduse, res, min, max;
00024 
00025   /* find sizes, we let |a| <= |b| which means we have to sort
00026    * them.  "x" will point to the input with the most digits
00027    */
00028   if (a->used > b->used) {
00029     min = b->used;
00030     max = a->used;
00031     x = a;
00032   } else {
00033     min = a->used;
00034     max = b->used;
00035     x = b;
00036   }
00037 
00038   /* init result */
00039   if (c->alloc < max + 1) {
00040     if ((res = mp_grow (c, max + 1)) != MP_OKAY) {
00041       return res;
00042     }
00043   }
00044 
00045   /* get old used digit count and set new one */
00046   olduse = c->used;
00047   c->used = max + 1;
00048 
00049   {
00050     register mp_digit u, *tmpa, *tmpb, *tmpc;
00051     register int i;
00052 
00053     /* alias for digit pointers */
00054 
00055     /* first input */
00056     tmpa = a->dp;
00057 
00058     /* second input */
00059     tmpb = b->dp;
00060 
00061     /* destination */
00062     tmpc = c->dp;
00063 
00064     /* zero the carry */
00065     u = 0;
00066     for (i = 0; i < min; i++) {
00067       /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
00068       *tmpc = *tmpa++ + *tmpb++ + u;
00069 
00070       /* U = carry bit of T[i] */
00071       u = *tmpc >> ((mp_digit)DIGIT_BIT);
00072 
00073       /* take away carry bit from T[i] */
00074       *tmpc++ &= MP_MASK;
00075     }
00076 
00077     /* now copy higher words if any, that is in A+B 
00078      * if A or B has more digits add those in 
00079      */
00080     if (min != max) {
00081       for (; i < max; i++) {
00082         /* T[i] = X[i] + U */
00083         *tmpc = x->dp[i] + u;
00084 
00085         /* U = carry bit of T[i] */
00086         u = *tmpc >> ((mp_digit)DIGIT_BIT);
00087 
00088         /* take away carry bit from T[i] */
00089         *tmpc++ &= MP_MASK;
00090       }
00091     }
00092 
00093     /* add carry */
00094     *tmpc++ = u;
00095 
00096     /* clear digits above oldused */
00097     for (i = c->used; i < olduse; i++) {
00098       *tmpc++ = 0;
00099     }
00100   }
00101 
00102   mp_clamp (c);
00103   return MP_OKAY;
00104 }
00105 #endif
00106 
00107 /* $Source: /cvsroot/tcl/libtommath/bn_s_mp_add.c,v $ */
00108 /* $Revision: 1.1.1.3 $ */
00109 /* $Date: 2006/12/01 00:08:11 $ */



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