xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/libtommath/bn_mp_mul_d.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: bn_mp_mul_d.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2 
3 #include <tommath.h>
4 #ifdef BN_MP_MUL_D_C
5 /* LibTomMath, multiple-precision integer library -- Tom St Denis
6  *
7  * LibTomMath is a library that provides multiple-precision
8  * integer arithmetic as well as number theoretic functionality.
9  *
10  * The library was designed directly after the MPI library by
11  * Michael Fromberger but has been written from scratch with
12  * additional optimizations in place.
13  *
14  * The library is free for all purposes without any express
15  * guarantee it works.
16  *
17  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18  */
19 
20 /* multiply by a digit */
21 int
mp_mul_d(mp_int * a,mp_digit b,mp_int * c)22 mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
23 {
24   mp_digit u, *tmpa, *tmpc;
25   mp_word  r;
26   int      ix, res, olduse;
27 
28   /* make sure c is big enough to hold a*b */
29   if (c->alloc < a->used + 1) {
30     if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
31       return res;
32     }
33   }
34 
35   /* get the original destinations used count */
36   olduse = c->used;
37 
38   /* set the sign */
39   c->sign = a->sign;
40 
41   /* alias for a->dp [source] */
42   tmpa = a->dp;
43 
44   /* alias for c->dp [dest] */
45   tmpc = c->dp;
46 
47   /* zero carry */
48   u = 0;
49 
50   /* compute columns */
51   for (ix = 0; ix < a->used; 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 
62   /* store final carry [if any] and increment ix offset  */
63   *tmpc++ = u;
64   ++ix;
65 
66   /* now zero digits above the top */
67   while (ix++ < olduse) {
68      *tmpc++ = 0;
69   }
70 
71   /* set used count */
72   c->used = a->used + 1;
73   mp_clamp(c);
74 
75   return MP_OKAY;
76 }
77 #endif
78 
79 /* Source: /cvs/libtom/libtommath/bn_mp_mul_d.c,v  */
80 /* Revision: 1.4  */
81 /* Date: 2006/12/28 01:25:13  */
82