From 86abac78a20dfe49aea28058956a3fe8b6ced9ee Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 2 Jul 1999 09:50:57 +0000 Subject: See ChangeLog: Fri Jul 2 11:45:54 CEST 1999 Werner Koch --- mpi/ChangeLog | 6 +++ mpi/mpi-bit.c | 48 ++++++++++++++++++ mpi/mpi-internal.h | 5 ++ mpi/mpi-mpow.c | 141 +++++++++++++++++++++++++++++++++++++++++++++-------- mpi/mpi-mul.c | 1 - 5 files changed, 181 insertions(+), 20 deletions(-) (limited to 'mpi') diff --git a/mpi/ChangeLog b/mpi/ChangeLog index 959f3b48c..1951dd3a4 100644 --- a/mpi/ChangeLog +++ b/mpi/ChangeLog @@ -1,3 +1,9 @@ +Fri Jul 2 11:45:54 CEST 1999 Werner Koch + + + * mpi-bit.c (mpi_lshift_limbs,mpi_rshift_limbs): New. + * mpi-mpow.c (barrett_mulm): New but diabled. + Tue Jun 1 16:01:46 CEST 1999 Werner Koch * config.links (i[56]86*-*-freebsdelf*): New. diff --git a/mpi/mpi-bit.c b/mpi/mpi-bit.c index 00aa5d086..f1eff8636 100644 --- a/mpi/mpi-bit.c +++ b/mpi/mpi-bit.c @@ -212,3 +212,51 @@ mpi_rshift( MPI x, MPI a, unsigned n ) x->nlimbs = xsize; } + +/**************** + * Shift A by COUNT limbs to the left + * This is used only within the MPI library + */ +void +mpi_lshift_limbs( MPI a, unsigned int count ) +{ + mpi_ptr_t ap = a->d; + int n = a->nlimbs; + int i; + + if( !count || !n ) + return; + + RESIZE_IF_NEEDED( a, n+count ); + + for( i = n-1; i >= 0; i-- ) + ap[i+count] = ap[i]; + for(i=0; i < count; i++ ) + ap[i] = 0; + a->nlimbs += count; +} + + +/**************** + * Shift A by COUNT limbs to the right + * This is used only within the MPI library + */ +void +mpi_rshift_limbs( MPI a, unsigned int count ) +{ + mpi_ptr_t ap = a->d; + mpi_size_t n = a->nlimbs; + unsigned int i; + + if( count >= n ) { + a->nlimbs = 0; + return; + } + + for( i = 0; i < n - count; i++ ) + ap[i] = ap[i+count]; + ap[i] = 0; + a->nlimbs -= count; +} + + diff --git a/mpi/mpi-internal.h b/mpi/mpi-internal.h index f73efb76c..035d33cb3 100644 --- a/mpi/mpi-internal.h +++ b/mpi/mpi-internal.h @@ -161,6 +161,11 @@ typedef int mpi_size_t; /* (must be a signed type) */ #endif void mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs ); +/*-- mpi-bit.c --*/ +void mpi_rshift_limbs( MPI a, unsigned int count ); +void mpi_lshift_limbs( MPI a, unsigned int count ); + + /*-- mpihelp-add.c --*/ mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, mpi_limb_t s2_limb ); diff --git a/mpi/mpi-mpow.c b/mpi/mpi-mpow.c index 689a7600f..a8c561dd1 100644 --- a/mpi/mpi-mpow.c +++ b/mpi/mpi-mpow.c @@ -1,5 +1,5 @@ /* mpi-mpow.c - MPI functions - * Copyright (C) 1998 Free Software Foundation, Inc. + * Copyright (C) 1998, 1999 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -25,6 +25,22 @@ #include "longlong.h" #include +/* Barrett is slower than the classical way. It can be tweaked by + * using partial multiplications + */ +/*#define USE_BARRETT*/ + + + +#ifdef USE_BARRETT +static void barrett_mulm( MPI w, MPI u, MPI v, MPI m, MPI y, int k, MPI r1, MPI r2 ); +static MPI init_barrett( MPI m, int *k, MPI *r1, MPI *r2 ); +static int calc_barrett( MPI r, MPI x, MPI m, MPI y, int k, MPI r1, MPI r2 ); +#else +#define barrett_mulm( w, u, v, m, y, k, r1, r2 ) mpi_mulm( (w), (u), (v), (m) ) +#endif + + static int build_index( MPI *exparray, int k, int i, int t ) { @@ -52,6 +68,10 @@ mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m) int i, j, idx; MPI *G; /* table with precomputed values of size 2^k */ MPI tmp; + #ifdef USE_BARRETT + MPI barrett_y, barrett_r1, barrett_r2; + int barrett_k; + #endif for(k=0; basearray[k]; k++ ) ; @@ -68,27 +88,15 @@ mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m) assert( k < 10 ); G = m_alloc_clear( (1<= 0 && idx < (1< 3 ? k-3:0; + + mpi_normalize( x ); + if( mpi_get_nlimbs(x) > 2*k ) + return 1; /* can't do it */ + + /* 1. q1 = floor( x / b^k-1) + * q2 = q1 * y + * q3 = floor( q2 / b^k+1 ) + * Actually, we don't need qx, we can work direct on r2 + */ + mpi_set( r2, x ); + mpi_rshift_limbs( r2, k-1 ); + mpi_mul( r2, r2, y ); + mpi_rshift_limbs( r2, k+1 ); + + /* 2. r1 = x mod b^k+1 + * r2 = q3 * m mod b^k+1 + * r = r1 - r2 + * 3. if r < 0 then r = r + b^k+1 + */ + mpi_set( r1, x ); + if( r1->nlimbs > k+1 ) /* quick modulo operation */ + r1->nlimbs = k+1; + mpi_mul( r2, r2, m ); + if( r2->nlimbs > k+1 ) /* quick modulo operation */ + r2->nlimbs = k+1; + mpi_sub( r, r1, r2 ); + + if( mpi_is_neg( r ) ) { + MPI tmp; + + tmp = mpi_alloc( k + 2 ); + mpi_set_ui( tmp, 1 ); + mpi_lshift_limbs( tmp, k+1 ); + mpi_add( r, r, tmp ); + mpi_free(tmp); + } + + /* 4. while r >= m do r = r - m */ + while( mpi_cmp( r, m ) >= 0 ) + mpi_sub( r, r, m ); + + return 0; +} +#endif /* USE_BARRETT */ + diff --git a/mpi/mpi-mul.c b/mpi/mpi-mul.c index f83824926..df8eb2586 100644 --- a/mpi/mpi-mul.c +++ b/mpi/mpi-mul.c @@ -189,7 +189,6 @@ mpi_mul( MPI w, MPI u, MPI v) } - void mpi_mulm( MPI w, MPI u, MPI v, MPI m) { -- cgit v1.2.3