1
|
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 /* multiplies |a| * |b| and does not compute the lower digs digits |
|
18 * [meant to get the higher part of the product] |
|
19 */ |
|
20 int |
|
21 s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
|
22 { |
|
23 mp_int t; |
|
24 int res, pa, pb, ix, iy; |
|
25 mp_digit u; |
|
26 mp_word r; |
|
27 mp_digit tmpx, *tmpt, *tmpy; |
|
28 |
|
29 /* can we use the fast multiplier? */ |
|
30 if (((a->used + b->used + 1) < MP_WARRAY) |
|
31 && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
|
32 return fast_s_mp_mul_high_digs (a, b, c, digs); |
|
33 } |
|
34 |
|
35 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) { |
|
36 return res; |
|
37 } |
|
38 t.used = a->used + b->used + 1; |
|
39 |
|
40 pa = a->used; |
|
41 pb = b->used; |
|
42 for (ix = 0; ix < pa; ix++) { |
|
43 /* clear the carry */ |
|
44 u = 0; |
|
45 |
|
46 /* left hand side of A[ix] * B[iy] */ |
|
47 tmpx = a->dp[ix]; |
|
48 |
|
49 /* alias to the address of where the digits will be stored */ |
|
50 tmpt = &(t.dp[digs]); |
|
51 |
|
52 /* alias for where to read the right hand side from */ |
|
53 tmpy = b->dp + (digs - ix); |
|
54 |
|
55 for (iy = digs - ix; iy < pb; iy++) { |
|
56 /* calculate the double precision result */ |
|
57 r = ((mp_word)*tmpt) + |
|
58 ((mp_word)tmpx) * ((mp_word)*tmpy++) + |
|
59 ((mp_word) u); |
|
60 |
|
61 /* get the lower part */ |
|
62 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
63 |
|
64 /* carry the carry */ |
|
65 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); |
|
66 } |
|
67 *tmpt = u; |
|
68 } |
|
69 mp_clamp (&t); |
|
70 mp_exch (&t, c); |
|
71 mp_clear (&t); |
|
72 return MP_OKAY; |
|
73 } |