usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / dropbear / libtommath / bn_mp_mul_2.c
blob0d27a9d2852e29aed6c475fc2f8a706eb6b36d82
1 #include <tommath.h>
2 #ifdef BN_MP_MUL_2_C
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
13 * guarantee it works.
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* b = a*2 */
19 int mp_mul_2(mp_int * a, mp_int * b)
21 int x, res, oldused;
23 /* grow to accomodate result */
24 if (b->alloc < a->used + 1) {
25 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
26 return res;
30 oldused = b->used;
31 b->used = a->used;
34 register mp_digit r, rr, *tmpa, *tmpb;
36 /* alias for source */
37 tmpa = a->dp;
39 /* alias for dest */
40 tmpb = b->dp;
42 /* carry */
43 r = 0;
44 for (x = 0; x < a->used; x++) {
46 /* get what will be the *next* carry bit from the
47 * MSB of the current digit
49 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
51 /* now shift up this digit, add in the carry [from the previous] */
52 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
54 /* copy the carry that would be from the source
55 * digit into the next iteration
57 r = rr;
60 /* new leading digit? */
61 if (r != 0) {
62 /* add a MSB which is always 1 at this point */
63 *tmpb = 1;
64 ++(b->used);
67 /* now zero any excess digits on the destination
68 * that we didn't write to
70 tmpb = b->dp + b->used;
71 for (x = b->used; x < oldused; x++) {
72 *tmpb++ = 0;
75 b->sign = a->sign;
76 return MP_OKAY;
78 #endif
80 /* $Source: /cvs/libtom/libtommath/bn_mp_mul_2.c,v $ */
81 /* $Revision: 1.3 $ */
82 /* $Date: 2006/03/31 14:18:44 $ */