^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* mpi-inv.c - MPI functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This file is part of Libgcrypt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Libgcrypt is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * it under the terms of the GNU Lesser General Public License as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * published by the Free Software Foundation; either version 2.1 of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * the License, or (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Libgcrypt is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * GNU Lesser General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * You should have received a copy of the GNU Lesser General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * License along with this program; if not, see <http://www.gnu.org/licenses/>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "mpi-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /****************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Calculate the multiplicative inverse X of A mod N
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * That is: Find the solution x for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * 1 = (a*x) mod n
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int mpi_invm(MPI x, MPI a, MPI n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* Extended Euclid's algorithm (See TAOCP Vol II, 4.5.2, Alg X)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * modified according to Michael Penk's solution for Exercise 35
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * with further enhancement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MPI u, v, u1, u2 = NULL, u3, v1, v2 = NULL, v3, t1, t2 = NULL, t3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int sign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int odd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!mpi_cmp_ui(a, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0; /* Inverse does not exists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!mpi_cmp_ui(n, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0; /* Inverse does not exists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u = mpi_copy(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) v = mpi_copy(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) for (k = 0; !mpi_test_bit(u, 0) && !mpi_test_bit(v, 0); k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) mpi_rshift(u, u, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) mpi_rshift(v, v, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) odd = mpi_test_bit(v, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) u1 = mpi_alloc_set_ui(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u2 = mpi_alloc_set_ui(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) u3 = mpi_copy(u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) v1 = mpi_copy(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!odd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) v2 = mpi_alloc(mpi_get_nlimbs(u));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) mpi_sub(v2, u1, u); /* U is used as const 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) v3 = mpi_copy(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (mpi_test_bit(u, 0)) { /* u is odd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) t1 = mpi_alloc_set_ui(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!odd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) t2 = mpi_alloc_set_ui(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) t2->sign = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) t3 = mpi_copy(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) t3->sign = !t3->sign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) goto Y4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) t1 = mpi_alloc_set_ui(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) t2 = mpi_alloc_set_ui(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) t3 = mpi_copy(u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!odd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* one is odd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) mpi_add(t1, t1, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) mpi_sub(t2, t2, u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) mpi_rshift(t1, t1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mpi_rshift(t2, t2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) mpi_rshift(t3, t3, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (mpi_test_bit(t1, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mpi_add(t1, t1, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) mpi_rshift(t1, t1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) mpi_rshift(t3, t3, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) Y4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } while (!mpi_test_bit(t3, 0)); /* while t3 is even */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (!t3->sign) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) mpi_set(u1, t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) mpi_set(u2, t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mpi_set(u3, t3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) mpi_sub(v1, v, t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sign = u->sign; u->sign = !u->sign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) mpi_sub(v2, u, t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u->sign = sign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) sign = t3->sign; t3->sign = !t3->sign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) mpi_set(v3, t3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) t3->sign = sign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mpi_sub(t1, u1, v1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) mpi_sub(t2, u2, v2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) mpi_sub(t3, u3, v3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (t1->sign) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) mpi_add(t1, t1, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) mpi_sub(t2, t2, u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } while (mpi_cmp_ui(t3, 0)); /* while t3 != 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* mpi_lshift( u3, k ); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) mpi_set(x, u1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mpi_free(u1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) mpi_free(v1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) mpi_free(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!odd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) mpi_free(u2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) mpi_free(v2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) mpi_free(t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) mpi_free(u3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mpi_free(v3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) mpi_free(t3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) mpi_free(u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) mpi_free(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) EXPORT_SYMBOL_GPL(mpi_invm);