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