1 /* $NetBSD: tommath.h,v 1.1.1.2 2014/04/24 12:45:31 pettai Exp $ */
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
26 #include <tommath_class.h>
29 #define MIN(x,y) ((x)<(y)?(x):(y))
33 #define MAX(x,y) ((x)>(y)?(x):(y))
39 /* C++ compilers don't like assigning void * to mp_digit * */
40 #define OPT_CAST(x) (x *)
44 /* C on the other hand doesn't care */
50 /* detect 64-bit mode if possible */
51 #if defined(__x86_64__)
52 #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
57 /* some default configurations.
59 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
60 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
62 * At the very least a mp_digit must be able to hold 7 bits
63 * [any size beyond that is ok provided it doesn't overflow the data type]
66 typedef unsigned char mp_digit
;
67 typedef unsigned short mp_word
;
68 #elif defined(MP_16BIT)
69 typedef unsigned short mp_digit
;
70 typedef unsigned long mp_word
;
71 #elif defined(MP_64BIT)
72 /* for GCC only on supported platforms */
74 typedef unsigned long long ulong64
;
75 typedef signed long long long64
;
78 typedef unsigned long mp_digit
;
79 typedef unsigned long mp_word
__attribute__ ((mode(TI
)));
83 /* this is the default case, 28-bit digits */
85 /* this is to make porting into LibTomCrypt easier :-) */
87 #if defined(_MSC_VER) || defined(__BORLANDC__)
88 typedef unsigned __int64 ulong64
;
89 typedef signed __int64 long64
;
91 typedef unsigned long long ulong64
;
92 typedef signed long long long64
;
96 typedef unsigned long mp_digit
;
97 typedef ulong64 mp_word
;
100 /* this is an extension that uses 31-bit digits */
103 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
109 /* define heap macros */
111 /* default to libc stuff */
113 #define XMALLOC malloc
115 #define XREALLOC realloc
116 #define XCALLOC calloc
118 /* prototypes for our heap functions */
119 extern void *XMALLOC(size_t n
);
120 extern void *XREALLOC(void *p
, size_t n
);
121 extern void *XCALLOC(size_t n
, size_t s
);
122 extern void XFREE(void *p
);
127 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
129 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
132 #define MP_DIGIT_BIT DIGIT_BIT
133 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
134 #define MP_DIGIT_MAX MP_MASK
137 #define MP_LT -1 /* less than */
138 #define MP_EQ 0 /* equal to */
139 #define MP_GT 1 /* greater than */
141 #define MP_ZPOS 0 /* positive integer */
142 #define MP_NEG 1 /* negative */
144 #define MP_OKAY 0 /* ok result */
145 #define MP_MEM -2 /* out of mem */
146 #define MP_VAL -3 /* invalid input */
147 #define MP_RANGE MP_VAL
149 #define MP_YES 1 /* yes response */
150 #define MP_NO 0 /* no response */
152 /* Primality generation flags */
153 #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
154 #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
155 #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
159 /* you'll have to tune these... */
160 extern int KARATSUBA_MUL_CUTOFF
,
161 KARATSUBA_SQR_CUTOFF
,
165 /* define this to use lower memory usage routines (exptmods mostly) */
166 /* #define MP_LOW_MEM */
168 /* default precision */
171 #define MP_PREC 32 /* default digits of precision */
173 #define MP_PREC 8 /* default digits of precision */
177 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
178 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
180 /* the infamous mp_int structure */
182 int used
, alloc
, sign
;
186 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
187 typedef int ltm_prime_callback(unsigned char *dst
, int len
, void *dat
);
190 #define USED(m) ((m)->used)
191 #define DIGIT(m,k) ((m)->dp[(k)])
192 #define SIGN(m) ((m)->sign)
194 /* error code to char* string */
195 char *mp_error_to_string(int code
);
197 /* ---> init and deinit bignum functions <--- */
199 int mp_init(mp_int
*a
);
202 void mp_clear(mp_int
*a
);
204 /* init a null terminated series of arguments */
205 int mp_init_multi(mp_int
*mp
, ...);
207 /* clear a null terminated series of arguments */
208 void mp_clear_multi(mp_int
*mp
, ...);
210 /* exchange two ints */
211 void mp_exch(mp_int
*a
, mp_int
*b
);
213 /* shrink ram required for a bignum */
214 int mp_shrink(mp_int
*a
);
216 /* grow an int to a given size */
217 int mp_grow(mp_int
*a
, int size
);
219 /* init to a given number of digits */
220 int mp_init_size(mp_int
*a
, int size
);
222 /* ---> Basic Manipulations <--- */
223 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
224 #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
225 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
226 #define mp_isneg(a) (((a)->sign) ? MP_YES : MP_NO)
229 void mp_zero(mp_int
*a
);
231 /* set to zero, multi */
232 void mp_zero_multi(mp_int
*a
, ...);
235 void mp_set(mp_int
*a
, mp_digit b
);
237 /* set a 32-bit const */
238 int mp_set_int(mp_int
*a
, unsigned long b
);
240 /* get a 32-bit value */
241 unsigned long mp_get_int(mp_int
* a
);
243 /* initialize and set a digit */
244 int mp_init_set (mp_int
* a
, mp_digit b
);
246 /* initialize and set 32-bit value */
247 int mp_init_set_int (mp_int
* a
, unsigned long b
);
250 int mp_copy(mp_int
*a
, mp_int
*b
);
252 /* inits and copies, a = b */
253 int mp_init_copy(mp_int
*a
, mp_int
*b
);
255 /* trim unused digits */
256 void mp_clamp(mp_int
*a
);
258 /* ---> digit manipulation <--- */
260 /* right shift by "b" digits */
261 void mp_rshd(mp_int
*a
, int b
);
263 /* left shift by "b" digits */
264 int mp_lshd(mp_int
*a
, int b
);
267 int mp_div_2d(mp_int
*a
, int b
, mp_int
*c
, mp_int
*d
);
270 int mp_div_2(mp_int
*a
, mp_int
*b
);
273 int mp_mul_2d(mp_int
*a
, int b
, mp_int
*c
);
276 int mp_mul_2(mp_int
*a
, mp_int
*b
);
279 int mp_mod_2d(mp_int
*a
, int b
, mp_int
*c
);
281 /* computes a = 2**b */
282 int mp_2expt(mp_int
*a
, int b
);
284 /* Counts the number of lsbs which are zero before the first zero bit */
285 int mp_cnt_lsb(mp_int
*a
);
289 /* makes a pseudo-random int of a given size */
290 int mp_rand(mp_int
*a
, int digits
);
292 /* ---> binary operations <--- */
294 int mp_xor(mp_int
*a
, mp_int
*b
, mp_int
*c
);
297 int mp_or(mp_int
*a
, mp_int
*b
, mp_int
*c
);
300 int mp_and(mp_int
*a
, mp_int
*b
, mp_int
*c
);
302 /* ---> Basic arithmetic <--- */
305 int mp_neg(mp_int
*a
, mp_int
*b
);
308 int mp_abs(mp_int
*a
, mp_int
*b
);
311 int mp_cmp(mp_int
*a
, mp_int
*b
);
313 /* compare |a| to |b| */
314 int mp_cmp_mag(mp_int
*a
, mp_int
*b
);
317 int mp_add(mp_int
*a
, mp_int
*b
, mp_int
*c
);
320 int mp_sub(mp_int
*a
, mp_int
*b
, mp_int
*c
);
323 int mp_mul(mp_int
*a
, mp_int
*b
, mp_int
*c
);
326 int mp_sqr(mp_int
*a
, mp_int
*b
);
328 /* a/b => cb + d == a */
329 int mp_div(mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
331 /* c = a mod b, 0 <= c < b */
332 int mp_mod(mp_int
*a
, mp_int
*b
, mp_int
*c
);
334 /* ---> single digit functions <--- */
336 /* compare against a single digit */
337 int mp_cmp_d(mp_int
*a
, mp_digit b
);
340 int mp_add_d(mp_int
*a
, mp_digit b
, mp_int
*c
);
343 int mp_sub_d(mp_int
*a
, mp_digit b
, mp_int
*c
);
346 int mp_mul_d(mp_int
*a
, mp_digit b
, mp_int
*c
);
348 /* a/b => cb + d == a */
349 int mp_div_d(mp_int
*a
, mp_digit b
, mp_int
*c
, mp_digit
*d
);
351 /* a/3 => 3c + d == a */
352 int mp_div_3(mp_int
*a
, mp_int
*c
, mp_digit
*d
);
355 int mp_expt_d(mp_int
*a
, mp_digit b
, mp_int
*c
);
357 /* c = a mod b, 0 <= c < b */
358 int mp_mod_d(mp_int
*a
, mp_digit b
, mp_digit
*c
);
360 /* ---> number theory <--- */
362 /* d = a + b (mod c) */
363 int mp_addmod(mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
365 /* d = a - b (mod c) */
366 int mp_submod(mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
368 /* d = a * b (mod c) */
369 int mp_mulmod(mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
371 /* c = a * a (mod b) */
372 int mp_sqrmod(mp_int
*a
, mp_int
*b
, mp_int
*c
);
374 /* c = 1/a (mod b) */
375 int mp_invmod(mp_int
*a
, mp_int
*b
, mp_int
*c
);
378 int mp_gcd(mp_int
*a
, mp_int
*b
, mp_int
*c
);
380 /* produces value such that U1*a + U2*b = U3 */
381 int mp_exteuclid(mp_int
*a
, mp_int
*b
, mp_int
*U1
, mp_int
*U2
, mp_int
*U3
);
383 /* c = [a, b] or (a*b)/(a, b) */
384 int mp_lcm(mp_int
*a
, mp_int
*b
, mp_int
*c
);
386 /* finds one of the b'th root of a, such that |c|**b <= |a|
388 * returns error if a < 0 and b is even
390 int mp_n_root(mp_int
*a
, mp_digit b
, mp_int
*c
);
392 /* special sqrt algo */
393 int mp_sqrt(mp_int
*arg
, mp_int
*ret
);
395 /* is number a square? */
396 int mp_is_square(mp_int
*arg
, int *ret
);
398 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
399 int mp_jacobi(mp_int
*a
, mp_int
*n
, int *c
);
401 /* used to setup the Barrett reduction for a given modulus b */
402 int mp_reduce_setup(mp_int
*a
, mp_int
*b
);
404 /* Barrett Reduction, computes a (mod b) with a precomputed value c
406 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
407 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
409 int mp_reduce(mp_int
*a
, mp_int
*b
, mp_int
*c
);
411 /* setups the montgomery reduction */
412 int mp_montgomery_setup(mp_int
*a
, mp_digit
*mp
);
414 /* computes a = B**n mod b without division or multiplication useful for
415 * normalizing numbers in a Montgomery system.
417 int mp_montgomery_calc_normalization(mp_int
*a
, mp_int
*b
);
419 /* computes x/R == x (mod N) via Montgomery Reduction */
420 int mp_montgomery_reduce(mp_int
*a
, mp_int
*m
, mp_digit mp
);
422 /* returns 1 if a is a valid DR modulus */
423 int mp_dr_is_modulus(mp_int
*a
);
425 /* sets the value of "d" required for mp_dr_reduce */
426 void mp_dr_setup(mp_int
*a
, mp_digit
*d
);
428 /* reduces a modulo b using the Diminished Radix method */
429 int mp_dr_reduce(mp_int
*a
, mp_int
*b
, mp_digit mp
);
431 /* returns true if a can be reduced with mp_reduce_2k */
432 int mp_reduce_is_2k(mp_int
*a
);
434 /* determines k value for 2k reduction */
435 int mp_reduce_2k_setup(mp_int
*a
, mp_digit
*d
);
437 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
438 int mp_reduce_2k(mp_int
*a
, mp_int
*n
, mp_digit d
);
440 /* returns true if a can be reduced with mp_reduce_2k_l */
441 int mp_reduce_is_2k_l(mp_int
*a
);
443 /* determines k value for 2k reduction */
444 int mp_reduce_2k_setup_l(mp_int
*a
, mp_int
*d
);
446 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
447 int mp_reduce_2k_l(mp_int
*a
, mp_int
*n
, mp_int
*d
);
449 /* d = a**b (mod c) */
450 int mp_exptmod(mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
452 /* ---> Primes <--- */
454 /* number of primes */
456 #define PRIME_SIZE 31
458 #define PRIME_SIZE 256
461 /* table of first PRIME_SIZE primes */
462 extern const mp_digit ltm_prime_tab
[];
464 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
465 int mp_prime_is_divisible(mp_int
*a
, int *result
);
467 /* performs one Fermat test of "a" using base "b".
468 * Sets result to 0 if composite or 1 if probable prime
470 int mp_prime_fermat(mp_int
*a
, mp_int
*b
, int *result
);
472 /* performs one Miller-Rabin test of "a" using base "b".
473 * Sets result to 0 if composite or 1 if probable prime
475 int mp_prime_miller_rabin(mp_int
*a
, mp_int
*b
, int *result
);
477 /* This gives [for a given bit size] the number of trials required
478 * such that Miller-Rabin gives a prob of failure lower than 2^-96
480 int mp_prime_rabin_miller_trials(int size
);
482 /* performs t rounds of Miller-Rabin on "a" using the first
483 * t prime bases. Also performs an initial sieve of trial
484 * division. Determines if "a" is prime with probability
485 * of error no more than (1/4)**t.
487 * Sets result to 1 if probably prime, 0 otherwise
489 int mp_prime_is_prime(mp_int
*a
, int t
, int *result
);
491 /* finds the next prime after the number "a" using "t" trials
494 * bbs_style = 1 means the prime must be congruent to 3 mod 4
496 int mp_prime_next_prime(mp_int
*a
, int t
, int bbs_style
);
498 /* makes a truly random prime of a given size (bytes),
499 * call with bbs = 1 if you want it to be congruent to 3 mod 4
501 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
502 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
505 * The prime generated will be larger than 2^(8*size).
507 #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)
509 /* makes a truly random prime of a given size (bits),
511 * Flags are as follows:
513 * LTM_PRIME_BBS - make prime congruent to 3 mod 4
514 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
515 * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
516 * LTM_PRIME_2MSB_ON - make the 2nd highest bit one
518 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
519 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
523 int mp_prime_random_ex(mp_int
*a
, int t
, int size
, int flags
, ltm_prime_callback cb
, void *dat
);
525 int mp_find_prime(mp_int
*a
);
527 int mp_isprime(mp_int
*a
);
529 /* ---> radix conversion <--- */
530 int mp_count_bits(mp_int
*a
);
532 int mp_unsigned_bin_size(mp_int
*a
);
533 int mp_read_unsigned_bin(mp_int
*a
, const unsigned char *b
, int c
);
534 int mp_to_unsigned_bin(mp_int
*a
, unsigned char *b
);
535 int mp_to_unsigned_bin_n (mp_int
* a
, unsigned char *b
, unsigned long *outlen
);
537 int mp_signed_bin_size(mp_int
*a
);
538 int mp_read_signed_bin(mp_int
*a
, const unsigned char *b
, int c
);
539 int mp_to_signed_bin(mp_int
*a
, unsigned char *b
);
540 int mp_to_signed_bin_n (mp_int
* a
, unsigned char *b
, unsigned long *outlen
);
542 int mp_read_radix(mp_int
*a
, const char *str
, int radix
);
543 int mp_toradix(mp_int
*a
, char *str
, int radix
);
544 int mp_toradix_n(mp_int
* a
, char *str
, int radix
, int maxlen
);
545 int mp_radix_size(mp_int
*a
, int radix
, int *size
);
547 int mp_fread(mp_int
*a
, int radix
, FILE *stream
);
548 int mp_fwrite(mp_int
*a
, int radix
, FILE *stream
);
550 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
551 #define mp_raw_size(mp) mp_signed_bin_size(mp)
552 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
553 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
554 #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
555 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
557 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
558 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
559 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
560 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
562 /* lowlevel functions, do not call! */
563 int s_mp_add(mp_int
*a
, mp_int
*b
, mp_int
*c
);
564 int s_mp_sub(mp_int
*a
, mp_int
*b
, mp_int
*c
);
565 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
566 int fast_s_mp_mul_digs(mp_int
*a
, mp_int
*b
, mp_int
*c
, int digs
);
567 int s_mp_mul_digs(mp_int
*a
, mp_int
*b
, mp_int
*c
, int digs
);
568 int fast_s_mp_mul_high_digs(mp_int
*a
, mp_int
*b
, mp_int
*c
, int digs
);
569 int s_mp_mul_high_digs(mp_int
*a
, mp_int
*b
, mp_int
*c
, int digs
);
570 int fast_s_mp_sqr(mp_int
*a
, mp_int
*b
);
571 int s_mp_sqr(mp_int
*a
, mp_int
*b
);
572 int mp_karatsuba_mul(mp_int
*a
, mp_int
*b
, mp_int
*c
);
573 int mp_toom_mul(mp_int
*a
, mp_int
*b
, mp_int
*c
);
574 int mp_karatsuba_sqr(mp_int
*a
, mp_int
*b
);
575 int mp_toom_sqr(mp_int
*a
, mp_int
*b
);
576 int fast_mp_invmod(mp_int
*a
, mp_int
*b
, mp_int
*c
);
577 int mp_invmod_slow (mp_int
* a
, mp_int
* b
, mp_int
* c
);
578 int fast_mp_montgomery_reduce(mp_int
*a
, mp_int
*m
, mp_digit mp
);
579 int mp_exptmod_fast(mp_int
*G
, mp_int
*X
, mp_int
*P
, mp_int
*Y
, int mode
);
580 int s_mp_exptmod (mp_int
* G
, mp_int
* X
, mp_int
* P
, mp_int
* Y
, int mode
);
581 void bn_reverse(unsigned char *s
, int len
);
583 extern const char *mp_s_rmap
;
592 /* Source: /cvs/libtom/libtommath/tommath.h,v */
594 /* Date: 2006/03/31 14:18:44 */