Mercurial > dropbear
comparison libtommath/bn_mp_add_d.c @ 330:5488db2e9e4e
merge of 332f709a4cb39cde4cedab7c3be89e05f3023067
and ca4ca78b82c5d430c69ce01bf794e8886ce81431
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 10 Jun 2006 16:39:40 +0000 |
parents | eed26cff980b |
children | 5ff8218bcee9 |
comparison
equal
deleted
inserted
replaced
329:8ed0dce45126 | 330:5488db2e9e4e |
---|---|
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, [email protected], http://math.libtomcrypt.org | |
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 sign */ | |
41 a->sign = c->sign = MP_NEG; | |
42 | |
43 return res; | |
44 } | |
45 | |
46 /* old number of used digits in c */ | |
47 oldused = c->used; | |
48 | |
49 /* sign always positive */ | |
50 c->sign = MP_ZPOS; | |
51 | |
52 /* source alias */ | |
53 tmpa = a->dp; | |
54 | |
55 /* destination alias */ | |
56 tmpc = c->dp; | |
57 | |
58 /* if a is positive */ | |
59 if (a->sign == MP_ZPOS) { | |
60 /* add digit, after this we're propagating | |
61 * the carry. | |
62 */ | |
63 *tmpc = *tmpa++ + b; | |
64 mu = *tmpc >> DIGIT_BIT; | |
65 *tmpc++ &= MP_MASK; | |
66 | |
67 /* now handle rest of the digits */ | |
68 for (ix = 1; ix < a->used; ix++) { | |
69 *tmpc = *tmpa++ + mu; | |
70 mu = *tmpc >> DIGIT_BIT; | |
71 *tmpc++ &= MP_MASK; | |
72 } | |
73 /* set final carry */ | |
74 ix++; | |
75 *tmpc++ = mu; | |
76 | |
77 /* setup size */ | |
78 c->used = a->used + 1; | |
79 } else { | |
80 /* a was negative and |a| < b */ | |
81 c->used = 1; | |
82 | |
83 /* the result is a single digit */ | |
84 if (a->used == 1) { | |
85 *tmpc++ = b - a->dp[0]; | |
86 } else { | |
87 *tmpc++ = b; | |
88 } | |
89 | |
90 /* setup count so the clearing of oldused | |
91 * can fall through correctly | |
92 */ | |
93 ix = 1; | |
94 } | |
95 | |
96 /* now zero to oldused */ | |
97 while (ix++ < oldused) { | |
98 *tmpc++ = 0; | |
99 } | |
100 mp_clamp(c); | |
101 | |
102 return MP_OKAY; | |
103 } | |
104 | |
105 #endif |