Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: mozilla/security/nss/lib/freebl/mpi/mpi.h

Issue 14249009: Change the NSS and NSPR source tree to the new directory structure to be (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/nss/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * mpi.h
3 *
4 * Arbitrary precision integer arithmetic library
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 /* $Id: mpi.h,v 1.26 2012/11/14 01:14:11 wtc%google.com Exp $ */
10
11 #ifndef _H_MPI_
12 #define _H_MPI_
13
14 #include "mpi-config.h"
15
16 #if MP_DEBUG
17 #undef MP_IOFUNC
18 #define MP_IOFUNC 1
19 #endif
20
21 #if MP_IOFUNC
22 #include <stdio.h>
23 #include <ctype.h>
24 #endif
25
26 #include <limits.h>
27
28 #if defined(BSDI)
29 #undef ULLONG_MAX
30 #endif
31
32 #include <sys/types.h>
33
34 #define MP_NEG 1
35 #define MP_ZPOS 0
36
37 #define MP_OKAY 0 /* no error, all is well */
38 #define MP_YES 0 /* yes (boolean result) */
39 #define MP_NO -1 /* no (boolean result) */
40 #define MP_MEM -2 /* out of memory */
41 #define MP_RANGE -3 /* argument out of range */
42 #define MP_BADARG -4 /* invalid parameter */
43 #define MP_UNDEF -5 /* answer is undefined */
44 #define MP_LAST_CODE MP_UNDEF
45
46 typedef unsigned int mp_sign;
47 typedef unsigned int mp_size;
48 typedef int mp_err;
49
50 #define MP_32BIT_MAX 4294967295U
51
52 #if !defined(ULONG_MAX)
53 #error "ULONG_MAX not defined"
54 #elif !defined(UINT_MAX)
55 #error "UINT_MAX not defined"
56 #elif !defined(USHRT_MAX)
57 #error "USHRT_MAX not defined"
58 #endif
59
60 #if defined(ULONG_LONG_MAX) /* GCC, HPUX */
61 #define MP_ULONG_LONG_MAX ULONG_LONG_MAX
62 #elif defined(ULLONG_MAX) /* Solaris */
63 #define MP_ULONG_LONG_MAX ULLONG_MAX
64 /* MP_ULONG_LONG_MAX was defined to be ULLONG_MAX */
65 #elif defined(ULONGLONG_MAX) /* IRIX, AIX */
66 #define MP_ULONG_LONG_MAX ULONGLONG_MAX
67 #endif
68
69 /* We only use unsigned long for mp_digit iff long is more than 32 bits. */
70 #if !defined(MP_USE_UINT_DIGIT) && ULONG_MAX > MP_32BIT_MAX
71 typedef unsigned long mp_digit;
72 #define MP_DIGIT_MAX ULONG_MAX
73 #define MP_DIGIT_FMT "%016lX" /* printf() format for 1 digit */
74 #define MP_HALF_DIGIT_MAX UINT_MAX
75 #undef MP_NO_MP_WORD
76 #define MP_NO_MP_WORD 1
77 #undef MP_USE_LONG_DIGIT
78 #define MP_USE_LONG_DIGIT 1
79 #undef MP_USE_LONG_LONG_DIGIT
80
81 #elif !defined(MP_USE_UINT_DIGIT) && defined(MP_ULONG_LONG_MAX)
82 typedef unsigned long long mp_digit;
83 #define MP_DIGIT_MAX MP_ULONG_LONG_MAX
84 #define MP_DIGIT_FMT "%016llX" /* printf() format for 1 digit */
85 #define MP_HALF_DIGIT_MAX UINT_MAX
86 #undef MP_NO_MP_WORD
87 #define MP_NO_MP_WORD 1
88 #undef MP_USE_LONG_LONG_DIGIT
89 #define MP_USE_LONG_LONG_DIGIT 1
90 #undef MP_USE_LONG_DIGIT
91
92 #else
93 typedef unsigned int mp_digit;
94 #define MP_DIGIT_MAX UINT_MAX
95 #define MP_DIGIT_FMT "%08X" /* printf() format for 1 digit */
96 #define MP_HALF_DIGIT_MAX USHRT_MAX
97 #undef MP_USE_UINT_DIGIT
98 #define MP_USE_UINT_DIGIT 1
99 #undef MP_USE_LONG_LONG_DIGIT
100 #undef MP_USE_LONG_DIGIT
101 #endif
102
103 #if !defined(MP_NO_MP_WORD)
104 #if defined(MP_USE_UINT_DIGIT) && \
105 (defined(MP_ULONG_LONG_MAX) || (ULONG_MAX > UINT_MAX))
106
107 #if (ULONG_MAX > UINT_MAX)
108 typedef unsigned long mp_word;
109 typedef long mp_sword;
110 #define MP_WORD_MAX ULONG_MAX
111
112 #else
113 typedef unsigned long long mp_word;
114 typedef long long mp_sword;
115 #define MP_WORD_MAX MP_ULONG_LONG_MAX
116 #endif
117
118 #else
119 #define MP_NO_MP_WORD 1
120 #endif
121 #endif /* !defined(MP_NO_MP_WORD) */
122
123 #if !defined(MP_WORD_MAX) && defined(MP_DEFINE_SMALL_WORD)
124 typedef unsigned int mp_word;
125 typedef int mp_sword;
126 #define MP_WORD_MAX UINT_MAX
127 #endif
128
129 #define MP_DIGIT_BIT (CHAR_BIT*sizeof(mp_digit))
130 #define MP_WORD_BIT (CHAR_BIT*sizeof(mp_word))
131 #define MP_RADIX (1+(mp_word)MP_DIGIT_MAX)
132
133 #define MP_HALF_DIGIT_BIT (MP_DIGIT_BIT/2)
134 #define MP_HALF_RADIX (1+(mp_digit)MP_HALF_DIGIT_MAX)
135 /* MP_HALF_RADIX really ought to be called MP_SQRT_RADIX, but it's named
136 ** MP_HALF_RADIX because it's the radix for MP_HALF_DIGITs, and it's
137 ** consistent with the other _HALF_ names.
138 */
139
140
141 /* Macros for accessing the mp_int internals */
142 #define MP_SIGN(MP) ((MP)->sign)
143 #define MP_USED(MP) ((MP)->used)
144 #define MP_ALLOC(MP) ((MP)->alloc)
145 #define MP_DIGITS(MP) ((MP)->dp)
146 #define MP_DIGIT(MP,N) (MP)->dp[(N)]
147
148 /* This defines the maximum I/O base (minimum is 2) */
149 #define MP_MAX_RADIX 64
150
151 typedef struct {
152 mp_sign sign; /* sign of this quantity */
153 mp_size alloc; /* how many digits allocated */
154 mp_size used; /* how many digits used */
155 mp_digit *dp; /* the digits themselves */
156 } mp_int;
157
158 /* Default precision */
159 mp_size mp_get_prec(void);
160 void mp_set_prec(mp_size prec);
161
162 /* Memory management */
163 mp_err mp_init(mp_int *mp);
164 mp_err mp_init_size(mp_int *mp, mp_size prec);
165 mp_err mp_init_copy(mp_int *mp, const mp_int *from);
166 mp_err mp_copy(const mp_int *from, mp_int *to);
167 void mp_exch(mp_int *mp1, mp_int *mp2);
168 void mp_clear(mp_int *mp);
169 void mp_zero(mp_int *mp);
170 void mp_set(mp_int *mp, mp_digit d);
171 mp_err mp_set_int(mp_int *mp, long z);
172 #define mp_set_long(mp,z) mp_set_int(mp,z)
173 mp_err mp_set_ulong(mp_int *mp, unsigned long z);
174
175 /* Single digit arithmetic */
176 mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b);
177 mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b);
178 mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b);
179 mp_err mp_mul_2(const mp_int *a, mp_int *c);
180 mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r);
181 mp_err mp_div_2(const mp_int *a, mp_int *c);
182 mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c);
183
184 /* Sign manipulations */
185 mp_err mp_abs(const mp_int *a, mp_int *b);
186 mp_err mp_neg(const mp_int *a, mp_int *b);
187
188 /* Full arithmetic */
189 mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c);
190 mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
191 mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c);
192 #if MP_SQUARE
193 mp_err mp_sqr(const mp_int *a, mp_int *b);
194 #else
195 #define mp_sqr(a, b) mp_mul(a, a, b)
196 #endif
197 mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r);
198 mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r);
199 mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c);
200 mp_err mp_2expt(mp_int *a, mp_digit k);
201 mp_err mp_sqrt(const mp_int *a, mp_int *b);
202
203 /* Modular arithmetic */
204 #if MP_MODARITH
205 mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c);
206 mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c);
207 mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
208 mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
209 mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
210 #if MP_SQUARE
211 mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c);
212 #else
213 #define mp_sqrmod(a, m, c) mp_mulmod(a, a, m, c)
214 #endif
215 mp_err mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
216 mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c);
217 #endif /* MP_MODARITH */
218
219 /* Comparisons */
220 int mp_cmp_z(const mp_int *a);
221 int mp_cmp_d(const mp_int *a, mp_digit d);
222 int mp_cmp(const mp_int *a, const mp_int *b);
223 int mp_cmp_mag(mp_int *a, mp_int *b);
224 int mp_cmp_int(const mp_int *a, long z);
225 int mp_isodd(const mp_int *a);
226 int mp_iseven(const mp_int *a);
227
228 /* Number theoretic */
229 #if MP_NUMTH
230 mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c);
231 mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c);
232 mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y );
233 mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c);
234 mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c);
235 #endif /* end MP_NUMTH */
236
237 /* Input and output */
238 #if MP_IOFUNC
239 void mp_print(mp_int *mp, FILE *ofp);
240 #endif /* end MP_IOFUNC */
241
242 /* Base conversion */
243 mp_err mp_read_raw(mp_int *mp, char *str, int len);
244 int mp_raw_size(mp_int *mp);
245 mp_err mp_toraw(mp_int *mp, char *str);
246 mp_err mp_read_radix(mp_int *mp, const char *str, int radix);
247 mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix);
248 int mp_radix_size(mp_int *mp, int radix);
249 mp_err mp_toradix(mp_int *mp, char *str, int radix);
250 int mp_tovalue(char ch, int r);
251
252 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
253 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
254 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
255 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
256
257 /* Error strings */
258 const char *mp_strerror(mp_err ec);
259
260 /* Octet string conversion functions */
261 mp_err mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len );
262 int mp_unsigned_octet_size(const mp_int *mp);
263 mp_err mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxle n);
264 mp_err mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen) ;
265 mp_err mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size len);
266
267 /* Miscellaneous */
268 mp_size mp_trailing_zeros(const mp_int *mp);
269 void freebl_cpuid(unsigned long op, unsigned long *eax,
270 unsigned long *ebx, unsigned long *ecx,
271 unsigned long *edx);
272
273
274 #define MP_CHECKOK(x) if (MP_OKAY > (res = (x))) goto CLEANUP
275 #define MP_CHECKERR(x) if (MP_OKAY > (res = (x))) goto CLEANUP
276
277 #if defined(MP_API_COMPATIBLE)
278 #define NEG MP_NEG
279 #define ZPOS MP_ZPOS
280 #define DIGIT_MAX MP_DIGIT_MAX
281 #define DIGIT_BIT MP_DIGIT_BIT
282 #define DIGIT_FMT MP_DIGIT_FMT
283 #define RADIX MP_RADIX
284 #define MAX_RADIX MP_MAX_RADIX
285 #define SIGN(MP) MP_SIGN(MP)
286 #define USED(MP) MP_USED(MP)
287 #define ALLOC(MP) MP_ALLOC(MP)
288 #define DIGITS(MP) MP_DIGITS(MP)
289 #define DIGIT(MP,N) MP_DIGIT(MP,N)
290
291 #if MP_ARGCHK == 1
292 #define ARGCHK(X,Y) {if(!(X)){return (Y);}}
293 #elif MP_ARGCHK == 2
294 #include <assert.h>
295 #define ARGCHK(X,Y) assert(X)
296 #else
297 #define ARGCHK(X,Y) /* */
298 #endif
299 #endif /* defined MP_API_COMPATIBLE */
300
301 #endif /* end _H_MPI_ */
OLDNEW
« no previous file with comments | « mozilla/security/nss/lib/freebl/mpi/mpcpucache.c ('k') | mozilla/security/nss/lib/freebl/mpi/mpi.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698