comparison bn_mp_mul.c @ 1:22d5cf7d4b1a libtommath

Renaming branch
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:23:46 +0000
parents
children a96ff234ff19
comparison
equal deleted inserted replaced
-1:000000000000 1:22d5cf7d4b1a
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2 *
3 * LibTomMath is a library that provides multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
5 *
6 * The library was designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
9 *
10 * The library is free for all purposes without any express
11 * guarantee it works.
12 *
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org
14 */
15 #include <tommath.h>
16
17 /* high level multiplication (handles sign) */
18 int mp_mul (mp_int * a, mp_int * b, mp_int * c)
19 {
20 int res, neg;
21 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
22
23 #ifndef NO_LTM_TOOM
24
25 /* use Toom-Cook? */
26 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
27 res = mp_toom_mul(a, b, c);
28 } else
29 #endif
30 #ifndef NO_LTM_KARATSUBA
31 /* use Karatsuba? */
32 if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
33 res = mp_karatsuba_mul (a, b, c);
34 } else
35 #endif
36 {
37 /* can we use the fast multiplier?
38 *
39 * The fast multiplier can be used if the output will
40 * have less than MP_WARRAY digits and the number of
41 * digits won't affect carry propagation
42 */
43 int digs = a->used + b->used + 1;
44
45 if ((digs < MP_WARRAY) &&
46 MIN(a->used, b->used) <=
47 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
48 res = fast_s_mp_mul_digs (a, b, c, digs);
49 } else {
50 res = s_mp_mul (a, b, c);
51 }
52 }
53 c->sign = neg;
54 return res;
55 }