1 /* TomsFastMath, a fast ISO C bignum library.
3 * This project is meant to fill in where LibTomMath
4 * falls short. That is speed ;-)
6 * This project is public domain and free for all purposes.
8 * Tom St Denis, tomstdenis@gmail.com
25 #define TFM_VERSION 0x000D0100
26 #define TFM_VERSION_S "v0.13.1"
29 #define MIN(x,y) ((x)<(y)?(x):(y))
33 #define MAX(x,y) ((x)>(y)?(x):(y))
36 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
37 #ifndef TFM_ALREADY_SET
39 /* do we want the large set of small multiplications ?
40 Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
41 Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
45 /* do we want huge code
46 Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
47 Less important on 64-bit machines as 32 digits == 2048 bits
83 /* do we want some overflow checks
84 Not required if you make sure your numbers are within range (e.g. by default a modulus for fp_exptmod() can only be upto 2048 bits long)
86 /* #define TFM_CHECK */
88 /* Is the target a P4 Prescott
90 /* #define TFM_PRESCOTT */
92 /* Do we want timing resistant fp_exptmod() ?
93 * This makes it slower but also timing invariant with respect to the exponent
95 /* #define TFM_TIMING_RESISTANT */
99 /* Max size of any number in bits. Basically the largest size you will be multiplying
100 * should be half [or smaller] of FP_MAX_SIZE-four_digit
102 * You can externally define this or it defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ]
105 #define FP_MAX_SIZE (4*4096+(8*DIGIT_BIT))
108 /* will this lib work? */
110 #error CHAR_BIT must be a multiple of eight.
112 #if FP_MAX_SIZE % CHAR_BIT
113 #error FP_MAX_SIZE must be a multiple of CHAR_BIT
117 #if __SIZEOF_LONG__ == 8
122 /* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */
123 #if defined(__x86_64__)
124 #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM)
125 #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid!
127 #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
131 #if defined(TFM_X86_64)
132 #if !defined(FP_64BIT)
137 /* try to detect x86-32 */
138 #if defined(__i386__) && !defined(TFM_SSE2)
139 #if defined(TFM_X86_64) || defined(TFM_ARM)
140 #error x86-32 detected, x86-64/ARM optimizations are not valid!
142 #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
147 /* make sure we're 32-bit for x86-32/sse/arm/ppc32 */
148 #if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT)
149 #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining)
159 #error TFM_ASM already defined!
165 #error TFM_ASM already defined!
171 #error TFM_ASM already defined!
177 #error TFM_ASM already defined!
183 #error TFM_ASM already defined!
189 #error TFM_ASM already defined!
194 /* we want no asm? */
257 /* use arc4random on platforms that support it */
258 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
259 #define FP_GEN_RANDOM() arc4random()
260 #define FP_GEN_RANDOM_MAX 0xffffffff
263 /* use rand() as fall-back if there's no better rand function */
264 #ifndef FP_GEN_RANDOM
265 #define FP_GEN_RANDOM() rand()
266 #define FP_GEN_RANDOM_MAX RAND_MAX
269 /* some default configurations.
271 #if defined(FP_64BIT)
272 #error cannot use 128 bit words
273 /* for GCC only on supported platforms */
275 typedef unsigned long long ulong64;
278 typedef ulong64 fp_digit;
279 #define SIZEOF_FP_DIGIT 8
280 typedef unsigned long fp_word __attribute__ ((mode(TI)));
284 /* this is to make porting into LibTomCrypt easier :-) */
286 #if defined(_MSC_VER) || defined(__BORLANDC__)
287 typedef unsigned __int64 ulong64;
288 typedef signed __int64 long64;
290 typedef unsigned long long ulong64;
291 typedef signed long long long64;
292 #endif /* defined(_MSC_VER) ... */
295 typedef unsigned int fp_digit;
296 #define SIZEOF_FP_DIGIT 4
297 typedef ulong64 fp_word;
298 #endif /* FP_64BIT */
300 /* # of digits this is */
301 #define DIGIT_BIT ((CHAR_BIT) * SIZEOF_FP_DIGIT)
302 #define FP_MASK (fp_digit)(-1)
303 #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
315 #define FP_LT -1 /* less than */
316 #define FP_EQ 0 /* equal to */
317 #define FP_GT 1 /* greater than */
320 #define FP_YES 1 /* yes response */
321 #define FP_NO 0 /* no response */
325 fp_digit dp[FP_SIZE];
332 /* returns a TFM ident string useful for debugging... */
333 const char *fp_ident(void);
335 /* initialize [or zero] an fp int */
336 #define fp_init(a) (void)memset((a), 0, sizeof(fp_int))
337 #define fp_zero(a) fp_init(a)
339 /* zero/even/odd ? */
340 #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
341 #define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
342 #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
344 /* set to a small digit */
345 void fp_set(fp_int *a, fp_digit b);
347 /* makes a pseudo-random int of a given size */
348 void fp_rand(fp_int *a, int digits);
350 /* copy from a to b */
351 #define fp_copy(a, b) (void)(((a) != (b)) && memcpy((b), (a), sizeof(fp_int)))
352 #define fp_init_copy(a, b) fp_copy(b, a)
355 #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
357 /* negate and absolute */
358 #define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
359 #define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; }
361 /* right shift x digits */
362 void fp_rshd(fp_int *a, int x);
364 /* left shift x digits */
365 void fp_lshd(fp_int *a, int x);
367 /* signed comparison */
368 int fp_cmp(fp_int *a, fp_int *b);
370 /* unsigned comparison */
371 int fp_cmp_mag(fp_int *a, fp_int *b);
373 /* power of 2 operations */
374 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
375 void fp_mod_2d(fp_int *a, int b, fp_int *c);
376 void fp_mul_2d(fp_int *a, int b, fp_int *c);
377 void fp_2expt (fp_int *a, int b);
378 void fp_mul_2(fp_int *a, fp_int *c);
379 void fp_div_2(fp_int *a, fp_int *c);
381 /* Counts the number of lsbs which are zero before the first zero bit */
382 int fp_cnt_lsb(fp_int *a);
385 void fp_add(fp_int *a, fp_int *b, fp_int *c);
388 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
391 void fp_mul(fp_int *a, fp_int *b, fp_int *c);
394 void fp_sqr(fp_int *a, fp_int *b);
396 /* a/b => cb + d == a */
397 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
399 /* c = a mod b, 0 <= c < b */
400 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
402 /* compare against a single digit */
403 int fp_cmp_d(fp_int *a, fp_digit b);
406 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
409 void fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
412 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
414 /* a/b => cb + d == a */
415 int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);
417 /* c = a mod b, 0 <= c < b */
418 int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);
420 /* ---> number theory <--- */
421 /* d = a + b (mod c) */
422 int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
424 /* d = a - b (mod c) */
425 int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
427 /* d = a * b (mod c) */
428 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
430 /* c = a * a (mod b) */
431 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
433 /* c = 1/a (mod b) */
434 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
437 void fp_gcd(fp_int *a, fp_int *b, fp_int *c);
440 void fp_lcm(fp_int *a, fp_int *b, fp_int *c);
442 /* setups the montgomery reduction */
443 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
445 /* computes a = B**n mod b without division or multiplication useful for
446 * normalizing numbers in a Montgomery system.
448 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
450 /* computes x/R == x (mod N) via Montgomery Reduction */
451 void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
453 /* d = a**b (mod c) */
454 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
456 /* primality stuff */
458 /* perform a Miller-Rabin test of a to the base b and store result in "result" */
459 void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);
461 #define FP_PRIME_SIZE 256
462 /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */
463 int fp_isprime(fp_int *a);
464 /* extended version of fp_isprime, do 't' Miller-Rabins instead of only 8 */
465 int fp_isprime_ex(fp_int *a, int t);
467 /* Primality generation flags */
468 #define TFM_PRIME_BBS 0x0001 /* BBS style prime */
469 #define TFM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
470 #define TFM_PRIME_2MSB_OFF 0x0004 /* force 2nd MSB to 0 */
471 #define TFM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
473 /* callback for fp_prime_random, should fill dst with random bytes and return how many read [upto len] */
474 typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);
476 #define fp_prime_random(a, t, size, bbs, cb, dat) fp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?TFM_PRIME_BBS:0, cb, dat)
478 int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);
480 /* radix conersions */
481 int fp_count_bits(fp_int *a);
483 int fp_unsigned_bin_size(fp_int *a);
484 void fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c);
485 void fp_to_unsigned_bin(fp_int *a, unsigned char *b);
487 int fp_signed_bin_size(fp_int *a);
488 void fp_read_signed_bin(fp_int *a, const unsigned char *b, int c);
489 void fp_to_signed_bin(fp_int *a, unsigned char *b);
491 int fp_read_radix(fp_int *a, const char *str, int radix);
493 int fp_radix_size(fp_int *a, int radix, int *size);
494 int fp_toradix(fp_int *a, char *str, int radix);
495 int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);