Line data Source code
1 : /* mpi-inv.c - MPI functions
2 : * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 : *
4 : * This file is part of Libgcrypt.
5 : *
6 : * Libgcrypt is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU Lesser General Public License as
8 : * published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * Libgcrypt 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 Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <config.h>
21 : #include <stdio.h>
22 : #include <stdlib.h>
23 : #include "mpi-internal.h"
24 : #include "g10lib.h"
25 :
26 : /****************
27 : * Calculate the multiplicative inverse X of A mod N
28 : * That is: Find the solution x for
29 : * 1 = (a*x) mod n
30 : */
31 : int
32 7613 : _gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t n)
33 : {
34 : #if 0
35 : gcry_mpi_t u, v, u1, u2, u3, v1, v2, v3, q, t1, t2, t3;
36 : gcry_mpi_t ta, tb, tc;
37 :
38 : u = mpi_copy(a);
39 : v = mpi_copy(n);
40 : u1 = mpi_alloc_set_ui(1);
41 : u2 = mpi_alloc_set_ui(0);
42 : u3 = mpi_copy(u);
43 : v1 = mpi_alloc_set_ui(0);
44 : v2 = mpi_alloc_set_ui(1);
45 : v3 = mpi_copy(v);
46 : q = mpi_alloc( mpi_get_nlimbs(u)+1 );
47 : t1 = mpi_alloc( mpi_get_nlimbs(u)+1 );
48 : t2 = mpi_alloc( mpi_get_nlimbs(u)+1 );
49 : t3 = mpi_alloc( mpi_get_nlimbs(u)+1 );
50 : while( mpi_cmp_ui( v3, 0 ) ) {
51 : mpi_fdiv_q( q, u3, v3 );
52 : mpi_mul(t1, v1, q); mpi_mul(t2, v2, q); mpi_mul(t3, v3, q);
53 : mpi_sub(t1, u1, t1); mpi_sub(t2, u2, t2); mpi_sub(t3, u3, t3);
54 : mpi_set(u1, v1); mpi_set(u2, v2); mpi_set(u3, v3);
55 : mpi_set(v1, t1); mpi_set(v2, t2); mpi_set(v3, t3);
56 : }
57 : /* log_debug("result:\n");
58 : log_mpidump("q =", q );
59 : log_mpidump("u1=", u1);
60 : log_mpidump("u2=", u2);
61 : log_mpidump("u3=", u3);
62 : log_mpidump("v1=", v1);
63 : log_mpidump("v2=", v2); */
64 : mpi_set(x, u1);
65 :
66 : mpi_free(u1);
67 : mpi_free(u2);
68 : mpi_free(u3);
69 : mpi_free(v1);
70 : mpi_free(v2);
71 : mpi_free(v3);
72 : mpi_free(q);
73 : mpi_free(t1);
74 : mpi_free(t2);
75 : mpi_free(t3);
76 : mpi_free(u);
77 : mpi_free(v);
78 : #elif 0
79 : /* Extended Euclid's algorithm (See TAOCP Vol II, 4.5.2, Alg X)
80 : * modified according to Michael Penk's solution for Exercise 35 */
81 :
82 : /* FIXME: we can simplify this in most cases (see Knuth) */
83 : gcry_mpi_t u, v, u1, u2, u3, v1, v2, v3, t1, t2, t3;
84 : unsigned k;
85 : int sign;
86 :
87 : u = mpi_copy(a);
88 : v = mpi_copy(n);
89 : for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
90 : mpi_rshift(u, u, 1);
91 : mpi_rshift(v, v, 1);
92 : }
93 :
94 :
95 : u1 = mpi_alloc_set_ui(1);
96 : u2 = mpi_alloc_set_ui(0);
97 : u3 = mpi_copy(u);
98 : v1 = mpi_copy(v); /* !-- used as const 1 */
99 : v2 = mpi_alloc( mpi_get_nlimbs(u) ); mpi_sub( v2, u1, u );
100 : v3 = mpi_copy(v);
101 : if( mpi_test_bit(u, 0) ) { /* u is odd */
102 : t1 = mpi_alloc_set_ui(0);
103 : t2 = mpi_alloc_set_ui(1); t2->sign = 1;
104 : t3 = mpi_copy(v); t3->sign = !t3->sign;
105 : goto Y4;
106 : }
107 : else {
108 : t1 = mpi_alloc_set_ui(1);
109 : t2 = mpi_alloc_set_ui(0);
110 : t3 = mpi_copy(u);
111 : }
112 : do {
113 : do {
114 : if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
115 : mpi_add(t1, t1, v);
116 : mpi_sub(t2, t2, u);
117 : }
118 : mpi_rshift(t1, t1, 1);
119 : mpi_rshift(t2, t2, 1);
120 : mpi_rshift(t3, t3, 1);
121 : Y4:
122 : ;
123 : } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
124 :
125 : if( !t3->sign ) {
126 : mpi_set(u1, t1);
127 : mpi_set(u2, t2);
128 : mpi_set(u3, t3);
129 : }
130 : else {
131 : mpi_sub(v1, v, t1);
132 : sign = u->sign; u->sign = !u->sign;
133 : mpi_sub(v2, u, t2);
134 : u->sign = sign;
135 : sign = t3->sign; t3->sign = !t3->sign;
136 : mpi_set(v3, t3);
137 : t3->sign = sign;
138 : }
139 : mpi_sub(t1, u1, v1);
140 : mpi_sub(t2, u2, v2);
141 : mpi_sub(t3, u3, v3);
142 : if( t1->sign ) {
143 : mpi_add(t1, t1, v);
144 : mpi_sub(t2, t2, u);
145 : }
146 : } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
147 : /* mpi_lshift( u3, k ); */
148 : mpi_set(x, u1);
149 :
150 : mpi_free(u1);
151 : mpi_free(u2);
152 : mpi_free(u3);
153 : mpi_free(v1);
154 : mpi_free(v2);
155 : mpi_free(v3);
156 : mpi_free(t1);
157 : mpi_free(t2);
158 : mpi_free(t3);
159 : #else
160 : /* Extended Euclid's algorithm (See TAOCP Vol II, 4.5.2, Alg X)
161 : * modified according to Michael Penk's solution for Exercise 35
162 : * with further enhancement */
163 7613 : gcry_mpi_t u, v, u1, u2=NULL, u3, v1, v2=NULL, v3, t1, t2=NULL, t3;
164 : unsigned k;
165 : int sign;
166 : int odd ;
167 :
168 7613 : if (!mpi_cmp_ui (a, 0))
169 0 : return 0; /* Inverse does not exists. */
170 7613 : if (!mpi_cmp_ui (n, 1))
171 0 : return 0; /* Inverse does not exists. */
172 :
173 7613 : u = mpi_copy(a);
174 7613 : v = mpi_copy(n);
175 :
176 7613 : for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
177 0 : mpi_rshift(u, u, 1);
178 0 : mpi_rshift(v, v, 1);
179 : }
180 7613 : odd = mpi_test_bit(v,0);
181 :
182 7613 : u1 = mpi_alloc_set_ui(1);
183 7613 : if( !odd )
184 36 : u2 = mpi_alloc_set_ui(0);
185 7613 : u3 = mpi_copy(u);
186 7613 : v1 = mpi_copy(v);
187 7613 : if( !odd ) {
188 36 : v2 = mpi_alloc( mpi_get_nlimbs(u) );
189 36 : mpi_sub( v2, u1, u ); /* U is used as const 1 */
190 : }
191 7613 : v3 = mpi_copy(v);
192 7613 : if( mpi_test_bit(u, 0) ) { /* u is odd */
193 4282 : t1 = mpi_alloc_set_ui(0);
194 4282 : if( !odd ) {
195 36 : t2 = mpi_alloc_set_ui(1); t2->sign = 1;
196 : }
197 4282 : t3 = mpi_copy(v); t3->sign = !t3->sign;
198 4282 : goto Y4;
199 : }
200 : else {
201 3331 : t1 = mpi_alloc_set_ui(1);
202 3331 : if( !odd )
203 0 : t2 = mpi_alloc_set_ui(0);
204 3331 : t3 = mpi_copy(u);
205 : }
206 : do {
207 : do {
208 4106040 : if( !odd ) {
209 51026 : if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
210 25582 : mpi_add(t1, t1, v);
211 25582 : mpi_sub(t2, t2, u);
212 : }
213 51026 : mpi_rshift(t1, t1, 1);
214 51026 : mpi_rshift(t2, t2, 1);
215 51026 : mpi_rshift(t3, t3, 1);
216 : }
217 : else {
218 4055014 : if( mpi_test_bit(t1, 0) )
219 2105587 : mpi_add(t1, t1, v);
220 4055014 : mpi_rshift(t1, t1, 1);
221 4055014 : mpi_rshift(t3, t3, 1);
222 : }
223 : Y4:
224 : ;
225 4110322 : } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
226 :
227 2219763 : if( !t3->sign ) {
228 918224 : mpi_set(u1, t1);
229 918224 : if( !odd )
230 2711 : mpi_set(u2, t2);
231 918224 : mpi_set(u3, t3);
232 : }
233 : else {
234 1301539 : mpi_sub(v1, v, t1);
235 1301539 : sign = u->sign; u->sign = !u->sign;
236 1301539 : if( !odd )
237 22921 : mpi_sub(v2, u, t2);
238 1301539 : u->sign = sign;
239 1301539 : sign = t3->sign; t3->sign = !t3->sign;
240 1301539 : mpi_set(v3, t3);
241 1301539 : t3->sign = sign;
242 : }
243 2219763 : mpi_sub(t1, u1, v1);
244 2219763 : if( !odd )
245 25632 : mpi_sub(t2, u2, v2);
246 2219763 : mpi_sub(t3, u3, v3);
247 2219763 : if( t1->sign ) {
248 1322713 : mpi_add(t1, t1, v);
249 1322713 : if( !odd )
250 22983 : mpi_sub(t2, t2, u);
251 : }
252 2219763 : } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
253 : /* mpi_lshift( u3, k ); */
254 7613 : mpi_set(x, u1);
255 :
256 7613 : mpi_free(u1);
257 7613 : mpi_free(v1);
258 7613 : mpi_free(t1);
259 7613 : if( !odd ) {
260 36 : mpi_free(u2);
261 36 : mpi_free(v2);
262 36 : mpi_free(t2);
263 : }
264 7613 : mpi_free(u3);
265 7613 : mpi_free(v3);
266 7613 : mpi_free(t3);
267 :
268 7613 : mpi_free(u);
269 7613 : mpi_free(v);
270 : #endif
271 7613 : return 1;
272 : }
|