Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
v5.14.15
  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 "mpi-internal.h"
 21
 22/****************
 23 * Calculate the multiplicative inverse X of A mod N
 24 * That is: Find the solution x for
 25 *		1 = (a*x) mod n
 26 */
 27int mpi_invm(MPI x, MPI a, MPI n)
 28{
 29	/* Extended Euclid's algorithm (See TAOCP Vol II, 4.5.2, Alg X)
 30	 * modified according to Michael Penk's solution for Exercise 35
 31	 * with further enhancement
 32	 */
 33	MPI u, v, u1, u2 = NULL, u3, v1, v2 = NULL, v3, t1, t2 = NULL, t3;
 34	unsigned int k;
 
 
 35	int sign;
 36	int odd;
 
 37
 38	if (!mpi_cmp_ui(a, 0))
 39		return 0; /* Inverse does not exists.  */
 40	if (!mpi_cmp_ui(n, 1))
 41		return 0; /* Inverse does not exists.  */
 42
 43	u = mpi_copy(a);
 44	v = mpi_copy(n);
 45
 46	for (k = 0; !mpi_test_bit(u, 0) && !mpi_test_bit(v, 0); k++) {
 47		mpi_rshift(u, u, 1);
 48		mpi_rshift(v, v, 1);
 
 
 49	}
 50	odd = mpi_test_bit(v, 0);
 51
 52	u1 = mpi_alloc_set_ui(1);
 53	if (!odd)
 
 
 54		u2 = mpi_alloc_set_ui(0);
 55	u3 = mpi_copy(u);
 56	v1 = mpi_copy(v);
 
 
 
 
 
 57	if (!odd) {
 58		v2 = mpi_alloc(mpi_get_nlimbs(u));
 59		mpi_sub(v2, u1, u); /* U is used as const 1 */
 
 
 
 60	}
 61	v3 = mpi_copy(v);
 62	if (mpi_test_bit(u, 0)) { /* u is odd */
 
 63		t1 = mpi_alloc_set_ui(0);
 
 
 64		if (!odd) {
 65			t2 = mpi_alloc_set_ui(1);
 
 
 66			t2->sign = 1;
 67		}
 68		t3 = mpi_copy(v);
 
 69		t3->sign = !t3->sign;
 70		goto Y4;
 71	} else {
 72		t1 = mpi_alloc_set_ui(1);
 73		if (!odd)
 
 
 74			t2 = mpi_alloc_set_ui(0);
 75		t3 = mpi_copy(u);
 
 
 
 
 76	}
 77
 78	do {
 79		do {
 80			if (!odd) {
 81				if (mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0)) {
 82					/* one is odd */
 83					mpi_add(t1, t1, v);
 84					mpi_sub(t2, t2, u);
 
 85				}
 86				mpi_rshift(t1, t1, 1);
 87				mpi_rshift(t2, t2, 1);
 88				mpi_rshift(t3, t3, 1);
 
 
 
 89			} else {
 90				if (mpi_test_bit(t1, 0))
 91					mpi_add(t1, t1, v);
 92				mpi_rshift(t1, t1, 1);
 93				mpi_rshift(t3, t3, 1);
 
 
 
 94			}
 95Y4:
 96			;
 97		} while (!mpi_test_bit(t3, 0)); /* while t3 is even */
 98
 99		if (!t3->sign) {
100			mpi_set(u1, t1);
 
101			if (!odd)
102				mpi_set(u2, t2);
103			mpi_set(u3, t3);
 
 
104		} else {
105			mpi_sub(v1, v, t1);
106			sign = u->sign; u->sign = !u->sign;
 
 
107			if (!odd)
108				mpi_sub(v2, u, t2);
 
109			u->sign = sign;
110			sign = t3->sign; t3->sign = !t3->sign;
111			mpi_set(v3, t3);
 
 
112			t3->sign = sign;
113		}
114		mpi_sub(t1, u1, v1);
 
115		if (!odd)
116			mpi_sub(t2, u2, v2);
117		mpi_sub(t3, u3, v3);
 
 
118		if (t1->sign) {
119			mpi_add(t1, t1, v);
 
120			if (!odd)
121				mpi_sub(t2, t2, u);
 
122		}
123	} while (mpi_cmp_ui(t3, 0)); /* while t3 != 0 */
124	/* mpi_lshift( u3, k ); */
125	mpi_set(x, u1);
126
 
127	mpi_free(u1);
128	mpi_free(v1);
129	mpi_free(t1);
130	if (!odd) {
131		mpi_free(u2);
132		mpi_free(v2);
133		mpi_free(t2);
134	}
135	mpi_free(u3);
136	mpi_free(v3);
137	mpi_free(t3);
138
139	mpi_free(u);
140	mpi_free(v);
141	return 1;
142}
143EXPORT_SYMBOL_GPL(mpi_invm);
v3.5.6
  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 * Calculate the multiplicative inverse X of A mod N
 25 * That is: Find the solution x for
 26 *		1 = (a*x) mod n
 27 */
 28int mpi_invm(MPI x, const MPI a, const MPI n)
 29{
 30	/* Extended Euclid's algorithm (See TAOPC Vol II, 4.5.2, Alg X)
 31	 * modified according to Michael Penk's solution for Exercice 35
 32	 * with further enhancement */
 33	MPI u = NULL, v = NULL;
 34	MPI u1 = NULL, u2 = NULL, u3 = NULL;
 35	MPI v1 = NULL, v2 = NULL, v3 = NULL;
 36	MPI t1 = NULL, t2 = NULL, t3 = NULL;
 37	unsigned k;
 38	int sign;
 39	int odd = 0;
 40	int rc = -ENOMEM;
 41
 42	if (mpi_copy(&u, a) < 0)
 43		goto cleanup;
 44	if (mpi_copy(&v, n) < 0)
 45		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)
 49			goto cleanup;
 50		if (mpi_rshift(v, v, 1) < 0)
 51			goto cleanup;
 52	}
 53	odd = mpi_test_bit(v, 0);
 54
 55	u1 = mpi_alloc_set_ui(1);
 56	if (!u1)
 57		goto cleanup;
 58	if (!odd) {
 59		u2 = mpi_alloc_set_ui(0);
 60		if (!u2)
 61			goto cleanup;
 62	}
 63	if (mpi_copy(&u3, u) < 0)
 64		goto cleanup;
 65	if (mpi_copy(&v1, v) < 0)
 66		goto cleanup;
 67	if (!odd) {
 68		v2 = mpi_alloc(mpi_get_nlimbs(u));
 69		if (!v2)
 70			goto cleanup;
 71		if (mpi_sub(v2, u1, u) < 0)
 72			goto cleanup;	/* U is used as const 1 */
 73	}
 74	if (mpi_copy(&v3, v) < 0)
 75		goto cleanup;
 76	if (mpi_test_bit(u, 0)) {	/* u is odd */
 77		t1 = mpi_alloc_set_ui(0);
 78		if (!t1)
 79			goto cleanup;
 80		if (!odd) {
 81			t2 = mpi_alloc_set_ui(1);
 82			if (!t2)
 83				goto cleanup;
 84			t2->sign = 1;
 85		}
 86		if (mpi_copy(&t3, v) < 0)
 87			goto cleanup;
 88		t3->sign = !t3->sign;
 89		goto Y4;
 90	} else {
 91		t1 = mpi_alloc_set_ui(1);
 92		if (!t1)
 93			goto cleanup;
 94		if (!odd) {
 95			t2 = mpi_alloc_set_ui(0);
 96			if (!t2)
 97				goto cleanup;
 98		}
 99		if (mpi_copy(&t3, u) < 0)
100			goto cleanup;
101	}
 
102	do {
103		do {
104			if (!odd) {
105				if (mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0)) {	/* one is odd */
106					if (mpi_add(t1, t1, v) < 0)
107						goto cleanup;
108					if (mpi_sub(t2, t2, u) < 0)
109						goto cleanup;
110				}
111				if (mpi_rshift(t1, t1, 1) < 0)
112					goto cleanup;
113				if (mpi_rshift(t2, t2, 1) < 0)
114					goto cleanup;
115				if (mpi_rshift(t3, t3, 1) < 0)
116					goto cleanup;
117			} else {
118				if (mpi_test_bit(t1, 0))
119					if (mpi_add(t1, t1, v) < 0)
120						goto cleanup;
121				if (mpi_rshift(t1, t1, 1) < 0)
122					goto cleanup;
123				if (mpi_rshift(t3, t3, 1) < 0)
124					goto cleanup;
125			}
126Y4:
127			;
128		} while (!mpi_test_bit(t3, 0));	/* while t3 is even */
129
130		if (!t3->sign) {
131			if (mpi_set(u1, t1) < 0)
132				goto cleanup;
133			if (!odd)
134				if (mpi_set(u2, t2) < 0)
135					goto cleanup;
136			if (mpi_set(u3, t3) < 0)
137				goto cleanup;
138		} else {
139			if (mpi_sub(v1, v, t1) < 0)
140				goto cleanup;
141			sign = u->sign;
142			u->sign = !u->sign;
143			if (!odd)
144				if (mpi_sub(v2, u, t2) < 0)
145					goto cleanup;
146			u->sign = sign;
147			sign = t3->sign;
148			t3->sign = !t3->sign;
149			if (mpi_set(v3, t3) < 0)
150				goto cleanup;
151			t3->sign = sign;
152		}
153		if (mpi_sub(t1, u1, v1) < 0)
154			goto cleanup;
155		if (!odd)
156			if (mpi_sub(t2, u2, v2) < 0)
157				goto cleanup;
158		if (mpi_sub(t3, u3, v3) < 0)
159			goto cleanup;
160		if (t1->sign) {
161			if (mpi_add(t1, t1, v) < 0)
162				goto cleanup;
163			if (!odd)
164				if (mpi_sub(t2, t2, u) < 0)
165					goto cleanup;
166		}
167	} while (mpi_cmp_ui(t3, 0));	/* while t3 != 0 */
168	/* mpi_lshift( u3, k ); */
169	rc = mpi_set(x, u1);
170
171cleanup:
172	mpi_free(u1);
173	mpi_free(v1);
174	mpi_free(t1);
175	if (!odd) {
176		mpi_free(u2);
177		mpi_free(v2);
178		mpi_free(t2);
179	}
180	mpi_free(u3);
181	mpi_free(v3);
182	mpi_free(t3);
183
184	mpi_free(u);
185	mpi_free(v);
186	return rc;
187}