s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / source4 / heimdal / lib / hcrypto / libtommath / bn_s_mp_mul_high_digs.c
1 #include "tommath_private.h"
2 #ifdef BN_S_MP_MUL_HIGH_DIGS_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
5
6 /* multiplies |a| * |b| and does not compute the lower digs digits
7  * [meant to get the higher part of the product]
8  */
9 mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
10 {
11    mp_int   t;
12    int      pa, pb, ix, iy;
13    mp_err   err;
14    mp_digit u;
15    mp_word  r;
16    mp_digit tmpx, *tmpt, *tmpy;
17
18    /* can we use the fast multiplier? */
19    if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)
20        && ((a->used + b->used + 1) < MP_WARRAY)
21        && (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
22       return s_mp_mul_high_digs_fast(a, b, c, digs);
23    }
24
25    if ((err = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) {
26       return err;
27    }
28    t.used = a->used + b->used + 1;
29
30    pa = a->used;
31    pb = b->used;
32    for (ix = 0; ix < pa; ix++) {
33       /* clear the carry */
34       u = 0;
35
36       /* left hand side of A[ix] * B[iy] */
37       tmpx = a->dp[ix];
38
39       /* alias to the address of where the digits will be stored */
40       tmpt = &(t.dp[digs]);
41
42       /* alias for where to read the right hand side from */
43       tmpy = b->dp + (digs - ix);
44
45       for (iy = digs - ix; iy < pb; iy++) {
46          /* calculate the double precision result */
47          r       = (mp_word)*tmpt +
48                    ((mp_word)tmpx * (mp_word)*tmpy++) +
49                    (mp_word)u;
50
51          /* get the lower part */
52          *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
53
54          /* carry the carry */
55          u       = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
56       }
57       *tmpt = u;
58    }
59    mp_clamp(&t);
60    mp_exch(&t, c);
61    mp_clear(&t);
62    return MP_OKAY;
63 }
64 #endif