1
/* mpi-inv.c - MPI functions
2
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
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.
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.
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
24
#include "mpi-internal.h"
28
* Calculate the multiplicative inverse X of A mod N
29
* That is: Find the solution x for
33
mpi_invm( MPI x, MPI a, MPI n )
36
MPI u, v, u1, u2, u3, v1, v2, v3, q, t1, t2, t3;
41
u1 = mpi_alloc_set_ui(1);
42
u2 = mpi_alloc_set_ui(0);
44
v1 = mpi_alloc_set_ui(0);
45
v2 = mpi_alloc_set_ui(1);
47
q = mpi_alloc( mpi_get_nlimbs(u)+1 );
48
t1 = mpi_alloc( mpi_get_nlimbs(u)+1 );
49
t2 = mpi_alloc( mpi_get_nlimbs(u)+1 );
50
t3 = mpi_alloc( mpi_get_nlimbs(u)+1 );
51
while( mpi_cmp_ui( v3, 0 ) ) {
52
mpi_fdiv_q( q, u3, v3 );
53
mpi_mul(t1, v1, q); mpi_mul(t2, v2, q); mpi_mul(t3, v3, q);
54
mpi_sub(t1, u1, t1); mpi_sub(t2, u2, t2); mpi_sub(t3, u3, t3);
55
mpi_set(u1, v1); mpi_set(u2, v2); mpi_set(u3, v3);
56
mpi_set(v1, t1); mpi_set(v2, t2); mpi_set(v3, t3);
58
/* log_debug("result:\n");
59
log_mpidump("q =", q );
60
log_mpidump("u1=", u1);
61
log_mpidump("u2=", u2);
62
log_mpidump("u3=", u3);
63
log_mpidump("v1=", v1);
64
log_mpidump("v2=", v2); */
80
/* Extended Euclid's algorithm (See TAOPC Vol II, 4.5.2, Alg X)
81
* modified according to Michael Penk's solution for Exercice 35 */
83
/* FIXME: we can simplify this in most cases (see Knuth) */
84
MPI u, v, u1, u2, u3, v1, v2, v3, t1, t2, t3;
90
for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
96
u1 = mpi_alloc_set_ui(1);
97
u2 = mpi_alloc_set_ui(0);
99
v1 = mpi_copy(v); /* !-- used as const 1 */
100
v2 = mpi_alloc( mpi_get_nlimbs(u) ); mpi_sub( v2, u1, u );
102
if( mpi_test_bit(u, 0) ) { /* u is odd */
103
t1 = mpi_alloc_set_ui(0);
104
t2 = mpi_alloc_set_ui(1); t2->sign = 1;
105
t3 = mpi_copy(v); t3->sign = !t3->sign;
109
t1 = mpi_alloc_set_ui(1);
110
t2 = mpi_alloc_set_ui(0);
115
if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
119
mpi_rshift(t1, t1, 1);
120
mpi_rshift(t2, t2, 1);
121
mpi_rshift(t3, t3, 1);
124
} while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
133
sign = u->sign; u->sign = !u->sign;
136
sign = t3->sign; t3->sign = !t3->sign;
147
} while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
148
/* mpi_lshift( u3, k ); */
161
/* Extended Euclid's algorithm (See TAOPC Vol II, 4.5.2, Alg X)
162
* modified according to Michael Penk's solution for Exercice 35
163
* with further enhancement */
164
MPI u, v, u1, u2=NULL, u3, v1, v2=NULL, v3, t1, t2=NULL, t3;
172
for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
176
odd = mpi_test_bit(v,0);
178
u1 = mpi_alloc_set_ui(1);
180
u2 = mpi_alloc_set_ui(0);
184
v2 = mpi_alloc( mpi_get_nlimbs(u) );
185
mpi_sub( v2, u1, u ); /* U is used as const 1 */
188
if( mpi_test_bit(u, 0) ) { /* u is odd */
189
t1 = mpi_alloc_set_ui(0);
191
t2 = mpi_alloc_set_ui(1); t2->sign = 1;
193
t3 = mpi_copy(v); t3->sign = !t3->sign;
197
t1 = mpi_alloc_set_ui(1);
199
t2 = mpi_alloc_set_ui(0);
205
if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
209
mpi_rshift(t1, t1, 1);
210
mpi_rshift(t2, t2, 1);
211
mpi_rshift(t3, t3, 1);
214
if( mpi_test_bit(t1, 0) )
216
mpi_rshift(t1, t1, 1);
217
mpi_rshift(t3, t3, 1);
221
} while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
231
sign = u->sign; u->sign = !u->sign;
235
sign = t3->sign; t3->sign = !t3->sign;
248
} while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
249
/* mpi_lshift( u3, k ); */