OLD | NEW |
| (Empty) |
1 /* | |
2 * mpi-priv.h - Private header file for MPI | |
3 * Arbitrary precision integer arithmetic library | |
4 * | |
5 * NOTE WELL: the content of this header file is NOT part of the "public" | |
6 * API for the MPI library, and may change at any time. | |
7 * Application programs that use libmpi should NOT include this header file. | |
8 * | |
9 * This Source Code Form is subject to the terms of the Mozilla Public | |
10 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
11 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
12 /* $Id: mpi-priv.h,v 1.25 2012/04/25 14:49:50 gerv%gerv.net Exp $ */ | |
13 #ifndef _MPI_PRIV_H_ | |
14 #define _MPI_PRIV_H_ 1 | |
15 | |
16 #include "mpi.h" | |
17 #include <stdlib.h> | |
18 #include <string.h> | |
19 #include <ctype.h> | |
20 | |
21 #if MP_DEBUG | |
22 #include <stdio.h> | |
23 | |
24 #define DIAG(T,V) {fprintf(stderr,T);mp_print(V,stderr);fputc('\n',stderr);} | |
25 #else | |
26 #define DIAG(T,V) | |
27 #endif | |
28 | |
29 /* If we aren't using a wired-in logarithm table, we need to include | |
30 the math library to get the log() function | |
31 */ | |
32 | |
33 /* {{{ s_logv_2[] - log table for 2 in various bases */ | |
34 | |
35 #if MP_LOGTAB | |
36 /* | |
37 A table of the logs of 2 for various bases (the 0 and 1 entries of | |
38 this table are meaningless and should not be referenced). | |
39 | |
40 This table is used to compute output lengths for the mp_toradix() | |
41 function. Since a number n in radix r takes up about log_r(n) | |
42 digits, we estimate the output size by taking the least integer | |
43 greater than log_r(n), where: | |
44 | |
45 log_r(n) = log_2(n) * log_r(2) | |
46 | |
47 This table, therefore, is a table of log_r(2) for 2 <= r <= 36, | |
48 which are the output bases supported. | |
49 */ | |
50 | |
51 extern const float s_logv_2[]; | |
52 #define LOG_V_2(R) s_logv_2[(R)] | |
53 | |
54 #else | |
55 | |
56 /* | |
57 If MP_LOGTAB is not defined, use the math library to compute the | |
58 logarithms on the fly. Otherwise, use the table. | |
59 Pick which works best for your system. | |
60 */ | |
61 | |
62 #include <math.h> | |
63 #define LOG_V_2(R) (log(2.0)/log(R)) | |
64 | |
65 #endif /* if MP_LOGTAB */ | |
66 | |
67 /* }}} */ | |
68 | |
69 /* {{{ Digit arithmetic macros */ | |
70 | |
71 /* | |
72 When adding and multiplying digits, the results can be larger than | |
73 can be contained in an mp_digit. Thus, an mp_word is used. These | |
74 macros mask off the upper and lower digits of the mp_word (the | |
75 mp_word may be more than 2 mp_digits wide, but we only concern | |
76 ourselves with the low-order 2 mp_digits) | |
77 */ | |
78 | |
79 #define CARRYOUT(W) (mp_digit)((W)>>DIGIT_BIT) | |
80 #define ACCUM(W) (mp_digit)(W) | |
81 | |
82 #define MP_MIN(a,b) (((a) < (b)) ? (a) : (b)) | |
83 #define MP_MAX(a,b) (((a) > (b)) ? (a) : (b)) | |
84 #define MP_HOWMANY(a,b) (((a) + (b) - 1)/(b)) | |
85 #define MP_ROUNDUP(a,b) (MP_HOWMANY(a,b) * (b)) | |
86 | |
87 /* }}} */ | |
88 | |
89 /* {{{ Comparison constants */ | |
90 | |
91 #define MP_LT -1 | |
92 #define MP_EQ 0 | |
93 #define MP_GT 1 | |
94 | |
95 /* }}} */ | |
96 | |
97 /* {{{ private function declarations */ | |
98 | |
99 /* | |
100 If MP_MACRO is false, these will be defined as actual functions; | |
101 otherwise, suitable macro definitions will be used. This works | |
102 around the fact that ANSI C89 doesn't support an 'inline' keyword | |
103 (although I hear C9x will ... about bloody time). At present, the | |
104 macro definitions are identical to the function bodies, but they'll | |
105 expand in place, instead of generating a function call. | |
106 | |
107 I chose these particular functions to be made into macros because | |
108 some profiling showed they are called a lot on a typical workload, | |
109 and yet they are primarily housekeeping. | |
110 */ | |
111 #if MP_MACRO == 0 | |
112 void s_mp_setz(mp_digit *dp, mp_size count); /* zero digits */ | |
113 void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count); /* copy */ | |
114 void *s_mp_alloc(size_t nb, size_t ni); /* general allocator */ | |
115 void s_mp_free(void *ptr); /* general free function */ | |
116 extern unsigned long mp_allocs; | |
117 extern unsigned long mp_frees; | |
118 extern unsigned long mp_copies; | |
119 #else | |
120 | |
121 /* Even if these are defined as macros, we need to respect the settings | |
122 of the MP_MEMSET and MP_MEMCPY configuration options... | |
123 */ | |
124 #if MP_MEMSET == 0 | |
125 #define s_mp_setz(dp, count) \ | |
126 {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=0;} | |
127 #else | |
128 #define s_mp_setz(dp, count) memset(dp, 0, (count) * sizeof(mp_digit)) | |
129 #endif /* MP_MEMSET */ | |
130 | |
131 #if MP_MEMCPY == 0 | |
132 #define s_mp_copy(sp, dp, count) \ | |
133 {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=(sp)[ix];} | |
134 #else | |
135 #define s_mp_copy(sp, dp, count) memcpy(dp, sp, (count) * sizeof(mp_digit)) | |
136 #endif /* MP_MEMCPY */ | |
137 | |
138 #define s_mp_alloc(nb, ni) calloc(nb, ni) | |
139 #define s_mp_free(ptr) {if(ptr) free(ptr);} | |
140 #endif /* MP_MACRO */ | |
141 | |
142 mp_err s_mp_grow(mp_int *mp, mp_size min); /* increase allocated size */ | |
143 mp_err s_mp_pad(mp_int *mp, mp_size min); /* left pad with zeroes */ | |
144 | |
145 #if MP_MACRO == 0 | |
146 void s_mp_clamp(mp_int *mp); /* clip leading zeroes */ | |
147 #else | |
148 #define s_mp_clamp(mp)\ | |
149 { mp_size used = MP_USED(mp); \ | |
150 while (used > 1 && DIGIT(mp, used - 1) == 0) --used; \ | |
151 MP_USED(mp) = used; \ | |
152 } | |
153 #endif /* MP_MACRO */ | |
154 | |
155 void s_mp_exch(mp_int *a, mp_int *b); /* swap a and b in place */ | |
156 | |
157 mp_err s_mp_lshd(mp_int *mp, mp_size p); /* left-shift by p digits */ | |
158 void s_mp_rshd(mp_int *mp, mp_size p); /* right-shift by p digits */ | |
159 mp_err s_mp_mul_2d(mp_int *mp, mp_digit d); /* multiply by 2^d in place */ | |
160 void s_mp_div_2d(mp_int *mp, mp_digit d); /* divide by 2^d in place */ | |
161 void s_mp_mod_2d(mp_int *mp, mp_digit d); /* modulo 2^d in place */ | |
162 void s_mp_div_2(mp_int *mp); /* divide by 2 in place */ | |
163 mp_err s_mp_mul_2(mp_int *mp); /* multiply by 2 in place */ | |
164 mp_err s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd); | |
165 /* normalize for division */ | |
166 mp_err s_mp_add_d(mp_int *mp, mp_digit d); /* unsigned digit addition */ | |
167 mp_err s_mp_sub_d(mp_int *mp, mp_digit d); /* unsigned digit subtract */ | |
168 mp_err s_mp_mul_d(mp_int *mp, mp_digit d); /* unsigned digit multiply */ | |
169 mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r); | |
170 /* unsigned digit divide */ | |
171 mp_err s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu); | |
172 /* Barrett reduction */ | |
173 mp_err s_mp_add(mp_int *a, const mp_int *b); /* magnitude addition */ | |
174 mp_err s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c); | |
175 mp_err s_mp_sub(mp_int *a, const mp_int *b); /* magnitude subtract */ | |
176 mp_err s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c); | |
177 mp_err s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset); | |
178 /* a += b * RADIX^offset */ | |
179 mp_err s_mp_mul(mp_int *a, const mp_int *b); /* magnitude multiply */ | |
180 #if MP_SQUARE | |
181 mp_err s_mp_sqr(mp_int *a); /* magnitude square */ | |
182 #else | |
183 #define s_mp_sqr(a) s_mp_mul(a, a) | |
184 #endif | |
185 mp_err s_mp_div(mp_int *rem, mp_int *div, mp_int *quot); /* magnitude div */ | |
186 mp_err s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int
*c); | |
187 mp_err s_mp_2expt(mp_int *a, mp_digit k); /* a = 2^k */ | |
188 int s_mp_cmp(const mp_int *a, const mp_int *b); /* magnitude comparison */ | |
189 int s_mp_cmp_d(const mp_int *a, mp_digit d); /* magnitude digit compare */ | |
190 int s_mp_ispow2(const mp_int *v); /* is v a power of 2? */ | |
191 int s_mp_ispow2d(mp_digit d); /* is d a power of 2? */ | |
192 | |
193 int s_mp_tovalue(char ch, int r); /* convert ch to value */ | |
194 char s_mp_todigit(mp_digit val, int r, int low); /* convert val to digit */ | |
195 int s_mp_outlen(int bits, int r); /* output length in bytes */ | |
196 mp_digit s_mp_invmod_radix(mp_digit P); /* returns (P ** -1) mod RADIX */ | |
197 mp_err s_mp_invmod_odd_m( const mp_int *a, const mp_int *m, mp_int *c); | |
198 mp_err s_mp_invmod_2d( const mp_int *a, mp_size k, mp_int *c); | |
199 mp_err s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c); | |
200 | |
201 #ifdef NSS_USE_COMBA | |
202 | |
203 #define IS_POWER_OF_2(a) ((a) && !((a) & ((a)-1))) | |
204 | |
205 void s_mp_mul_comba_4(const mp_int *A, const mp_int *B, mp_int *C); | |
206 void s_mp_mul_comba_8(const mp_int *A, const mp_int *B, mp_int *C); | |
207 void s_mp_mul_comba_16(const mp_int *A, const mp_int *B, mp_int *C); | |
208 void s_mp_mul_comba_32(const mp_int *A, const mp_int *B, mp_int *C); | |
209 | |
210 void s_mp_sqr_comba_4(const mp_int *A, mp_int *B); | |
211 void s_mp_sqr_comba_8(const mp_int *A, mp_int *B); | |
212 void s_mp_sqr_comba_16(const mp_int *A, mp_int *B); | |
213 void s_mp_sqr_comba_32(const mp_int *A, mp_int *B); | |
214 | |
215 #endif /* end NSS_USE_COMBA */ | |
216 | |
217 /* ------ mpv functions, operate on arrays of digits, not on mp_int's ------ */ | |
218 #if defined (__OS2__) && defined (__IBMC__) | |
219 #define MPI_ASM_DECL __cdecl | |
220 #else | |
221 #define MPI_ASM_DECL | |
222 #endif | |
223 | |
224 #ifdef MPI_AMD64 | |
225 | |
226 mp_digit MPI_ASM_DECL s_mpv_mul_set_vec64(mp_digit*, mp_digit *, mp_size, mp_dig
it); | |
227 mp_digit MPI_ASM_DECL s_mpv_mul_add_vec64(mp_digit*, const mp_digit*, mp_size, m
p_digit); | |
228 | |
229 /* c = a * b */ | |
230 #define s_mpv_mul_d(a, a_len, b, c) \ | |
231 ((mp_digit *)c)[a_len] = s_mpv_mul_set_vec64(c, a, a_len, b) | |
232 | |
233 /* c += a * b */ | |
234 #define s_mpv_mul_d_add(a, a_len, b, c) \ | |
235 ((mp_digit *)c)[a_len] = s_mpv_mul_add_vec64(c, a, a_len, b) | |
236 | |
237 | |
238 #else | |
239 | |
240 void MPI_ASM_DECL s_mpv_mul_d(const mp_digit *a, mp_size a_len, | |
241 mp_digit b, mp_digit *c); | |
242 void MPI_ASM_DECL s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, | |
243 mp_digit b, mp_digit *c); | |
244 | |
245 #endif | |
246 | |
247 void MPI_ASM_DECL s_mpv_mul_d_add_prop(const mp_digit *a, | |
248 mp_size a_len, mp_digit b, | |
249 mp_digit *c); | |
250 void MPI_ASM_DECL s_mpv_sqr_add_prop(const mp_digit *a, | |
251 mp_size a_len, | |
252 mp_digit *sqrs); | |
253 | |
254 mp_err MPI_ASM_DECL s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo, | |
255 mp_digit divisor, mp_digit *quot, mp_digit *rem); | |
256 | |
257 /* c += a * b * (MP_RADIX ** offset); */ | |
258 #define s_mp_mul_d_add_offset(a, b, c, off) \ | |
259 (s_mpv_mul_d_add_prop(MP_DIGITS(a), MP_USED(a), b, MP_DIGITS(c) + off), MP_OKAY) | |
260 | |
261 typedef struct { | |
262 mp_int N; /* modulus N */ | |
263 mp_digit n0prime; /* n0' = - (n0 ** -1) mod MP_RADIX */ | |
264 } mp_mont_modulus; | |
265 | |
266 mp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c, | |
267 mp_mont_modulus *mmm); | |
268 mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm); | |
269 | |
270 /* | |
271 * s_mpi_getProcessorLineSize() returns the size in bytes of the cache line | |
272 * if a cache exists, or zero if there is no cache. If more than one | |
273 * cache line exists, it should return the smallest line size (which is | |
274 * usually the L1 cache). | |
275 * | |
276 * mp_modexp uses this information to make sure that private key information | |
277 * isn't being leaked through the cache. | |
278 * | |
279 * see mpcpucache.c for the implementation. | |
280 */ | |
281 unsigned long s_mpi_getProcessorLineSize(); | |
282 | |
283 /* }}} */ | |
284 #endif | |
285 | |
OLD | NEW |