Mercurial > dropbear
comparison libtommath/bn_mp_add_d.c @ 293:9d110777f345 contrib-blacklist
propagate from branch 'au.asn.ucc.matt.dropbear' (head 7ad1775ed65e75dbece27fe6b65bf1a234db386a)
to branch 'au.asn.ucc.matt.dropbear.contrib.blacklist' (head 1d86a4f0a401cc68c2670d821a2f6366c37af143)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 10 Mar 2006 06:31:29 +0000 |
parents | eed26cff980b |
children | 5ff8218bcee9 |
comparison
equal
deleted
inserted
replaced
247:c07de41b53d7 | 293:9d110777f345 |
---|---|
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 |