Mercurial > dropbear
comparison libtommath/bn_mp_exptmod_fast.c @ 348:cd14c94fe89c debug-unrandom
add some more copious debugging
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 06 Aug 2006 15:29:41 +0000 |
parents | eed26cff980b |
children | e66eec4dcba7 |
comparison
equal
deleted
inserted
replaced
347:381834084475 | 348:cd14c94fe89c |
---|---|
1 #include "../dbutil.h" | |
1 #include <tommath.h> | 2 #include <tommath.h> |
2 #ifdef BN_MP_EXPTMOD_FAST_C | 3 #ifdef BN_MP_EXPTMOD_FAST_C |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | 4 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
4 * | 5 * |
5 * LibTomMath is a library that provides multiple-precision | 6 * LibTomMath is a library that provides multiple-precision |
63 if (winsize > 5) { | 64 if (winsize > 5) { |
64 winsize = 5; | 65 winsize = 5; |
65 } | 66 } |
66 #endif | 67 #endif |
67 | 68 |
69 dropbear_trace("mp_exptmod_fast x bits %d redmode %d", x, redmode); | |
70 | |
68 /* init M array */ | 71 /* init M array */ |
69 /* init first cell */ | 72 /* init first cell */ |
70 if ((err = mp_init(&M[1])) != MP_OKAY) { | 73 if ((err = mp_init(&M[1])) != MP_OKAY) { |
71 return err; | 74 return err; |
72 } | 75 } |
208 /* read next digit and reset bitcnt */ | 211 /* read next digit and reset bitcnt */ |
209 buf = X->dp[digidx--]; | 212 buf = X->dp[digidx--]; |
210 bitcnt = (int)DIGIT_BIT; | 213 bitcnt = (int)DIGIT_BIT; |
211 } | 214 } |
212 | 215 |
216 dropbear_trace("mp_exptmod_fast digidx %d buf %u", digidx+1, buf); | |
217 | |
213 /* grab the next msb from the exponent */ | 218 /* grab the next msb from the exponent */ |
214 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1; | 219 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1; |
215 buf <<= (mp_digit)1; | 220 buf <<= (mp_digit)1; |
216 | 221 |
217 /* if the bit is zero and mode == 0 then we ignore it | 222 /* if the bit is zero and mode == 0 then we ignore it |
229 goto LBL_RES; | 234 goto LBL_RES; |
230 } | 235 } |
231 if ((err = redux (&res, P, mp)) != MP_OKAY) { | 236 if ((err = redux (&res, P, mp)) != MP_OKAY) { |
232 goto LBL_RES; | 237 goto LBL_RES; |
233 } | 238 } |
239 print_mp_int("mp_exptmod_fast bit=0 mode=1 sq", &res); | |
234 continue; | 240 continue; |
235 } | 241 } |
236 | 242 |
237 /* else we add it to the window */ | 243 /* else we add it to the window */ |
238 bitbuf |= (y << (winsize - ++bitcpy)); | 244 bitbuf |= (y << (winsize - ++bitcpy)); |
261 /* empty window and reset */ | 267 /* empty window and reset */ |
262 bitcpy = 0; | 268 bitcpy = 0; |
263 bitbuf = 0; | 269 bitbuf = 0; |
264 mode = 1; | 270 mode = 1; |
265 } | 271 } |
272 print_mp_int("mp_exptmod_fast other way", &res); | |
266 } | 273 } |
267 | 274 |
268 /* if bits remain then square/multiply */ | 275 /* if bits remain then square/multiply */ |
269 if (mode == 2 && bitcpy > 0) { | 276 if (mode == 2 && bitcpy > 0) { |
270 /* square then multiply if the bit is set */ | 277 /* square then multiply if the bit is set */ |
287 goto LBL_RES; | 294 goto LBL_RES; |
288 } | 295 } |
289 } | 296 } |
290 } | 297 } |
291 } | 298 } |
299 print_mp_int("mp_exptmod_fast remaining bits", &res); | |
292 | 300 |
293 if (redmode == 0) { | 301 if (redmode == 0) { |
294 /* fixup result if Montgomery reduction is used | 302 /* fixup result if Montgomery reduction is used |
295 * recall that any value in a Montgomery system is | 303 * recall that any value in a Montgomery system is |
296 * actually multiplied by R mod n. So we have | 304 * actually multiplied by R mod n. So we have |
298 * of R. | 306 * of R. |
299 */ | 307 */ |
300 if ((err = redux(&res, P, mp)) != MP_OKAY) { | 308 if ((err = redux(&res, P, mp)) != MP_OKAY) { |
301 goto LBL_RES; | 309 goto LBL_RES; |
302 } | 310 } |
311 print_mp_int("mp_exptmod_fast fixup", &res); | |
303 } | 312 } |
304 | 313 |
305 /* swap res with Y */ | 314 /* swap res with Y */ |
306 mp_exch (&res, Y); | 315 mp_exch (&res, Y); |
307 err = MP_OKAY; | 316 err = MP_OKAY; |