2 #ifdef BN_MP_INVMOD_SLOW_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
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.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* hac 14.61, pp608 */
19 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
21 mp_int x, y, u, v, A, B, C, D;
24 /* b cannot be negative */
25 if (b->sign == MP_NEG || mp_iszero(b) == 1) {
30 if ((res = mp_init_multi(&x, &y, &u, &v,
31 &A, &B, &C, &D, NULL)) != MP_OKAY) {
36 if ((res = mp_mod(a, b, &x)) != MP_OKAY) {
39 if ((res = mp_copy (b, &y)) != MP_OKAY) {
43 /* 2. [modified] if x,y are both even then return an error! */
44 if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
49 /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
50 if ((res = mp_copy (&x, &u)) != MP_OKAY) {
53 if ((res = mp_copy (&y, &v)) != MP_OKAY) {
60 /* 4. while u is even do */
61 while (mp_iseven (&u) == 1) {
63 if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
66 /* 4.2 if A or B is odd then */
67 if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
68 /* A = (A+y)/2, B = (B-x)/2 */
69 if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
72 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
76 /* A = A/2, B = B/2 */
77 if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
80 if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
85 /* 5. while v is even do */
86 while (mp_iseven (&v) == 1) {
88 if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
91 /* 5.2 if C or D is odd then */
92 if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
93 /* C = (C+y)/2, D = (D-x)/2 */
94 if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
97 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
101 /* C = C/2, D = D/2 */
102 if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
105 if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
110 /* 6. if u >= v then */
111 if (mp_cmp (&u, &v) != MP_LT) {
112 /* u = u - v, A = A - C, B = B - D */
113 if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
117 if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
121 if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
125 /* v - v - u, C = C - A, D = D - B */
126 if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
130 if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
134 if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
139 /* if not zero goto step 4 */
140 if (mp_iszero (&u) == 0)
143 /* now a = C, b = D, gcd == g*v */
145 /* if v != 1 then there is no inverse */
146 if (mp_cmp_d (&v, 1) != MP_EQ) {
152 while (mp_cmp_d(&C, 0) == MP_LT) {
153 if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
159 while (mp_cmp_mag(&C, b) != MP_LT) {
160 if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
165 /* C is now the inverse */
168 LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
173 /* $Source: /cvs/libtom/libtommath/bn_mp_invmod_slow.c,v $ */
174 /* $Revision: 1.3 $ */
175 /* $Date: 2006/03/31 14:18:44 $ */