raw
mpi-genesis             1 /* mpi-internal.h  -  Internal to the Multi Precision Integers
mpi-genesis 2 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
mpi-genesis 3 * Copyright (C) 1998, 2000 Free Software Foundation, Inc.
mpi-genesis 4 *
mpi-genesis 5 * This file is part of GnuPG.
mpi-genesis 6 *
mpi-genesis 7 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis 8 * it under the terms of the GNU General Public License as published by
mpi-genesis 9 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 10 * (at your option) any later version.
mpi-genesis 11 *
mpi-genesis 12 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 15 * GNU General Public License for more details.
mpi-genesis 16 *
mpi-genesis 17 * You should have received a copy of the GNU General Public License
mpi-genesis 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis 19 *
mpi-genesis 20 * Note: This code is heavily based on the GNU MP Library.
mpi-genesis 21 * Actually it's the same code with only minor changes in the
mpi-genesis 22 * way the data is stored; this is to support the abstraction
mpi-genesis 23 * of an optional secure memory allocation which may be used
mpi-genesis 24 * to avoid revealing of sensitive data due to paging etc.
mpi-genesis 25 * The GNU MP Library itself is published under the LGPL;
mpi-genesis 26 * however I decided to publish this code under the plain GPL.
mpi-genesis 27 */
mpi-genesis 28
mpi-genesis 29 #ifndef G10_MPI_INTERNAL_H
mpi-genesis 30 #define G10_MPI_INTERNAL_H
mpi-genesis 31
mpi-genesis 32 #include "mpi.h"
mpi-genesis 33 #include "mpi-asm-defs.h"
mpi-genesis 34
mpi-genesis 35 #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
mpi-genesis 36 typedef unsigned int mpi_limb_t;
mpi-genesis 37 typedef signed int mpi_limb_signed_t;
mpi-genesis 38 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
mpi-genesis 39 typedef unsigned long int mpi_limb_t;
mpi-genesis 40 typedef signed long int mpi_limb_signed_t;
mpi-genesis 41 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
mpi-genesis 42 typedef unsigned long long int mpi_limb_t;
mpi-genesis 43 typedef signed long long int mpi_limb_signed_t;
mpi-genesis 44 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
mpi-genesis 45 typedef unsigned short int mpi_limb_t;
mpi-genesis 46 typedef signed short int mpi_limb_signed_t;
mpi-genesis 47 #else
mpi-genesis 48 #error BYTES_PER_MPI_LIMB does not match any C type
mpi-genesis 49 #endif
mpi-genesis 50 #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
mpi-genesis 51
mpi-genesis 52
mpi-genesis 53 struct gcry_mpi {
mpi-genesis 54 int alloced; /* array size (# of allocated limbs) */
mpi-genesis 55 int nlimbs; /* number of valid limbs */
mpi-genesis 56 unsigned int nbits; /* the real number of valid bits (info only) */
mpi-genesis 57 int sign; /* indicates a negative number */
mpi-genesis 58 unsigned flags; /* bit 0: array must be allocated in secure memory space */
mpi-genesis 59 /* bit 1: not used */
mpi-genesis 60 /* bit 2: the limb is a pointer to some xmalloced data */
mpi-genesis 61 mpi_limb_t *d; /* array with the limbs */
mpi-genesis 62 };
mpi-genesis 63
mpi-genesis 64
mpi-genesis 65
mpi-genesis 66 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
mpi-genesis 67 * value which is good on most machines. */
mpi-genesis 68
mpi-genesis 69 /* tested 4, 16, 32 and 64, where 16 gave the best performance when
mpi-genesis 70 * checking a 768 and a 1024 bit ElGamal signature.
mpi-genesis 71 * (wk 22.12.97) */
mpi-genesis 72 #ifndef KARATSUBA_THRESHOLD
mpi-genesis 73 #define KARATSUBA_THRESHOLD 16
mpi-genesis 74 #endif
mpi-genesis 75
mpi-genesis 76 /* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
mpi-genesis 77 #if KARATSUBA_THRESHOLD < 2
mpi-genesis 78 #undef KARATSUBA_THRESHOLD
mpi-genesis 79 #define KARATSUBA_THRESHOLD 2
mpi-genesis 80 #endif
mpi-genesis 81
mpi-genesis 82
mpi-genesis 83 typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
mpi-genesis 84 typedef int mpi_size_t; /* (must be a signed type) */
mpi-genesis 85
mpi-genesis 86 #define ABS(x) (x >= 0 ? x : -x)
mpi-genesis 87 #define MIN(l,o) ((l) < (o) ? (l) : (o))
mpi-genesis 88 #define MAX(h,i) ((h) > (i) ? (h) : (i))
mpi-genesis 89 #define RESIZE_IF_NEEDED(a,b) \
mpi-genesis 90 do { \
mpi-genesis 91 if( (a)->alloced < (b) ) \
mpi-genesis 92 mpi_resize((a), (b)); \
mpi-genesis 93 } while(0)
mpi-genesis 94
mpi-genesis 95 /* Copy N limbs from S to D. */
mpi-genesis 96 #define MPN_COPY( d, s, n) \
mpi-genesis 97 do { \
mpi-genesis 98 mpi_size_t _i; \
mpi-genesis 99 for( _i = 0; _i < (n); _i++ ) \
mpi-genesis 100 (d)[_i] = (s)[_i]; \
mpi-genesis 101 } while(0)
mpi-genesis 102
mpi-genesis 103 #define MPN_COPY_INCR( d, s, n) \
mpi-genesis 104 do { \
mpi-genesis 105 mpi_size_t _i; \
mpi-genesis 106 for( _i = 0; _i < (n); _i++ ) \
mpi-genesis 107 (d)[_i] = (d)[_i]; \
mpi-genesis 108 } while (0)
mpi-genesis 109
mpi-genesis 110 #define MPN_COPY_DECR( d, s, n ) \
mpi-genesis 111 do { \
mpi-genesis 112 mpi_size_t _i; \
mpi-genesis 113 for( _i = (n)-1; _i >= 0; _i--) \
mpi-genesis 114 (d)[_i] = (s)[_i]; \
mpi-genesis 115 } while(0)
mpi-genesis 116
mpi-genesis 117 /* Zero N limbs at D */
mpi-genesis 118 #define MPN_ZERO(d, n) \
mpi-genesis 119 do { \
mpi-genesis 120 int _i; \
mpi-genesis 121 for( _i = 0; _i < (n); _i++ ) \
mpi-genesis 122 (d)[_i] = 0; \
mpi-genesis 123 } while (0)
mpi-genesis 124
mpi-genesis 125 #define MPN_NORMALIZE(d, n) \
mpi-genesis 126 do { \
mpi-genesis 127 while( (n) > 0 ) { \
mpi-genesis 128 if( (d)[(n)-1] ) \
mpi-genesis 129 break; \
mpi-genesis 130 (n)--; \
mpi-genesis 131 } \
mpi-genesis 132 } while(0)
mpi-genesis 133
mpi-genesis 134 #define MPN_NORMALIZE_NOT_ZERO(d, n) \
mpi-genesis 135 do { \
mpi-genesis 136 for(;;) { \
mpi-genesis 137 if( (d)[(n)-1] ) \
mpi-genesis 138 break; \
mpi-genesis 139 (n)--; \
mpi-genesis 140 } \
mpi-genesis 141 } while(0)
mpi-genesis 142
mpi-genesis 143 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
mpi-genesis 144 do { \
mpi-genesis 145 if( (size) < KARATSUBA_THRESHOLD ) \
mpi-genesis 146 mul_n_basecase (prodp, up, vp, size); \
mpi-genesis 147 else \
mpi-genesis 148 mul_n (prodp, up, vp, size, tspace); \
mpi-genesis 149 } while (0);
mpi-genesis 150
mpi-genesis 151
mpi-genesis 152 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
mpi-genesis 153 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
mpi-genesis 154 * If this would yield overflow, DI should be the largest possible number
mpi-genesis 155 * (i.e., only ones). For correct operation, the most significant bit of D
mpi-genesis 156 * has to be set. Put the quotient in Q and the remainder in R.
mpi-genesis 157 */
mpi-genesis 158 #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
mpi-genesis 159 do { \
mpi-genesis 160 mpi_limb_t _q, _ql, _r; \
mpi-genesis 161 mpi_limb_t _xh, _xl; \
mpi-genesis 162 umul_ppmm (_q, _ql, (nh), (di)); \
mpi-genesis 163 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
mpi-genesis 164 umul_ppmm (_xh, _xl, _q, (d)); \
mpi-genesis 165 sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
mpi-genesis 166 if( _xh ) { \
mpi-genesis 167 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
mpi-genesis 168 _q++; \
mpi-genesis 169 if( _xh) { \
mpi-genesis 170 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
mpi-genesis 171 _q++; \
mpi-genesis 172 } \
mpi-genesis 173 } \
mpi-genesis 174 if( _r >= (d) ) { \
mpi-genesis 175 _r -= (d); \
mpi-genesis 176 _q++; \
mpi-genesis 177 } \
mpi-genesis 178 (r) = _r; \
mpi-genesis 179 (q) = _q; \
mpi-genesis 180 } while (0)
mpi-genesis 181
mpi-genesis 182
mpi-genesis 183 /*-- mpiutil.c --*/
mpi-genesis 184 #ifdef M_DEBUG
mpi-genesis 185 #define mpi_alloc_limb_space(n,f) mpi_debug_alloc_limb_space((n),(f), M_DBGINFO( __LINE__ ) )
mpi-genesis 186 #define mpi_free_limb_space(n) mpi_debug_free_limb_space((n), M_DBGINFO( __LINE__ ) )
mpi-genesis 187 mpi_ptr_t mpi_debug_alloc_limb_space( unsigned nlimbs, int sec, const char *info );
mpi-genesis 188 void mpi_debug_free_limb_space( mpi_ptr_t a, const char *info );
mpi-genesis 189 #else
mpi-genesis 190 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs, int sec );
mpi-genesis 191 void mpi_free_limb_space( mpi_ptr_t a );
mpi-genesis 192 #endif
mpi-genesis 193 void mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs );
mpi-genesis 194
mpi-genesis 195 /*-- mpi-bit.c --*/
mpi-genesis 196 void mpi_rshift_limbs( MPI a, unsigned int count );
mpi-genesis 197 void mpi_lshift_limbs( MPI a, unsigned int count );
mpi-genesis 198
mpi-genesis 199
mpi-genesis 200 /*-- mpihelp-add.c --*/
mpi-genesis 201 mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 202 mpi_size_t s1_size, mpi_limb_t s2_limb );
mpi-genesis 203 mpi_limb_t mpihelp_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 204 mpi_ptr_t s2_ptr, mpi_size_t size);
mpi-genesis 205 mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
mpi-genesis 206 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
mpi-genesis 207
mpi-genesis 208 /*-- mpihelp-sub.c --*/
mpi-genesis 209 mpi_limb_t mpihelp_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 210 mpi_size_t s1_size, mpi_limb_t s2_limb );
mpi-genesis 211 mpi_limb_t mpihelp_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 212 mpi_ptr_t s2_ptr, mpi_size_t size);
mpi-genesis 213 mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
mpi-genesis 214 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
mpi-genesis 215
mpi-genesis 216 /*-- mpihelp-cmp.c --*/
mpi-genesis 217 int mpihelp_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
mpi-genesis 218
mpi-genesis 219 /*-- mpihelp-mul.c --*/
mpi-genesis 220
mpi-genesis 221 struct karatsuba_ctx {
mpi-genesis 222 struct karatsuba_ctx *next;
mpi-genesis 223 mpi_ptr_t tspace;
mpi-genesis 224 mpi_size_t tspace_size;
mpi-genesis 225 mpi_ptr_t tp;
mpi-genesis 226 mpi_size_t tp_size;
mpi-genesis 227 };
mpi-genesis 228
mpi-genesis 229 void mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
mpi-genesis 230
mpi-genesis 231 mpi_limb_t mpihelp_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 232 mpi_size_t s1_size, mpi_limb_t s2_limb);
mpi-genesis 233 mpi_limb_t mpihelp_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 234 mpi_size_t s1_size, mpi_limb_t s2_limb);
mpi-genesis 235 void mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
mpi-genesis 236 mpi_size_t size);
mpi-genesis 237 mpi_limb_t mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
mpi-genesis 238 mpi_ptr_t vp, mpi_size_t vsize);
mpi-genesis 239 void mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
mpi-genesis 240 void mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
mpi-genesis 241 mpi_ptr_t tspace);
mpi-genesis 242
mpi-genesis 243 void mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
mpi-genesis 244 mpi_ptr_t up, mpi_size_t usize,
mpi-genesis 245 mpi_ptr_t vp, mpi_size_t vsize,
mpi-genesis 246 struct karatsuba_ctx *ctx );
mpi-genesis 247
mpi-genesis 248
mpi-genesis 249 /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
mpi-genesis 250 mpi_limb_t mpihelp_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi-genesis 251 mpi_size_t s1_size, mpi_limb_t s2_limb);
mpi-genesis 252
mpi-genesis 253 /*-- mpihelp-div.c --*/
mpi-genesis 254 mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
mpi-genesis 255 mpi_limb_t divisor_limb);
mpi-genesis 256 mpi_limb_t mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
mpi-genesis 257 mpi_ptr_t np, mpi_size_t nsize,
mpi-genesis 258 mpi_ptr_t dp, mpi_size_t dsize);
mpi-genesis 259 mpi_limb_t mpihelp_divmod_1( mpi_ptr_t quot_ptr,
mpi-genesis 260 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
mpi-genesis 261 mpi_limb_t divisor_limb);
mpi-genesis 262
mpi-genesis 263 /*-- mpihelp-shift.c --*/
mpi-genesis 264 mpi_limb_t mpihelp_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
mpi-genesis 265 unsigned cnt);
mpi-genesis 266 mpi_limb_t mpihelp_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
mpi-genesis 267 unsigned cnt);
mpi-genesis 268
mpi-genesis 269
mpi-genesis 270 /* Define stuff for longlong.h. */
mpi-genesis 271 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
mpi-genesis 272 typedef mpi_limb_t UWtype;
mpi-genesis 273 typedef unsigned int UHWtype;
mpi-genesis 274 #if defined (__GNUC__)
mpi-genesis 275 typedef unsigned int UQItype __attribute__ ((mode (QI)));
mpi-genesis 276 typedef int SItype __attribute__ ((mode (SI)));
mpi-genesis 277 typedef unsigned int USItype __attribute__ ((mode (SI)));
mpi-genesis 278 typedef int DItype __attribute__ ((mode (DI)));
mpi-genesis 279 typedef unsigned int UDItype __attribute__ ((mode (DI)));
mpi-genesis 280 #else
mpi-genesis 281 typedef unsigned char UQItype;
mpi-genesis 282 typedef long SItype;
mpi-genesis 283 typedef unsigned long USItype;
mpi-genesis 284 #endif
mpi-genesis 285
mpi-genesis 286 #ifdef __GNUC__
mpi-genesis 287 #include "mpi-inline.h"
mpi-genesis 288 #endif
mpi-genesis 289
mpi-genesis 290 #endif /*G10_MPI_INTERNAL_H*/