Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (61 sloc) 1.752 kb
e549ccf added libtommath-0.32
Tom St Denis authored
1 #include <tommath.h>
2 #ifdef BN_MP_MUL_C
57354e1 added libtommath-0.12
Tom St Denis authored
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa added libtommath-0.25
Tom St Denis authored
5 * LibTomMath is a library that provides multiple-precision
57354e1 added libtommath-0.12
Tom St Denis authored
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa added libtommath-0.25
Tom St Denis authored
8 * The library was designed directly after the MPI library by
57354e1 added libtommath-0.12
Tom St Denis authored
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 *
333aebc added libtommath-0.41
Tom St Denis authored
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
57354e1 added libtommath-0.12
Tom St Denis authored
16 */
17
18 /* high level multiplication (handles sign) */
455bb4d added libtommath-0.28
Tom St Denis authored
19 int mp_mul (mp_int * a, mp_int * b, mp_int * c)
57354e1 added libtommath-0.12
Tom St Denis authored
20 {
b66471f added libtommath-0.13
Tom St Denis authored
21 int res, neg;
57354e1 added libtommath-0.12
Tom St Denis authored
22 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
c1da6aa added libtommath-0.25
Tom St Denis authored
23
24 /* use Toom-Cook? */
e549ccf added libtommath-0.32
Tom St Denis authored
25 #ifdef BN_MP_TOOM_MUL_C
0ef44ce added libtommath-0.18
Tom St Denis authored
26 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
27 res = mp_toom_mul(a, b, c);
e549ccf added libtommath-0.32
Tom St Denis authored
28 } else
29 #endif
30 #ifdef BN_MP_KARATSUBA_MUL_C
c1da6aa added libtommath-0.25
Tom St Denis authored
31 /* use Karatsuba? */
e549ccf added libtommath-0.32
Tom St Denis authored
32 if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
57354e1 added libtommath-0.12
Tom St Denis authored
33 res = mp_karatsuba_mul (a, b, c);
e549ccf added libtommath-0.32
Tom St Denis authored
34 } else
35 #endif
36 {
fd181cc added libtommath-0.17
Tom St Denis authored
37 /* can we use the fast multiplier?
b66471f added libtommath-0.13
Tom St Denis authored
38 *
0ef44ce added libtommath-0.18
Tom St Denis authored
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
b66471f added libtommath-0.13
Tom St Denis authored
42 */
43 int digs = a->used + b->used + 1;
44
e549ccf added libtommath-0.32
Tom St Denis authored
45 #ifdef BN_FAST_S_MP_MUL_DIGS_C
0ef44ce added libtommath-0.18
Tom St Denis authored
46 if ((digs < MP_WARRAY) &&
47 MIN(a->used, b->used) <=
48 (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
b66471f added libtommath-0.13
Tom St Denis authored
49 res = fast_s_mp_mul_digs (a, b, c, digs);
e549ccf added libtommath-0.32
Tom St Denis authored
50 } else
51 #endif
52 #ifdef BN_S_MP_MUL_DIGS_C
53 res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
54 #else
55 res = MP_VAL;
56 #endif
57
57354e1 added libtommath-0.12
Tom St Denis authored
58 }
8eaa988 added libtommath-0.31
Tom St Denis authored
59 c->sign = (c->used > 0) ? neg : MP_ZPOS;
57354e1 added libtommath-0.12
Tom St Denis authored
60 return res;
61 }
e549ccf added libtommath-0.32
Tom St Denis authored
62 #endif
9294e79 added libtommath-0.36
Tom St Denis authored
63
64 /* $Source$ */
65 /* $Revision$ */
66 /* $Date$ */
Something went wrong with that request. Please try again.