bn_mp_sub.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_SUB_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 subtraction (handles signs) */
00019 int
00020 mp_sub (mp_int * a, mp_int * b, mp_int * c)
00021 {
00022   int     sa, sb, res;
00023 
00024   sa = a->sign;
00025   sb = b->sign;
00026 
00027   if (sa != sb) {
00028     /* subtract a negative from a positive, OR */
00029     /* subtract a positive from a negative. */
00030     /* In either case, ADD their magnitudes, */
00031     /* and use the sign of the first number. */
00032     c->sign = sa;
00033     res = s_mp_add (a, b, c);
00034   } else {
00035     /* subtract a positive from a positive, OR */
00036     /* subtract a negative from a negative. */
00037     /* First, take the difference between their */
00038     /* magnitudes, then... */
00039     if (mp_cmp_mag (a, b) != MP_LT) {
00040       /* Copy the sign from the first */
00041       c->sign = sa;
00042       /* The first has a larger or equal magnitude */
00043       res = s_mp_sub (a, b, c);
00044     } else {
00045       /* The result has the *opposite* sign from */
00046       /* the first number. */
00047       c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
00048       /* The second has a larger magnitude */
00049       res = s_mp_sub (b, a, c);
00050     }
00051   }
00052   return res;
00053 }
00054 
00055 #endif
00056 
00057 /* $Source: /cvsroot/tcl/libtommath/bn_mp_sub.c,v $ */
00058 /* $Revision: 1.1.1.3 $ */
00059 /* $Date: 2006/12/01 00:08:11 $ */



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