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