Line data Source code
1 : #include <tommath.h>
2 : #ifdef BN_MP_MUL_2D_C
3 : /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 : *
5 : * LibTomMath is a library that provides multiple-precision
6 : * integer arithmetic as well as number theoretic functionality.
7 : *
8 : * The library was designed directly after the MPI library by
9 : * Michael Fromberger but has been written from scratch with
10 : * additional optimizations in place.
11 : *
12 : * The library is free for all purposes without any express
13 : * guarantee it works.
14 : *
15 : * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
16 : */
17 :
18 : /* shift left by a certain bit count */
19 744988 : int mp_mul_2d (mp_int * a, int b, mp_int * c)
20 : {
21 : mp_digit d;
22 : int res;
23 :
24 : /* copy */
25 744988 : if (a != c) {
26 0 : if ((res = mp_copy (a, c)) != MP_OKAY) {
27 0 : return res;
28 : }
29 : }
30 :
31 744988 : if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
32 1668 : if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
33 0 : return res;
34 : }
35 : }
36 :
37 : /* shift by as many digits in the bit count */
38 744988 : if (b >= (int)DIGIT_BIT) {
39 0 : if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
40 0 : return res;
41 : }
42 : }
43 :
44 : /* shift any bit count < DIGIT_BIT */
45 744988 : d = (mp_digit) (b % DIGIT_BIT);
46 744988 : if (d != 0) {
47 : register mp_digit *tmpc, shift, mask, r, rr;
48 : register int x;
49 :
50 : /* bitmask for carries */
51 744988 : mask = (((mp_digit)1) << d) - 1;
52 :
53 : /* shift for msbs */
54 744988 : shift = DIGIT_BIT - d;
55 :
56 : /* alias */
57 744988 : tmpc = c->dp;
58 :
59 : /* carry */
60 744988 : r = 0;
61 29942986 : for (x = 0; x < c->used; x++) {
62 : /* get the higher bits of the current word */
63 29197998 : rr = (*tmpc >> shift) & mask;
64 :
65 : /* shift the current word and OR in the carry */
66 29197998 : *tmpc = ((*tmpc << d) | r) & MP_MASK;
67 29197998 : ++tmpc;
68 :
69 : /* set the carry to the carry bits of the current word */
70 29197998 : r = rr;
71 : }
72 :
73 : /* set final carry */
74 744988 : if (r != 0) {
75 1260 : c->dp[(c->used)++] = r;
76 : }
77 : }
78 744988 : mp_clamp (c);
79 744988 : return MP_OKAY;
80 : }
81 : #endif
82 :
83 : /* $Source: /cvs/libtom/libtommath/bn_mp_mul_2d.c,v $ */
84 : /* $Revision: 1.4 $ */
85 : /* $Date: 2006/12/28 01:25:13 $ */
|