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