bn_mp_expt_d.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_EXPT_D_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 /* calculate c = a**b  using a square-multiply algorithm */
00019 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
00020 {
00021   int     res, x;
00022   mp_int  g;
00023 
00024   if ((res = mp_init_copy (&g, a)) != MP_OKAY) {
00025     return res;
00026   }
00027 
00028   /* set initial result */
00029   mp_set (c, 1);
00030 
00031   for (x = 0; x < (int) DIGIT_BIT; x++) {
00032     /* square */
00033     if ((res = mp_sqr (c, c)) != MP_OKAY) {
00034       mp_clear (&g);
00035       return res;
00036     }
00037 
00038     /* if the bit is set multiply */
00039     if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
00040       if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
00041          mp_clear (&g);
00042          return res;
00043       }
00044     }
00045 
00046     /* shift to next bit */
00047     b <<= 1;
00048   }
00049 
00050   mp_clear (&g);
00051   return MP_OKAY;
00052 }
00053 #endif
00054 
00055 /* $Source: /cvsroot/tcl/libtommath/bn_mp_expt_d.c,v $ */
00056 /* $Revision: 1.1.1.3 $ */
00057 /* $Date: 2006/12/01 00:08:11 $ */



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