~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to mpi/mpi-mpow.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 <config.h>
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include "mpi-internal.h"
25
 
#include "longlong.h"
26
 
#include <assert.h>
27
 
 
28
 
static int
29
 
build_index( MPI *exparray, int k, int i, int t )
30
 
{
31
 
    int j, bitno;
32
 
    int index = 0;
33
 
 
34
 
    bitno = t-i;
35
 
    for(j=k-1; j >= 0; j-- ) {
36
 
        index <<= 1;
37
 
        if( mpi_test_bit( exparray[j], bitno ) )
38
 
            index |= 1;
39
 
    }
40
 
    return index;
41
 
}
42
 
 
43
 
/****************
44
 
 * RES = (BASE[0] ^ EXP[0]) *  (BASE[1] ^ EXP[1]) * ... * mod M
45
 
 */
46
 
void
47
 
mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m)
48
 
{
49
 
    int k;      /* number of elements */
50
 
    int t;      /* bit size of largest exponent */
51
 
    int i, j, idx;
52
 
    MPI *G;     /* table with precomputed values of size 2^k */
53
 
    MPI tmp;
54
 
 
55
 
    for(k=0; basearray[k]; k++ )
56
 
        ;
57
 
    assert(k);
58
 
    for(t=0, i=0; (tmp=exparray[i]); i++ ) {
59
 
        j = mpi_get_nbits(tmp);
60
 
        if( j > t )
61
 
            t = j;
62
 
    }
63
 
    assert(i==k);
64
 
    assert(t);
65
 
    assert( k < 10 );
66
 
 
67
 
    G = m_alloc_clear( (1<<k) * sizeof *G );
68
 
    /* and calculate */
69
 
    tmp =  mpi_alloc( mpi_get_nlimbs(m)+1 );
70
 
    mpi_set_ui( res, 1 );
71
 
    for(i = 1; i <= t; i++ ) {
72
 
        mpi_mulm(tmp, res, res, m );
73
 
        idx = build_index( exparray, k, i, t );
74
 
        assert( idx >= 0 && idx < (1<<k) );
75
 
        if( !G[idx] ) {
76
 
            if( !idx )
77
 
                 G[0] = mpi_alloc_set_ui( 1 );
78
 
            else {
79
 
                for(j=0; j < k; j++ ) {
80
 
                    if( (idx & (1<<j) ) ) {
81
 
                        if( !G[idx] )
82
 
                            G[idx] = mpi_copy( basearray[j] );
83
 
                        else
84
 
                            mpi_mulm( G[idx], G[idx], basearray[j], m );
85
 
                    }
86
 
                }
87
 
                if( !G[idx] )
88
 
                    G[idx] = mpi_alloc(0);
89
 
            }
90
 
        }
91
 
        mpi_mulm(res, tmp, G[idx], m );
92
 
    }
93
 
 
94
 
    /* cleanup */
95
 
    mpi_free(tmp);
96
 
    for(i=0; i < (1<<k); i++ )
97
 
        mpi_free(G[i]);
98
 
    m_free(G);
99
 
}
100
 
 
101