comparison bn_mp_mul_2.c @ 2:86e0b50a9b58 libtommath-orig ltm-0.30-orig

ltm 0.30 orig import
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:25:22 +0000
parents
children d29b64170cf0
comparison
equal deleted inserted replaced
-1:000000000000 2:86e0b50a9b58
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2 *
3 * LibTomMath is a library that provides multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
5 *
6 * The library was designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
9 *
10 * The library is free for all purposes without any express
11 * guarantee it works.
12 *
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org
14 */
15 #include <tommath.h>
16
17 /* b = a*2 */
18 int mp_mul_2(mp_int * a, mp_int * b)
19 {
20 int x, res, oldused;
21
22 /* grow to accomodate result */
23 if (b->alloc < a->used + 1) {
24 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
25 return res;
26 }
27 }
28
29 oldused = b->used;
30 b->used = a->used;
31
32 {
33 register mp_digit r, rr, *tmpa, *tmpb;
34
35 /* alias for source */
36 tmpa = a->dp;
37
38 /* alias for dest */
39 tmpb = b->dp;
40
41 /* carry */
42 r = 0;
43 for (x = 0; x < a->used; x++) {
44
45 /* get what will be the *next* carry bit from the
46 * MSB of the current digit
47 */
48 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
49
50 /* now shift up this digit, add in the carry [from the previous] */
51 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
52
53 /* copy the carry that would be from the source
54 * digit into the next iteration
55 */
56 r = rr;
57 }
58
59 /* new leading digit? */
60 if (r != 0) {
61 /* add a MSB which is always 1 at this point */
62 *tmpb = 1;
63 ++(b->used);
64 }
65
66 /* now zero any excess digits on the destination
67 * that we didn't write to
68 */
69 tmpb = b->dp + b->used;
70 for (x = b->used; x < oldused; x++) {
71 *tmpb++ = 0;
72 }
73 }
74 b->sign = a->sign;
75 return MP_OKAY;
76 }