-
+ 66EDF48A07BB672B7D9329150436A40AB2F1628F1F289B4C52B2E4BDB987B9105F36C441FB21A141346406A675D34B6265A4A0BC00D3C4DE471F461D0ACC5765
mpi/mpi-inv.c
(0 . 0)(1 . 266)
7067 /* mpi-inv.c - MPI functions
7068 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
7069 *
7070 * This file is part of GnuPG.
7071 *
7072 * GnuPG is free software; you can redistribute it and/or modify
7073 * it under the terms of the GNU General Public License as published by
7074 * the Free Software Foundation; either version 3 of the License, or
7075 * (at your option) any later version.
7076 *
7077 * GnuPG is distributed in the hope that it will be useful,
7078 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7079 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7080 * GNU General Public License for more details.
7081 *
7082 * You should have received a copy of the GNU General Public License
7083 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7084 */
7085
7086 #include <config.h>
7087 #include <stdio.h>
7088 #include <stdlib.h>
7089 #include "mpi-internal.h"
7090
7091
7092 /****************
7093 * Calculate the multiplicative inverse X of A mod N
7094 * That is: Find the solution x for
7095 * 1 = (a*x) mod n
7096 */
7097 void
7098 mpi_invm( MPI x, MPI a, MPI n )
7099 {
7100 #if 0
7101 MPI u, v, u1, u2, u3, v1, v2, v3, q, t1, t2, t3;
7102 MPI ta, tb, tc;
7103
7104 u = mpi_copy(a);
7105 v = mpi_copy(n);
7106 u1 = mpi_alloc_set_ui(1);
7107 u2 = mpi_alloc_set_ui(0);
7108 u3 = mpi_copy(u);
7109 v1 = mpi_alloc_set_ui(0);
7110 v2 = mpi_alloc_set_ui(1);
7111 v3 = mpi_copy(v);
7112 q = mpi_alloc( mpi_get_nlimbs(u)+1 );
7113 t1 = mpi_alloc( mpi_get_nlimbs(u)+1 );
7114 t2 = mpi_alloc( mpi_get_nlimbs(u)+1 );
7115 t3 = mpi_alloc( mpi_get_nlimbs(u)+1 );
7116 while( mpi_cmp_ui( v3, 0 ) ) {
7117 mpi_fdiv_q( q, u3, v3 );
7118 mpi_mul(t1, v1, q); mpi_mul(t2, v2, q); mpi_mul(t3, v3, q);
7119 mpi_sub(t1, u1, t1); mpi_sub(t2, u2, t2); mpi_sub(t3, u3, t3);
7120 mpi_set(u1, v1); mpi_set(u2, v2); mpi_set(u3, v3);
7121 mpi_set(v1, t1); mpi_set(v2, t2); mpi_set(v3, t3);
7122 }
7123 /* log_debug("result:\n");
7124 log_mpidump("q =", q );
7125 log_mpidump("u1=", u1);
7126 log_mpidump("u2=", u2);
7127 log_mpidump("u3=", u3);
7128 log_mpidump("v1=", v1);
7129 log_mpidump("v2=", v2); */
7130 mpi_set(x, u1);
7131
7132 mpi_free(u1);
7133 mpi_free(u2);
7134 mpi_free(u3);
7135 mpi_free(v1);
7136 mpi_free(v2);
7137 mpi_free(v3);
7138 mpi_free(q);
7139 mpi_free(t1);
7140 mpi_free(t2);
7141 mpi_free(t3);
7142 mpi_free(u);
7143 mpi_free(v);
7144 #elif 0
7145 /* Extended Euclid's algorithm (See TAOPC Vol II, 4.5.2, Alg X)
7146 * modified according to Michael Penk's solution for Exercice 35 */
7147
7148 /* FIXME: we can simplify this in most cases (see Knuth) */
7149 MPI u, v, u1, u2, u3, v1, v2, v3, t1, t2, t3;
7150 unsigned k;
7151 int sign;
7152
7153 u = mpi_copy(a);
7154 v = mpi_copy(n);
7155 for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
7156 mpi_rshift(u, u, 1);
7157 mpi_rshift(v, v, 1);
7158 }
7159
7160
7161 u1 = mpi_alloc_set_ui(1);
7162 u2 = mpi_alloc_set_ui(0);
7163 u3 = mpi_copy(u);
7164 v1 = mpi_copy(v); /* !-- used as const 1 */
7165 v2 = mpi_alloc( mpi_get_nlimbs(u) ); mpi_sub( v2, u1, u );
7166 v3 = mpi_copy(v);
7167 if( mpi_test_bit(u, 0) ) { /* u is odd */
7168 t1 = mpi_alloc_set_ui(0);
7169 t2 = mpi_alloc_set_ui(1); t2->sign = 1;
7170 t3 = mpi_copy(v); t3->sign = !t3->sign;
7171 goto Y4;
7172 }
7173 else {
7174 t1 = mpi_alloc_set_ui(1);
7175 t2 = mpi_alloc_set_ui(0);
7176 t3 = mpi_copy(u);
7177 }
7178 do {
7179 do {
7180 if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
7181 mpi_add(t1, t1, v);
7182 mpi_sub(t2, t2, u);
7183 }
7184 mpi_rshift(t1, t1, 1);
7185 mpi_rshift(t2, t2, 1);
7186 mpi_rshift(t3, t3, 1);
7187 Y4:
7188 ;
7189 } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
7190
7191 if( !t3->sign ) {
7192 mpi_set(u1, t1);
7193 mpi_set(u2, t2);
7194 mpi_set(u3, t3);
7195 }
7196 else {
7197 mpi_sub(v1, v, t1);
7198 sign = u->sign; u->sign = !u->sign;
7199 mpi_sub(v2, u, t2);
7200 u->sign = sign;
7201 sign = t3->sign; t3->sign = !t3->sign;
7202 mpi_set(v3, t3);
7203 t3->sign = sign;
7204 }
7205 mpi_sub(t1, u1, v1);
7206 mpi_sub(t2, u2, v2);
7207 mpi_sub(t3, u3, v3);
7208 if( t1->sign ) {
7209 mpi_add(t1, t1, v);
7210 mpi_sub(t2, t2, u);
7211 }
7212 } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
7213 /* mpi_lshift( u3, k ); */
7214 mpi_set(x, u1);
7215
7216 mpi_free(u1);
7217 mpi_free(u2);
7218 mpi_free(u3);
7219 mpi_free(v1);
7220 mpi_free(v2);
7221 mpi_free(v3);
7222 mpi_free(t1);
7223 mpi_free(t2);
7224 mpi_free(t3);
7225 #else
7226 /* Extended Euclid's algorithm (See TAOPC Vol II, 4.5.2, Alg X)
7227 * modified according to Michael Penk's solution for Exercice 35
7228 * with further enhancement */
7229 MPI u, v, u1, u2=NULL, u3, v1, v2=NULL, v3, t1, t2=NULL, t3;
7230 unsigned k;
7231 int sign;
7232 int odd ;
7233
7234 u = mpi_copy(a);
7235 v = mpi_copy(n);
7236
7237 for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
7238 mpi_rshift(u, u, 1);
7239 mpi_rshift(v, v, 1);
7240 }
7241 odd = mpi_test_bit(v,0);
7242
7243 u1 = mpi_alloc_set_ui(1);
7244 if( !odd )
7245 u2 = mpi_alloc_set_ui(0);
7246 u3 = mpi_copy(u);
7247 v1 = mpi_copy(v);
7248 if( !odd ) {
7249 v2 = mpi_alloc( mpi_get_nlimbs(u) );
7250 mpi_sub( v2, u1, u ); /* U is used as const 1 */
7251 }
7252 v3 = mpi_copy(v);
7253 if( mpi_test_bit(u, 0) ) { /* u is odd */
7254 t1 = mpi_alloc_set_ui(0);
7255 if( !odd ) {
7256 t2 = mpi_alloc_set_ui(1); t2->sign = 1;
7257 }
7258 t3 = mpi_copy(v); t3->sign = !t3->sign;
7259 goto Y4;
7260 }
7261 else {
7262 t1 = mpi_alloc_set_ui(1);
7263 if( !odd )
7264 t2 = mpi_alloc_set_ui(0);
7265 t3 = mpi_copy(u);
7266 }
7267 do {
7268 do {
7269 if( !odd ) {
7270 if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
7271 mpi_add(t1, t1, v);
7272 mpi_sub(t2, t2, u);
7273 }
7274 mpi_rshift(t1, t1, 1);
7275 mpi_rshift(t2, t2, 1);
7276 mpi_rshift(t3, t3, 1);
7277 }
7278 else {
7279 if( mpi_test_bit(t1, 0) )
7280 mpi_add(t1, t1, v);
7281 mpi_rshift(t1, t1, 1);
7282 mpi_rshift(t3, t3, 1);
7283 }
7284 Y4:
7285 ;
7286 } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
7287
7288 if( !t3->sign ) {
7289 mpi_set(u1, t1);
7290 if( !odd )
7291 mpi_set(u2, t2);
7292 mpi_set(u3, t3);
7293 }
7294 else {
7295 mpi_sub(v1, v, t1);
7296 sign = u->sign; u->sign = !u->sign;
7297 if( !odd )
7298 mpi_sub(v2, u, t2);
7299 u->sign = sign;
7300 sign = t3->sign; t3->sign = !t3->sign;
7301 mpi_set(v3, t3);
7302 t3->sign = sign;
7303 }
7304 mpi_sub(t1, u1, v1);
7305 if( !odd )
7306 mpi_sub(t2, u2, v2);
7307 mpi_sub(t3, u3, v3);
7308 if( t1->sign ) {
7309 mpi_add(t1, t1, v);
7310 if( !odd )
7311 mpi_sub(t2, t2, u);
7312 }
7313 } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
7314 /* mpi_lshift( u3, k ); */
7315 mpi_set(x, u1);
7316
7317 mpi_free(u1);
7318 mpi_free(v1);
7319 mpi_free(t1);
7320 if( !odd ) {
7321 mpi_free(u2);
7322 mpi_free(v2);
7323 mpi_free(t2);
7324 }
7325 mpi_free(u3);
7326 mpi_free(v3);
7327 mpi_free(t3);
7328
7329 mpi_free(u);
7330 mpi_free(v);
7331 #endif
7332 }