| 1 | #include <tommath.h> | 
|---|
| 2 | #ifdef BN_MP_ADD_D_C | 
|---|
| 3 | /* LibTomMath, multiple-precision integer library -- Tom St Denis | 
|---|
| 4 | * | 
|---|
| 5 | * LibTomMath is a library that provides multiple-precision | 
|---|
| 6 | * integer arithmetic as well as number theoretic functionality. | 
|---|
| 7 | * | 
|---|
| 8 | * The library was designed directly after the MPI library by | 
|---|
| 9 | * Michael Fromberger but has been written from scratch with | 
|---|
| 10 | * additional optimizations in place. | 
|---|
| 11 | * | 
|---|
| 12 | * The library is free for all purposes without any express | 
|---|
| 13 | * guarantee it works. | 
|---|
| 14 | * | 
|---|
| 15 | * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com | 
|---|
| 16 | */ | 
|---|
| 17 |  | 
|---|
| 18 | /* single digit addition */ | 
|---|
| 19 | int | 
|---|
| 20 | mp_add_d (mp_int * a, mp_digit b, mp_int * c) | 
|---|
| 21 | { | 
|---|
| 22 | int     res, ix, oldused; | 
|---|
| 23 | mp_digit *tmpa, *tmpc, mu; | 
|---|
| 24 |  | 
|---|
| 25 | /* grow c as required */ | 
|---|
| 26 | if (c->alloc < a->used + 1) { | 
|---|
| 27 | if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { | 
|---|
| 28 | return res; | 
|---|
| 29 | } | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | /* if a is negative and |a| >= b, call c = |a| - b */ | 
|---|
| 33 | if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) { | 
|---|
| 34 | /* temporarily fix sign of a */ | 
|---|
| 35 | a->sign = MP_ZPOS; | 
|---|
| 36 |  | 
|---|
| 37 | /* c = |a| - b */ | 
|---|
| 38 | res = mp_sub_d(a, b, c); | 
|---|
| 39 |  | 
|---|
| 40 | /* fix signs  */ | 
|---|
| 41 | a->sign = MP_NEG; | 
|---|
| 42 | c->sign = (c->used) ? MP_NEG : MP_ZPOS; | 
|---|
| 43 |  | 
|---|
| 44 | /* clamp */ | 
|---|
| 45 | mp_clamp(c); | 
|---|
| 46 |  | 
|---|
| 47 | return res; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | /* old number of used digits in c */ | 
|---|
| 51 | oldused = c->used; | 
|---|
| 52 |  | 
|---|
| 53 | /* sign always positive */ | 
|---|
| 54 | c->sign = MP_ZPOS; | 
|---|
| 55 |  | 
|---|
| 56 | /* source alias */ | 
|---|
| 57 | tmpa    = a->dp; | 
|---|
| 58 |  | 
|---|
| 59 | /* destination alias */ | 
|---|
| 60 | tmpc    = c->dp; | 
|---|
| 61 |  | 
|---|
| 62 | /* if a is positive */ | 
|---|
| 63 | if (a->sign == MP_ZPOS) { | 
|---|
| 64 | /* add digit, after this we're propagating | 
|---|
| 65 | * the carry. | 
|---|
| 66 | */ | 
|---|
| 67 | *tmpc   = *tmpa++ + b; | 
|---|
| 68 | mu      = *tmpc >> DIGIT_BIT; | 
|---|
| 69 | *tmpc++ &= MP_MASK; | 
|---|
| 70 |  | 
|---|
| 71 | /* now handle rest of the digits */ | 
|---|
| 72 | for (ix = 1; ix < a->used; ix++) { | 
|---|
| 73 | *tmpc   = *tmpa++ + mu; | 
|---|
| 74 | mu      = *tmpc >> DIGIT_BIT; | 
|---|
| 75 | *tmpc++ &= MP_MASK; | 
|---|
| 76 | } | 
|---|
| 77 | /* set final carry */ | 
|---|
| 78 | ix++; | 
|---|
| 79 | *tmpc++  = mu; | 
|---|
| 80 |  | 
|---|
| 81 | /* setup size */ | 
|---|
| 82 | c->used = a->used + 1; | 
|---|
| 83 | } else { | 
|---|
| 84 | /* a was negative and |a| < b */ | 
|---|
| 85 | c->used  = 1; | 
|---|
| 86 |  | 
|---|
| 87 | /* the result is a single digit */ | 
|---|
| 88 | if (a->used == 1) { | 
|---|
| 89 | *tmpc++  =  b - a->dp[0]; | 
|---|
| 90 | } else { | 
|---|
| 91 | *tmpc++  =  b; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | /* setup count so the clearing of oldused | 
|---|
| 95 | * can fall through correctly | 
|---|
| 96 | */ | 
|---|
| 97 | ix       = 1; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /* now zero to oldused */ | 
|---|
| 101 | while (ix++ < oldused) { | 
|---|
| 102 | *tmpc++ = 0; | 
|---|
| 103 | } | 
|---|
| 104 | mp_clamp(c); | 
|---|
| 105 |  | 
|---|
| 106 | return MP_OKAY; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | #endif | 
|---|
| 110 |  | 
|---|
| 111 | /* $Source: /cvsroot/tcl/libtommath/bn_mp_add_d.c,v $ */ | 
|---|
| 112 | /* Tom's revision is 1.2 */ | 
|---|
| 113 | /* $Revision: 1.4 $ */ | 
|---|
| 114 | /* $Date: 2006/12/01 00:31:32 $ */ | 
|---|