1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 * LibTomMath is library that provides for multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
6 * The library is designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
10 * The library is free for all purposes without any express
13 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
17 /* multiplies |a| * |b| and does not compute the lower digs digits
18 * [meant to get the higher part of the product]
21 s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
24 int res, pa, pb, ix, iy;
27 mp_digit tmpx, *tmpt, *tmpy;
30 /* can we use the fast multiplier? */
31 if (((a->used + b->used + 1) < MP_WARRAY)
32 && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
33 return fast_s_mp_mul_high_digs (a, b, c, digs);
36 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) {
39 t.used = a->used + b->used + 1;
43 for (ix = 0; ix < pa; ix++) {
47 /* left hand side of A[ix] * B[iy] */
50 /* alias to the address of where the digits will be stored */
53 /* alias for where to read the right hand side from */
54 tmpy = b->dp + (digs - ix);
56 for (iy = digs - ix; iy < pb; iy++) {
57 /* calculate the double precision result */
58 r = ((mp_word) * tmpt) + ((mp_word) tmpx) * ((mp_word) * tmpy++) + ((mp_word) u);
60 /* get the lower part */
61 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
64 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));