comparison libtommath/bn_fast_s_mp_mul_high_digs.c @ 284:eed26cff980b

propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583) to branch 'au.asn.ucc.matt.dropbear' (head fff0894a0399405a9410ea1c6d118f342cf2aa64)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:49 +0000
parents
children 5ff8218bcee9
comparison
equal deleted inserted replaced
283:bd240aa12ba7 284:eed26cff980b
1 #include <tommath.h>
2 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
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 /* 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.
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 fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
28 {
29 int olduse, res, pa, ix, iz;
30 mp_digit W[MP_WARRAY];
31 mp_word _W;
32
33 /* grow the destination as required */
34 pa = a->used + b->used;
35 if (c->alloc < pa) {
36 if ((res = mp_grow (c, pa)) != MP_OKAY) {
37 return res;
38 }
39 }
40
41 /* number of output digits to produce */
42 pa = a->used + b->used;
43 _W = 0;
44 for (ix = digs; ix < pa; ix++) {
45 int tx, ty, iy;
46 mp_digit *tmpx, *tmpy;
47
48 /* get offsets into the two bignums */
49 ty = MIN(b->used-1, ix);
50 tx = ix - ty;
51
52 /* setup temp aliases */
53 tmpx = a->dp + tx;
54 tmpy = b->dp + ty;
55
56 /* this is the number of times the loop will iterrate, essentially its
57 while (tx++ < a->used && ty-- >= 0) { ... }
58 */
59 iy = MIN(a->used-tx, ty+1);
60
61 /* execute loop */
62 for (iz = 0; iz < iy; iz++) {
63 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
64 }
65
66 /* store term */
67 W[ix] = ((mp_digit)_W) & MP_MASK;
68
69 /* make next carry */
70 _W = _W >> ((mp_word)DIGIT_BIT);
71 }
72
73 /* store final carry */
74 W[ix] = (mp_digit)(_W & MP_MASK);
75
76 /* setup dest */
77 olduse = c->used;
78 c->used = pa;
79
80 {
81 register mp_digit *tmpc;
82
83 tmpc = c->dp + digs;
84 for (ix = digs; ix <= pa; ix++) {
85 /* now extract the previous digit [below the carry] */
86 *tmpc++ = W[ix];
87 }
88
89 /* clear unused digits [that existed in the old copy of c] */
90 for (; ix < olduse; ix++) {
91 *tmpc++ = 0;
92 }
93 }
94 mp_clamp (c);
95 return MP_OKAY;
96 }
97 #endif