1
/* This file was automatically imported with
2
import_gcry.py. Please don't modify it */
3
/* mpi-internal.h - Internal to the Multi Precision Integers
4
* Copyright (C) 1994, 1996, 1998, 2000, 2002,
5
* 2003 Free Software Foundation, Inc.
7
* This file is part of Libgcrypt.
9
* Libgcrypt is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as
11
* published by the Free Software Foundation; either version 2.1 of
12
* the License, or (at your option) any later version.
14
* Libgcrypt is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23
* Note: This code is heavily based on the GNU MP Library.
24
* Actually it's the same code with only minor changes in the
25
* way the data is stored; this is to support the abstraction
26
* of an optional secure memory allocation which may be used
27
* to avoid revealing of sensitive data due to paging etc.
30
#ifndef G10_MPI_INTERNAL_H
31
#define G10_MPI_INTERNAL_H
33
#include "mpi-asm-defs.h"
35
#ifndef BITS_PER_MPI_LIMB
36
#if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
37
typedef unsigned int mpi_limb_t;
38
typedef signed int mpi_limb_signed_t;
39
#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
40
typedef unsigned long int mpi_limb_t;
41
typedef signed long int mpi_limb_signed_t;
42
#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
43
typedef unsigned long long int mpi_limb_t;
44
typedef signed long long int mpi_limb_signed_t;
45
#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
46
typedef unsigned short int mpi_limb_t;
47
typedef signed short int mpi_limb_signed_t;
49
#error BYTES_PER_MPI_LIMB does not match any C type
51
#define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
52
#endif /*BITS_PER_MPI_LIMB*/
56
/* If KARATSUBA_THRESHOLD is not already defined, define it to a
57
* value which is good on most machines. */
59
/* tested 4, 16, 32 and 64, where 16 gave the best performance when
60
* checking a 768 and a 1024 bit ElGamal signature.
62
#ifndef KARATSUBA_THRESHOLD
63
#define KARATSUBA_THRESHOLD 16
66
/* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
67
#if KARATSUBA_THRESHOLD < 2
68
#undef KARATSUBA_THRESHOLD
69
#define KARATSUBA_THRESHOLD 2
73
typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
74
typedef int mpi_size_t; /* (must be a signed type) */
76
#define ABS(x) (x >= 0 ? x : -x)
77
#define MIN(l,o) ((l) < (o) ? (l) : (o))
78
#define MAX(h,i) ((h) > (i) ? (h) : (i))
79
#define RESIZE_IF_NEEDED(a,b) \
81
if( (a)->alloced < (b) ) \
82
mpi_resize((a), (b)); \
85
/* Copy N limbs from S to D. */
86
#define MPN_COPY( d, s, n) \
89
for( _i = 0; _i < (n); _i++ ) \
93
#define MPN_COPY_INCR( d, s, n) \
96
for( _i = 0; _i < (n); _i++ ) \
100
#define MPN_COPY_DECR( d, s, n ) \
103
for( _i = (n)-1; _i >= 0; _i--) \
107
/* Zero N limbs at D */
108
#define MPN_ZERO(d, n) \
111
for( _i = 0; _i < (n); _i++ ) \
115
#define MPN_NORMALIZE(d, n) \
124
#define MPN_NORMALIZE_NOT_ZERO(d, n) \
133
#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
135
if( (size) < KARATSUBA_THRESHOLD ) \
136
mul_n_basecase (prodp, up, vp, size); \
138
mul_n (prodp, up, vp, size, tspace); \
142
/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
143
* limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
144
* If this would yield overflow, DI should be the largest possible number
145
* (i.e., only ones). For correct operation, the most significant bit of D
146
* has to be set. Put the quotient in Q and the remainder in R.
148
#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
150
mpi_limb_t _q, _ql, _r; \
151
mpi_limb_t _xh, _xl; \
152
umul_ppmm (_q, _ql, (nh), (di)); \
153
_q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
154
umul_ppmm (_xh, _xl, _q, (d)); \
155
sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
157
sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
160
sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
174
#define mpi_alloc_limb_space(n,f) _gcry_mpi_alloc_limb_space((n),(f))
175
mpi_ptr_t _gcry_mpi_alloc_limb_space( unsigned nlimbs, int sec );
176
void _gcry_mpi_free_limb_space( mpi_ptr_t a, unsigned int nlimbs );
177
void _gcry_mpi_assign_limb_space( gcry_mpi_t a, mpi_ptr_t ap, unsigned nlimbs );
180
#define mpi_rshift_limbs(a,n) _gcry_mpi_rshift_limbs ((a), (n))
181
#define mpi_lshift_limbs(a,n) _gcry_mpi_lshift_limbs ((a), (n))
183
void _gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count );
184
void _gcry_mpi_lshift_limbs( gcry_mpi_t a, unsigned int count );
188
mpi_limb_t _gcry_mpih_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
189
mpi_size_t s1_size, mpi_limb_t s2_limb );
190
mpi_limb_t _gcry_mpih_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
191
mpi_ptr_t s2_ptr, mpi_size_t size);
192
mpi_limb_t _gcry_mpih_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
193
mpi_ptr_t s2_ptr, mpi_size_t s2_size);
196
mpi_limb_t _gcry_mpih_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
197
mpi_size_t s1_size, mpi_limb_t s2_limb );
198
mpi_limb_t _gcry_mpih_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
199
mpi_ptr_t s2_ptr, mpi_size_t size);
200
mpi_limb_t _gcry_mpih_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
201
mpi_ptr_t s2_ptr, mpi_size_t s2_size);
204
int _gcry_mpih_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
208
struct karatsuba_ctx {
209
struct karatsuba_ctx *next;
211
unsigned int tspace_nlimbs;
212
mpi_size_t tspace_size;
214
unsigned int tp_nlimbs;
218
void _gcry_mpih_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
220
mpi_limb_t _gcry_mpih_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
221
mpi_size_t s1_size, mpi_limb_t s2_limb);
222
mpi_limb_t _gcry_mpih_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
223
mpi_size_t s1_size, mpi_limb_t s2_limb);
224
void _gcry_mpih_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
226
mpi_limb_t _gcry_mpih_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
227
mpi_ptr_t vp, mpi_size_t vsize);
228
void _gcry_mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
229
void _gcry_mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
232
void _gcry_mpih_mul_karatsuba_case( mpi_ptr_t prodp,
233
mpi_ptr_t up, mpi_size_t usize,
234
mpi_ptr_t vp, mpi_size_t vsize,
235
struct karatsuba_ctx *ctx );
238
/*-- mpih-mul_1.c (or xxx/cpu/ *.S) --*/
239
mpi_limb_t _gcry_mpih_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
240
mpi_size_t s1_size, mpi_limb_t s2_limb);
243
mpi_limb_t _gcry_mpih_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
244
mpi_limb_t divisor_limb);
245
mpi_limb_t _gcry_mpih_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
246
mpi_ptr_t np, mpi_size_t nsize,
247
mpi_ptr_t dp, mpi_size_t dsize);
248
mpi_limb_t _gcry_mpih_divmod_1( mpi_ptr_t quot_ptr,
249
mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
250
mpi_limb_t divisor_limb);
252
/*-- mpih-shift.c --*/
253
mpi_limb_t _gcry_mpih_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
255
mpi_limb_t _gcry_mpih_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
259
/* Define stuff for longlong.h. */
260
#define W_TYPE_SIZE BITS_PER_MPI_LIMB
261
typedef mpi_limb_t UWtype;
262
typedef unsigned int UHWtype;
263
#if defined (__GNUC__)
264
typedef unsigned int UQItype __attribute__ ((mode (QI)));
265
typedef int SItype __attribute__ ((mode (SI)));
266
typedef unsigned int USItype __attribute__ ((mode (SI)));
267
typedef int DItype __attribute__ ((mode (DI)));
268
typedef unsigned int UDItype __attribute__ ((mode (DI)));
270
typedef unsigned char UQItype;
272
typedef unsigned long USItype;
276
#include "mpi-inline.h"
279
#endif /*G10_MPI_INTERNAL_H*/