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