]> pd.if.org Git - zpackage/blob - tomsfastmath/src/headers/tfm.h
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / headers / tfm.h
1 /* TomsFastMath, a fast ISO C bignum library.
2  *
3  * This project is meant to fill in where LibTomMath
4  * falls short.  That is speed ;-)
5  *
6  * This project is public domain and free for all purposes.
7  *
8  * Tom St Denis, tomstdenis@gmail.com
9  */
10 #ifndef TFM_H_
11 #define TFM_H_
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <limits.h>
18
19 /* 0xMaMiPaXX
20  * Major
21  * Minor
22  * Patch
23  * XX - undefined
24  */
25 #define TFM_VERSION     0x000D0100
26 #define TFM_VERSION_S   "v0.13.1"
27
28 #ifndef MIN
29    #define MIN(x,y) ((x)<(y)?(x):(y))
30 #endif
31
32 #ifndef MAX
33    #define MAX(x,y) ((x)>(y)?(x):(y))
34 #endif
35
36 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
37 #ifndef TFM_ALREADY_SET
38
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 ;-)
42  */
43 #define TFM_SMALL_SET
44
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
48  */
49 #if 0
50 #define TFM_MUL3
51 #define TFM_MUL4
52 #define TFM_MUL6
53 #define TFM_MUL7
54 #define TFM_MUL8
55 #define TFM_MUL9
56 #define TFM_MUL12
57 #define TFM_MUL17
58 #endif
59 #define TFM_MUL20
60 #define TFM_MUL24
61 #define TFM_MUL28
62 #define TFM_MUL32
63 #define TFM_MUL48
64 #define TFM_MUL64
65
66 #if 0
67 #define TFM_SQR3
68 #define TFM_SQR4
69 #define TFM_SQR6
70 #define TFM_SQR7
71 #define TFM_SQR8
72 #define TFM_SQR9
73 #define TFM_SQR12
74 #define TFM_SQR17
75 #endif
76 #define TFM_SQR20
77 #define TFM_SQR24
78 #define TFM_SQR28
79 #define TFM_SQR32
80 #define TFM_SQR48
81 #define TFM_SQR64
82
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)
85  */
86 /* #define TFM_CHECK */
87
88 /* Is the target a P4 Prescott
89  */
90 /* #define TFM_PRESCOTT */
91
92 /* Do we want timing resistant fp_exptmod() ?
93  * This makes it slower but also timing invariant with respect to the exponent
94  */
95 /* #define TFM_TIMING_RESISTANT */
96
97 #endif
98
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
101  *
102  * You can externally define this or it defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ]
103  */
104 #ifndef FP_MAX_SIZE
105    #define FP_MAX_SIZE           (4*4096+(8*DIGIT_BIT))
106 #endif
107
108 /* will this lib work? */
109 #if (CHAR_BIT & 7)
110    #error CHAR_BIT must be a multiple of eight.
111 #endif
112 #if FP_MAX_SIZE % CHAR_BIT
113    #error FP_MAX_SIZE must be a multiple of CHAR_BIT
114 #endif
115
116 #if 0
117 #if __SIZEOF_LONG__ == 8
118         #define FP_64BIT
119 #endif
120 #endif
121
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!
126    #endif
127    #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
128       #define TFM_X86_64
129    #endif
130 #endif
131 #if defined(TFM_X86_64)
132     #if !defined(FP_64BIT)
133        #define FP_64BIT
134     #endif
135 #endif
136
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!
141    #endif
142    #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
143       #define TFM_X86
144    #endif
145 #endif
146
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)
150    #undef FP_64BIT
151 #endif
152
153 /* multi asms? */
154 #ifdef TFM_X86
155    #define TFM_ASM
156 #endif
157 #ifdef TFM_X86_64
158    #ifdef TFM_ASM
159       #error TFM_ASM already defined!
160    #endif
161    #define TFM_ASM
162 #endif
163 #ifdef TFM_SSE2
164    #ifdef TFM_ASM
165       #error TFM_ASM already defined!
166    #endif
167    #define TFM_ASM
168 #endif
169 #ifdef TFM_ARM
170    #ifdef TFM_ASM
171       #error TFM_ASM already defined!
172    #endif
173    #define TFM_ASM
174 #endif
175 #ifdef TFM_PPC32
176    #ifdef TFM_ASM
177       #error TFM_ASM already defined!
178    #endif
179    #define TFM_ASM
180 #endif
181 #ifdef TFM_PPC64
182    #ifdef TFM_ASM
183       #error TFM_ASM already defined!
184    #endif
185    #define TFM_ASM
186 #endif
187 #ifdef TFM_AVR32
188    #ifdef TFM_ASM
189       #error TFM_ASM already defined!
190    #endif
191    #define TFM_ASM
192 #endif
193
194 /* we want no asm? */
195 #ifdef TFM_NO_ASM
196    #undef TFM_X86
197    #undef TFM_X86_64
198    #undef TFM_SSE2
199    #undef TFM_ARM
200    #undef TFM_PPC32
201    #undef TFM_PPC64
202    #undef TFM_AVR32
203    #undef TFM_ASM
204 #endif
205
206 /* ECC helpers */
207 #ifdef TFM_ECC192
208    #ifdef FP_64BIT
209        #define TFM_MUL3
210        #define TFM_SQR3
211    #else
212        #define TFM_MUL6
213        #define TFM_SQR6
214    #endif
215 #endif
216
217 #ifdef TFM_ECC224
218    #ifdef FP_64BIT
219        #define TFM_MUL4
220        #define TFM_SQR4
221    #else
222        #define TFM_MUL7
223        #define TFM_SQR7
224    #endif
225 #endif
226
227 #ifdef TFM_ECC256
228    #ifdef FP_64BIT
229        #define TFM_MUL4
230        #define TFM_SQR4
231    #else
232        #define TFM_MUL8
233        #define TFM_SQR8
234    #endif
235 #endif
236
237 #ifdef TFM_ECC384
238    #ifdef FP_64BIT
239        #define TFM_MUL6
240        #define TFM_SQR6
241    #else
242        #define TFM_MUL12
243        #define TFM_SQR12
244    #endif
245 #endif
246
247 #ifdef TFM_ECC521
248    #ifdef FP_64BIT
249        #define TFM_MUL9
250        #define TFM_SQR9
251    #else
252        #define TFM_MUL17
253        #define TFM_SQR17
254    #endif
255 #endif
256
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
261 #endif
262
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
267 #endif
268
269 /* some default configurations.
270  */
271 #if defined(FP_64BIT)
272 #error cannot use 128 bit words
273    /* for GCC only on supported platforms */
274 #ifndef CRYPT
275    typedef unsigned long long ulong64;
276 #endif /* CRYPT */
277
278    typedef ulong64            fp_digit;
279 #define SIZEOF_FP_DIGIT 8
280    typedef unsigned long      fp_word __attribute__ ((mode(TI)));
281
282 #else
283
284    /* this is to make porting into LibTomCrypt easier :-) */
285 #ifndef CRYPT
286    #if defined(_MSC_VER) || defined(__BORLANDC__)
287       typedef unsigned __int64   ulong64;
288       typedef signed __int64     long64;
289    #else
290       typedef unsigned long long ulong64;
291       typedef signed long long   long64;
292    #endif /* defined(_MSC_VER) ... */
293 #endif /* CRYPT */
294
295    typedef unsigned int       fp_digit;
296 #define SIZEOF_FP_DIGIT 4
297    typedef ulong64            fp_word;
298 #endif /* FP_64BIT */
299
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)
304
305 /* signs */
306 #define FP_ZPOS     0
307 #define FP_NEG      1
308
309 /* return codes */
310 #define FP_OKAY     0
311 #define FP_VAL      1
312 #define FP_MEM      2
313
314 /* equalities */
315 #define FP_LT        -1   /* less than */
316 #define FP_EQ         0   /* equal to */
317 #define FP_GT         1   /* greater than */
318
319 /* replies */
320 #define FP_YES        1   /* yes response */
321 #define FP_NO         0   /* no response */
322
323 /* a FP type */
324 typedef struct {
325     fp_digit dp[FP_SIZE];
326     int      used,
327              sign;
328 } fp_int;
329
330 /* functions */
331
332 /* returns a TFM ident string useful for debugging... */
333 const char *fp_ident(void);
334
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)
338
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)
343
344 /* set to a small digit */
345 void fp_set(fp_int *a, fp_digit b);
346
347 /* makes a pseudo-random int of a given size */
348 void fp_rand(fp_int *a, int digits);
349
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)
353
354 /* clamp digits */
355 #define fp_clamp(a)   { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
356
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; }
360
361 /* right shift x digits */
362 void fp_rshd(fp_int *a, int x);
363
364 /* left shift x digits */
365 void fp_lshd(fp_int *a, int x);
366
367 /* signed comparison */
368 int fp_cmp(fp_int *a, fp_int *b);
369
370 /* unsigned comparison */
371 int fp_cmp_mag(fp_int *a, fp_int *b);
372
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);
380
381 /* Counts the number of lsbs which are zero before the first zero bit */
382 int fp_cnt_lsb(fp_int *a);
383
384 /* c = a + b */
385 void fp_add(fp_int *a, fp_int *b, fp_int *c);
386
387 /* c = a - b */
388 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
389
390 /* c = a * b */
391 void fp_mul(fp_int *a, fp_int *b, fp_int *c);
392
393 /* b = a*a  */
394 void fp_sqr(fp_int *a, fp_int *b);
395
396 /* a/b => cb + d == a */
397 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
398
399 /* c = a mod b, 0 <= c < b  */
400 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
401
402 /* compare against a single digit */
403 int fp_cmp_d(fp_int *a, fp_digit b);
404
405 /* c = a + b */
406 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
407
408 /* c = a - b */
409 void fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
410
411 /* c = a * b */
412 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
413
414 /* a/b => cb + d == a */
415 int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);
416
417 /* c = a mod b, 0 <= c < b  */
418 int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);
419
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);
423
424 /* d = a - b (mod c) */
425 int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
426
427 /* d = a * b (mod c) */
428 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
429
430 /* c = a * a (mod b) */
431 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
432
433 /* c = 1/a (mod b) */
434 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
435
436 /* c = (a, b) */
437 void fp_gcd(fp_int *a, fp_int *b, fp_int *c);
438
439 /* c = [a, b] */
440 void fp_lcm(fp_int *a, fp_int *b, fp_int *c);
441
442 /* setups the montgomery reduction */
443 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
444
445 /* computes a = B**n mod b without division or multiplication useful for
446  * normalizing numbers in a Montgomery system.
447  */
448 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
449
450 /* computes x/R == x (mod N) via Montgomery Reduction */
451 void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
452
453 /* d = a**b (mod c) */
454 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
455
456 /* primality stuff */
457
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);
460
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);
466
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 */
472
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);
475
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)
477
478 int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);
479
480 /* radix conersions */
481 int fp_count_bits(fp_int *a);
482
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);
486
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);
490
491 int fp_read_radix(fp_int *a, const char *str, int radix);
492
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);
496
497 #endif
498
499 /* $Source$ */
500 /* $Revision$ */
501 /* $Date$ */