ded3a3c6b84a07366883cb22804d2b6c589c7209
[bcm963xx.git] / userapps / opensource / sshd / libtommath / bn_mp_mul_2d.c
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2  *
3  * LibTomMath is library that provides for multiple-precision
4  * integer arithmetic as well as number theoretic functionality.
5  *
6  * The library is 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, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14  */
15 #include <tommath.h>
16
17 /* NOTE:  This routine requires updating.  For instance the c->used = c->alloc bit
18    is wrong.  We should just shift c->used digits then set the carry as c->dp[c->used] = carry
19  
20    To be fixed for LTM 0.18
21  */
22
23 /* shift left by a certain bit count */
24 int
25 mp_mul_2d (mp_int * a, int b, mp_int * c)
26 {
27   mp_digit d;
28   int      res;
29
30   /* copy */
31   if (a != c) {
32      if ((res = mp_copy (a, c)) != MP_OKAY) {
33        return res;
34      }
35   }
36
37   if (c->alloc < (int)(c->used + b/DIGIT_BIT + 2)) {
38      if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 2)) != MP_OKAY) {
39        return res;
40      }
41   }
42
43   /* shift by as many digits in the bit count */
44   if (b >= (int)DIGIT_BIT) {
45     if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
46       return res;
47     }
48   }
49   c->used = c->alloc;
50
51   /* shift any bit count < DIGIT_BIT */
52   d = (mp_digit) (b % DIGIT_BIT);
53   if (d != 0) {
54     register mp_digit *tmpc, mask, r, rr;
55     register int x;
56
57     /* bitmask for carries */
58     mask = (((mp_digit)1) << d) - 1;
59
60     /* alias */
61     tmpc = c->dp;
62
63     /* carry */
64     r    = 0;
65     for (x = 0; x < c->used; x++) {
66       /* get the higher bits of the current word */
67       rr = (*tmpc >> (DIGIT_BIT - d)) & mask;
68
69       /* shift the current word and OR in the carry */
70       *tmpc = ((*tmpc << d) | r) & MP_MASK;
71       ++tmpc;
72
73       /* set the carry to the carry bits of the current word */
74       r = rr;
75     }
76   }
77   mp_clamp (c);
78   return MP_OKAY;
79 }