282
|
1 #include <tommath.h> |
|
2 #ifdef BN_MP_MUL_2_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 /* b = a*2 */ |
|
19 int mp_mul_2(mp_int * a, mp_int * b) |
|
20 { |
|
21 int x, res, oldused; |
|
22 |
|
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; |
|
27 } |
|
28 } |
|
29 |
|
30 oldused = b->used; |
|
31 b->used = a->used; |
|
32 |
|
33 { |
|
34 register mp_digit r, rr, *tmpa, *tmpb; |
|
35 |
|
36 /* alias for source */ |
|
37 tmpa = a->dp; |
|
38 |
|
39 /* alias for dest */ |
|
40 tmpb = b->dp; |
|
41 |
|
42 /* carry */ |
|
43 r = 0; |
|
44 for (x = 0; x < a->used; x++) { |
|
45 |
|
46 /* get what will be the *next* carry bit from the |
|
47 * MSB of the current digit |
|
48 */ |
|
49 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); |
|
50 |
|
51 /* now shift up this digit, add in the carry [from the previous] */ |
|
52 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; |
|
53 |
|
54 /* copy the carry that would be from the source |
|
55 * digit into the next iteration |
|
56 */ |
|
57 r = rr; |
|
58 } |
|
59 |
|
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); |
|
65 } |
|
66 |
|
67 /* now zero any excess digits on the destination |
|
68 * that we didn't write to |
|
69 */ |
|
70 tmpb = b->dp + b->used; |
|
71 for (x = b->used; x < oldused; x++) { |
|
72 *tmpb++ = 0; |
|
73 } |
|
74 } |
|
75 b->sign = a->sign; |
|
76 return MP_OKAY; |
|
77 } |
|
78 #endif |