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