This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / crypto / mpi / mpi-inv.c
1 /* mpi-inv.c  -  MPI functions
2  * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include "mpi-internal.h"
22
23
24 /****************
25  * Calculate the multiplicative inverse X of A mod N
26  * That is: Find the solution x for
27  *              1 = (a*x) mod n
28  */
29 int
30 mpi_invm( MPI x, const MPI a, const MPI n )
31 {
32         /* Extended Euclid's algorithm (See TAOPC Vol II, 4.5.2, Alg X)
33          * modified according to Michael Penk's solution for Exercice 35
34          * with further enhancement */
35         MPI u = NULL, v = NULL;
36         MPI u1 = NULL, u2 = NULL, u3 = NULL;
37         MPI v1 = NULL, v2 = NULL, v3 = NULL;
38         MPI t1 = NULL, t2 = NULL, t3 = NULL;
39         unsigned k;
40         int sign;
41         int odd = 0;
42         int rc = -ENOMEM;
43
44         if (mpi_copy(&u, a) < 0) goto cleanup;
45         if (mpi_copy(&v, n) < 0) goto cleanup;
46
47         for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
48                 if (mpi_rshift(u, u, 1) < 0) goto cleanup;
49                 if (mpi_rshift(v, v, 1) < 0) goto cleanup;
50         }
51         odd = mpi_test_bit(v,0);
52
53         u1 = mpi_alloc_set_ui(1); if (!u1) goto cleanup;
54         if( !odd ) {
55                 u2 = mpi_alloc_set_ui(0);
56                 if (!u2) goto cleanup;
57         }
58         if (mpi_copy(&u3, u) < 0) goto cleanup;
59         if (mpi_copy(&v1, v) < 0) goto cleanup;
60         if( !odd ) {
61                 v2 = mpi_alloc( mpi_get_nlimbs(u) );  if (!v2) goto cleanup;
62                 if (mpi_sub( v2, u1, u ) < 0) goto cleanup; /* U is used as const 1 */
63         }
64         if (mpi_copy(&v3, v) < 0) goto cleanup;
65         if( mpi_test_bit(u, 0) ) { /* u is odd */
66                 t1 = mpi_alloc_set_ui(0); if (!t1) goto cleanup;
67                 if( !odd ) {
68                         t2 = mpi_alloc_set_ui(1); if (!t2) goto cleanup;
69                         t2->sign = 1; 
70                 }
71                 if (mpi_copy(&t3, v) < 0) goto cleanup;
72                 t3->sign = !t3->sign;
73                 goto Y4;
74         }
75         else {
76                 t1 = mpi_alloc_set_ui(1); if (!t1) goto cleanup;
77                 if( !odd ) {
78                         t2 = mpi_alloc_set_ui(0); if (!t2) goto cleanup;
79                 }
80                 if (mpi_copy(&t3, u) < 0) goto cleanup;
81         }
82         do {
83                 do {
84                         if( !odd ) {
85                                 if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
86                                         if (mpi_add(t1, t1, v) < 0) goto cleanup;
87                                         if (mpi_sub(t2, t2, u) < 0) goto cleanup;
88                                 }
89                                 if (mpi_rshift(t1, t1, 1) < 0) goto cleanup;
90                                 if (mpi_rshift(t2, t2, 1) < 0) goto cleanup;
91                                 if (mpi_rshift(t3, t3, 1) < 0) goto cleanup;
92                         }
93                         else {
94                                 if( mpi_test_bit(t1, 0) )
95                                         if (mpi_add(t1, t1, v) < 0) goto cleanup;
96                                 if (mpi_rshift(t1, t1, 1) < 0) goto cleanup;
97                                 if (mpi_rshift(t3, t3, 1) < 0) goto cleanup;
98                         }
99                 Y4:
100                         ;
101                 } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
102
103                 if( !t3->sign ) {
104                         if (mpi_set(u1, t1) < 0) goto cleanup;
105                         if( !odd )
106                                 if (mpi_set(u2, t2) < 0) goto cleanup;
107                         if (mpi_set(u3, t3) < 0) goto cleanup;
108                 }
109                 else {
110                         if (mpi_sub(v1, v, t1) < 0) goto cleanup;
111                         sign = u->sign; u->sign = !u->sign;
112                         if( !odd )
113                                 if (mpi_sub(v2, u, t2) < 0) goto cleanup;
114                         u->sign = sign;
115                         sign = t3->sign; t3->sign = !t3->sign;
116                         if (mpi_set(v3, t3) < 0) goto cleanup;
117                         t3->sign = sign;
118                 }
119                 if (mpi_sub(t1, u1, v1) < 0) goto cleanup;
120                 if( !odd )
121                         if (mpi_sub(t2, u2, v2) < 0) goto cleanup;
122                 if (mpi_sub(t3, u3, v3) < 0) goto cleanup;
123                 if( t1->sign ) {
124                         if (mpi_add(t1, t1, v) < 0) goto cleanup;
125                         if( !odd )
126                                 if (mpi_sub(t2, t2, u) < 0) goto cleanup;
127                 }
128         } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
129         /* mpi_lshift( u3, k ); */
130         rc = mpi_set(x, u1);
131
132  cleanup:
133         mpi_free(u1);
134         mpi_free(v1);
135         mpi_free(t1);
136         if( !odd ) {
137                 mpi_free(u2);
138                 mpi_free(v2);
139                 mpi_free(t2);
140         }
141         mpi_free(u3);
142         mpi_free(v3);
143         mpi_free(t3);
144
145         mpi_free(u);
146         mpi_free(v);
147         return rc;
148 }