xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/libtommath/bn_mp_mul.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: bn_mp_mul.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2 
3 #include <tommath.h>
4 #ifdef BN_MP_MUL_C
5 /* LibTomMath, multiple-precision integer library -- Tom St Denis
6  *
7  * LibTomMath is a library that provides multiple-precision
8  * integer arithmetic as well as number theoretic functionality.
9  *
10  * The library was designed directly after the MPI library by
11  * Michael Fromberger but has been written from scratch with
12  * additional optimizations in place.
13  *
14  * The library is free for all purposes without any express
15  * guarantee it works.
16  *
17  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18  */
19 
20 /* high level multiplication (handles sign) */
mp_mul(mp_int * a,mp_int * b,mp_int * c)21 int mp_mul (mp_int * a, mp_int * b, mp_int * c)
22 {
23   int     res, neg;
24   neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
25 
26   /* use Toom-Cook? */
27 #ifdef BN_MP_TOOM_MUL_C
28   if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
29     res = mp_toom_mul(a, b, c);
30   } else
31 #endif
32 #ifdef BN_MP_KARATSUBA_MUL_C
33   /* use Karatsuba? */
34   if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
35     res = mp_karatsuba_mul (a, b, c);
36   } else
37 #endif
38   {
39     /* can we use the fast multiplier?
40      *
41      * The fast multiplier can be used if the output will
42      * have less than MP_WARRAY digits and the number of
43      * digits won't affect carry propagation
44      */
45     int     digs = a->used + b->used + 1;
46 
47 #ifdef BN_FAST_S_MP_MUL_DIGS_C
48     if ((digs < MP_WARRAY) &&
49         MIN(a->used, b->used) <=
50         (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
51       res = fast_s_mp_mul_digs (a, b, c, digs);
52     } else
53 #endif
54 #ifdef BN_S_MP_MUL_DIGS_C
55       res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
56 #else
57       res = MP_VAL;
58 #endif
59 
60   }
61   c->sign = (c->used > 0) ? neg : MP_ZPOS;
62   return res;
63 }
64 #endif
65 
66 /* Source: /cvs/libtom/libtommath/bn_mp_mul.c,v  */
67 /* Revision: 1.4  */
68 /* Date: 2006/12/28 01:25:13  */
69