3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
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.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* single digit addition */
20 mp_add_d (mp_int
* a
, mp_digit b
, mp_int
* c
)
23 mp_digit
*tmpa
, *tmpc
, mu
;
25 /* grow c as required */
26 if (c
->alloc
< a
->used
+ 1) {
27 if ((res
= mp_grow(c
, a
->used
+ 1)) != MP_OKAY
) {
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 */
38 res
= mp_sub_d(a
, b
, c
);
41 a
->sign
= c
->sign
= MP_NEG
;
49 /* old number of used digits in c */
52 /* sign always positive */
58 /* destination alias */
61 /* if a is positive */
62 if (a
->sign
== MP_ZPOS
) {
63 /* add digit, after this we're propagating
67 mu
= *tmpc
>> DIGIT_BIT
;
70 /* now handle rest of the digits */
71 for (ix
= 1; ix
< a
->used
; ix
++) {
73 mu
= *tmpc
>> DIGIT_BIT
;
81 c
->used
= a
->used
+ 1;
83 /* a was negative and |a| < b */
86 /* the result is a single digit */
88 *tmpc
++ = b
- a
->dp
[0];
93 /* setup count so the clearing of oldused
94 * can fall through correctly
99 /* now zero to oldused */
100 while (ix
++ < oldused
) {
110 /* $Source: /cvs/libtom/libtommath/bn_mp_add_d.c,v $ */
111 /* $Revision: 1.4 $ */
112 /* $Date: 2006/03/31 14:18:44 $ */