142
|
1 #include <tommath.h> |
|
2 #ifdef BN_FAST_S_MP_MUL_HIGH_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 |
142
|
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 |
2
|
20 * to see how it works. |
|
21 * |
|
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. |
|
24 * |
|
25 * Based on Algorithm 14.12 on pp.595 of HAC. |
|
26 */ |
|
27 int |
|
28 fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
|
29 { |
142
|
30 int olduse, res, pa, ix, iz; |
|
31 mp_digit W[MP_WARRAY]; |
|
32 mp_word _W; |
2
|
33 |
142
|
34 /* grow the destination as required */ |
|
35 pa = a->used + b->used; |
|
36 if (c->alloc < pa) { |
|
37 if ((res = mp_grow (c, pa)) != MP_OKAY) { |
2
|
38 return res; |
|
39 } |
|
40 } |
|
41 |
142
|
42 /* number of output digits to produce */ |
|
43 pa = a->used + b->used; |
|
44 _W = 0; |
|
45 for (ix = digs; ix <= pa; ix++) { |
|
46 int tx, ty, iy; |
|
47 mp_digit *tmpx, *tmpy; |
2
|
48 |
142
|
49 /* get offsets into the two bignums */ |
|
50 ty = MIN(b->used-1, ix); |
|
51 tx = ix - ty; |
2
|
52 |
142
|
53 /* setup temp aliases */ |
|
54 tmpx = a->dp + tx; |
|
55 tmpy = b->dp + ty; |
2
|
56 |
142
|
57 /* this is the number of times the loop will iterrate, essentially its |
|
58 while (tx++ < a->used && ty-- >= 0) { ... } |
2
|
59 */ |
142
|
60 iy = MIN(a->used-tx, ty+1); |
|
61 |
|
62 /* execute loop */ |
|
63 for (iz = 0; iz < iy; iz++) { |
|
64 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
2
|
65 } |
|
66 |
142
|
67 /* store term */ |
|
68 W[ix] = ((mp_digit)_W) & MP_MASK; |
|
69 |
|
70 /* make next carry */ |
|
71 _W = _W >> ((mp_word)DIGIT_BIT); |
2
|
72 } |
|
73 |
|
74 /* setup dest */ |
142
|
75 olduse = c->used; |
|
76 c->used = pa; |
|
77 |
|
78 { |
|
79 register mp_digit *tmpc; |
2
|
80 |
142
|
81 tmpc = c->dp + digs; |
|
82 for (ix = digs; ix <= pa; ix++) { |
|
83 /* now extract the previous digit [below the carry] */ |
|
84 *tmpc++ = W[ix]; |
|
85 } |
2
|
86 |
142
|
87 /* clear unused digits [that existed in the old copy of c] */ |
|
88 for (; ix < olduse; ix++) { |
|
89 *tmpc++ = 0; |
|
90 } |
2
|
91 } |
|
92 mp_clamp (c); |
|
93 return MP_OKAY; |
|
94 } |
142
|
95 #endif |