raw
ch1_mpi                 1 /* mpi-mpow.c  -  MPI functions
ch1_mpi 2 * Modified by No Such Labs. (C) 2015. See README.
ch1_mpi 3 *
ch1_mpi 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
ch1_mpi 5 * SHA256(gnupg-1.4.10.tar.gz):
ch1_mpi 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
ch1_mpi 7 * (C) 1994-2005 Free Software Foundation, Inc.
ch1_mpi 8 *
ch1_mpi 9 * This program is free software: you can redistribute it and/or modify
ch1_mpi 10 * it under the terms of the GNU General Public License as published by
ch1_mpi 11 * the Free Software Foundation, either version 3 of the License, or
ch1_mpi 12 * (at your option) any later version.
ch1_mpi 13 *
ch1_mpi 14 * This program is distributed in the hope that it will be useful,
ch1_mpi 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ch1_mpi 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ch1_mpi 17 * GNU General Public License for more details.
ch1_mpi 18 *
ch1_mpi 19 * You should have received a copy of the GNU General Public License
ch1_mpi 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ch1_mpi 21 */
ch1_mpi 22
ch1_mpi 23 #include <stdio.h>
ch1_mpi 24 #include <stdlib.h>
ch1_mpi 25 #include <assert.h>
ch1_mpi 26
ch1_mpi 27 #include "knobs.h"
ch1_mpi 28 #include "mpi-internal.h"
ch1_mpi 29 #include "longlong.h"
ch1_mpi 30
ch1_mpi 31
ch1_mpi 32 static int
ch1_mpi 33 build_index( MPI *exparray, int k, int i, int t )
ch1_mpi 34 {
ch1_mpi 35 int j, bitno;
ch1_mpi 36 int idx = 0;
ch1_mpi 37
ch1_mpi 38 bitno = t-i;
ch1_mpi 39 for(j=k-1; j >= 0; j-- ) {
ch1_mpi 40 idx <<= 1;
ch1_mpi 41 if( mpi_test_bit( exparray[j], bitno ) )
ch1_mpi 42 idx |= 1;
ch1_mpi 43 }
ch1_mpi 44 return idx;
ch1_mpi 45 }
ch1_mpi 46
ch1_mpi 47 /****************
ch1_mpi 48 * RES = (BASE[0] ^ EXP[0]) * (BASE[1] ^ EXP[1]) * ... * mod M
ch1_mpi 49 */
ch1_mpi 50 void
ch1_mpi 51 mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m)
ch1_mpi 52 {
ch1_mpi 53 int k; /* number of elements */
ch1_mpi 54 int t; /* bit size of largest exponent */
ch1_mpi 55 int i, j, idx;
ch1_mpi 56 MPI *G; /* table with precomputed values of size 2^k */
ch1_mpi 57 MPI tmp;
ch1_mpi 58
ch1_mpi 59 for(k=0; basearray[k]; k++ )
ch1_mpi 60 ;
ch1_mpi 61 assert(k);
ch1_mpi 62 for(t=0, i=0; (tmp=exparray[i]); i++ ) {
ch1_mpi 63 j = mpi_get_nbits(tmp);
ch1_mpi 64 if( j > t )
ch1_mpi 65 t = j;
ch1_mpi 66 }
ch1_mpi 67 assert(i==k);
ch1_mpi 68 assert(t);
ch1_mpi 69 assert( k < 10 );
ch1_mpi 70
ch1_mpi 71 G = xmalloc_clear( (1<<k) * sizeof *G );
ch1_mpi 72 /* and calculate */
ch1_mpi 73 tmp = mpi_alloc( mpi_get_nlimbs(m)+1 );
ch1_mpi 74 mpi_set_ui( res, 1 );
ch1_mpi 75 for(i = 1; i <= t; i++ ) {
ch1_mpi 76 mpi_mulm(tmp, res, res, m );
ch1_mpi 77 idx = build_index( exparray, k, i, t );
ch1_mpi 78 assert( idx >= 0 && idx < (1<<k) );
ch1_mpi 79 if( !G[idx] ) {
ch1_mpi 80 if( !idx )
ch1_mpi 81 G[0] = mpi_alloc_set_ui( 1 );
ch1_mpi 82 else {
ch1_mpi 83 for(j=0; j < k; j++ ) {
ch1_mpi 84 if( (idx & (1<<j) ) ) {
ch1_mpi 85 if( !G[idx] )
ch1_mpi 86 G[idx] = mpi_copy( basearray[j] );
ch1_mpi 87 else
ch1_mpi 88 mpi_mulm( G[idx], G[idx], basearray[j], m );
ch1_mpi 89 }
ch1_mpi 90 }
ch1_mpi 91 if( !G[idx] )
ch1_mpi 92 G[idx] = mpi_alloc(0);
ch1_mpi 93 }
ch1_mpi 94 }
ch1_mpi 95 mpi_mulm(res, tmp, G[idx], m );
ch1_mpi 96 }
ch1_mpi 97
ch1_mpi 98 /* cleanup */
ch1_mpi 99 mpi_free(tmp);
ch1_mpi 100 for(i=0; i < (1<<k); i++ )
ch1_mpi 101 mpi_free(G[i]);
ch1_mpi 102 xfree(G);
ch1_mpi 103 }