1 /* mpi-inv.c - MPI functions
2 * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
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.
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.
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/>.
23 #include "mpi-internal.h"
27 * Calculate the multiplicative inverse X of A mod N
28 * That is: Find the solution x for
32 gcry_mpi_invm( gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t n )
35 gcry_mpi_t u, v, u1, u2, u3, v1, v2, v3, q, t1, t2, t3;
36 gcry_mpi_t ta, tb, tc;
40 u1 = mpi_alloc_set_ui(1);
41 u2 = mpi_alloc_set_ui(0);
43 v1 = mpi_alloc_set_ui(0);
44 v2 = mpi_alloc_set_ui(1);
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);
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); */
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 */
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;
89 for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
95 u1 = mpi_alloc_set_ui(1);
96 u2 = mpi_alloc_set_ui(0);
98 v1 = mpi_copy(v); /* !-- used as const 1 */
99 v2 = mpi_alloc( mpi_get_nlimbs(u) ); mpi_sub( v2, u1, u );
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;
108 t1 = mpi_alloc_set_ui(1);
109 t2 = mpi_alloc_set_ui(0);
114 if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
118 mpi_rshift(t1, t1, 1);
119 mpi_rshift(t2, t2, 1);
120 mpi_rshift(t3, t3, 1);
123 } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
132 sign = u->sign; u->sign = !u->sign;
135 sign = t3->sign; t3->sign = !t3->sign;
146 } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
147 /* mpi_lshift( u3, k ); */
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 gcry_mpi_t u, v, u1, u2=NULL, u3, v1, v2=NULL, v3, t1, t2=NULL, t3;
171 for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
175 odd = mpi_test_bit(v,0);
177 u1 = mpi_alloc_set_ui(1);
179 u2 = mpi_alloc_set_ui(0);
183 v2 = mpi_alloc( mpi_get_nlimbs(u) );
184 mpi_sub( v2, u1, u ); /* U is used as const 1 */
187 if( mpi_test_bit(u, 0) ) { /* u is odd */
188 t1 = mpi_alloc_set_ui(0);
190 t2 = mpi_alloc_set_ui(1); t2->sign = 1;
192 t3 = mpi_copy(v); t3->sign = !t3->sign;
196 t1 = mpi_alloc_set_ui(1);
198 t2 = mpi_alloc_set_ui(0);
204 if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
208 mpi_rshift(t1, t1, 1);
209 mpi_rshift(t2, t2, 1);
210 mpi_rshift(t3, t3, 1);
213 if( mpi_test_bit(t1, 0) )
215 mpi_rshift(t1, t1, 1);
216 mpi_rshift(t3, t3, 1);
220 } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
230 sign = u->sign; u->sign = !u->sign;
234 sign = t3->sign; t3->sign = !t3->sign;
247 } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
248 /* mpi_lshift( u3, k ); */