| 1 | #cxx . . $ `<file>/lib/hcrypto/libtommath/bn_mp_montgomery_reduce.c`/include <tommath.h>cxx . . $ `<file>/lib/hcrypto/libtommath/tommath.h`/ |
| 2 | #ifdef BN_MP_MONTGOMERY_REDUCE_Ccxx . . $ `lib/hcrypto/libtommath/tommath_class.h:349:12`! |
| 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 | /* computes xR**-1 == x (mod N) via Montgomery Reduction */ |
| 19 | int |
| 20 | mp_montgomery_reducecxx . . $ mp_montgomery_reduce(2b464b709f76f4ca). (mp_intcxx . . $ mp_int# * x, mp_intcxx . . $ mp_int# * n, mp_digitcxx . . $ mp_digit# rho) |
| 21 | { |
| 22 | int ix, res, digs; |
| 23 | mp_digitcxx . . $ mp_digit# mu; |
| 24 | |
| 25 | /* can the fast reduction [comba] method be used? |
| 26 | * |
| 27 | * Note that unlike in mul you're safely allowed *less* |
| 28 | * than the available columns [255 per default] since carries |
| 29 | * are fixed up in the inner loop. |
| 30 | */ |
| 31 | digs = n->usedcxx . . $ $anonymous_type_9884617309faeb8c_0#used. * 2 + 1; |
| 32 | if ((digs < MP_WARRAYcxx . . $ `lib/hcrypto/libtommath/tommath.h:176:9`!) && |
| 33 | n->usedcxx . . $ $anonymous_type_9884617309faeb8c_0#used. < |
| 34 | (1 << ((CHAR_BITcxx . . $ `<external>/2419331716647297546/limits.h:64:9`! * sizeof (mp_wordcxx . . $ mp_word#)) - (2 * DIGIT_BITcxx . . $ `lib/hcrypto/libtommath/tommath.h:79:12`!)))) { |
| 35 | return fast_mp_montgomery_reducecxx . . $ fast_mp_montgomery_reduce(2b464b709f76f4ca). (x, n, rho); |
| 36 | } |
| 37 | |
| 38 | /* grow the input as required */ |
| 39 | if (x->alloccxx . . $ $anonymous_type_9884617309faeb8c_0#alloc. < digs) { |
| 40 | if ((res = mp_growcxx . . $ mp_grow(7361bc60091cb21a). (x, digs)) != MP_OKAYcxx . . $ `lib/hcrypto/libtommath/tommath.h:142:9`!) { |
| 41 | return res; |
| 42 | } |
| 43 | } |
| 44 | x->usedcxx . . $ $anonymous_type_9884617309faeb8c_0#used. = digs; |
| 45 | |
| 46 | for (ix = 0; ix < n->usedcxx . . $ $anonymous_type_9884617309faeb8c_0#used.; ix++) { |
| 47 | /* mu = ai * rho mod b |
| 48 | * |
| 49 | * The value of rho must be precalculated via |
| 50 | * montgomery_setup() such that |
| 51 | * it equals -1/n0 mod b this allows the |
| 52 | * following inner loop to reduce the |
| 53 | * input one digit at a time |
| 54 | */ |
| 55 | mu = (mp_digitcxx . . $ mp_digit#) (((mp_wordcxx . . $ mp_word#)x->dpcxx . . $ $anonymous_type_9884617309faeb8c_0#dp.[ix]) * ((mp_wordcxx . . $ mp_word#)rho) & MP_MASKcxx . . $ `lib/hcrypto/libtommath/tommath.h:131:9`!); |
| 56 | |
| 57 | /* a = a + mu * m * b**i */ |
| 58 | { |
| 59 | register int iy; |
| 60 | register mp_digitcxx . . $ mp_digit# *tmpn, *tmpx, u; |
| 61 | register mp_wordcxx . . $ mp_word# r; |
| 62 | |
| 63 | /* alias for digits of the modulus */ |
| 64 | tmpn = n->dpcxx . . $ $anonymous_type_9884617309faeb8c_0#dp.; |
| 65 | |
| 66 | /* alias for the digits of x [the input] */ |
| 67 | tmpx = x->dpcxx . . $ $anonymous_type_9884617309faeb8c_0#dp. + ix; |
| 68 | |
| 69 | /* set the carry to zero */ |
| 70 | u = 0; |
| 71 | |
| 72 | /* Multiply and add in place */ |
| 73 | for (iy = 0; iy < n->usedcxx . . $ $anonymous_type_9884617309faeb8c_0#used.; iy++) { |
| 74 | /* compute product and sum */ |
| 75 | r = ((mp_wordcxx . . $ mp_word#)mu) * ((mp_wordcxx . . $ mp_word#)*tmpn++) + |
| 76 | ((mp_wordcxx . . $ mp_word#) u) + ((mp_wordcxx . . $ mp_word#) * tmpx); |
| 77 | |
| 78 | /* get carry */ |
| 79 | u = (mp_digitcxx . . $ mp_digit#)(r >> ((mp_wordcxx . . $ mp_word#) DIGIT_BITcxx . . $ `lib/hcrypto/libtommath/tommath.h:79:12`!)); |
| 80 | |
| 81 | /* fix digit */ |
| 82 | *tmpx++ = (mp_digitcxx . . $ mp_digit#)(r & ((mp_wordcxx . . $ mp_word#) MP_MASKcxx . . $ `lib/hcrypto/libtommath/tommath.h:131:9`!)); |
| 83 | } |
| 84 | /* At this point the ix'th digit of x should be zero */ |
| 85 | |
| 86 | |
| 87 | /* propagate carries upwards as required*/ |
| 88 | while (u) { |
| 89 | *tmpx += u; |
| 90 | u = *tmpx >> DIGIT_BITcxx . . $ `lib/hcrypto/libtommath/tommath.h:79:12`!; |
| 91 | *tmpx++ &= MP_MASKcxx . . $ `lib/hcrypto/libtommath/tommath.h:131:9`!; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* at this point the n.used'th least |
| 97 | * significant digits of x are all zero |
| 98 | * which means we can shift x to the |
| 99 | * right by n.used digits and the |
| 100 | * residue is unchanged. |
| 101 | */ |
| 102 | |
| 103 | /* x = x/b**n.used */ |
| 104 | mp_clampcxx . . $ mp_clamp(31834e326202325c).(x); |
| 105 | mp_rshdcxx . . $ mp_rshd(34df32d4968831ff). (x, n->usedcxx . . $ $anonymous_type_9884617309faeb8c_0#used.); |
| 106 | |
| 107 | /* if x >= n then x = x - n */ |
| 108 | if (mp_cmp_magcxx . . $ mp_cmp_mag(90c7f7e4c7870e62). (x, n) != MP_LTcxx . . $ `lib/hcrypto/libtommath/tommath.h:135:9`!) { |
| 109 | return s_mp_subcxx . . $ s_mp_sub(b4da12eeac664d95). (x, n, x); |
| 110 | } |
| 111 | |
| 112 | return MP_OKAYcxx . . $ `lib/hcrypto/libtommath/tommath.h:142:9`!; |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | /* $Source: /cvs/libtom/libtommath/bn_mp_montgomery_reduce.c,v $ */ |
| 117 | /* $Revision: 1.4 $ */ |
| 118 | /* $Date: 2006/12/28 01:25:13 $ */ |