1 /* $NetBSD: bn_mp_mul_2.c,v 1.2 2017/01/28 21:31:47 christos Exp $ */
2
3 #include <tommath.h>
4 #ifdef BN_MP_MUL_2_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 /* b = a*2 */
mp_mul_2(mp_int * a,mp_int * b)21 int mp_mul_2(mp_int * a, mp_int * b)
22 {
23 int x, res, oldused;
24
25 /* grow to accomodate result */
26 if (b->alloc < a->used + 1) {
27 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
28 return res;
29 }
30 }
31
32 oldused = b->used;
33 b->used = a->used;
34
35 {
36 register mp_digit r, rr, *tmpa, *tmpb;
37
38 /* alias for source */
39 tmpa = a->dp;
40
41 /* alias for dest */
42 tmpb = b->dp;
43
44 /* carry */
45 r = 0;
46 for (x = 0; x < a->used; x++) {
47
48 /* get what will be the *next* carry bit from the
49 * MSB of the current digit
50 */
51 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
52
53 /* now shift up this digit, add in the carry [from the previous] */
54 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
55
56 /* copy the carry that would be from the source
57 * digit into the next iteration
58 */
59 r = rr;
60 }
61
62 /* new leading digit? */
63 if (r != 0) {
64 /* add a MSB which is always 1 at this point */
65 *tmpb = 1;
66 ++(b->used);
67 }
68
69 /* now zero any excess digits on the destination
70 * that we didn't write to
71 */
72 tmpb = b->dp + b->used;
73 for (x = b->used; x < oldused; x++) {
74 *tmpb++ = 0;
75 }
76 }
77 b->sign = a->sign;
78 return MP_OKAY;
79 }
80 #endif
81
82 /* Source: /cvs/libtom/libtommath/bn_mp_mul_2.c,v */
83 /* Revision: 1.4 */
84 /* Date: 2006/12/28 01:25:13 */
85