bn_mp_sub_d.cGo to the documentation of this file.00001 #include <tommath.h> 00002 #ifdef BN_MP_SUB_D_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 /* single digit subtraction */ 00019 int 00020 mp_sub_d (mp_int * a, mp_digit b, mp_int * c) 00021 { 00022 mp_digit *tmpa, *tmpc, mu; 00023 int res, ix, oldused; 00024 00025 /* grow c as required */ 00026 if (c->alloc < a->used + 1) { 00027 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { 00028 return res; 00029 } 00030 } 00031 00032 /* if a is negative just do an unsigned 00033 * addition [with fudged signs] 00034 */ 00035 if (a->sign == MP_NEG) { 00036 a->sign = MP_ZPOS; 00037 res = mp_add_d(a, b, c); 00038 a->sign = c->sign = MP_NEG; 00039 00040 /* clamp */ 00041 mp_clamp(c); 00042 00043 return res; 00044 } 00045 00046 /* setup regs */ 00047 oldused = c->used; 00048 tmpa = a->dp; 00049 tmpc = c->dp; 00050 00051 /* if a <= b simply fix the single digit */ 00052 if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) { 00053 if (a->used == 1) { 00054 *tmpc++ = b - *tmpa; 00055 } else { 00056 *tmpc++ = b; 00057 } 00058 ix = 1; 00059 00060 /* negative/1digit */ 00061 c->sign = MP_NEG; 00062 c->used = 1; 00063 } else { 00064 /* positive/size */ 00065 c->sign = MP_ZPOS; 00066 c->used = a->used; 00067 00068 /* subtract first digit */ 00069 *tmpc = *tmpa++ - b; 00070 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1); 00071 *tmpc++ &= MP_MASK; 00072 00073 /* handle rest of the digits */ 00074 for (ix = 1; ix < a->used; ix++) { 00075 *tmpc = *tmpa++ - mu; 00076 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1); 00077 *tmpc++ &= MP_MASK; 00078 } 00079 } 00080 00081 /* zero excess digits */ 00082 while (ix++ < oldused) { 00083 *tmpc++ = 0; 00084 } 00085 mp_clamp(c); 00086 return MP_OKAY; 00087 } 00088 00089 #endif 00090 00091 /* $Source: /cvsroot/tcl/libtommath/bn_mp_sub_d.c,v $ */ 00092 /* $Revision: 1.1.1.4 $ */ 00093 /* $Date: 2006/12/01 00:08:11 $ */
Generated on Wed Mar 12 12:18:25 2008 by 1.5.1 |