Mercurial > dropbear
annotate libtommath/bn_mp_expt_d_ex.c @ 1515:d68d61e7056a
include config.h for options.h. don't need to include options.h when
includes.h brings it in
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 18 Feb 2018 11:22:13 +0800 |
parents | 8bba51a55704 |
children | f52919ffd3b1 |
rev | line source |
---|---|
1436 | 1 #include <tommath_private.h> |
2 #ifdef BN_MP_EXPT_D_EX_C | |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | |
4 * | |
5 * LibTomMath is a library that provides multiple-precision | |
6 * integer arithmetic as well as number theoretic functionality. | |
7 * | |
8 * The library was designed directly after the MPI library by | |
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 * | |
15 * Tom St Denis, [email protected], http://libtom.org | |
16 */ | |
17 | |
18 /* calculate c = a**b using a square-multiply algorithm */ | |
19 int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast) | |
20 { | |
21 int res; | |
22 unsigned int x; | |
23 | |
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 if (fast != 0) { | |
34 while (b > 0) { | |
35 /* if the bit is set multiply */ | |
36 if ((b & 1) != 0) { | |
37 if ((res = mp_mul (c, &g, c)) != MP_OKAY) { | |
38 mp_clear (&g); | |
39 return res; | |
40 } | |
41 } | |
42 | |
43 /* square */ | |
44 if (b > 1) { | |
45 if ((res = mp_sqr (&g, &g)) != MP_OKAY) { | |
46 mp_clear (&g); | |
47 return res; | |
48 } | |
49 } | |
50 | |
51 /* shift to next bit */ | |
52 b >>= 1; | |
53 } | |
54 } | |
55 else { | |
56 for (x = 0; x < DIGIT_BIT; x++) { | |
57 /* square */ | |
58 if ((res = mp_sqr (c, c)) != MP_OKAY) { | |
59 mp_clear (&g); | |
60 return res; | |
61 } | |
62 | |
63 /* if the bit is set multiply */ | |
64 if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) { | |
65 if ((res = mp_mul (c, &g, c)) != MP_OKAY) { | |
66 mp_clear (&g); | |
67 return res; | |
68 } | |
69 } | |
70 | |
71 /* shift to next bit */ | |
72 b <<= 1; | |
73 } | |
74 } /* if ... else */ | |
75 | |
76 mp_clear (&g); | |
77 return MP_OKAY; | |
78 } | |
79 #endif | |
80 | |
1470
8bba51a55704
Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents:
1436
diff
changeset
|
81 /* ref: $Format:%D$ */ |
8bba51a55704
Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents:
1436
diff
changeset
|
82 /* git commit: $Format:%H$ */ |
8bba51a55704
Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents:
1436
diff
changeset
|
83 /* commit time: $Format:%ai$ */ |