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