Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/ec/impl/mpi.h
38918 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* Use is subject to license terms.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public6* License as published by the Free Software Foundation; either7* version 2.1 of the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public License15* along with this library; if not, write to the Free Software Foundation,16* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* *********************************************************************24*25* The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.26*27* The Initial Developer of the Original Code is28* Michael J. Fromberger.29* Portions created by the Initial Developer are Copyright (C) 199830* the Initial Developer. All Rights Reserved.31*32* Contributor(s):33* Netscape Communications Corporation34*35*********************************************************************** */3637/* Arbitrary precision integer arithmetic library */3839#ifndef _MPI_H40#define _MPI_H4142/* $Id: mpi.h,v 1.22 2004/04/27 23:04:36 gerv%gerv.net Exp $ */4344#include "mpi-config.h"4546#ifndef _WIN3247#include <sys/param.h>48#endif /* _WIN32 */4950#ifdef _KERNEL51#include <sys/debug.h>52#include <sys/systm.h>53#define assert ASSERT54#define labs(a) (a >= 0 ? a : -a)55#define UCHAR_MAX 25556#define memset(s, c, n) bzero(s, n)57#define memcpy(a,b,c) bcopy((caddr_t)b, (caddr_t)a, c)58/*59* Generic #define's to cover missing things in the kernel60*/61#ifndef isdigit62#define isdigit(x) ((x) >= '0' && (x) <= '9')63#endif64#ifndef isupper65#define isupper(x) (((unsigned)(x) >= 'A') && ((unsigned)(x) <= 'Z'))66#endif67#ifndef islower68#define islower(x) (((unsigned)(x) >= 'a') && ((unsigned)(x) <= 'z'))69#endif70#ifndef isalpha71#define isalpha(x) (isupper(x) || islower(x))72#endif73#ifndef toupper74#define toupper(x) (islower(x) ? (x) - 'a' + 'A' : (x))75#endif76#ifndef tolower77#define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x))78#endif79#ifndef isspace80#define isspace(x) (((x) == ' ') || ((x) == '\r') || ((x) == '\n') || \81((x) == '\t') || ((x) == '\b'))82#endif83#endif /* _KERNEL */8485#if MP_DEBUG86#undef MP_IOFUNC87#define MP_IOFUNC 188#endif8990#if MP_IOFUNC91#include <stdio.h>92#include <ctype.h>93#endif9495#ifndef _KERNEL96#include <limits.h>97#endif9899#if defined(BSDI)100#undef ULLONG_MAX101#endif102103#if defined( macintosh )104#include <Types.h>105#elif defined( _WIN32_WCE)106/* #include <sys/types.h> What do we need here ?? */107#else108#include <sys/types.h>109#endif110111#define MP_NEG 1112#define MP_ZPOS 0113114#define MP_OKAY 0 /* no error, all is well */115#define MP_YES 0 /* yes (boolean result) */116#define MP_NO -1 /* no (boolean result) */117#define MP_MEM -2 /* out of memory */118#define MP_RANGE -3 /* argument out of range */119#define MP_BADARG -4 /* invalid parameter */120#define MP_UNDEF -5 /* answer is undefined */121#define MP_LAST_CODE MP_UNDEF122123typedef unsigned int mp_sign;124typedef unsigned int mp_size;125typedef int mp_err;126typedef int mp_flag;127128#define MP_32BIT_MAX 4294967295U129130#if !defined(ULONG_MAX)131#error "ULONG_MAX not defined"132#elif !defined(UINT_MAX)133#error "UINT_MAX not defined"134#elif !defined(USHRT_MAX)135#error "USHRT_MAX not defined"136#endif137138#if defined(ULONG_LONG_MAX) /* GCC, HPUX */139#define MP_ULONG_LONG_MAX ULONG_LONG_MAX140#elif defined(ULLONG_MAX) /* Solaris */141#define MP_ULONG_LONG_MAX ULLONG_MAX142/* MP_ULONG_LONG_MAX was defined to be ULLONG_MAX */143#elif defined(ULONGLONG_MAX) /* IRIX, AIX */144#define MP_ULONG_LONG_MAX ULONGLONG_MAX145#endif146147/* We only use unsigned long for mp_digit iff long is more than 32 bits. */148#if !defined(MP_USE_UINT_DIGIT) && ULONG_MAX > MP_32BIT_MAX149typedef unsigned long mp_digit;150#define MP_DIGIT_MAX ULONG_MAX151#define MP_DIGIT_FMT "%016lX" /* printf() format for 1 digit */152#define MP_HALF_DIGIT_MAX UINT_MAX153#undef MP_NO_MP_WORD154#define MP_NO_MP_WORD 1155#undef MP_USE_LONG_DIGIT156#define MP_USE_LONG_DIGIT 1157#undef MP_USE_LONG_LONG_DIGIT158159#elif !defined(MP_USE_UINT_DIGIT) && defined(MP_ULONG_LONG_MAX)160typedef unsigned long long mp_digit;161#define MP_DIGIT_MAX MP_ULONG_LONG_MAX162#define MP_DIGIT_FMT "%016llX" /* printf() format for 1 digit */163#define MP_HALF_DIGIT_MAX UINT_MAX164#undef MP_NO_MP_WORD165#define MP_NO_MP_WORD 1166#undef MP_USE_LONG_LONG_DIGIT167#define MP_USE_LONG_LONG_DIGIT 1168#undef MP_USE_LONG_DIGIT169170#else171typedef unsigned int mp_digit;172#define MP_DIGIT_MAX UINT_MAX173#define MP_DIGIT_FMT "%08X" /* printf() format for 1 digit */174#define MP_HALF_DIGIT_MAX USHRT_MAX175#undef MP_USE_UINT_DIGIT176#define MP_USE_UINT_DIGIT 1177#undef MP_USE_LONG_LONG_DIGIT178#undef MP_USE_LONG_DIGIT179#endif180181#if !defined(MP_NO_MP_WORD)182#if defined(MP_USE_UINT_DIGIT) && \183(defined(MP_ULONG_LONG_MAX) || (ULONG_MAX > UINT_MAX))184185#if (ULONG_MAX > UINT_MAX)186typedef unsigned long mp_word;187typedef long mp_sword;188#define MP_WORD_MAX ULONG_MAX189190#else191typedef unsigned long long mp_word;192typedef long long mp_sword;193#define MP_WORD_MAX MP_ULONG_LONG_MAX194#endif195196#else197#define MP_NO_MP_WORD 1198#endif199#endif /* !defined(MP_NO_MP_WORD) */200201#if !defined(MP_WORD_MAX) && defined(MP_DEFINE_SMALL_WORD)202typedef unsigned int mp_word;203typedef int mp_sword;204#define MP_WORD_MAX UINT_MAX205#endif206207#ifndef CHAR_BIT208#define CHAR_BIT 8209#endif210211#define MP_DIGIT_BIT (CHAR_BIT*sizeof(mp_digit))212#define MP_WORD_BIT (CHAR_BIT*sizeof(mp_word))213#define MP_RADIX (1+(mp_word)MP_DIGIT_MAX)214215#define MP_HALF_DIGIT_BIT (MP_DIGIT_BIT/2)216#define MP_HALF_RADIX (1+(mp_digit)MP_HALF_DIGIT_MAX)217/* MP_HALF_RADIX really ought to be called MP_SQRT_RADIX, but it's named218** MP_HALF_RADIX because it's the radix for MP_HALF_DIGITs, and it's219** consistent with the other _HALF_ names.220*/221222223/* Macros for accessing the mp_int internals */224#define MP_FLAG(MP) ((MP)->flag)225#define MP_SIGN(MP) ((MP)->sign)226#define MP_USED(MP) ((MP)->used)227#define MP_ALLOC(MP) ((MP)->alloc)228#define MP_DIGITS(MP) ((MP)->dp)229#define MP_DIGIT(MP,N) (MP)->dp[(N)]230231/* This defines the maximum I/O base (minimum is 2) */232#define MP_MAX_RADIX 64233234typedef struct {235mp_sign flag; /* KM_SLEEP/KM_NOSLEEP */236mp_sign sign; /* sign of this quantity */237mp_size alloc; /* how many digits allocated */238mp_size used; /* how many digits used */239mp_digit *dp; /* the digits themselves */240} mp_int;241242/* Default precision */243mp_size mp_get_prec(void);244void mp_set_prec(mp_size prec);245246/* Memory management */247mp_err mp_init(mp_int *mp, int kmflag);248mp_err mp_init_size(mp_int *mp, mp_size prec, int kmflag);249mp_err mp_init_copy(mp_int *mp, const mp_int *from);250mp_err mp_copy(const mp_int *from, mp_int *to);251void mp_exch(mp_int *mp1, mp_int *mp2);252void mp_clear(mp_int *mp);253void mp_zero(mp_int *mp);254void mp_set(mp_int *mp, mp_digit d);255mp_err mp_set_int(mp_int *mp, long z);256#define mp_set_long(mp,z) mp_set_int(mp,z)257mp_err mp_set_ulong(mp_int *mp, unsigned long z);258259/* Single digit arithmetic */260mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b);261mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b);262mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b);263mp_err mp_mul_2(const mp_int *a, mp_int *c);264mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r);265mp_err mp_div_2(const mp_int *a, mp_int *c);266mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c);267268/* Sign manipulations */269mp_err mp_abs(const mp_int *a, mp_int *b);270mp_err mp_neg(const mp_int *a, mp_int *b);271272/* Full arithmetic */273mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c);274mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c);275mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c);276#if MP_SQUARE277mp_err mp_sqr(const mp_int *a, mp_int *b);278#else279#define mp_sqr(a, b) mp_mul(a, a, b)280#endif281mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r);282mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r);283mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c);284mp_err mp_2expt(mp_int *a, mp_digit k);285mp_err mp_sqrt(const mp_int *a, mp_int *b);286287/* Modular arithmetic */288#if MP_MODARITH289mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c);290mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c);291mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);292mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);293mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);294#if MP_SQUARE295mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c);296#else297#define mp_sqrmod(a, m, c) mp_mulmod(a, a, m, c)298#endif299mp_err mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);300mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c);301#endif /* MP_MODARITH */302303/* Comparisons */304int mp_cmp_z(const mp_int *a);305int mp_cmp_d(const mp_int *a, mp_digit d);306int mp_cmp(const mp_int *a, const mp_int *b);307int mp_cmp_mag(mp_int *a, mp_int *b);308int mp_cmp_int(const mp_int *a, long z, int kmflag);309int mp_isodd(const mp_int *a);310int mp_iseven(const mp_int *a);311312/* Number theoretic */313#if MP_NUMTH314mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c);315mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c);316mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y);317mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c);318mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c);319#endif /* end MP_NUMTH */320321/* Input and output */322#if MP_IOFUNC323void mp_print(mp_int *mp, FILE *ofp);324#endif /* end MP_IOFUNC */325326/* Base conversion */327mp_err mp_read_raw(mp_int *mp, char *str, int len);328int mp_raw_size(mp_int *mp);329mp_err mp_toraw(mp_int *mp, char *str);330mp_err mp_read_radix(mp_int *mp, const char *str, int radix);331mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix);332int mp_radix_size(mp_int *mp, int radix);333mp_err mp_toradix(mp_int *mp, char *str, int radix);334int mp_tovalue(char ch, int r);335336#define mp_tobinary(M, S) mp_toradix((M), (S), 2)337#define mp_tooctal(M, S) mp_toradix((M), (S), 8)338#define mp_todecimal(M, S) mp_toradix((M), (S), 10)339#define mp_tohex(M, S) mp_toradix((M), (S), 16)340341/* Error strings */342const char *mp_strerror(mp_err ec);343344/* Octet string conversion functions */345mp_err mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len);346int mp_unsigned_octet_size(const mp_int *mp);347mp_err mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen);348mp_err mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen);349mp_err mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size len);350351/* Miscellaneous */352mp_size mp_trailing_zeros(const mp_int *mp);353354#define MP_CHECKOK(x) if (MP_OKAY > (res = (x))) goto CLEANUP355#define MP_CHECKERR(x) if (MP_OKAY > (res = (x))) goto CLEANUP356357#if defined(MP_API_COMPATIBLE)358#define NEG MP_NEG359#define ZPOS MP_ZPOS360#define DIGIT_MAX MP_DIGIT_MAX361#define DIGIT_BIT MP_DIGIT_BIT362#define DIGIT_FMT MP_DIGIT_FMT363#define RADIX MP_RADIX364#define MAX_RADIX MP_MAX_RADIX365#define FLAG(MP) MP_FLAG(MP)366#define SIGN(MP) MP_SIGN(MP)367#define USED(MP) MP_USED(MP)368#define ALLOC(MP) MP_ALLOC(MP)369#define DIGITS(MP) MP_DIGITS(MP)370#define DIGIT(MP,N) MP_DIGIT(MP,N)371372#if MP_ARGCHK == 1373#define ARGCHK(X,Y) {if(!(X)){return (Y);}}374#elif MP_ARGCHK == 2375#ifdef _KERNEL376#define ARGCHK(X,Y) ASSERT(X)377#else378#include <assert.h>379#define ARGCHK(X,Y) assert(X)380#endif381#else382#define ARGCHK(X,Y) /* */383#endif384#endif /* defined MP_API_COMPATIBLE */385386#endif /* _MPI_H */387388389