bn_mp_add.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_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 /* high level addition (handles signs) */
00019 int mp_add (mp_int * a, mp_int * b, mp_int * c)
00020 {
00021   int     sa, sb, res;
00022 
00023   /* get sign of both inputs */
00024   sa = a->sign;
00025   sb = b->sign;
00026 
00027   /* handle two cases, not four */
00028   if (sa == sb) {
00029     /* both positive or both negative */
00030     /* add their magnitudes, copy the sign */
00031     c->sign = sa;
00032     res = s_mp_add (a, b, c);
00033   } else {
00034     /* one positive, the other negative */
00035     /* subtract the one with the greater magnitude from */
00036     /* the one of the lesser magnitude.  The result gets */
00037     /* the sign of the one with the greater magnitude. */
00038     if (mp_cmp_mag (a, b) == MP_LT) {
00039       c->sign = sb;
00040       res = s_mp_sub (b, a, c);
00041     } else {
00042       c->sign = sa;
00043       res = s_mp_sub (a, b, c);
00044     }
00045   }
00046   return res;
00047 }
00048 
00049 #endif
00050 
00051 /* $Source: /cvsroot/tcl/libtommath/bn_mp_add.c,v $ */
00052 /* $Revision: 1.3 $ */
00053 /* $Date: 2006/12/01 19:45:38 $ */



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