summaryrefslogtreecommitdiff
path: root/mpi/mpi-mpow.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>1999-07-02 09:50:57 +0000
committerWerner Koch <wk@gnupg.org>1999-07-02 09:50:57 +0000
commit745415540374611ab1a944a1ee376c814adb9318 (patch)
tree5d426a7623f04c9ceaa97f86b6544e9ae125e41e /mpi/mpi-mpow.c
parent26a58078810c1d71303554d0eb3e7912c060a548 (diff)
downloadlibgcrypt-745415540374611ab1a944a1ee376c814adb9318.tar.gz
See ChangeLog: Fri Jul 2 11:45:54 CEST 1999 Werner Koch
Diffstat (limited to 'mpi/mpi-mpow.c')
-rw-r--r--mpi/mpi-mpow.c141
1 files changed, 122 insertions, 19 deletions
diff --git a/mpi/mpi-mpow.c b/mpi/mpi-mpow.c
index 689a7600..a8c561dd 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 <assert.h>
+/* 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<<k) * sizeof *G );
- #if 0
- /* do the precomputation */
- G[0] = mpi_alloc_set_ui( 1 );
- for(i=1; i < (1<<k); i++ ) {
- for(j=0; j < k; j++ ) {
- if( (i & (1<<j) ) ) {
- if( !G[i] )
- G[i] = mpi_copy( basearray[j] );
- else
- mpi_mulm( G[i], G[i], basearray[j], m );
- }
- }
- if( !G[i] )
- G[i] = mpi_alloc(0);
- }
+ #ifdef USE_BARRETT
+ barrett_y = init_barrett( m, &barrett_k, &barrett_r1, &barrett_r2 );
#endif
/* and calculate */
tmp = mpi_alloc( mpi_get_nlimbs(m)+1 );
mpi_set_ui( res, 1 );
for(i = 1; i <= t; i++ ) {
- mpi_mulm(tmp, res, res, m );
+ barrett_mulm(tmp, res, res, m, barrett_y, barrett_k,
+ barrett_r1, barrett_r2 );
idx = build_index( exparray, k, i, t );
assert( idx >= 0 && idx < (1<<k) );
if( !G[idx] ) {
@@ -100,20 +108,115 @@ mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m)
if( !G[idx] )
G[idx] = mpi_copy( basearray[j] );
else
- mpi_mulm( G[idx], G[idx], basearray[j], m );
+ barrett_mulm( G[idx], G[idx], basearray[j],
+ m, barrett_y, barrett_k, barrett_r1, barrett_r2 );
}
}
if( !G[idx] )
G[idx] = mpi_alloc(0);
}
}
- mpi_mulm(res, tmp, G[idx], m );
+ barrett_mulm(res, tmp, G[idx], m, barrett_y, barrett_k, barrett_r1, barrett_r2 );
}
/* cleanup */
mpi_free(tmp);
+ #ifdef USE_BARRETT
+ mpi_free(barrett_y);
+ mpi_free(barrett_r1);
+ mpi_free(barrett_r2);
+ #endif
for(i=0; i < (1<<k); i++ )
mpi_free(G[i]);
m_free(G);
}
+
+
+#ifdef USE_BARRETT
+static void
+barrett_mulm( MPI w, MPI u, MPI v, MPI m, MPI y, int k, MPI r1, MPI r2 )
+{
+ mpi_mul(w, u, v);
+ if( calc_barrett( w, w, m, y, k, r1, r2 ) )
+ mpi_fdiv_r( w, w, m );
+}
+
+/****************
+ * Barrett precalculation: y = floor(b^(2k) / m)
+ */
+static MPI
+init_barrett( MPI m, int *k, MPI *r1, MPI *r2 )
+{
+ MPI tmp;
+
+ mpi_normalize( m );
+ *k = mpi_get_nlimbs( m );
+ tmp = mpi_alloc( *k + 1 );
+ mpi_set_ui( tmp, 1 );
+ mpi_lshift_limbs( tmp, 2 * *k );
+ mpi_fdiv_q( tmp, tmp, m );
+ *r1 = mpi_alloc( 2* *k + 1 );
+ *r2 = mpi_alloc( 2* *k + 1 );
+ return tmp;
+}
+
+/****************
+ * Barrett reduction: We assume that these conditions are met:
+ * Given x =(x_2k-1 ...x_0)_b
+ * m =(m_k-1 ....m_0)_b with m_k-1 != 0
+ * Output r = x mod m
+ * Before using this function init_barret must be used to calucalte y and k.
+ * Returns: false = no error
+ * true = can't perform barret reduction
+ */
+static int
+calc_barrett( MPI r, MPI x, MPI m, MPI y, int k, MPI r1, MPI r2 )
+{
+ int xx = k > 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 */
+