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