comparison 3rdparty/libtommath/tommath.h @ 0:2cf249471370

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