bn_mp_mul.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_MUL_C
00003 /* LibTomMath, multiple-precision integer library -- Tom St Denis
00004  *
00005  * LibTomMath is a library that provides multiple-precision
00006  * integer arithmetic as well as number theoretic functionality.
00007  *
00008  * The library was designed directly after the MPI library by
00009  * Michael Fromberger but has been written from scratch with
00010  * additional optimizations in place.
00011  *
00012  * The library is free for all purposes without any express
00013  * guarantee it works.
00014  *
00015  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
00016  */
00017 
00018 /* high level multiplication (handles sign) */
00019 int mp_mul (mp_int * a, mp_int * b, mp_int * c)
00020 {
00021   int     res, neg;
00022   neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
00023 
00024   /* use Toom-Cook? */
00025 #ifdef BN_MP_TOOM_MUL_C
00026   if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
00027     res = mp_toom_mul(a, b, c);
00028   } else 
00029 #endif
00030 #ifdef BN_MP_KARATSUBA_MUL_C
00031   /* use Karatsuba? */
00032   if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
00033     res = mp_karatsuba_mul (a, b, c);
00034   } else 
00035 #endif
00036   {
00037     /* can we use the fast multiplier?
00038      *
00039      * The fast multiplier can be used if the output will 
00040      * have less than MP_WARRAY digits and the number of 
00041      * digits won't affect carry propagation
00042      */
00043     int     digs = a->used + b->used + 1;
00044 
00045 #ifdef BN_FAST_S_MP_MUL_DIGS_C
00046     if ((digs < MP_WARRAY) &&
00047         MIN(a->used, b->used) <= 
00048         (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
00049       res = fast_s_mp_mul_digs (a, b, c, digs);
00050     } else 
00051 #endif
00052 #ifdef BN_S_MP_MUL_DIGS_C
00053       res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
00054 #else
00055       res = MP_VAL;
00056 #endif
00057 
00058   }
00059   c->sign = (c->used > 0) ? neg : MP_ZPOS;
00060   return res;
00061 }
00062 #endif
00063 
00064 /* $Source: /cvsroot/tcl/libtommath/bn_mp_mul.c,v $ */
00065 /* $Revision: 1.1.1.3 $ */
00066 /* $Date: 2006/12/01 00:08:11 $ */



Generated on Wed Mar 12 12:18:24 2008 by  doxygen 1.5.1