1 #include "tommath_private.h"
2 #ifdef BN_S_MP_MUL_HIGH_DIGS_FAST_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* this is a modified version of fast_s_mul_digs that only produces
7 * output digits *above* digs. See the comments for fast_s_mul_digs
10 * This is used in the Barrett reduction since for one of the multiplications
11 * only the higher digits were needed. This essentially halves the work.
13 * Based on Algorithm 14.12 on pp.595 of HAC.
15 mp_err
s_mp_mul_high_digs_fast(const mp_int
*a
, const mp_int
*b
, mp_int
*c
, int digs
)
17 int olduse
, pa
, ix
, iz
;
19 mp_digit W
[MP_WARRAY
];
26 /* grow the destination as required */
27 pa
= a
->used
+ b
->used
;
29 if ((err
= mp_grow(c
, pa
)) != MP_OKAY
) {
34 /* number of output digits to produce */
35 pa
= a
->used
+ b
->used
;
37 for (ix
= digs
; ix
< pa
; ix
++) {
39 mp_digit
*tmpx
, *tmpy
;
41 /* get offsets into the two bignums */
42 ty
= MP_MIN(b
->used
-1, ix
);
45 /* setup temp aliases */
49 /* this is the number of times the loop will iterrate, essentially its
50 while (tx++ < a->used && ty-- >= 0) { ... }
52 iy
= MP_MIN(a
->used
-tx
, ty
+1);
55 for (iz
= 0; iz
< iy
; iz
++) {
56 _W
+= (mp_word
)*tmpx
++ * (mp_word
)*tmpy
--;
60 W
[ix
] = (mp_digit
)_W
& MP_MASK
;
63 _W
= _W
>> (mp_word
)MP_DIGIT_BIT
;
74 for (ix
= digs
; ix
< pa
; ix
++) {
75 /* now extract the previous digit [below the carry] */
79 /* clear unused digits [that existed in the old copy of c] */
80 MP_ZERO_DIGITS(tmpc
, olduse
- ix
);