1*1c9681d1Schristos /*	$NetBSD: tommath.h,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2f59d82ffSelric 
3f59d82ffSelric /* LibTomMath, multiple-precision integer library -- Tom St Denis
4f59d82ffSelric  *
5f59d82ffSelric  * LibTomMath is a library that provides multiple-precision
6f59d82ffSelric  * integer arithmetic as well as number theoretic functionality.
7f59d82ffSelric  *
8f59d82ffSelric  * The library was designed directly after the MPI library by
9f59d82ffSelric  * Michael Fromberger but has been written from scratch with
10f59d82ffSelric  * additional optimizations in place.
11f59d82ffSelric  *
12f59d82ffSelric  * The library is free for all purposes without any express
13f59d82ffSelric  * guarantee it works.
14f59d82ffSelric  *
15f59d82ffSelric  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
16f59d82ffSelric  */
17f59d82ffSelric #ifndef BN_H_
18f59d82ffSelric #define BN_H_
19f59d82ffSelric 
20f59d82ffSelric #include <stdio.h>
21f59d82ffSelric #include <string.h>
22f59d82ffSelric #include <stdlib.h>
23f59d82ffSelric #include <ctype.h>
24f59d82ffSelric #include <limits.h>
25f59d82ffSelric 
26f59d82ffSelric #include <tommath_class.h>
27f59d82ffSelric 
28f59d82ffSelric #ifndef MIN
29f59d82ffSelric    #define MIN(x,y) ((x)<(y)?(x):(y))
30f59d82ffSelric #endif
31f59d82ffSelric 
32f59d82ffSelric #ifndef MAX
33f59d82ffSelric    #define MAX(x,y) ((x)>(y)?(x):(y))
34f59d82ffSelric #endif
35f59d82ffSelric 
36f59d82ffSelric #ifdef __cplusplus
37f59d82ffSelric extern "C" {
38f59d82ffSelric 
39f59d82ffSelric /* C++ compilers don't like assigning void * to mp_digit * */
40f59d82ffSelric #define  OPT_CAST(x)  (x *)
41f59d82ffSelric 
42f59d82ffSelric #else
43f59d82ffSelric 
44f59d82ffSelric /* C on the other hand doesn't care */
45f59d82ffSelric #define  OPT_CAST(x)
46f59d82ffSelric 
47f59d82ffSelric #endif
48f59d82ffSelric 
49f59d82ffSelric 
50f59d82ffSelric /* detect 64-bit mode if possible */
51e0895134Schristos #if defined(__x86_64__) && !defined(__ILP32__)
52f59d82ffSelric    #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
53f59d82ffSelric       #define MP_64BIT
54f59d82ffSelric    #endif
55f59d82ffSelric #endif
56f59d82ffSelric 
57f59d82ffSelric /* some default configurations.
58f59d82ffSelric  *
59f59d82ffSelric  * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
60f59d82ffSelric  * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
61f59d82ffSelric  *
62f59d82ffSelric  * At the very least a mp_digit must be able to hold 7 bits
63f59d82ffSelric  * [any size beyond that is ok provided it doesn't overflow the data type]
64f59d82ffSelric  */
65f59d82ffSelric #ifdef MP_8BIT
66f59d82ffSelric    typedef unsigned char      mp_digit;
67f59d82ffSelric    typedef unsigned short     mp_word;
68f59d82ffSelric #elif defined(MP_16BIT)
69f59d82ffSelric    typedef unsigned short     mp_digit;
70f59d82ffSelric    typedef unsigned long      mp_word;
71f59d82ffSelric #elif defined(MP_64BIT)
72f59d82ffSelric    /* for GCC only on supported platforms */
73f59d82ffSelric #ifndef CRYPT
74f59d82ffSelric    typedef unsigned long long ulong64;
75f59d82ffSelric    typedef signed long long   long64;
76f59d82ffSelric #endif
77f59d82ffSelric 
78f59d82ffSelric    typedef unsigned long      mp_digit;
79f59d82ffSelric    typedef unsigned long      mp_word __attribute__ ((mode(TI)));
80f59d82ffSelric 
81f59d82ffSelric    #define DIGIT_BIT          60
82f59d82ffSelric #else
83f59d82ffSelric    /* this is the default case, 28-bit digits */
84f59d82ffSelric 
85f59d82ffSelric    /* this is to make porting into LibTomCrypt easier :-) */
86f59d82ffSelric #ifndef CRYPT
87f59d82ffSelric    #if defined(_MSC_VER) || defined(__BORLANDC__)
88f59d82ffSelric       typedef unsigned __int64   ulong64;
89f59d82ffSelric       typedef signed __int64     long64;
90f59d82ffSelric    #else
91f59d82ffSelric       typedef unsigned long long ulong64;
92f59d82ffSelric       typedef signed long long   long64;
93f59d82ffSelric    #endif
94f59d82ffSelric #endif
95f59d82ffSelric 
96f59d82ffSelric    typedef unsigned long      mp_digit;
97f59d82ffSelric    typedef ulong64            mp_word;
98f59d82ffSelric 
99f59d82ffSelric #ifdef MP_31BIT
100f59d82ffSelric    /* this is an extension that uses 31-bit digits */
101f59d82ffSelric    #define DIGIT_BIT          31
102f59d82ffSelric #else
103f59d82ffSelric    /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
104f59d82ffSelric    #define DIGIT_BIT          28
105f59d82ffSelric    #define MP_28BIT
106f59d82ffSelric #endif
107f59d82ffSelric #endif
108f59d82ffSelric 
109f59d82ffSelric /* define heap macros */
110f59d82ffSelric #ifndef CRYPT
111f59d82ffSelric    /* default to libc stuff */
112f59d82ffSelric    #ifndef XMALLOC
113f59d82ffSelric        #define XMALLOC  malloc
114f59d82ffSelric        #define XFREE    free
115f59d82ffSelric        #define XREALLOC realloc
116f59d82ffSelric        #define XCALLOC  calloc
117f59d82ffSelric    #else
118f59d82ffSelric       /* prototypes for our heap functions */
119f59d82ffSelric       extern void *XMALLOC(size_t n);
120f59d82ffSelric       extern void *XREALLOC(void *p, size_t n);
121f59d82ffSelric       extern void *XCALLOC(size_t n, size_t s);
122f59d82ffSelric       extern void XFREE(void *p);
123f59d82ffSelric    #endif
124f59d82ffSelric #endif
125f59d82ffSelric 
126f59d82ffSelric 
127f59d82ffSelric /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
128f59d82ffSelric #ifndef DIGIT_BIT
129f59d82ffSelric    #define DIGIT_BIT     ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))  /* bits per digit */
130f59d82ffSelric #endif
131f59d82ffSelric 
132f59d82ffSelric #define MP_DIGIT_BIT     DIGIT_BIT
133f59d82ffSelric #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
134f59d82ffSelric #define MP_DIGIT_MAX     MP_MASK
135f59d82ffSelric 
136f59d82ffSelric /* equalities */
137f59d82ffSelric #define MP_LT        -1   /* less than */
138f59d82ffSelric #define MP_EQ         0   /* equal to */
139f59d82ffSelric #define MP_GT         1   /* greater than */
140f59d82ffSelric 
141f59d82ffSelric #define MP_ZPOS       0   /* positive integer */
142f59d82ffSelric #define MP_NEG        1   /* negative */
143f59d82ffSelric 
144f59d82ffSelric #define MP_OKAY       0   /* ok result */
145f59d82ffSelric #define MP_MEM        -2  /* out of mem */
146f59d82ffSelric #define MP_VAL        -3  /* invalid input */
147f59d82ffSelric #define MP_RANGE      MP_VAL
148f59d82ffSelric 
149f59d82ffSelric #define MP_YES        1   /* yes response */
150f59d82ffSelric #define MP_NO         0   /* no response */
151f59d82ffSelric 
152f59d82ffSelric /* Primality generation flags */
153f59d82ffSelric #define LTM_PRIME_BBS      0x0001 /* BBS style prime */
154f59d82ffSelric #define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
155f59d82ffSelric #define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */
156f59d82ffSelric 
157f59d82ffSelric typedef int           mp_err;
158f59d82ffSelric 
159f59d82ffSelric /* you'll have to tune these... */
160f59d82ffSelric extern int KARATSUBA_MUL_CUTOFF,
161f59d82ffSelric            KARATSUBA_SQR_CUTOFF,
162f59d82ffSelric            TOOM_MUL_CUTOFF,
163f59d82ffSelric            TOOM_SQR_CUTOFF;
164f59d82ffSelric 
165f59d82ffSelric /* define this to use lower memory usage routines (exptmods mostly) */
166f59d82ffSelric /* #define MP_LOW_MEM */
167f59d82ffSelric 
168f59d82ffSelric /* default precision */
169f59d82ffSelric #ifndef MP_PREC
170f59d82ffSelric    #ifndef MP_LOW_MEM
171f59d82ffSelric       #define MP_PREC                 32     /* default digits of precision */
172f59d82ffSelric    #else
173f59d82ffSelric       #define MP_PREC                 8      /* default digits of precision */
174f59d82ffSelric    #endif
175f59d82ffSelric #endif
176f59d82ffSelric 
177f59d82ffSelric /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
178f59d82ffSelric #define MP_WARRAY               (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
179f59d82ffSelric 
180f59d82ffSelric /* the infamous mp_int structure */
181f59d82ffSelric typedef struct  {
182f59d82ffSelric     int used, alloc, sign;
183f59d82ffSelric     mp_digit *dp;
184f59d82ffSelric } mp_int;
185f59d82ffSelric 
186f59d82ffSelric /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
187f59d82ffSelric typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
188f59d82ffSelric 
189f59d82ffSelric 
190f59d82ffSelric #define USED(m)    ((m)->used)
191f59d82ffSelric #define DIGIT(m,k) ((m)->dp[(k)])
192f59d82ffSelric #define SIGN(m)    ((m)->sign)
193f59d82ffSelric 
194e0895134Schristos /* error code to const char* string */
195e0895134Schristos const char *mp_error_to_string(int code);
196f59d82ffSelric 
197f59d82ffSelric /* ---> init and deinit bignum functions <--- */
198f59d82ffSelric /* init a bignum */
199f59d82ffSelric int mp_init(mp_int *a);
200f59d82ffSelric 
201f59d82ffSelric /* free a bignum */
202f59d82ffSelric void mp_clear(mp_int *a);
203f59d82ffSelric 
204f59d82ffSelric /* init a null terminated series of arguments */
205f59d82ffSelric int mp_init_multi(mp_int *mp, ...);
206f59d82ffSelric 
207f59d82ffSelric /* clear a null terminated series of arguments */
208f59d82ffSelric void mp_clear_multi(mp_int *mp, ...);
209f59d82ffSelric 
210f59d82ffSelric /* exchange two ints */
211f59d82ffSelric void mp_exch(mp_int *a, mp_int *b);
212f59d82ffSelric 
213f59d82ffSelric /* shrink ram required for a bignum */
214f59d82ffSelric int mp_shrink(mp_int *a);
215f59d82ffSelric 
216f59d82ffSelric /* grow an int to a given size */
217f59d82ffSelric int mp_grow(mp_int *a, int size);
218f59d82ffSelric 
219f59d82ffSelric /* init to a given number of digits */
220f59d82ffSelric int mp_init_size(mp_int *a, int size);
221f59d82ffSelric 
222f59d82ffSelric /* ---> Basic Manipulations <--- */
223f59d82ffSelric #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
224f59d82ffSelric #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
225f59d82ffSelric #define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
226f59d82ffSelric #define mp_isneg(a)  (((a)->sign) ? MP_YES : MP_NO)
227f59d82ffSelric 
228f59d82ffSelric /* set to zero */
229f59d82ffSelric void mp_zero(mp_int *a);
230f59d82ffSelric 
231f59d82ffSelric /* set to zero, multi */
232f59d82ffSelric void mp_zero_multi(mp_int *a, ...);
233f59d82ffSelric 
234f59d82ffSelric /* set to a digit */
235f59d82ffSelric void mp_set(mp_int *a, mp_digit b);
236f59d82ffSelric 
237f59d82ffSelric /* set a 32-bit const */
238f59d82ffSelric int mp_set_int(mp_int *a, unsigned long b);
239f59d82ffSelric 
240f59d82ffSelric /* get a 32-bit value */
241f59d82ffSelric unsigned long mp_get_int(mp_int * a);
242f59d82ffSelric 
243f59d82ffSelric /* initialize and set a digit */
244f59d82ffSelric int mp_init_set (mp_int * a, mp_digit b);
245f59d82ffSelric 
246f59d82ffSelric /* initialize and set 32-bit value */
247f59d82ffSelric int mp_init_set_int (mp_int * a, unsigned long b);
248f59d82ffSelric 
249f59d82ffSelric /* copy, b = a */
250f59d82ffSelric int mp_copy(mp_int *a, mp_int *b);
251f59d82ffSelric 
252f59d82ffSelric /* inits and copies, a = b */
253f59d82ffSelric int mp_init_copy(mp_int *a, mp_int *b);
254f59d82ffSelric 
255f59d82ffSelric /* trim unused digits */
256f59d82ffSelric void mp_clamp(mp_int *a);
257f59d82ffSelric 
258f59d82ffSelric /* ---> digit manipulation <--- */
259f59d82ffSelric 
260f59d82ffSelric /* right shift by "b" digits */
261f59d82ffSelric void mp_rshd(mp_int *a, int b);
262f59d82ffSelric 
263f59d82ffSelric /* left shift by "b" digits */
264f59d82ffSelric int mp_lshd(mp_int *a, int b);
265f59d82ffSelric 
266f59d82ffSelric /* c = a / 2**b */
267f59d82ffSelric int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
268f59d82ffSelric 
269f59d82ffSelric /* b = a/2 */
270f59d82ffSelric int mp_div_2(mp_int *a, mp_int *b);
271f59d82ffSelric 
272f59d82ffSelric /* c = a * 2**b */
273f59d82ffSelric int mp_mul_2d(mp_int *a, int b, mp_int *c);
274f59d82ffSelric 
275f59d82ffSelric /* b = a*2 */
276f59d82ffSelric int mp_mul_2(mp_int *a, mp_int *b);
277f59d82ffSelric 
278f59d82ffSelric /* c = a mod 2**d */
279f59d82ffSelric int mp_mod_2d(mp_int *a, int b, mp_int *c);
280f59d82ffSelric 
281f59d82ffSelric /* computes a = 2**b */
282f59d82ffSelric int mp_2expt(mp_int *a, int b);
283f59d82ffSelric 
284f59d82ffSelric /* Counts the number of lsbs which are zero before the first zero bit */
285f59d82ffSelric int mp_cnt_lsb(mp_int *a);
286f59d82ffSelric 
287f59d82ffSelric /* I Love Earth! */
288f59d82ffSelric 
289f59d82ffSelric /* makes a pseudo-random int of a given size */
290f59d82ffSelric int mp_rand(mp_int *a, int digits);
291f59d82ffSelric 
292f59d82ffSelric /* ---> binary operations <--- */
293f59d82ffSelric /* c = a XOR b  */
294f59d82ffSelric int mp_xor(mp_int *a, mp_int *b, mp_int *c);
295f59d82ffSelric 
296f59d82ffSelric /* c = a OR b */
297f59d82ffSelric int mp_or(mp_int *a, mp_int *b, mp_int *c);
298f59d82ffSelric 
299f59d82ffSelric /* c = a AND b */
300f59d82ffSelric int mp_and(mp_int *a, mp_int *b, mp_int *c);
301f59d82ffSelric 
302f59d82ffSelric /* ---> Basic arithmetic <--- */
303f59d82ffSelric 
304f59d82ffSelric /* b = -a */
305f59d82ffSelric int mp_neg(mp_int *a, mp_int *b);
306f59d82ffSelric 
307f59d82ffSelric /* b = |a| */
308f59d82ffSelric int mp_abs(mp_int *a, mp_int *b);
309f59d82ffSelric 
310f59d82ffSelric /* compare a to b */
311f59d82ffSelric int mp_cmp(mp_int *a, mp_int *b);
312f59d82ffSelric 
313f59d82ffSelric /* compare |a| to |b| */
314f59d82ffSelric int mp_cmp_mag(mp_int *a, mp_int *b);
315f59d82ffSelric 
316f59d82ffSelric /* c = a + b */
317f59d82ffSelric int mp_add(mp_int *a, mp_int *b, mp_int *c);
318f59d82ffSelric 
319f59d82ffSelric /* c = a - b */
320f59d82ffSelric int mp_sub(mp_int *a, mp_int *b, mp_int *c);
321f59d82ffSelric 
322f59d82ffSelric /* c = a * b */
323f59d82ffSelric int mp_mul(mp_int *a, mp_int *b, mp_int *c);
324f59d82ffSelric 
325f59d82ffSelric /* b = a*a  */
326f59d82ffSelric int mp_sqr(mp_int *a, mp_int *b);
327f59d82ffSelric 
328f59d82ffSelric /* a/b => cb + d == a */
329f59d82ffSelric int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
330f59d82ffSelric 
331f59d82ffSelric /* c = a mod b, 0 <= c < b  */
332f59d82ffSelric int mp_mod(mp_int *a, mp_int *b, mp_int *c);
333f59d82ffSelric 
334f59d82ffSelric /* ---> single digit functions <--- */
335f59d82ffSelric 
336f59d82ffSelric /* compare against a single digit */
337f59d82ffSelric int mp_cmp_d(mp_int *a, mp_digit b);
338f59d82ffSelric 
339f59d82ffSelric /* c = a + b */
340f59d82ffSelric int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
341f59d82ffSelric 
342f59d82ffSelric /* c = a - b */
343f59d82ffSelric int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
344f59d82ffSelric 
345f59d82ffSelric /* c = a * b */
346f59d82ffSelric int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
347f59d82ffSelric 
348f59d82ffSelric /* a/b => cb + d == a */
349f59d82ffSelric int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
350f59d82ffSelric 
351f59d82ffSelric /* a/3 => 3c + d == a */
352f59d82ffSelric int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
353f59d82ffSelric 
354f59d82ffSelric /* c = a**b */
355f59d82ffSelric int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
356f59d82ffSelric 
357f59d82ffSelric /* c = a mod b, 0 <= c < b  */
358f59d82ffSelric int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
359f59d82ffSelric 
360f59d82ffSelric /* ---> number theory <--- */
361f59d82ffSelric 
362f59d82ffSelric /* d = a + b (mod c) */
363f59d82ffSelric int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
364f59d82ffSelric 
365f59d82ffSelric /* d = a - b (mod c) */
366f59d82ffSelric int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
367f59d82ffSelric 
368f59d82ffSelric /* d = a * b (mod c) */
369f59d82ffSelric int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
370f59d82ffSelric 
371f59d82ffSelric /* c = a * a (mod b) */
372f59d82ffSelric int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
373f59d82ffSelric 
374f59d82ffSelric /* c = 1/a (mod b) */
375f59d82ffSelric int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
376f59d82ffSelric 
377f59d82ffSelric /* c = (a, b) */
378f59d82ffSelric int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
379f59d82ffSelric 
380f59d82ffSelric /* produces value such that U1*a + U2*b = U3 */
381f59d82ffSelric int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
382f59d82ffSelric 
383f59d82ffSelric /* c = [a, b] or (a*b)/(a, b) */
384f59d82ffSelric int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
385f59d82ffSelric 
386f59d82ffSelric /* finds one of the b'th root of a, such that |c|**b <= |a|
387f59d82ffSelric  *
388f59d82ffSelric  * returns error if a < 0 and b is even
389f59d82ffSelric  */
390f59d82ffSelric int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
391f59d82ffSelric 
392f59d82ffSelric /* special sqrt algo */
393f59d82ffSelric int mp_sqrt(mp_int *arg, mp_int *ret);
394f59d82ffSelric 
395f59d82ffSelric /* is number a square? */
396f59d82ffSelric int mp_is_square(mp_int *arg, int *ret);
397f59d82ffSelric 
398f59d82ffSelric /* computes the jacobi c = (a | n) (or Legendre if b is prime)  */
399f59d82ffSelric int mp_jacobi(mp_int *a, mp_int *n, int *c);
400f59d82ffSelric 
401f59d82ffSelric /* used to setup the Barrett reduction for a given modulus b */
402f59d82ffSelric int mp_reduce_setup(mp_int *a, mp_int *b);
403f59d82ffSelric 
404f59d82ffSelric /* Barrett Reduction, computes a (mod b) with a precomputed value c
405f59d82ffSelric  *
406f59d82ffSelric  * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
407f59d82ffSelric  * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
408f59d82ffSelric  */
409f59d82ffSelric int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
410f59d82ffSelric 
411f59d82ffSelric /* setups the montgomery reduction */
412f59d82ffSelric int mp_montgomery_setup(mp_int *a, mp_digit *mp);
413f59d82ffSelric 
414f59d82ffSelric /* computes a = B**n mod b without division or multiplication useful for
415f59d82ffSelric  * normalizing numbers in a Montgomery system.
416f59d82ffSelric  */
417f59d82ffSelric int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
418f59d82ffSelric 
419f59d82ffSelric /* computes x/R == x (mod N) via Montgomery Reduction */
420f59d82ffSelric int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
421f59d82ffSelric 
422f59d82ffSelric /* returns 1 if a is a valid DR modulus */
423f59d82ffSelric int mp_dr_is_modulus(mp_int *a);
424f59d82ffSelric 
425f59d82ffSelric /* sets the value of "d" required for mp_dr_reduce */
426f59d82ffSelric void mp_dr_setup(mp_int *a, mp_digit *d);
427f59d82ffSelric 
428f59d82ffSelric /* reduces a modulo b using the Diminished Radix method */
429f59d82ffSelric int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
430f59d82ffSelric 
431f59d82ffSelric /* returns true if a can be reduced with mp_reduce_2k */
432f59d82ffSelric int mp_reduce_is_2k(mp_int *a);
433f59d82ffSelric 
434f59d82ffSelric /* determines k value for 2k reduction */
435f59d82ffSelric int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
436f59d82ffSelric 
437f59d82ffSelric /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
438f59d82ffSelric int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
439f59d82ffSelric 
440f59d82ffSelric /* returns true if a can be reduced with mp_reduce_2k_l */
441f59d82ffSelric int mp_reduce_is_2k_l(mp_int *a);
442f59d82ffSelric 
443f59d82ffSelric /* determines k value for 2k reduction */
444f59d82ffSelric int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
445f59d82ffSelric 
446f59d82ffSelric /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
447f59d82ffSelric int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
448f59d82ffSelric 
449f59d82ffSelric /* d = a**b (mod c) */
450f59d82ffSelric int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
451f59d82ffSelric 
452f59d82ffSelric /* ---> Primes <--- */
453f59d82ffSelric 
454f59d82ffSelric /* number of primes */
455f59d82ffSelric #ifdef MP_8BIT
456f59d82ffSelric    #define PRIME_SIZE      31
457f59d82ffSelric #else
458f59d82ffSelric    #define PRIME_SIZE      256
459f59d82ffSelric #endif
460f59d82ffSelric 
461f59d82ffSelric /* table of first PRIME_SIZE primes */
462f59d82ffSelric extern const mp_digit ltm_prime_tab[];
463f59d82ffSelric 
464f59d82ffSelric /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
465f59d82ffSelric int mp_prime_is_divisible(mp_int *a, int *result);
466f59d82ffSelric 
467f59d82ffSelric /* performs one Fermat test of "a" using base "b".
468f59d82ffSelric  * Sets result to 0 if composite or 1 if probable prime
469f59d82ffSelric  */
470f59d82ffSelric int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
471f59d82ffSelric 
472f59d82ffSelric /* performs one Miller-Rabin test of "a" using base "b".
473f59d82ffSelric  * Sets result to 0 if composite or 1 if probable prime
474f59d82ffSelric  */
475f59d82ffSelric int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
476f59d82ffSelric 
477f59d82ffSelric /* This gives [for a given bit size] the number of trials required
478f59d82ffSelric  * such that Miller-Rabin gives a prob of failure lower than 2^-96
479f59d82ffSelric  */
480f59d82ffSelric int mp_prime_rabin_miller_trials(int size);
481f59d82ffSelric 
482f59d82ffSelric /* performs t rounds of Miller-Rabin on "a" using the first
483f59d82ffSelric  * t prime bases.  Also performs an initial sieve of trial
484f59d82ffSelric  * division.  Determines if "a" is prime with probability
485f59d82ffSelric  * of error no more than (1/4)**t.
486f59d82ffSelric  *
487f59d82ffSelric  * Sets result to 1 if probably prime, 0 otherwise
488f59d82ffSelric  */
489f59d82ffSelric int mp_prime_is_prime(mp_int *a, int t, int *result);
490f59d82ffSelric 
491f59d82ffSelric /* finds the next prime after the number "a" using "t" trials
492f59d82ffSelric  * of Miller-Rabin.
493f59d82ffSelric  *
494f59d82ffSelric  * bbs_style = 1 means the prime must be congruent to 3 mod 4
495f59d82ffSelric  */
496f59d82ffSelric int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
497f59d82ffSelric 
498f59d82ffSelric /* makes a truly random prime of a given size (bytes),
499f59d82ffSelric  * call with bbs = 1 if you want it to be congruent to 3 mod 4
500f59d82ffSelric  *
501f59d82ffSelric  * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
502f59d82ffSelric  * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
503f59d82ffSelric  * so it can be NULL
504f59d82ffSelric  *
505f59d82ffSelric  * The prime generated will be larger than 2^(8*size).
506f59d82ffSelric  */
507f59d82ffSelric #define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
508f59d82ffSelric 
509f59d82ffSelric /* makes a truly random prime of a given size (bits),
510f59d82ffSelric  *
511f59d82ffSelric  * Flags are as follows:
512f59d82ffSelric  *
513f59d82ffSelric  *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4
514f59d82ffSelric  *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
515f59d82ffSelric  *   LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
516f59d82ffSelric  *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one
517f59d82ffSelric  *
518f59d82ffSelric  * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
519f59d82ffSelric  * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
520f59d82ffSelric  * so it can be NULL
521f59d82ffSelric  *
522f59d82ffSelric  */
523f59d82ffSelric int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
524f59d82ffSelric 
525e0895134Schristos int mp_find_prime(mp_int *a, int t);
526f59d82ffSelric 
527f59d82ffSelric /* ---> radix conversion <--- */
528f59d82ffSelric int mp_count_bits(mp_int *a);
529f59d82ffSelric 
530f59d82ffSelric int mp_unsigned_bin_size(mp_int *a);
531f59d82ffSelric int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
532f59d82ffSelric int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
533f59d82ffSelric int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
534f59d82ffSelric 
535f59d82ffSelric int mp_signed_bin_size(mp_int *a);
536f59d82ffSelric int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
537f59d82ffSelric int mp_to_signed_bin(mp_int *a,  unsigned char *b);
538f59d82ffSelric int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
539f59d82ffSelric 
540f59d82ffSelric int mp_read_radix(mp_int *a, const char *str, int radix);
541f59d82ffSelric int mp_toradix(mp_int *a, char *str, int radix);
542f59d82ffSelric int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
543f59d82ffSelric int mp_radix_size(mp_int *a, int radix, int *size);
544f59d82ffSelric 
545f59d82ffSelric int mp_fread(mp_int *a, int radix, FILE *stream);
546f59d82ffSelric int mp_fwrite(mp_int *a, int radix, FILE *stream);
547f59d82ffSelric 
548f59d82ffSelric #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
549f59d82ffSelric #define mp_raw_size(mp)           mp_signed_bin_size(mp)
550f59d82ffSelric #define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
551f59d82ffSelric #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
552f59d82ffSelric #define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
553f59d82ffSelric #define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
554f59d82ffSelric 
555f59d82ffSelric #define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
556f59d82ffSelric #define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
557f59d82ffSelric #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
558f59d82ffSelric #define mp_tohex(M, S)     mp_toradix((M), (S), 16)
559f59d82ffSelric 
560f59d82ffSelric /* lowlevel functions, do not call! */
561f59d82ffSelric int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
562f59d82ffSelric int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
563f59d82ffSelric #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
564f59d82ffSelric int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
565f59d82ffSelric int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
566f59d82ffSelric int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
567f59d82ffSelric int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
568f59d82ffSelric int fast_s_mp_sqr(mp_int *a, mp_int *b);
569f59d82ffSelric int s_mp_sqr(mp_int *a, mp_int *b);
570f59d82ffSelric int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
571f59d82ffSelric int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
572f59d82ffSelric int mp_karatsuba_sqr(mp_int *a, mp_int *b);
573f59d82ffSelric int mp_toom_sqr(mp_int *a, mp_int *b);
574f59d82ffSelric int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
575f59d82ffSelric int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
576f59d82ffSelric int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
577f59d82ffSelric int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
578f59d82ffSelric int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode);
579f59d82ffSelric void bn_reverse(unsigned char *s, int len);
580f59d82ffSelric 
581f59d82ffSelric extern const char *mp_s_rmap;
582f59d82ffSelric 
583f59d82ffSelric #ifdef __cplusplus
584f59d82ffSelric    }
585f59d82ffSelric #endif
586f59d82ffSelric 
587f59d82ffSelric #endif
588f59d82ffSelric 
589f59d82ffSelric 
590f59d82ffSelric /* Source: /cvs/libtom/libtommath/tommath.h,v  */
591f59d82ffSelric /* Revision: 1.8  */
592f59d82ffSelric /* Date: 2006/03/31 14:18:44  */
593