This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / crypto / mpi / mpi-mpow.c
1 /* mpi-mpow.c  -  MPI functions
2  * Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include "mpi-internal.h"
22 #include "longlong.h"
23
24
25 static int
26 build_index(const MPI *exparray, int k, int i, int t )
27 {
28     int j, bitno;
29     int index = 0;
30
31     bitno = t-i;
32     for(j=k-1; j >= 0; j-- ) {
33         index <<= 1;
34         if( mpi_test_bit( exparray[j], bitno ) )
35             index |= 1;
36     }
37     return index;
38 }
39
40 /****************
41  * RES = (BASE[0] ^ EXP[0]) *  (BASE[1] ^ EXP[1]) * ... * mod M
42  */
43 int
44 mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m)
45 {
46         int rc = -ENOMEM;
47         int k;  /* number of elements */
48         int t;  /* bit size of largest exponent */
49         int i, j, idx;
50         MPI *G = NULL;  /* table with precomputed values of size 2^k */
51         MPI tmp = NULL;
52
53         for(k=0; basearray[k]; k++ )
54                 ;
55         if (!k) { printk("mpi_mulpowm: assert(k) failed\n"); BUG(); }
56         for(t=0, i=0; (tmp=exparray[i]); i++ ) {
57                 j = mpi_get_nbits(tmp);
58                 if( j > t )
59                         t = j;
60         }
61         if (i!=k) { printk("mpi_mulpowm: assert(i==k) failed\n"); BUG(); }
62         if (!t)   { printk("mpi_mulpowm: assert(t) failed\n"); BUG(); }
63         if (k>=10) { printk("mpi_mulpowm: assert(k<10) failed\n"); BUG(); }
64
65         G = kmalloc( (1<<k) * sizeof *G, GFP_KERNEL );
66         if (!G) goto nomem;
67         memset(G,0,(1<<k) * sizeof *G);
68         /* and calculate */
69         tmp =  mpi_alloc( mpi_get_nlimbs(m)+1 ); if (!tmp) goto nomem;
70         if (mpi_set_ui( res, 1 ) < 0) goto nomem;
71         for(i = 1; i <= t; i++ ) {
72                 if (mpi_mulm(tmp, res, res, m ) < 0) goto nomem;
73                 idx = build_index( exparray, k, i, t );
74                 if (!(idx >= 0 && idx < (1<<k))) {
75                         printk("mpi_mulpowm: assert(idx >= 0 && idx < (1<<k)) failed\n");
76                         BUG();
77                 }
78                 if( !G[idx] ) {
79                         if( !idx ) {
80                                 G[0] = mpi_alloc_set_ui( 1 );
81                                 if (!G[0]) goto nomem;
82                         }
83                         else {
84                                 for(j=0; j < k; j++ ) {
85                                         if( (idx & (1<<j) ) ) {
86                                                 if( !G[idx] ) {
87                                                         if (mpi_copy( &G[idx], basearray[j] ) < 0)
88                                                                 goto nomem;
89                                                 }
90                                                 else {
91                                                         if (mpi_mulm(G[idx],G[idx],basearray[j],m) < 0)
92                                                                 goto nomem;
93                                                 }
94                                         }
95                                 }
96                                 if( !G[idx] ) {
97                                         G[idx] = mpi_alloc(0);
98                                         if (!G[idx]) goto nomem;
99                                 }
100                         }
101                 }
102                 if (mpi_mulm(res, tmp, G[idx], m ) < 0) goto nomem;
103         }
104
105         rc = 0;
106  nomem:
107         /* cleanup */
108         mpi_free(tmp);
109         for(i=0; i < (1<<k); i++ )
110                 mpi_free(G[i]);
111         kfree(G);
112         return rc;
113 }