comparison libtommath/bn_mp_exptmod_fast.c @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 4fbf9a7556ed
children 8bba51a55704
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
1 #include <tommath.h> 1 #include <tommath_private.h>
2 #ifdef BN_MP_EXPTMOD_FAST_C 2 #ifdef BN_MP_EXPTMOD_FAST_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.
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * The library is free for all purposes without any express
13 * guarantee it works. 13 * guarantee it works.
14 * 14 *
15 * Tom St Denis, [email protected], http://math.libtomcrypt.com 15 * Tom St Denis, [email protected], http://libtom.org
16 */ 16 */
17 17
18 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85 18 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
19 * 19 *
20 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation. 20 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
94 goto LBL_M; 94 goto LBL_M;
95 #endif 95 #endif
96 96
97 /* automatically pick the comba one if available (saves quite a few calls/ifs) */ 97 /* automatically pick the comba one if available (saves quite a few calls/ifs) */
98 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C 98 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
99 if (((P->used * 2 + 1) < MP_WARRAY) && 99 if ((((P->used * 2) + 1) < MP_WARRAY) &&
100 P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { 100 (P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
101 redux = fast_mp_montgomery_reduce; 101 redux = fast_mp_montgomery_reduce;
102 } else 102 } else
103 #endif 103 #endif
104 { 104 {
105 #ifdef BN_MP_MONTGOMERY_REDUCE_C 105 #ifdef BN_MP_MONTGOMERY_REDUCE_C
217 /* if the bit is zero and mode == 0 then we ignore it 217 /* if the bit is zero and mode == 0 then we ignore it
218 * These represent the leading zero bits before the first 1 bit 218 * These represent the leading zero bits before the first 1 bit
219 * in the exponent. Technically this opt is not required but it 219 * in the exponent. Technically this opt is not required but it
220 * does lower the # of trivial squaring/reductions used 220 * does lower the # of trivial squaring/reductions used
221 */ 221 */
222 if (mode == 0 && y == 0) { 222 if ((mode == 0) && (y == 0)) {
223 continue; 223 continue;
224 } 224 }
225 225
226 /* if the bit is zero and mode == 1 then we square */ 226 /* if the bit is zero and mode == 1 then we square */
227 if (mode == 1 && y == 0) { 227 if ((mode == 1) && (y == 0)) {
228 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { 228 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
229 goto LBL_RES; 229 goto LBL_RES;
230 } 230 }
231 if ((err = redux (&res, P, mp)) != MP_OKAY) { 231 if ((err = redux (&res, P, mp)) != MP_OKAY) {
232 goto LBL_RES; 232 goto LBL_RES;
264 mode = 1; 264 mode = 1;
265 } 265 }
266 } 266 }
267 267
268 /* if bits remain then square/multiply */ 268 /* if bits remain then square/multiply */
269 if (mode == 2 && bitcpy > 0) { 269 if ((mode == 2) && (bitcpy > 0)) {
270 /* square then multiply if the bit is set */ 270 /* square then multiply if the bit is set */
271 for (x = 0; x < bitcpy; x++) { 271 for (x = 0; x < bitcpy; x++) {
272 if ((err = mp_sqr (&res, &res)) != MP_OKAY) { 272 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
273 goto LBL_RES; 273 goto LBL_RES;
274 } 274 }
314 return err; 314 return err;
315 } 315 }
316 #endif 316 #endif
317 317
318 318
319 /* $Source: /cvs/libtom/libtommath/bn_mp_exptmod_fast.c,v $ */ 319 /* $Source$ */
320 /* $Revision: 1.3 $ */ 320 /* $Revision$ */
321 /* $Date: 2006/03/31 14:18:44 $ */ 321 /* $Date$ */