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