-
+ 0EC90EEED7BB58E2D737FDDAF5CD8101CFB114C9D0824457384E5C199B4E1A75F1762BCD465CD4B675FE19DA36197EE9BFF0D3E24ED6B9E4C26A1A05FFC7386E
smg_comms/mpi/include/mpi-internal.h
(0 . 0)(1 . 287)
2084 /* mpi-internal.h - Internal to the Multi Precision Integers
2085 * Modified by No Such Labs. (C) 2015. See README.
2086 * Modified by S.MG, 2017: fixing broken MPN_COPY_INCR macro. See EuCrypt patches.
2087 *
2088 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
2089 * SHA256(gnupg-1.4.10.tar.gz):
2090 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
2091 * (C) 1994-2005 Free Software Foundation, Inc.
2092 *
2093 * This program is free software: you can redistribute it and/or modify
2094 * it under the terms of the GNU General Public License as published by
2095 * the Free Software Foundation, either version 3 of the License, or
2096 * (at your option) any later version.
2097 *
2098 * This program is distributed in the hope that it will be useful,
2099 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2100 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2101 * GNU General Public License for more details.
2102 *
2103 * You should have received a copy of the GNU General Public License
2104 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2105 */
2106
2107 #ifndef G10_MPI_INTERNAL_H
2108 #define G10_MPI_INTERNAL_H
2109
2110 #include "mpi.h"
2111
2112 /* from the old mpi-asm-defs.h */
2113 #define BYTES_PER_MPI_LIMB (SIZEOF_UNSIGNED_LONG)
2114
2115 #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
2116 typedef unsigned int mpi_limb_t;
2117 typedef signed int mpi_limb_signed_t;
2118 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
2119 typedef unsigned long int mpi_limb_t;
2120 typedef signed long int mpi_limb_signed_t;
2121 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
2122 typedef unsigned long long int mpi_limb_t;
2123 typedef signed long long int mpi_limb_signed_t;
2124 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
2125 typedef unsigned short int mpi_limb_t;
2126 typedef signed short int mpi_limb_signed_t;
2127 #else
2128 #error BYTES_PER_MPI_LIMB does not match any C type
2129 #endif
2130 #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
2131
2132
2133 struct gcry_mpi {
2134 int alloced; /* array size (# of allocated limbs) */
2135 int nlimbs; /* number of valid limbs */
2136 unsigned int nbits; /* the real number of valid bits (info only) */
2137 int sign; /* indicates a negative number */
2138 unsigned flags; /* bit 0: array must be allocated in secure memory space */
2139 /* bit 1: not used */
2140 /* bit 2: the limb is a pointer to some xmalloced data */
2141 mpi_limb_t *d; /* array with the limbs */
2142 };
2143
2144
2145
2146 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
2147 * value which is good on most machines. */
2148
2149 /* tested 4, 16, 32 and 64, where 16 gave the best performance when
2150 * checking a 768 and a 1024 bit ElGamal signature.
2151 * (wk 22.12.97) */
2152 #ifndef KARATSUBA_THRESHOLD
2153 #define KARATSUBA_THRESHOLD 16
2154 #endif
2155
2156 /* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
2157 #if KARATSUBA_THRESHOLD < 2
2158 #undef KARATSUBA_THRESHOLD
2159 #define KARATSUBA_THRESHOLD 2
2160 #endif
2161
2162
2163 typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
2164 typedef int mpi_size_t; /* (must be a signed type) */
2165
2166 #define ABS(x) (x >= 0 ? x : -x)
2167 #define MIN(l,o) ((l) < (o) ? (l) : (o))
2168 #define MAX(h,i) ((h) > (i) ? (h) : (i))
2169 #define RESIZE_IF_NEEDED(a,b) \
2170 do { \
2171 if( (a)->alloced < (b) ) \
2172 mpi_resize((a), (b)); \
2173 } while(0)
2174
2175 /* Copy N limbs from S to D. */
2176 #define MPN_COPY( d, s, n) \
2177 do { \
2178 mpi_size_t _i; \
2179 for( _i = 0; _i < (n); _i++ ) \
2180 (d)[_i] = (s)[_i]; \
2181 } while(0)
2182
2183 #define MPN_COPY_INCR( d, s, n) \
2184 do { \
2185 mpi_size_t _i; \
2186 for( _i = 0; _i < (n); _i++ ) \
2187 (d)[_i] = (s)[_i]; \
2188 } while (0)
2189
2190 #define MPN_COPY_DECR( d, s, n ) \
2191 do { \
2192 mpi_size_t _i; \
2193 for( _i = (n)-1; _i >= 0; _i--) \
2194 (d)[_i] = (s)[_i]; \
2195 } while(0)
2196
2197 /* Zero N limbs at D */
2198 #define MPN_ZERO(d, n) \
2199 do { \
2200 int _i; \
2201 for( _i = 0; _i < (n); _i++ ) \
2202 (d)[_i] = 0; \
2203 } while (0)
2204
2205 #define MPN_NORMALIZE(d, n) \
2206 do { \
2207 while( (n) > 0 ) { \
2208 if( (d)[(n)-1] ) \
2209 break; \
2210 (n)--; \
2211 } \
2212 } while(0)
2213
2214 #define MPN_NORMALIZE_NOT_ZERO(d, n) \
2215 do { \
2216 for(;;) { \
2217 if( (d)[(n)-1] ) \
2218 break; \
2219 (n)--; \
2220 } \
2221 } while(0)
2222
2223 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
2224 do { \
2225 if( (size) < KARATSUBA_THRESHOLD ) \
2226 mul_n_basecase (prodp, up, vp, size); \
2227 else \
2228 mul_n (prodp, up, vp, size, tspace); \
2229 } while (0);
2230
2231
2232 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
2233 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
2234 * If this would yield overflow, DI should be the largest possible number
2235 * (i.e., only ones). For correct operation, the most significant bit of D
2236 * has to be set. Put the quotient in Q and the remainder in R.
2237 */
2238 #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
2239 do { \
2240 mpi_limb_t _q, _ql, _r; \
2241 mpi_limb_t _xh, _xl; \
2242 umul_ppmm (_q, _ql, (nh), (di)); \
2243 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
2244 umul_ppmm (_xh, _xl, _q, (d)); \
2245 sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
2246 if( _xh ) { \
2247 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
2248 _q++; \
2249 if( _xh) { \
2250 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
2251 _q++; \
2252 } \
2253 } \
2254 if( _r >= (d) ) { \
2255 _r -= (d); \
2256 _q++; \
2257 } \
2258 (r) = _r; \
2259 (q) = _q; \
2260 } while (0)
2261
2262
2263 /*-- mpiutil.c --*/
2264 #ifdef M_DEBUG
2265 #define mpi_alloc_limb_space(n,f) mpi_debug_alloc_limb_space((n),(f), M_DBGINFO( __LINE__ ) )
2266 #define mpi_free_limb_space(n) mpi_debug_free_limb_space((n), M_DBGINFO( __LINE__ ) )
2267 mpi_ptr_t mpi_debug_alloc_limb_space( unsigned nlimbs, int sec, const char *info );
2268 void mpi_debug_free_limb_space( mpi_ptr_t a, const char *info );
2269 #else
2270 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs, int sec );
2271 void mpi_free_limb_space( mpi_ptr_t a );
2272 #endif
2273 void mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs );
2274
2275 /*-- mpi-bit.c --*/
2276 void mpi_rshift_limbs( MPI a, unsigned int count );
2277 void mpi_lshift_limbs( MPI a, unsigned int count );
2278
2279
2280 /*-- mpihelp-add.c --*/
2281 mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2282 mpi_size_t s1_size, mpi_limb_t s2_limb );
2283 mpi_limb_t mpihelp_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2284 mpi_ptr_t s2_ptr, mpi_size_t size);
2285 mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
2286 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
2287
2288 /*-- mpihelp-sub.c --*/
2289 mpi_limb_t mpihelp_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2290 mpi_size_t s1_size, mpi_limb_t s2_limb );
2291 mpi_limb_t mpihelp_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2292 mpi_ptr_t s2_ptr, mpi_size_t size);
2293 mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
2294 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
2295
2296 /*-- mpihelp-cmp.c --*/
2297 int mpihelp_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
2298
2299 /*-- mpihelp-mul.c --*/
2300
2301 struct karatsuba_ctx {
2302 struct karatsuba_ctx *next;
2303 mpi_ptr_t tspace;
2304 mpi_size_t tspace_size;
2305 mpi_ptr_t tp;
2306 mpi_size_t tp_size;
2307 };
2308
2309 void mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
2310
2311 mpi_limb_t mpihelp_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2312 mpi_size_t s1_size, mpi_limb_t s2_limb);
2313 mpi_limb_t mpihelp_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2314 mpi_size_t s1_size, mpi_limb_t s2_limb);
2315 void mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
2316 mpi_size_t size);
2317 mpi_limb_t mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
2318 mpi_ptr_t vp, mpi_size_t vsize);
2319 void mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
2320 void mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
2321 mpi_ptr_t tspace);
2322
2323 void mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
2324 mpi_ptr_t up, mpi_size_t usize,
2325 mpi_ptr_t vp, mpi_size_t vsize,
2326 struct karatsuba_ctx *ctx );
2327
2328
2329 /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
2330 mpi_limb_t mpihelp_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2331 mpi_size_t s1_size, mpi_limb_t s2_limb);
2332
2333 /*-- mpihelp-div.c --*/
2334 mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
2335 mpi_limb_t divisor_limb);
2336 mpi_limb_t mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
2337 mpi_ptr_t np, mpi_size_t nsize,
2338 mpi_ptr_t dp, mpi_size_t dsize);
2339 mpi_limb_t mpihelp_divmod_1( mpi_ptr_t quot_ptr,
2340 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
2341 mpi_limb_t divisor_limb);
2342
2343 /*-- mpihelp-shift.c --*/
2344 mpi_limb_t mpihelp_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
2345 unsigned cnt);
2346 mpi_limb_t mpihelp_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
2347 unsigned cnt);
2348
2349
2350 /* Define stuff for longlong.h. */
2351 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
2352 typedef mpi_limb_t UWtype;
2353 typedef unsigned int UHWtype;
2354 #if defined (__GNUC__)
2355 typedef unsigned int UQItype __attribute__ ((mode (QI)));
2356 typedef int SItype __attribute__ ((mode (SI)));
2357 typedef unsigned int USItype __attribute__ ((mode (SI)));
2358 typedef int DItype __attribute__ ((mode (DI)));
2359 typedef unsigned int UDItype __attribute__ ((mode (DI)));
2360 #else
2361 typedef unsigned char UQItype;
2362 typedef long SItype;
2363 typedef unsigned long USItype;
2364 #endif
2365
2366 #ifdef __GNUC__
2367 #include "mpi-inline.h"
2368 #endif
2369
2370 #endif /*G10_MPI_INTERNAL_H*/