comparison libtommath/bn_mp_expt_d_ex.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_EXPT_D_EX_C 2 #ifdef BN_MP_EXPT_D_EX_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* calculate c = a**b using a square-multiply algorithm */ 15 /* 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) 16 int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
20 { 17 {
21 int res; 18 int res;
22 unsigned int x; 19 unsigned int x;
23 20
24 mp_int g; 21 mp_int g;
25 22
26 if ((res = mp_init_copy (&g, a)) != MP_OKAY) { 23 if ((res = mp_init_copy(&g, a)) != MP_OKAY) {
27 return res; 24 return res;
28 } 25 }
29 26
30 /* set initial result */ 27 /* set initial result */
31 mp_set (c, 1); 28 mp_set(c, 1uL);
32 29
33 if (fast != 0) { 30 if (fast != 0) {
34 while (b > 0) { 31 while (b > 0u) {
35 /* if the bit is set multiply */ 32 /* if the bit is set multiply */
36 if ((b & 1) != 0) { 33 if ((b & 1u) != 0u) {
37 if ((res = mp_mul (c, &g, c)) != MP_OKAY) { 34 if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
38 mp_clear (&g); 35 mp_clear(&g);
39 return res; 36 return res;
40 } 37 }
38 }
39
40 /* square */
41 if (b > 1u) {
42 if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
43 mp_clear(&g);
44 return res;
45 }
46 }
47
48 /* shift to next bit */
49 b >>= 1;
41 } 50 }
51 } else {
52 for (x = 0; x < (unsigned)DIGIT_BIT; x++) {
53 /* square */
54 if ((res = mp_sqr(c, c)) != MP_OKAY) {
55 mp_clear(&g);
56 return res;
57 }
42 58
43 /* square */ 59 /* if the bit is set multiply */
44 if (b > 1) { 60 if ((b & ((mp_digit)1 << (DIGIT_BIT - 1))) != 0u) {
45 if ((res = mp_sqr (&g, &g)) != MP_OKAY) { 61 if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
46 mp_clear (&g); 62 mp_clear(&g);
47 return res; 63 return res;
48 } 64 }
65 }
66
67 /* shift to next bit */
68 b <<= 1;
49 } 69 }
70 } /* if ... else */
50 71
51 /* shift to next bit */ 72 mp_clear(&g);
52 b >>= 1; 73 return MP_OKAY;
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 } 74 }
79 #endif 75 #endif
80 76
81 /* ref: $Format:%D$ */ 77 /* ref: HEAD -> master, tag: v1.1.0 */
82 /* git commit: $Format:%H$ */ 78 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
83 /* commit time: $Format:%ai$ */ 79 /* commit time: 2019-01-28 20:32:32 +0100 */