aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-mpow.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2011-06-27 15:59:06 +1000
committerDavid Walter Seikel2011-06-27 15:59:06 +1000
commit92bd9b2503e7dd927f416cb9b17f169ea925fa56 (patch)
treef0f3776cfc0c6e6224db66f5678387c580095f16 /linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-mpow.c
parentMake use of the new packaging stuff from upstream. (diff)
downloadmeta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.zip
meta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.tar.gz
meta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.tar.bz2
meta-impy-92bd9b2503e7dd927f416cb9b17f169ea925fa56.tar.xz
Clean up line endings, execute bits, and other bits of errant nonsense from Windows.
Diffstat (limited to '')
-rw-r--r--[-rwxr-xr-x]linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-mpow.c448
1 files changed, 224 insertions, 224 deletions
diff --git a/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-mpow.c b/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-mpow.c
index 463dee8..f37c6f9 100755..100644
--- a/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-mpow.c
+++ b/linden/indra/libgcrypt/libgcrypt-1.2.2/mpi/mpi-mpow.c
@@ -1,224 +1,224 @@
1/* mpi-mpow.c - MPI functions 1/* mpi-mpow.c - MPI functions
2 * Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc. 2 * Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3 * 3 *
4 * This file is part of Libgcrypt. 4 * This file is part of Libgcrypt.
5 * 5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify 6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as 7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of 8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version. 9 * the License, or (at your option) any later version.
10 * 10 *
11 * Libgcrypt is distributed in the hope that it will be useful, 11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details. 14 * GNU Lesser General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Lesser General Public 16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software 17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */ 19 */
20 20
21#include <config.h> 21#include <config.h>
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include "mpi-internal.h" 24#include "mpi-internal.h"
25#include "longlong.h" 25#include "longlong.h"
26#include "g10lib.h" 26#include "g10lib.h"
27#include <assert.h> 27#include <assert.h>
28 28
29 29
30/* Barrett is slower than the classical way. It can be tweaked by 30/* Barrett is slower than the classical way. It can be tweaked by
31 * using partial multiplications 31 * using partial multiplications
32 */ 32 */
33/*#define USE_BARRETT*/ 33/*#define USE_BARRETT*/
34 34
35 35
36 36
37#ifdef USE_BARRETT 37#ifdef USE_BARRETT
38static void barrett_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 ); 38static void barrett_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 );
39static gcry_mpi_t init_barrett( gcry_mpi_t m, int *k, gcry_mpi_t *r1, gcry_mpi_t *r2 ); 39static gcry_mpi_t init_barrett( gcry_mpi_t m, int *k, gcry_mpi_t *r1, gcry_mpi_t *r2 );
40static int calc_barrett( gcry_mpi_t r, gcry_mpi_t x, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 ); 40static int calc_barrett( gcry_mpi_t r, gcry_mpi_t x, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 );
41#else 41#else
42#define barrett_mulm( w, u, v, m, y, k, r1, r2 ) gcry_mpi_mulm( (w), (u), (v), (m) ) 42#define barrett_mulm( w, u, v, m, y, k, r1, r2 ) gcry_mpi_mulm( (w), (u), (v), (m) )
43#endif 43#endif
44 44
45 45
46static int 46static int
47build_index( gcry_mpi_t *exparray, int k, int i, int t ) 47build_index( gcry_mpi_t *exparray, int k, int i, int t )
48{ 48{
49 int j, bitno; 49 int j, bitno;
50 int idx = 0; 50 int idx = 0;
51 51
52 bitno = t-i; 52 bitno = t-i;
53 for(j=k-1; j >= 0; j-- ) { 53 for(j=k-1; j >= 0; j-- ) {
54 idx <<= 1; 54 idx <<= 1;
55 if( mpi_test_bit( exparray[j], bitno ) ) 55 if( mpi_test_bit( exparray[j], bitno ) )
56 idx |= 1; 56 idx |= 1;
57 } 57 }
58 /*log_debug("t=%d i=%d idx=%d\n", t, i, idx );*/ 58 /*log_debug("t=%d i=%d idx=%d\n", t, i, idx );*/
59 return idx; 59 return idx;
60} 60}
61 61
62/**************** 62/****************
63 * RES = (BASE[0] ^ EXP[0]) * (BASE[1] ^ EXP[1]) * ... * mod M 63 * RES = (BASE[0] ^ EXP[0]) * (BASE[1] ^ EXP[1]) * ... * mod M
64 */ 64 */
65void 65void
66_gcry_mpi_mulpowm( gcry_mpi_t res, gcry_mpi_t *basearray, gcry_mpi_t *exparray, gcry_mpi_t m) 66_gcry_mpi_mulpowm( gcry_mpi_t res, gcry_mpi_t *basearray, gcry_mpi_t *exparray, gcry_mpi_t m)
67{ 67{
68 int k; /* number of elements */ 68 int k; /* number of elements */
69 int t; /* bit size of largest exponent */ 69 int t; /* bit size of largest exponent */
70 int i, j, idx; 70 int i, j, idx;
71 gcry_mpi_t *G; /* table with precomputed values of size 2^k */ 71 gcry_mpi_t *G; /* table with precomputed values of size 2^k */
72 gcry_mpi_t tmp; 72 gcry_mpi_t tmp;
73#ifdef USE_BARRETT 73#ifdef USE_BARRETT
74 gcry_mpi_t barrett_y, barrett_r1, barrett_r2; 74 gcry_mpi_t barrett_y, barrett_r1, barrett_r2;
75 int barrett_k; 75 int barrett_k;
76#endif 76#endif
77 77
78 for(k=0; basearray[k]; k++ ) 78 for(k=0; basearray[k]; k++ )
79 ; 79 ;
80 assert(k); 80 assert(k);
81 for(t=0, i=0; (tmp=exparray[i]); i++ ) { 81 for(t=0, i=0; (tmp=exparray[i]); i++ ) {
82 /*log_mpidump("exp: ", tmp );*/ 82 /*log_mpidump("exp: ", tmp );*/
83 j = mpi_get_nbits(tmp); 83 j = mpi_get_nbits(tmp);
84 if( j > t ) 84 if( j > t )
85 t = j; 85 t = j;
86 } 86 }
87 /*log_mpidump("mod: ", m );*/ 87 /*log_mpidump("mod: ", m );*/
88 assert(i==k); 88 assert(i==k);
89 assert(t); 89 assert(t);
90 assert( k < 10 ); 90 assert( k < 10 );
91 91
92 G = gcry_xcalloc( (1<<k) , sizeof *G ); 92 G = gcry_xcalloc( (1<<k) , sizeof *G );
93#ifdef USE_BARRETT 93#ifdef USE_BARRETT
94 barrett_y = init_barrett( m, &barrett_k, &barrett_r1, &barrett_r2 ); 94 barrett_y = init_barrett( m, &barrett_k, &barrett_r1, &barrett_r2 );
95#endif 95#endif
96 /* and calculate */ 96 /* and calculate */
97 tmp = mpi_alloc( mpi_get_nlimbs(m)+1 ); 97 tmp = mpi_alloc( mpi_get_nlimbs(m)+1 );
98 mpi_set_ui( res, 1 ); 98 mpi_set_ui( res, 1 );
99 for(i = 1; i <= t; i++ ) { 99 for(i = 1; i <= t; i++ ) {
100 barrett_mulm(tmp, res, res, m, barrett_y, barrett_k, 100 barrett_mulm(tmp, res, res, m, barrett_y, barrett_k,
101 barrett_r1, barrett_r2 ); 101 barrett_r1, barrett_r2 );
102 idx = build_index( exparray, k, i, t ); 102 idx = build_index( exparray, k, i, t );
103 assert( idx >= 0 && idx < (1<<k) ); 103 assert( idx >= 0 && idx < (1<<k) );
104 if( !G[idx] ) { 104 if( !G[idx] ) {
105 if( !idx ) 105 if( !idx )
106 G[0] = mpi_alloc_set_ui( 1 ); 106 G[0] = mpi_alloc_set_ui( 1 );
107 else { 107 else {
108 for(j=0; j < k; j++ ) { 108 for(j=0; j < k; j++ ) {
109 if( (idx & (1<<j) ) ) { 109 if( (idx & (1<<j) ) ) {
110 if( !G[idx] ) 110 if( !G[idx] )
111 G[idx] = mpi_copy( basearray[j] ); 111 G[idx] = mpi_copy( basearray[j] );
112 else 112 else
113 barrett_mulm( G[idx], G[idx], basearray[j], 113 barrett_mulm( G[idx], G[idx], basearray[j],
114 m, barrett_y, barrett_k, barrett_r1, barrett_r2 ); 114 m, barrett_y, barrett_k, barrett_r1, barrett_r2 );
115 } 115 }
116 } 116 }
117 if( !G[idx] ) 117 if( !G[idx] )
118 G[idx] = mpi_alloc(0); 118 G[idx] = mpi_alloc(0);
119 } 119 }
120 } 120 }
121 barrett_mulm(res, tmp, G[idx], m, barrett_y, barrett_k, barrett_r1, barrett_r2 ); 121 barrett_mulm(res, tmp, G[idx], m, barrett_y, barrett_k, barrett_r1, barrett_r2 );
122 } 122 }
123 123
124 /* cleanup */ 124 /* cleanup */
125 mpi_free(tmp); 125 mpi_free(tmp);
126#ifdef USE_BARRETT 126#ifdef USE_BARRETT
127 mpi_free(barrett_y); 127 mpi_free(barrett_y);
128 mpi_free(barrett_r1); 128 mpi_free(barrett_r1);
129 mpi_free(barrett_r2); 129 mpi_free(barrett_r2);
130#endif 130#endif
131 for(i=0; i < (1<<k); i++ ) 131 for(i=0; i < (1<<k); i++ )
132 mpi_free(G[i]); 132 mpi_free(G[i]);
133 gcry_free(G); 133 gcry_free(G);
134} 134}
135 135
136 136
137 137
138#ifdef USE_BARRETT 138#ifdef USE_BARRETT
139static void 139static void
140barrett_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 ) 140barrett_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 )
141{ 141{
142 mpi_mul(w, u, v); 142 mpi_mul(w, u, v);
143 if( calc_barrett( w, w, m, y, k, r1, r2 ) ) 143 if( calc_barrett( w, w, m, y, k, r1, r2 ) )
144 mpi_fdiv_r( w, w, m ); 144 mpi_fdiv_r( w, w, m );
145} 145}
146 146
147/**************** 147/****************
148 * Barrett precalculation: y = floor(b^(2k) / m) 148 * Barrett precalculation: y = floor(b^(2k) / m)
149 */ 149 */
150static gcry_mpi_t 150static gcry_mpi_t
151init_barrett( gcry_mpi_t m, int *k, gcry_mpi_t *r1, gcry_mpi_t *r2 ) 151init_barrett( gcry_mpi_t m, int *k, gcry_mpi_t *r1, gcry_mpi_t *r2 )
152{ 152{
153 gcry_mpi_t tmp; 153 gcry_mpi_t tmp;
154 154
155 mpi_normalize( m ); 155 mpi_normalize( m );
156 *k = mpi_get_nlimbs( m ); 156 *k = mpi_get_nlimbs( m );
157 tmp = mpi_alloc( *k + 1 ); 157 tmp = mpi_alloc( *k + 1 );
158 mpi_set_ui( tmp, 1 ); 158 mpi_set_ui( tmp, 1 );
159 mpi_lshift_limbs( tmp, 2 * *k ); 159 mpi_lshift_limbs( tmp, 2 * *k );
160 mpi_fdiv_q( tmp, tmp, m ); 160 mpi_fdiv_q( tmp, tmp, m );
161 *r1 = mpi_alloc( 2* *k + 1 ); 161 *r1 = mpi_alloc( 2* *k + 1 );
162 *r2 = mpi_alloc( 2* *k + 1 ); 162 *r2 = mpi_alloc( 2* *k + 1 );
163 return tmp; 163 return tmp;
164} 164}
165 165
166/**************** 166/****************
167 * Barrett reduction: We assume that these conditions are met: 167 * Barrett reduction: We assume that these conditions are met:
168 * Given x =(x_2k-1 ...x_0)_b 168 * Given x =(x_2k-1 ...x_0)_b
169 * m =(m_k-1 ....m_0)_b with m_k-1 != 0 169 * m =(m_k-1 ....m_0)_b with m_k-1 != 0
170 * Output r = x mod m 170 * Output r = x mod m
171 * Before using this function init_barret must be used to calucalte y and k. 171 * Before using this function init_barret must be used to calucalte y and k.
172 * Returns: false = no error 172 * Returns: false = no error
173 * true = can't perform barret reduction 173 * true = can't perform barret reduction
174 */ 174 */
175static int 175static int
176calc_barrett( gcry_mpi_t r, gcry_mpi_t x, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 ) 176calc_barrett( gcry_mpi_t r, gcry_mpi_t x, gcry_mpi_t m, gcry_mpi_t y, int k, gcry_mpi_t r1, gcry_mpi_t r2 )
177{ 177{
178 int xx = k > 3 ? k-3:0; 178 int xx = k > 3 ? k-3:0;
179 179
180 mpi_normalize( x ); 180 mpi_normalize( x );
181 if( mpi_get_nlimbs(x) > 2*k ) 181 if( mpi_get_nlimbs(x) > 2*k )
182 return 1; /* can't do it */ 182 return 1; /* can't do it */
183 183
184 /* 1. q1 = floor( x / b^k-1) 184 /* 1. q1 = floor( x / b^k-1)
185 * q2 = q1 * y 185 * q2 = q1 * y
186 * q3 = floor( q2 / b^k+1 ) 186 * q3 = floor( q2 / b^k+1 )
187 * Actually, we don't need qx, we can work direct on r2 187 * Actually, we don't need qx, we can work direct on r2
188 */ 188 */
189 mpi_set( r2, x ); 189 mpi_set( r2, x );
190 mpi_rshift_limbs( r2, k-1 ); 190 mpi_rshift_limbs( r2, k-1 );
191 mpi_mul( r2, r2, y ); 191 mpi_mul( r2, r2, y );
192 mpi_rshift_limbs( r2, k+1 ); 192 mpi_rshift_limbs( r2, k+1 );
193 193
194 /* 2. r1 = x mod b^k+1 194 /* 2. r1 = x mod b^k+1
195 * r2 = q3 * m mod b^k+1 195 * r2 = q3 * m mod b^k+1
196 * r = r1 - r2 196 * r = r1 - r2
197 * 3. if r < 0 then r = r + b^k+1 197 * 3. if r < 0 then r = r + b^k+1
198 */ 198 */
199 mpi_set( r1, x ); 199 mpi_set( r1, x );
200 if( r1->nlimbs > k+1 ) /* quick modulo operation */ 200 if( r1->nlimbs > k+1 ) /* quick modulo operation */
201 r1->nlimbs = k+1; 201 r1->nlimbs = k+1;
202 mpi_mul( r2, r2, m ); 202 mpi_mul( r2, r2, m );
203 if( r2->nlimbs > k+1 ) /* quick modulo operation */ 203 if( r2->nlimbs > k+1 ) /* quick modulo operation */
204 r2->nlimbs = k+1; 204 r2->nlimbs = k+1;
205 mpi_sub( r, r1, r2 ); 205 mpi_sub( r, r1, r2 );
206 206
207 if( mpi_is_neg( r ) ) { 207 if( mpi_is_neg( r ) ) {
208 gcry_mpi_t tmp; 208 gcry_mpi_t tmp;
209 209
210 tmp = mpi_alloc( k + 2 ); 210 tmp = mpi_alloc( k + 2 );
211 mpi_set_ui( tmp, 1 ); 211 mpi_set_ui( tmp, 1 );
212 mpi_lshift_limbs( tmp, k+1 ); 212 mpi_lshift_limbs( tmp, k+1 );
213 mpi_add( r, r, tmp ); 213 mpi_add( r, r, tmp );
214 mpi_free(tmp); 214 mpi_free(tmp);
215 } 215 }
216 216
217 /* 4. while r >= m do r = r - m */ 217 /* 4. while r >= m do r = r - m */
218 while( mpi_cmp( r, m ) >= 0 ) 218 while( mpi_cmp( r, m ) >= 0 )
219 mpi_sub( r, r, m ); 219 mpi_sub( r, r, m );
220 220
221 return 0; 221 return 0;
222} 222}
223#endif /* USE_BARRETT */ 223#endif /* USE_BARRETT */
224 224