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