# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtommath / bn_mp_mul_d.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 /* multiply by a digit */
18 int
19 mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
20 {
21   int     res, pa, olduse;
22
23   /* make sure c is big enough to hold a*b */
24   pa = a->used;
25   if (c->alloc < pa + 1) {
26     if ((res = mp_grow (c, pa + 1)) != MP_OKAY) {
27       return res;
28     }
29   }
30
31   /* get the original destinations used count */
32   olduse = c->used;
33
34   /* set the new temporary used count */
35   c->used = pa + 1;
36   c->sign = a->sign;
37
38   {
39     register mp_digit u, *tmpa, *tmpc;
40     register mp_word r;
41     register int ix;
42
43     /* alias for a->dp [source] */
44     tmpa = a->dp;
45
46     /* alias for c->dp [dest] */
47     tmpc = c->dp;
48
49     /* zero carry */
50     u = 0;
51     for (ix = 0; ix < pa; ix++) {
52       /* compute product and carry sum for this term */
53       r = ((mp_word) u) + ((mp_word) * tmpa++) * ((mp_word) b);
54
55       /* mask off higher bits to get a single digit */
56       *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
57
58       /* send carry into next iteration */
59       u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
60     }
61     /* store final carry [if any] */
62     *tmpc++ = u;
63
64     /* now zero digits above the top */
65     for (; pa < olduse; pa++) {
66        *tmpc++ = 0;
67     }
68   }
69
70   mp_clamp (c);
71   return MP_OKAY;
72 }