1 /*	$NetBSD: bn_mp_expt_d.c,v 1.1.1.1 2011/04/13 18:14:54 elric Exp $	*/
2 
3 #include <tommath.h>
4 #ifdef BN_MP_EXPT_D_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 /* calculate c = a**b  using a square-multiply algorithm */
21 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
22 {
23   int     res, x;
24   mp_int  g;
25 
26   if ((res = mp_init_copy (&g, a)) != MP_OKAY) {
27     return res;
28   }
29 
30   /* set initial result */
31   mp_set (c, 1);
32 
33   for (x = 0; x < (int) DIGIT_BIT; x++) {
34     /* square */
35     if ((res = mp_sqr (c, c)) != MP_OKAY) {
36       mp_clear (&g);
37       return res;
38     }
39 
40     /* if the bit is set multiply */
41     if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
42       if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
43          mp_clear (&g);
44          return res;
45       }
46     }
47 
48     /* shift to next bit */
49     b <<= 1;
50   }
51 
52   mp_clear (&g);
53   return MP_OKAY;
54 }
55 #endif
56 
57 /* Source: /cvs/libtom/libtommath/bn_mp_expt_d.c,v */
58 /* Revision: 1.4 */
59 /* Date: 2006/12/28 01:25:13 */
60