Mercurial > dropbear
comparison libtommath/bn_fast_mp_invmod.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 | 5ff8218bcee9 |
children | 8bba51a55704 |
comparison
equal
deleted
inserted
replaced
1435:f849a5ca2efc | 1436:60fc6476e044 |
---|---|
1 #include <tommath.h> | 1 #include <tommath_private.h> |
2 #ifdef BN_FAST_MP_INVMOD_C | 2 #ifdef BN_FAST_MP_INVMOD_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 the modular inverse via binary extended euclidean algorithm, | 18 /* computes the modular inverse via binary extended euclidean algorithm, |
19 * that is c = 1/a mod b | 19 * that is c = 1/a mod b |
20 * | 20 * |
25 { | 25 { |
26 mp_int x, y, u, v, B, D; | 26 mp_int x, y, u, v, B, D; |
27 int res, neg; | 27 int res, neg; |
28 | 28 |
29 /* 2. [modified] b must be odd */ | 29 /* 2. [modified] b must be odd */ |
30 if (mp_iseven (b) == 1) { | 30 if (mp_iseven (b) == MP_YES) { |
31 return MP_VAL; | 31 return MP_VAL; |
32 } | 32 } |
33 | 33 |
34 /* init all our temps */ | 34 /* init all our temps */ |
35 if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) { | 35 if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) { |
55 } | 55 } |
56 mp_set (&D, 1); | 56 mp_set (&D, 1); |
57 | 57 |
58 top: | 58 top: |
59 /* 4. while u is even do */ | 59 /* 4. while u is even do */ |
60 while (mp_iseven (&u) == 1) { | 60 while (mp_iseven (&u) == MP_YES) { |
61 /* 4.1 u = u/2 */ | 61 /* 4.1 u = u/2 */ |
62 if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { | 62 if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { |
63 goto LBL_ERR; | 63 goto LBL_ERR; |
64 } | 64 } |
65 /* 4.2 if B is odd then */ | 65 /* 4.2 if B is odd then */ |
66 if (mp_isodd (&B) == 1) { | 66 if (mp_isodd (&B) == MP_YES) { |
67 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { | 67 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { |
68 goto LBL_ERR; | 68 goto LBL_ERR; |
69 } | 69 } |
70 } | 70 } |
71 /* B = B/2 */ | 71 /* B = B/2 */ |
73 goto LBL_ERR; | 73 goto LBL_ERR; |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 /* 5. while v is even do */ | 77 /* 5. while v is even do */ |
78 while (mp_iseven (&v) == 1) { | 78 while (mp_iseven (&v) == MP_YES) { |
79 /* 5.1 v = v/2 */ | 79 /* 5.1 v = v/2 */ |
80 if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { | 80 if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { |
81 goto LBL_ERR; | 81 goto LBL_ERR; |
82 } | 82 } |
83 /* 5.2 if D is odd then */ | 83 /* 5.2 if D is odd then */ |
84 if (mp_isodd (&D) == 1) { | 84 if (mp_isodd (&D) == MP_YES) { |
85 /* D = (D-x)/2 */ | 85 /* D = (D-x)/2 */ |
86 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { | 86 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { |
87 goto LBL_ERR; | 87 goto LBL_ERR; |
88 } | 88 } |
89 } | 89 } |
113 goto LBL_ERR; | 113 goto LBL_ERR; |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 /* if not zero goto step 4 */ | 117 /* if not zero goto step 4 */ |
118 if (mp_iszero (&u) == 0) { | 118 if (mp_iszero (&u) == MP_NO) { |
119 goto top; | 119 goto top; |
120 } | 120 } |
121 | 121 |
122 /* now a = C, b = D, gcd == g*v */ | 122 /* now a = C, b = D, gcd == g*v */ |
123 | 123 |
141 LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL); | 141 LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL); |
142 return res; | 142 return res; |
143 } | 143 } |
144 #endif | 144 #endif |
145 | 145 |
146 /* $Source: /cvs/libtom/libtommath/bn_fast_mp_invmod.c,v $ */ | 146 /* $Source$ */ |
147 /* $Revision: 1.3 $ */ | 147 /* $Revision$ */ |
148 /* $Date: 2006/03/31 14:18:44 $ */ | 148 /* $Date$ */ |