Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/ec/impl/mpi-priv.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 library38*39* NOTE WELL: the content of this header file is NOT part of the "public"40* API for the MPI library, and may change at any time.41* Application programs that use libmpi should NOT include this header file.42*/4344#ifndef _MPI_PRIV_H45#define _MPI_PRIV_H4647/* $Id: mpi-priv.h,v 1.20 2005/11/22 07:16:43 relyea%netscape.com Exp $ */4849#include "mpi.h"50#ifndef _KERNEL51#include <stdlib.h>52#include <string.h>53#include <ctype.h>54#endif /* _KERNEL */5556#if MP_DEBUG57#include <stdio.h>5859#define DIAG(T,V) {fprintf(stderr,T);mp_print(V,stderr);fputc('\n',stderr);}60#else61#define DIAG(T,V)62#endif6364/* If we aren't using a wired-in logarithm table, we need to include65the math library to get the log() function66*/6768/* {{{ s_logv_2[] - log table for 2 in various bases */6970#if MP_LOGTAB71/*72A table of the logs of 2 for various bases (the 0 and 1 entries of73this table are meaningless and should not be referenced).7475This table is used to compute output lengths for the mp_toradix()76function. Since a number n in radix r takes up about log_r(n)77digits, we estimate the output size by taking the least integer78greater than log_r(n), where:7980log_r(n) = log_2(n) * log_r(2)8182This table, therefore, is a table of log_r(2) for 2 <= r <= 36,83which are the output bases supported.84*/8586extern const float s_logv_2[];87#define LOG_V_2(R) s_logv_2[(R)]8889#else9091/*92If MP_LOGTAB is not defined, use the math library to compute the93logarithms on the fly. Otherwise, use the table.94Pick which works best for your system.95*/9697#include <math.h>98#define LOG_V_2(R) (log(2.0)/log(R))99100#endif /* if MP_LOGTAB */101102/* }}} */103104/* {{{ Digit arithmetic macros */105106/*107When adding and multiplying digits, the results can be larger than108can be contained in an mp_digit. Thus, an mp_word is used. These109macros mask off the upper and lower digits of the mp_word (the110mp_word may be more than 2 mp_digits wide, but we only concern111ourselves with the low-order 2 mp_digits)112*/113114#define CARRYOUT(W) (mp_digit)((W)>>DIGIT_BIT)115#define ACCUM(W) (mp_digit)(W)116117#define MP_MIN(a,b) (((a) < (b)) ? (a) : (b))118#define MP_MAX(a,b) (((a) > (b)) ? (a) : (b))119#define MP_HOWMANY(a,b) (((a) + (b) - 1)/(b))120#define MP_ROUNDUP(a,b) (MP_HOWMANY(a,b) * (b))121122/* }}} */123124/* {{{ Comparison constants */125126#define MP_LT -1127#define MP_EQ 0128#define MP_GT 1129130/* }}} */131132/* {{{ private function declarations */133134/*135If MP_MACRO is false, these will be defined as actual functions;136otherwise, suitable macro definitions will be used. This works137around the fact that ANSI C89 doesn't support an 'inline' keyword138(although I hear C9x will ... about bloody time). At present, the139macro definitions are identical to the function bodies, but they'll140expand in place, instead of generating a function call.141142I chose these particular functions to be made into macros because143some profiling showed they are called a lot on a typical workload,144and yet they are primarily housekeeping.145*/146#if MP_MACRO == 0147void s_mp_setz(mp_digit *dp, mp_size count); /* zero digits */148void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count); /* copy */149void *s_mp_alloc(size_t nb, size_t ni, int flag); /* general allocator */150void s_mp_free(void *ptr, mp_size); /* general free function */151extern unsigned long mp_allocs;152extern unsigned long mp_frees;153extern unsigned long mp_copies;154#else155156/* Even if these are defined as macros, we need to respect the settings157of the MP_MEMSET and MP_MEMCPY configuration options...158*/159#if MP_MEMSET == 0160#define s_mp_setz(dp, count) \161{int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=0;}162#else163#define s_mp_setz(dp, count) memset(dp, 0, (count) * sizeof(mp_digit))164#endif /* MP_MEMSET */165166#if MP_MEMCPY == 0167#define s_mp_copy(sp, dp, count) \168{int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=(sp)[ix];}169#else170#define s_mp_copy(sp, dp, count) memcpy(dp, sp, (count) * sizeof(mp_digit))171#endif /* MP_MEMCPY */172173#define s_mp_alloc(nb, ni) calloc(nb, ni)174#define s_mp_free(ptr) {if(ptr) free(ptr);}175#endif /* MP_MACRO */176177mp_err s_mp_grow(mp_int *mp, mp_size min); /* increase allocated size */178mp_err s_mp_pad(mp_int *mp, mp_size min); /* left pad with zeroes */179180#if MP_MACRO == 0181void s_mp_clamp(mp_int *mp); /* clip leading zeroes */182#else183#define s_mp_clamp(mp)\184{ mp_size used = MP_USED(mp); \185while (used > 1 && DIGIT(mp, used - 1) == 0) --used; \186MP_USED(mp) = used; \187}188#endif /* MP_MACRO */189190void s_mp_exch(mp_int *a, mp_int *b); /* swap a and b in place */191192mp_err s_mp_lshd(mp_int *mp, mp_size p); /* left-shift by p digits */193void s_mp_rshd(mp_int *mp, mp_size p); /* right-shift by p digits */194mp_err s_mp_mul_2d(mp_int *mp, mp_digit d); /* multiply by 2^d in place */195void s_mp_div_2d(mp_int *mp, mp_digit d); /* divide by 2^d in place */196void s_mp_mod_2d(mp_int *mp, mp_digit d); /* modulo 2^d in place */197void s_mp_div_2(mp_int *mp); /* divide by 2 in place */198mp_err s_mp_mul_2(mp_int *mp); /* multiply by 2 in place */199mp_err s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd);200/* normalize for division */201mp_err s_mp_add_d(mp_int *mp, mp_digit d); /* unsigned digit addition */202mp_err s_mp_sub_d(mp_int *mp, mp_digit d); /* unsigned digit subtract */203mp_err s_mp_mul_d(mp_int *mp, mp_digit d); /* unsigned digit multiply */204mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r);205/* unsigned digit divide */206mp_err s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu);207/* Barrett reduction */208mp_err s_mp_add(mp_int *a, const mp_int *b); /* magnitude addition */209mp_err s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c);210mp_err s_mp_sub(mp_int *a, const mp_int *b); /* magnitude subtract */211mp_err s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c);212mp_err s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset);213/* a += b * RADIX^offset */214mp_err s_mp_mul(mp_int *a, const mp_int *b); /* magnitude multiply */215#if MP_SQUARE216mp_err s_mp_sqr(mp_int *a); /* magnitude square */217#else218#define s_mp_sqr(a) s_mp_mul(a, a)219#endif220mp_err s_mp_div(mp_int *rem, mp_int *div, mp_int *quot); /* magnitude div */221mp_err s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);222mp_err s_mp_2expt(mp_int *a, mp_digit k); /* a = 2^k */223int s_mp_cmp(const mp_int *a, const mp_int *b); /* magnitude comparison */224int s_mp_cmp_d(const mp_int *a, mp_digit d); /* magnitude digit compare */225int s_mp_ispow2(const mp_int *v); /* is v a power of 2? */226int s_mp_ispow2d(mp_digit d); /* is d a power of 2? */227228int s_mp_tovalue(char ch, int r); /* convert ch to value */229char s_mp_todigit(mp_digit val, int r, int low); /* convert val to digit */230int s_mp_outlen(int bits, int r); /* output length in bytes */231mp_digit s_mp_invmod_radix(mp_digit P); /* returns (P ** -1) mod RADIX */232mp_err s_mp_invmod_odd_m( const mp_int *a, const mp_int *m, mp_int *c);233mp_err s_mp_invmod_2d( const mp_int *a, mp_size k, mp_int *c);234mp_err s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c);235236#ifdef NSS_USE_COMBA237238#define IS_POWER_OF_2(a) ((a) && !((a) & ((a)-1)))239240void s_mp_mul_comba_4(const mp_int *A, const mp_int *B, mp_int *C);241void s_mp_mul_comba_8(const mp_int *A, const mp_int *B, mp_int *C);242void s_mp_mul_comba_16(const mp_int *A, const mp_int *B, mp_int *C);243void s_mp_mul_comba_32(const mp_int *A, const mp_int *B, mp_int *C);244245void s_mp_sqr_comba_4(const mp_int *A, mp_int *B);246void s_mp_sqr_comba_8(const mp_int *A, mp_int *B);247void s_mp_sqr_comba_16(const mp_int *A, mp_int *B);248void s_mp_sqr_comba_32(const mp_int *A, mp_int *B);249250#endif /* end NSS_USE_COMBA */251252/* ------ mpv functions, operate on arrays of digits, not on mp_int's ------ */253#if defined (__OS2__) && defined (__IBMC__)254#define MPI_ASM_DECL __cdecl255#else256#define MPI_ASM_DECL257#endif258259#ifdef MPI_AMD64260261mp_digit MPI_ASM_DECL s_mpv_mul_set_vec64(mp_digit*, mp_digit *, mp_size, mp_digit);262mp_digit MPI_ASM_DECL s_mpv_mul_add_vec64(mp_digit*, const mp_digit*, mp_size, mp_digit);263264/* c = a * b */265#define s_mpv_mul_d(a, a_len, b, c) \266((unsigned long*)c)[a_len] = s_mpv_mul_set_vec64(c, a, a_len, b)267268/* c += a * b */269#define s_mpv_mul_d_add(a, a_len, b, c) \270((unsigned long*)c)[a_len] = s_mpv_mul_add_vec64(c, a, a_len, b)271272#else273274void MPI_ASM_DECL s_mpv_mul_d(const mp_digit *a, mp_size a_len,275mp_digit b, mp_digit *c);276void MPI_ASM_DECL s_mpv_mul_d_add(const mp_digit *a, mp_size a_len,277mp_digit b, mp_digit *c);278279#endif280281void MPI_ASM_DECL s_mpv_mul_d_add_prop(const mp_digit *a,282mp_size a_len, mp_digit b,283mp_digit *c);284void MPI_ASM_DECL s_mpv_sqr_add_prop(const mp_digit *a,285mp_size a_len,286mp_digit *sqrs);287288mp_err MPI_ASM_DECL s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo,289mp_digit divisor, mp_digit *quot, mp_digit *rem);290291/* c += a * b * (MP_RADIX ** offset); */292#define s_mp_mul_d_add_offset(a, b, c, off) \293(s_mpv_mul_d_add_prop(MP_DIGITS(a), MP_USED(a), b, MP_DIGITS(c) + off), MP_OKAY)294295typedef struct {296mp_int N; /* modulus N */297mp_digit n0prime; /* n0' = - (n0 ** -1) mod MP_RADIX */298mp_size b; /* R == 2 ** b, also b = # significant bits in N */299} mp_mont_modulus;300301mp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c,302mp_mont_modulus *mmm);303mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm);304305/*306* s_mpi_getProcessorLineSize() returns the size in bytes of the cache line307* if a cache exists, or zero if there is no cache. If more than one308* cache line exists, it should return the smallest line size (which is309* usually the L1 cache).310*311* mp_modexp uses this information to make sure that private key information312* isn't being leaked through the cache.313*314* see mpcpucache.c for the implementation.315*/316unsigned long s_mpi_getProcessorLineSize();317318/* }}} */319#endif /* _MPI_PRIV_H */320321322