summaryrefslogtreecommitdiffstats
path: root/mpi/mpi-inv.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>1997-11-19 14:12:23 +0100
committerWerner Koch <wk@gnupg.org>1997-11-19 14:12:23 +0100
commit25c8f1a3d79a3ae14dbb824082b4107f7a182b13 (patch)
tree9a4d2e413e7e388822f8e37efe0e58b6979ac6a8 /mpi/mpi-inv.c
parentinitially checkin (diff)
downloadgnupg2-25c8f1a3d79a3ae14dbb824082b4107f7a182b13.tar.xz
gnupg2-25c8f1a3d79a3ae14dbb824082b4107f7a182b13.zip
Output armor works, RSA keygen works.
Diffstat (limited to 'mpi/mpi-inv.c')
-rw-r--r--mpi/mpi-inv.c152
1 files changed, 94 insertions, 58 deletions
diff --git a/mpi/mpi-inv.c b/mpi/mpi-inv.c
index acde6055a..f37f4e511 100644
--- a/mpi/mpi-inv.c
+++ b/mpi/mpi-inv.c
@@ -23,20 +23,21 @@
#include <stdlib.h>
#include "mpi-internal.h"
+
/****************
- * Calculate the multiplicative inverse X of U mod V
- * That is: Find the solution for
- * 1 = (u*x) mod v
- * This has only a unique solution if U and V are relatively prime.
- * Returns 0 if a solution was found.
+ * Calculate the multiplicative inverse X of A mod N
+ * That is: Find the solution x for
+ * 1 = (a*x) mod n
*/
-int
-mpi_inv_mod( MPI x, MPI u, MPI v )
+void
+mpi_inv_mod( MPI x, MPI a, MPI n )
{
#if 0
- /* Extended Euclid's algorithm (See TAOPC Vol II, 4.52. Alg X) */
- MPI u1, u2, u3, v1, v2, v3, q, t1, t2, t3;
+ MPI u, v, u1, u2, u3, v1, v2, v3, q, t1, t2, t3;
+ MPI ta, tb, tc;
+ u = mpi_copy(a);
+ v = mpi_copy(n);
u1 = mpi_alloc_set_ui(1);
u2 = mpi_alloc_set_ui(0);
u3 = mpi_copy(u);
@@ -48,22 +49,20 @@ mpi_inv_mod( MPI x, MPI u, MPI v )
t2 = mpi_alloc( mpi_get_nlimbs(u) );
t3 = mpi_alloc( mpi_get_nlimbs(u) );
while( mpi_cmp_ui( v3, 0 ) ) {
- /*log_debug("----------------------\n");
- log_mpidump("q =", u1);
- log_mpidump("u1=", u1);
- log_mpidump("u2=", u2);
- log_mpidump("u3=", u3);
- log_mpidump("v1=", v1);
- log_mpidump("v2=", v2);
- log_mpidump("v3=", v3); */
mpi_fdiv_q( q, u3, v3 );
mpi_mul(t1, v1, q); mpi_mul(t2, v2, q); mpi_mul(t3, v3, q);
mpi_sub(t1, u1, t1); mpi_sub(t2, u2, t2); mpi_sub(t3, u3, t3);
-
mpi_set(u1, v1); mpi_set(u2, v2); mpi_set(u3, v3);
mpi_set(v1, t1); mpi_set(v2, t2); mpi_set(v3, t3);
}
- mpi_set(x, u3);
+ /* log_debug("result:\n");
+ log_mpidump("q =", q );
+ log_mpidump("u1=", u1);
+ log_mpidump("u2=", u2);
+ log_mpidump("u3=", u3);
+ log_mpidump("v1=", v1);
+ log_mpidump("v2=", v2); */
+ mpi_set(x, u1);
mpi_free(u1);
mpi_free(u2);
@@ -75,52 +74,89 @@ mpi_inv_mod( MPI x, MPI u, MPI v )
mpi_free(t1);
mpi_free(t2);
mpi_free(t3);
- #endif
+ mpi_free(u);
+ mpi_free(v);
+ #else
+ /* Extended Euclid's algorithm (See TAOPC Vol II, 4.5.2, Alg X)
+ * modified according to Michael Penk's solution for Exercice 35 */
- /*****************************
- * 1. Init: g0 = u g1 = v v0 = 0 v1 = 1
- * 2. Test: if g1 is 0 terminate. Result = v0 < 0: v0 + n
- * else: v0
- * 3. Divide: div,rem = g0 / g1
- * t1 = v0 - div * v1
- * v0 = v1
- * v1 = t1
- * g0 = g1
- * g1 = rem
- * continue with step 2.
- */
- MPI g0, g1, v0, v1, div, rem, t1;
+ /* FIXME: we can simplify this in most cases (see Knuth) */
+ MPI u, v, u1, u2, u3, v1, v2, v3, t1, t2, t3;
+ unsigned k;
+ int sign;
+
+ u = mpi_copy(a);
+ v = mpi_copy(n);
+ for(k=0; !mpi_test_bit(u,0) && !mpi_test_bit(v,0); k++ ) {
+ mpi_rshift(u, u, 1);
+ mpi_rshift(v, v, 1);
+ }
- g0 = mpi_copy(v);
- g1 = mpi_copy(u);
- v0 = mpi_alloc_set_ui( 0 );
- v1 = mpi_alloc_set_ui( 1 );
- div = mpi_alloc(mpi_get_nlimbs(v));
- rem = mpi_alloc(mpi_get_nlimbs(v));
- t1 = mpi_alloc(mpi_get_nlimbs(v));
- while( mpi_cmp_ui( g1, 0) ) {
- mpi_fdiv_qr(div, rem, g0, g1);
- mpi_mul(t1, div, v1);
- mpi_sub(t1, v0, t1);
- mpi_set(v0, v1);
- mpi_set(v1, t1);
- mpi_set(g0, g1);
- mpi_set(g1, rem);
+ u1 = mpi_alloc_set_ui(1);
+ u2 = mpi_alloc_set_ui(0);
+ u3 = mpi_copy(u);
+ v1 = mpi_copy(v); /* !-- used as const 1 */
+ v2 = mpi_alloc( mpi_get_nlimbs(u) ); mpi_sub( v2, u1, u );
+ v3 = mpi_copy(v);
+ if( mpi_test_bit(u, 0) ) { /* u is odd */
+ t1 = mpi_alloc_set_ui(0);
+ t2 = mpi_alloc_set_ui(1); t2->sign = 1;
+ t3 = mpi_copy(v); t3->sign = !t3->sign;
+ goto Y4;
+ }
+ else {
+ t1 = mpi_alloc_set_ui(1);
+ t2 = mpi_alloc_set_ui(0);
+ t3 = mpi_copy(u);
}
- if( mpi_cmp_ui( v0, 0) < 0 )
- mpi_add( x, v0, v);
- else
- mpi_set( x, v0);
+ do {
+ do {
+ if( mpi_test_bit(t1, 0) || mpi_test_bit(t2, 0) ) { /* one is odd */
+ mpi_add(t1, t1, v);
+ mpi_sub(t2, t2, u);
+ }
+ mpi_rshift(t1, t1, 1);
+ mpi_rshift(t2, t2, 1);
+ mpi_rshift(t3, t3, 1);
+ Y4:
+ } while( !mpi_test_bit( t3, 0 ) ); /* while t3 is even */
- mpi_free(g0);
- mpi_free(g1);
- mpi_free(v0);
+ if( !t3->sign ) {
+ mpi_set(u1, t1);
+ mpi_set(u2, t2);
+ mpi_set(u3, t3);
+ }
+ else {
+ mpi_sub(v1, v, t1);
+ sign = u->sign; u->sign = !u->sign;
+ mpi_sub(v2, u, t2);
+ u->sign = sign;
+ sign = t3->sign; t3->sign = !t3->sign;
+ mpi_set(v3, t3);
+ t3->sign = sign;
+ }
+ mpi_sub(t1, u1, v1);
+ mpi_sub(t2, u2, v2);
+ mpi_sub(t3, u3, v3);
+ if( t1->sign ) {
+ mpi_add(t1, t1, v);
+ mpi_sub(t2, t2, u);
+ }
+ } while( mpi_cmp_ui( t3, 0 ) ); /* while t3 != 0 */
+ /* mpi_lshift( u3, k ); */
+ mpi_set(x, u1);
+
+ mpi_free(u1);
+ mpi_free(u2);
+ mpi_free(u3);
mpi_free(v1);
- mpi_free(div);
- mpi_free(rem);
+ mpi_free(v2);
+ mpi_free(v3);
mpi_free(t1);
- return 0;
+ mpi_free(t2);
+ mpi_free(t3);
+ #endif
}