2 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
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.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* this is a modified version of fast_s_mul_digs that only produces
19 * output digits *above* digs. See the comments for fast_s_mul_digs
20 * to see how it works.
22 * This is used in the Barrett reduction since for one of the multiplications
23 * only the higher digits were needed. This essentially halves the work.
25 * Based on Algorithm 14.12 on pp.595 of HAC.
27 int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
29 int olduse, res, pa, ix, iz;
30 mp_digit W[MP_WARRAY];
33 /* grow the destination as required */
34 pa = a->used + b->used;
36 if ((res = mp_grow (c, pa)) != MP_OKAY) {
41 /* number of output digits to produce */
42 pa = a->used + b->used;
44 for (ix = digs; ix < pa; ix++) {
46 mp_digit *tmpx, *tmpy;
48 /* get offsets into the two bignums */
49 ty = MIN(b->used-1, ix);
52 /* setup temp aliases */
56 /* this is the number of times the loop will iterrate, essentially its
57 while (tx++ < a->used && ty-- >= 0) { ... }
59 iy = MIN(a->used-tx, ty+1);
62 for (iz = 0; iz < iy; iz++) {
63 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
67 W[ix] = ((mp_digit)_W) & MP_MASK;
70 _W = _W >> ((mp_word)DIGIT_BIT);
78 register mp_digit *tmpc;
81 for (ix = digs; ix <= pa; ix++) {
82 /* now extract the previous digit [below the carry] */
86 /* clear unused digits [that existed in the old copy of c] */
87 for (; ix < olduse; ix++) {
96 /* $Source: /cvs/libtom/libtommath/bn_fast_s_mp_mul_high_digs.c,v $ */
97 /* $Revision: 1.4 $ */
98 /* $Date: 2006/03/31 14:18:44 $ */