xref: /illumos-gate/usr/src/common/mpi/mpi.h (revision 2e07277863d69344215bd0c72e171d0c854dbe56)
1 /* BEGIN CSTYLED */
2 /*
3  *  mpi.h
4  *
5  *  Arbitrary precision integer arithmetic library
6  *
7  * ***** BEGIN LICENSE BLOCK *****
8  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9  *
10  * The contents of this file are subject to the Mozilla Public License Version
11  * 1.1 (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS" basis,
16  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17  * for the specific language governing rights and limitations under the
18  * License.
19  *
20  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
21  *
22  * The Initial Developer of the Original Code is
23  * Michael J. Fromberger.
24  * Portions created by the Initial Developer are Copyright (C) 1998
25  * the Initial Developer. All Rights Reserved.
26  *
27  * Contributor(s):
28  *   Netscape Communications Corporation
29  *
30  * Alternatively, the contents of this file may be used under the terms of
31  * either the GNU General Public License Version 2 or later (the "GPL"), or
32  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33  * in which case the provisions of the GPL or the LGPL are applicable instead
34  * of those above. If you wish to allow use of your version of this file only
35  * under the terms of either the GPL or the LGPL, and not to allow others to
36  * use your version of this file under the terms of the MPL, indicate your
37  * decision by deleting the provisions above and replace them with the notice
38  * and other provisions required by the GPL or the LGPL. If you do not delete
39  * the provisions above, a recipient may use your version of this file under
40  * the terms of any one of the MPL, the GPL or the LGPL.
41  *
42  * ***** END LICENSE BLOCK ***** */
43 /*
44  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
45  *
46  * Sun elects to use this software under the MPL license.
47  */
48 
49 #ifndef _MPI_H
50 #define _MPI_H
51 
52 /* $Id: mpi.h,v 1.22 2004/04/27 23:04:36 gerv%gerv.net Exp $ */
53 
54 #include "mpi-config.h"
55 #include <sys/param.h>
56 #ifdef _KERNEL
57 #include <sys/debug.h>
58 #include <sys/systm.h>
59 #define assert ASSERT
60 #define	labs(a) (a >= 0 ? a : -a)
61 #define	memset(s, c, n) bzero(s, n)
62 #define	memcpy(a,b,c) bcopy((caddr_t)b, (caddr_t)a, c)
63 /*
64  * Generic #define's to cover missing things in the kernel
65  */
66 #ifndef isdigit
67 #define isdigit(x)	((x) >= '0' && (x) <= '9')
68 #endif
69 #ifndef isupper
70 #define isupper(x)	(((unsigned)(x) >= 'A') && ((unsigned)(x) <= 'Z'))
71 #endif
72 #ifndef islower
73 #define islower(x)	(((unsigned)(x) >= 'a') && ((unsigned)(x) <= 'z'))
74 #endif
75 #ifndef isalpha
76 #define isalpha(x)	(isupper(x) || islower(x))
77 #endif
78 #ifndef toupper
79 #define toupper(x)	(islower(x) ? (x) - 'a' + 'A' : (x))
80 #endif
81 #ifndef tolower
82 #define tolower(x)	(isupper(x) ? (x) + 'a' - 'A' : (x))
83 #endif
84 #ifndef isspace
85 #define isspace(x)	(((x) == ' ') || ((x) == '\r') || ((x) == '\n') || \
86 			 ((x) == '\t') || ((x) == '\b'))
87 #endif
88 #endif /* _KERNEL */
89 
90 #if MP_DEBUG
91 #undef MP_IOFUNC
92 #define MP_IOFUNC 1
93 #endif
94 
95 #if MP_IOFUNC
96 #include <stdio.h>
97 #include <ctype.h>
98 #endif
99 
100 #ifndef _KERNEL
101 #include <limits.h>
102 #endif
103 
104 #if defined(BSDI)
105 #undef ULLONG_MAX
106 #endif
107 
108 #if defined( macintosh )
109 #include <Types.h>
110 #elif defined( _WIN32_WCE)
111 /* #include <sys/types.h> What do we need here ?? */
112 #else
113 #include <sys/types.h>
114 #endif
115 
116 #define  MP_NEG    1
117 #define  MP_ZPOS   0
118 
119 #define  MP_OKAY          0 /* no error, all is well */
120 #define  MP_YES           0 /* yes (boolean result)  */
121 #define  MP_NO           -1 /* no (boolean result)   */
122 #define  MP_MEM          -2 /* out of memory         */
123 #define  MP_RANGE        -3 /* argument out of range */
124 #define  MP_BADARG       -4 /* invalid parameter     */
125 #define  MP_UNDEF        -5 /* answer is undefined   */
126 #define  MP_LAST_CODE    MP_UNDEF
127 
128 typedef unsigned int      mp_sign;
129 typedef unsigned int      mp_size;
130 typedef int               mp_err;
131 typedef int               mp_flag;
132 
133 #define MP_32BIT_MAX 4294967295U
134 
135 #if !defined(ULONG_MAX)
136 #error "ULONG_MAX not defined"
137 #elif !defined(UINT_MAX)
138 #error "UINT_MAX not defined"
139 #elif !defined(USHRT_MAX)
140 #error "USHRT_MAX not defined"
141 #endif
142 
143 #if defined(ULONG_LONG_MAX)			/* GCC, HPUX */
144 #define MP_ULONG_LONG_MAX ULONG_LONG_MAX
145 #elif defined(ULLONG_MAX)			/* Solaris */
146 #define MP_ULONG_LONG_MAX ULLONG_MAX
147 /* MP_ULONG_LONG_MAX was defined to be ULLONG_MAX */
148 #elif defined(ULONGLONG_MAX)			/* IRIX, AIX */
149 #define MP_ULONG_LONG_MAX ULONGLONG_MAX
150 #endif
151 
152 /* We only use unsigned long for mp_digit iff long is more than 32 bits. */
153 #if !defined(MP_USE_UINT_DIGIT) && ULONG_MAX > MP_32BIT_MAX
154 typedef unsigned long     mp_digit;
155 #define MP_DIGIT_MAX      ULONG_MAX
156 #define MP_DIGIT_FMT      "%016lX"   /* printf() format for 1 digit */
157 #define MP_HALF_DIGIT_MAX UINT_MAX
158 #undef  MP_NO_MP_WORD
159 #define MP_NO_MP_WORD 1
160 #undef  MP_USE_LONG_DIGIT
161 #define MP_USE_LONG_DIGIT 1
162 #undef  MP_USE_LONG_LONG_DIGIT
163 
164 #elif !defined(MP_USE_UINT_DIGIT) && defined(MP_ULONG_LONG_MAX)
165 typedef unsigned long long mp_digit;
166 #define MP_DIGIT_MAX       MP_ULONG_LONG_MAX
167 #define MP_DIGIT_FMT      "%016llX"  /* printf() format for 1 digit */
168 #define MP_HALF_DIGIT_MAX  UINT_MAX
169 #undef  MP_NO_MP_WORD
170 #define MP_NO_MP_WORD 1
171 #undef  MP_USE_LONG_LONG_DIGIT
172 #define MP_USE_LONG_LONG_DIGIT 1
173 #undef  MP_USE_LONG_DIGIT
174 
175 #else
176 typedef unsigned int      mp_digit;
177 #define MP_DIGIT_MAX      UINT_MAX
178 #define MP_DIGIT_FMT      "%08X"     /* printf() format for 1 digit */
179 #define MP_HALF_DIGIT_MAX USHRT_MAX
180 #undef  MP_USE_UINT_DIGIT
181 #define MP_USE_UINT_DIGIT 1
182 #undef  MP_USE_LONG_LONG_DIGIT
183 #undef  MP_USE_LONG_DIGIT
184 #endif
185 
186 #if !defined(MP_NO_MP_WORD)
187 #if  defined(MP_USE_UINT_DIGIT) && \
188     (defined(MP_ULONG_LONG_MAX) || (ULONG_MAX > UINT_MAX))
189 
190 #if (ULONG_MAX > UINT_MAX)
191 typedef unsigned long     mp_word;
192 typedef          long     mp_sword;
193 #define MP_WORD_MAX       ULONG_MAX
194 
195 #else
196 typedef unsigned long long mp_word;
197 typedef          long long mp_sword;
198 #define MP_WORD_MAX       MP_ULONG_LONG_MAX
199 #endif
200 
201 #else
202 #define MP_NO_MP_WORD 1
203 #endif
204 #endif /* !defined(MP_NO_MP_WORD) */
205 
206 #if !defined(MP_WORD_MAX) && defined(MP_DEFINE_SMALL_WORD)
207 typedef unsigned int      mp_word;
208 typedef          int      mp_sword;
209 #define MP_WORD_MAX       UINT_MAX
210 #endif
211 
212 #ifndef CHAR_BIT
213 #define	CHAR_BIT 8
214 #endif
215 
216 #define MP_DIGIT_BIT      (CHAR_BIT*sizeof(mp_digit))
217 #define MP_WORD_BIT       (CHAR_BIT*sizeof(mp_word))
218 #define MP_RADIX          (1+(mp_word)MP_DIGIT_MAX)
219 
220 #define MP_HALF_DIGIT_BIT (MP_DIGIT_BIT/2)
221 #define MP_HALF_RADIX     (1+(mp_digit)MP_HALF_DIGIT_MAX)
222 /* MP_HALF_RADIX really ought to be called MP_SQRT_RADIX, but it's named
223 ** MP_HALF_RADIX because it's the radix for MP_HALF_DIGITs, and it's
224 ** consistent with the other _HALF_ names.
225 */
226 
227 
228 /* Macros for accessing the mp_int internals           */
229 #define  MP_FLAG(MP)     ((MP)->flag)
230 #define  MP_SIGN(MP)     ((MP)->sign)
231 #define  MP_USED(MP)     ((MP)->used)
232 #define  MP_ALLOC(MP)    ((MP)->alloc)
233 #define  MP_DIGITS(MP)   ((MP)->dp)
234 #define  MP_DIGIT(MP,N)  (MP)->dp[(N)]
235 
236 /* This defines the maximum I/O base (minimum is 2)   */
237 #define MP_MAX_RADIX         64
238 
239 typedef struct {
240   mp_flag       flag;    /* KM_SLEEP/KM_NOSLEEP        */
241   mp_sign       sign;    /* sign of this quantity      */
242   mp_size       alloc;   /* how many digits allocated  */
243   mp_size       used;    /* how many digits used       */
244   mp_digit     *dp;      /* the digits themselves      */
245 } mp_int;
246 
247 /* Default precision       */
248 mp_size mp_get_prec(void);
249 void    mp_set_prec(mp_size prec);
250 
251 /* Memory management       */
252 mp_err mp_init(mp_int *mp, int kmflag);
253 mp_err mp_init_size(mp_int *mp, mp_size prec, int kmflag);
254 mp_err mp_init_copy(mp_int *mp, const mp_int *from);
255 mp_err mp_copy(const mp_int *from, mp_int *to);
256 void   mp_exch(mp_int *mp1, mp_int *mp2);
257 void   mp_clear(mp_int *mp);
258 void   mp_zero(mp_int *mp);
259 void   mp_set(mp_int *mp, mp_digit d);
260 mp_err mp_set_int(mp_int *mp, long z);
261 #define mp_set_long(mp,z) mp_set_int(mp,z)
262 mp_err mp_set_ulong(mp_int *mp, unsigned long z);
263 
264 /* Single digit arithmetic */
265 mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b);
266 mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b);
267 mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b);
268 mp_err mp_mul_2(const mp_int *a, mp_int *c);
269 mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r);
270 mp_err mp_div_2(const mp_int *a, mp_int *c);
271 mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c);
272 
273 /* Sign manipulations      */
274 mp_err mp_abs(const mp_int *a, mp_int *b);
275 mp_err mp_neg(const mp_int *a, mp_int *b);
276 
277 /* Full arithmetic         */
278 mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c);
279 mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
280 mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c);
281 #if MP_SQUARE
282 mp_err mp_sqr(const mp_int *a, mp_int *b);
283 #else
284 #define mp_sqr(a, b) mp_mul(a, a, b)
285 #endif
286 mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r);
287 mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r);
288 mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c);
289 mp_err mp_2expt(mp_int *a, mp_digit k);
290 mp_err mp_sqrt(const mp_int *a, mp_int *b);
291 
292 /* Modular arithmetic      */
293 #if MP_MODARITH
294 mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c);
295 mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c);
296 mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
297 mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
298 mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
299 #if MP_SQUARE
300 mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c);
301 #else
302 #define mp_sqrmod(a, m, c) mp_mulmod(a, a, m, c)
303 #endif
304 mp_err mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
305 mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c);
306 #endif /* MP_MODARITH */
307 
308 /* Comparisons             */
309 int    mp_cmp_z(const mp_int *a);
310 int    mp_cmp_d(const mp_int *a, mp_digit d);
311 int    mp_cmp(const mp_int *a, const mp_int *b);
312 int    mp_cmp_mag(mp_int *a, mp_int *b);
313 int    mp_cmp_int(const mp_int *a, long z, int kmflag);
314 int    mp_isodd(const mp_int *a);
315 int    mp_iseven(const mp_int *a);
316 
317 /* Number theoretic        */
318 #if MP_NUMTH
319 mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c);
320 mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c);
321 mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y);
322 mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c);
323 mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c);
324 #endif /* end MP_NUMTH */
325 
326 /* Input and output        */
327 #if MP_IOFUNC
328 void   mp_print(mp_int *mp, FILE *ofp);
329 #endif /* end MP_IOFUNC */
330 
331 /* Base conversion         */
332 mp_err mp_read_raw(mp_int *mp, char *str, int len);
333 int    mp_raw_size(mp_int *mp);
334 mp_err mp_toraw(mp_int *mp, char *str);
335 mp_err mp_read_radix(mp_int *mp, const char *str, int radix);
336 mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix);
337 int    mp_radix_size(mp_int *mp, int radix);
338 mp_err mp_toradix(mp_int *mp, char *str, int radix);
339 int    mp_tovalue(char ch, int r);
340 
341 #define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
342 #define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
343 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
344 #define mp_tohex(M, S)     mp_toradix((M), (S), 16)
345 
346 /* Error strings           */
347 const  char  *mp_strerror(mp_err ec);
348 
349 /* Octet string conversion functions */
350 mp_err mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len);
351 int    mp_unsigned_octet_size(const mp_int *mp);
352 mp_err mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen);
353 mp_err mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen);
354 mp_err mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size len);
355 
356 /* Miscellaneous */
357 mp_size mp_trailing_zeros(const mp_int *mp);
358 
359 #define MP_CHECKOK(x)  if (MP_OKAY > (res = (x))) goto CLEANUP
360 #define MP_CHECKERR(x) if (MP_OKAY > (res = (x))) goto CLEANUP
361 
362 #if defined(MP_API_COMPATIBLE)
363 #define NEG             MP_NEG
364 #define ZPOS            MP_ZPOS
365 #define DIGIT_MAX       MP_DIGIT_MAX
366 #define DIGIT_BIT       MP_DIGIT_BIT
367 #define DIGIT_FMT       MP_DIGIT_FMT
368 #define RADIX           MP_RADIX
369 #define MAX_RADIX       MP_MAX_RADIX
370 #define FLAG(MP)        MP_FLAG(MP)
371 #define SIGN(MP)        MP_SIGN(MP)
372 #define USED(MP)        MP_USED(MP)
373 #define ALLOC(MP)       MP_ALLOC(MP)
374 #define DIGITS(MP)      MP_DIGITS(MP)
375 #define DIGIT(MP,N)     MP_DIGIT(MP,N)
376 
377 #if MP_ARGCHK == 1
378 #define  ARGCHK(X,Y)  {if(!(X)){return (Y);}}
379 #elif MP_ARGCHK == 2
380 #ifdef _KERNEL
381 #define  ARGCHK(X,Y)  ASSERT(X)
382 #else
383 #include <assert.h>
384 #define  ARGCHK(X,Y)  assert(X)
385 #endif
386 #else
387 #define  ARGCHK(X,Y)  /*  */
388 #endif
389 #endif /* defined MP_API_COMPATIBLE */
390 
391 #endif /* _MPI_H */
392 /* END CSTYLED */
393