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 /* low level addition, based on HAC pp.594, Algorithm 14.7 */
20 s_mp_add (mp_int
* a
, mp_int
* b
, mp_int
* c
)
23 int olduse
, res
, min
, max
;
25 /* find sizes, we let |a| <= |b| which means we have to sort
26 * them. "x" will point to the input with the most digits
28 if (a
->used
> b
->used
) {
39 if (c
->alloc
< max
+ 1) {
40 if ((res
= mp_grow (c
, max
+ 1)) != MP_OKAY
) {
45 /* get old used digit count and set new one */
50 register mp_digit u
, *tmpa
, *tmpb
, *tmpc
;
53 /* alias for digit pointers */
66 for (i
= 0; i
< min
; i
++) {
67 /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
68 *tmpc
= *tmpa
++ + *tmpb
++ + u
;
70 /* U = carry bit of T[i] */
71 u
= *tmpc
>> ((mp_digit
)DIGIT_BIT
);
73 /* take away carry bit from T[i] */
77 /* now copy higher words if any, that is in A+B
78 * if A or B has more digits add those in
81 for (; i
< max
; i
++) {
85 /* U = carry bit of T[i] */
86 u
= *tmpc
>> ((mp_digit
)DIGIT_BIT
);
88 /* take away carry bit from T[i] */
96 /* clear digits above oldused */
97 for (i
= c
->used
; i
< olduse
; i
++) {
107 /* $Source: /cvs/libtom/libtommath/bn_s_mp_add.c,v $ */
108 /* $Revision: 1.3 $ */
109 /* $Date: 2006/03/31 14:18:44 $ */