142
|
1 #include <tommath.h> |
|
2 #ifdef BN_FAST_S_MP_MUL_DIGS_C |
2
|
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 * The library is free for all purposes without any express |
|
13 * guarantee it works. |
|
14 * |
|
15 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
16 */ |
|
17 |
|
18 /* Fast (comba) multiplier |
|
19 * |
|
20 * This is the fast column-array [comba] multiplier. It is |
|
21 * designed to compute the columns of the product first |
|
22 * then handle the carries afterwards. This has the effect |
|
23 * of making the nested loops that compute the columns very |
|
24 * simple and schedulable on super-scalar processors. |
|
25 * |
|
26 * This has been modified to produce a variable number of |
|
27 * digits of output so if say only a half-product is required |
|
28 * you don't have to compute the upper half (a feature |
|
29 * required for fast Barrett reduction). |
|
30 * |
|
31 * Based on Algorithm 14.12 on pp.595 of HAC. |
|
32 * |
|
33 */ |
|
34 int |
|
35 fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
|
36 { |
142
|
37 int olduse, res, pa, ix, iz; |
|
38 mp_digit W[MP_WARRAY]; |
|
39 register mp_word _W; |
2
|
40 |
|
41 /* grow the destination as required */ |
|
42 if (c->alloc < digs) { |
|
43 if ((res = mp_grow (c, digs)) != MP_OKAY) { |
|
44 return res; |
|
45 } |
|
46 } |
|
47 |
142
|
48 /* number of output digits to produce */ |
|
49 pa = MIN(digs, a->used + b->used); |
2
|
50 |
142
|
51 /* clear the carry */ |
|
52 _W = 0; |
|
53 for (ix = 0; ix <= pa; ix++) { |
|
54 int tx, ty; |
|
55 int iy; |
|
56 mp_digit *tmpx, *tmpy; |
|
57 |
|
58 /* get offsets into the two bignums */ |
|
59 ty = MIN(b->used-1, ix); |
|
60 tx = ix - ty; |
2
|
61 |
142
|
62 /* setup temp aliases */ |
|
63 tmpx = a->dp + tx; |
|
64 tmpy = b->dp + ty; |
2
|
65 |
142
|
66 /* this is the number of times the loop will iterrate, essentially its |
|
67 while (tx++ < a->used && ty-- >= 0) { ... } |
2
|
68 */ |
142
|
69 iy = MIN(a->used-tx, ty+1); |
2
|
70 |
142
|
71 /* execute loop */ |
|
72 for (iz = 0; iz < iy; ++iz) { |
|
73 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
|
74 } |
2
|
75 |
142
|
76 /* store term */ |
|
77 W[ix] = ((mp_digit)_W) & MP_MASK; |
2
|
78 |
142
|
79 /* make next carry */ |
|
80 _W = _W >> ((mp_word)DIGIT_BIT); |
2
|
81 } |
|
82 |
|
83 /* setup dest */ |
142
|
84 olduse = c->used; |
2
|
85 c->used = digs; |
|
86 |
|
87 { |
|
88 register mp_digit *tmpc; |
|
89 tmpc = c->dp; |
142
|
90 for (ix = 0; ix < digs; ix++) { |
2
|
91 /* now extract the previous digit [below the carry] */ |
142
|
92 *tmpc++ = W[ix]; |
2
|
93 } |
|
94 |
|
95 /* clear unused digits [that existed in the old copy of c] */ |
|
96 for (; ix < olduse; ix++) { |
|
97 *tmpc++ = 0; |
|
98 } |
|
99 } |
|
100 mp_clamp (c); |
|
101 return MP_OKAY; |
|
102 } |
142
|
103 #endif |