tommath.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  2. *
  3. * LibTomMath is library that provides for multiple-precision
  4. * integer arithmetic as well as number theoretic functionality.
  5. *
  6. * The library is 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, [email protected], http://math.libtomcrypt.org
  14. */
  15. #ifndef BN_H_
  16. #define BN_H_
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <limits.h>
  22. #undef MIN
  23. #define MIN(x,y) ((x)<(y)?(x):(y))
  24. #undef MAX
  25. #define MAX(x,y) ((x)>(y)?(x):(y))
  26. #ifdef __cplusplus
  27. extern "C" {
  28. /* C++ compilers don't like assigning void * to mp_digit * */
  29. #define OPT_CAST (mp_digit *)
  30. #else
  31. /* C on the other hand doesn't care */
  32. #define OPT_CAST
  33. #endif
  34. /* some default configurations.
  35. *
  36. * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
  37. * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
  38. *
  39. * At the very least a mp_digit must be able to hold 7 bits
  40. * [any size beyond that is ok provided it doesn't overflow the data type]
  41. */
  42. #ifdef MP_8BIT
  43. typedef unsigned char mp_digit;
  44. typedef unsigned short mp_word;
  45. #elif defined(MP_16BIT)
  46. typedef unsigned short mp_digit;
  47. typedef unsigned long mp_word;
  48. #elif defined(MP_64BIT)
  49. /* for GCC only on supported platforms */
  50. #ifndef CRYPT
  51. typedef unsigned long long ulong64;
  52. typedef signed long long long64;
  53. #endif
  54. typedef ulong64 mp_digit;
  55. typedef unsigned long mp_word __attribute__ ((mode(TI)));
  56. #define DIGIT_BIT 60
  57. #else
  58. /* this is the default case, 28-bit digits */
  59. /* this is to make porting into LibTomCrypt easier :-) */
  60. #ifndef CRYPT
  61. #if defined(_MSC_VER) || defined(__BORLANDC__)
  62. typedef unsigned __int64 ulong64;
  63. typedef signed __int64 long64;
  64. #else
  65. typedef unsigned long long ulong64;
  66. typedef signed long long long64;
  67. #endif
  68. #endif
  69. typedef unsigned long mp_digit;
  70. typedef ulong64 mp_word;
  71. #ifdef MP_31BIT
  72. #define DIGIT_BIT 31
  73. #else
  74. #define DIGIT_BIT 28
  75. #define MP_28BIT
  76. #endif
  77. #endif
  78. /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
  79. #ifndef DIGIT_BIT
  80. #define DIGIT_BIT ((CHAR_BIT * sizeof(mp_digit) - 1)) /* bits per digit */
  81. #endif
  82. #define MP_DIGIT_BIT DIGIT_BIT
  83. #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
  84. #define MP_DIGIT_MAX MP_MASK
  85. /* equalities */
  86. #define MP_LT -1 /* less than */
  87. #define MP_EQ 0 /* equal to */
  88. #define MP_GT 1 /* greater than */
  89. #define MP_ZPOS 0 /* positive integer */
  90. #define MP_NEG 1 /* negative */
  91. #define MP_OKAY 0 /* ok result */
  92. #define MP_MEM -2 /* out of mem */
  93. #define MP_VAL -3 /* invalid input */
  94. #define MP_RANGE MP_VAL
  95. typedef int mp_err;
  96. /* you'll have to tune these... */
  97. extern int KARATSUBA_MUL_CUTOFF,
  98. KARATSUBA_SQR_CUTOFF,
  99. TOOM_MUL_CUTOFF,
  100. TOOM_SQR_CUTOFF;
  101. /* various build options */
  102. #define MP_PREC 64 /* default digits of precision (must be power of two) */
  103. /* define this to use lower memory usage routines (exptmods mostly) */
  104. /* #define MP_LOW_MEM */
  105. /* have no cpu based mult? */
  106. /* #define SLOW_MULT */
  107. #ifdef SLOW_MULT
  108. #define MULT(x, y) s_mp_mult((x), (y))
  109. mp_word s_mp_mult(mp_digit, mp_digit);
  110. #else
  111. #define MULT(x, y) (((mp_word)(x)) * ((mp_word)(y)))
  112. #endif
  113. /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
  114. #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
  115. typedef struct {
  116. int used, alloc, sign;
  117. mp_digit *dp;
  118. } mp_int;
  119. #define USED(m) ((m)->used)
  120. #define DIGIT(m,k) ((m)->dp[k])
  121. #define SIGN(m) ((m)->sign)
  122. /* ---> init and deinit bignum functions <--- */
  123. /* init a bignum */
  124. int mp_init(mp_int *a);
  125. /* free a bignum */
  126. void mp_clear(mp_int *a);
  127. /* init a null terminated series of arguments */
  128. int mp_init_multi(mp_int *mp, ...);
  129. /* clear a null terminated series of arguments */
  130. void mp_clear_multi(mp_int *mp, ...);
  131. /* exchange two ints */
  132. void mp_exch(mp_int *a, mp_int *b);
  133. /* shrink ram required for a bignum */
  134. int mp_shrink(mp_int *a);
  135. /* grow an int to a given size */
  136. int mp_grow(mp_int *a, int size);
  137. /* init to a given number of digits */
  138. int mp_init_size(mp_int *a, int size);
  139. /* ---> Basic Manipulations <--- */
  140. #define mp_iszero(a) (((a)->used == 0) ? 1 : 0)
  141. #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? 1 : 0)
  142. #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? 1 : 0)
  143. /* set to zero */
  144. void mp_zero(mp_int *a);
  145. /* set to a digit */
  146. void mp_set(mp_int *a, mp_digit b);
  147. /* set a 32-bit const */
  148. int mp_set_int(mp_int *a, unsigned int b);
  149. /* copy, b = a */
  150. int mp_copy(mp_int *a, mp_int *b);
  151. /* inits and copies, a = b */
  152. int mp_init_copy(mp_int *a, mp_int *b);
  153. /* trim unused digits */
  154. void mp_clamp(mp_int *a);
  155. /* ---> digit manipulation <--- */
  156. /* right shift by "b" digits */
  157. void mp_rshd(mp_int *a, int b);
  158. /* left shift by "b" digits */
  159. int mp_lshd(mp_int *a, int b);
  160. /* c = a / 2**b */
  161. int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
  162. /* b = a/2 */
  163. int mp_div_2(mp_int *a, mp_int *b);
  164. /* c = a * 2**b */
  165. int mp_mul_2d(mp_int *a, int b, mp_int *c);
  166. /* b = a*2 */
  167. int mp_mul_2(mp_int *a, mp_int *b);
  168. /* c = a mod 2**d */
  169. int mp_mod_2d(mp_int *a, int b, mp_int *c);
  170. /* computes a = 2**b */
  171. int mp_2expt(mp_int *a, int b);
  172. /* Counts the number of lsbs which are zero before the first zero bit */
  173. int mp_cnt_lsb(mp_int *a);
  174. /* makes a pseudo-random int of a given size */
  175. int mp_rand(mp_int *a, int digits);
  176. /* ---> binary operations <--- */
  177. /* c = a XOR b */
  178. int mp_xor(mp_int *a, mp_int *b, mp_int *c);
  179. /* c = a OR b */
  180. int mp_or(mp_int *a, mp_int *b, mp_int *c);
  181. /* c = a AND b */
  182. int mp_and(mp_int *a, mp_int *b, mp_int *c);
  183. /* ---> Basic arithmetic <--- */
  184. /* b = -a */
  185. int mp_neg(mp_int *a, mp_int *b);
  186. /* b = |a| */
  187. int mp_abs(mp_int *a, mp_int *b);
  188. /* compare a to b */
  189. int mp_cmp(mp_int *a, mp_int *b);
  190. /* compare |a| to |b| */
  191. int mp_cmp_mag(mp_int *a, mp_int *b);
  192. /* c = a + b */
  193. int mp_add(mp_int *a, mp_int *b, mp_int *c);
  194. /* c = a - b */
  195. int mp_sub(mp_int *a, mp_int *b, mp_int *c);
  196. /* c = a * b */
  197. int mp_mul(mp_int *a, mp_int *b, mp_int *c);
  198. /* b = a*a */
  199. int mp_sqr(mp_int *a, mp_int *b);
  200. /* a/b => cb + d == a */
  201. int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  202. /* c = a mod b, 0 <= c < b */
  203. int mp_mod(mp_int *a, mp_int *b, mp_int *c);
  204. /* ---> single digit functions <--- */
  205. /* compare against a single digit */
  206. int mp_cmp_d(mp_int *a, mp_digit b);
  207. /* c = a + b */
  208. int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
  209. /* c = a - b */
  210. int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
  211. /* c = a * b */
  212. int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
  213. /* a/b => cb + d == a */
  214. int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
  215. /* a/3 => 3c + d == a */
  216. int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
  217. /* c = a**b */
  218. int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
  219. /* c = a mod b, 0 <= c < b */
  220. int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
  221. /* ---> number theory <--- */
  222. /* d = a + b (mod c) */
  223. int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  224. /* d = a - b (mod c) */
  225. int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  226. /* d = a * b (mod c) */
  227. int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  228. /* c = a * a (mod b) */
  229. int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
  230. /* c = 1/a (mod b) */
  231. int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
  232. /* c = (a, b) */
  233. int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
  234. /* c = [a, b] or (a*b)/(a, b) */
  235. int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
  236. /* finds one of the b'th root of a, such that |c|**b <= |a|
  237. *
  238. * returns error if a < 0 and b is even
  239. */
  240. int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
  241. /* shortcut for square root */
  242. #define mp_sqrt(a, b) mp_n_root(a, 2, b)
  243. /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
  244. int mp_jacobi(mp_int *a, mp_int *n, int *c);
  245. /* used to setup the Barrett reduction for a given modulus b */
  246. int mp_reduce_setup(mp_int *a, mp_int *b);
  247. /* Barrett Reduction, computes a (mod b) with a precomputed value c
  248. *
  249. * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
  250. * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
  251. */
  252. int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
  253. /* setups the montgomery reduction */
  254. int mp_montgomery_setup(mp_int *a, mp_digit *mp);
  255. /* computes a = B**n mod b without division or multiplication useful for
  256. * normalizing numbers in a Montgomery system.
  257. */
  258. int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
  259. /* computes x/R == x (mod N) via Montgomery Reduction */
  260. int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
  261. /* returns 1 if a is a valid DR modulus */
  262. int mp_dr_is_modulus(mp_int *a);
  263. /* sets the value of "d" required for mp_dr_reduce */
  264. void mp_dr_setup(mp_int *a, mp_digit *d);
  265. /* reduces a modulo b using the Diminished Radix method */
  266. int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
  267. /* returns true if a can be reduced with mp_reduce_2k */
  268. int mp_reduce_is_2k(mp_int *a);
  269. /* determines k value for 2k reduction */
  270. int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
  271. /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
  272. int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit k);
  273. /* d = a**b (mod c) */
  274. int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
  275. /* ---> Primes <--- */
  276. /* number of primes */
  277. #ifdef MP_8BIT
  278. #define PRIME_SIZE 31
  279. #else
  280. #define PRIME_SIZE 256
  281. #endif
  282. /* table of first PRIME_SIZE primes */
  283. extern const mp_digit __prime_tab[];
  284. /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
  285. int mp_prime_is_divisible(mp_int *a, int *result);
  286. /* performs one Fermat test of "a" using base "b".
  287. * Sets result to 0 if composite or 1 if probable prime
  288. */
  289. int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
  290. /* performs one Miller-Rabin test of "a" using base "b".
  291. * Sets result to 0 if composite or 1 if probable prime
  292. */
  293. int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
  294. /* performs t rounds of Miller-Rabin on "a" using the first
  295. * t prime bases. Also performs an initial sieve of trial
  296. * division. Determines if "a" is prime with probability
  297. * of error no more than (1/4)**t.
  298. *
  299. * Sets result to 1 if probably prime, 0 otherwise
  300. */
  301. int mp_prime_is_prime(mp_int *a, int t, int *result);
  302. /* finds the next prime after the number "a" using "t" trials
  303. * of Miller-Rabin.
  304. */
  305. int mp_prime_next_prime(mp_int *a, int t);
  306. /* ---> radix conversion <--- */
  307. int mp_count_bits(mp_int *a);
  308. int mp_unsigned_bin_size(mp_int *a);
  309. int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
  310. int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
  311. int mp_signed_bin_size(mp_int *a);
  312. int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
  313. int mp_to_signed_bin(mp_int *a, unsigned char *b);
  314. int mp_read_radix(mp_int *a, char *str, int radix);
  315. int mp_toradix(mp_int *a, char *str, int radix);
  316. int mp_radix_size(mp_int *a, int radix);
  317. int mp_fread(mp_int *a, int radix, FILE *stream);
  318. int mp_fwrite(mp_int *a, int radix, FILE *stream);
  319. #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
  320. #define mp_raw_size(mp) mp_signed_bin_size(mp)
  321. #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
  322. #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
  323. #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
  324. #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
  325. #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
  326. #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
  327. #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
  328. #define mp_tohex(M, S) mp_toradix((M), (S), 16)
  329. /* lowlevel functions, do not call! */
  330. int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
  331. int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
  332. #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
  333. int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  334. int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  335. int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  336. int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
  337. int fast_s_mp_sqr(mp_int *a, mp_int *b);
  338. int s_mp_sqr(mp_int *a, mp_int *b);
  339. int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
  340. int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
  341. int mp_karatsuba_sqr(mp_int *a, mp_int *b);
  342. int mp_toom_sqr(mp_int *a, mp_int *b);
  343. int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
  344. int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
  345. int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
  346. int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
  347. void bn_reverse(unsigned char *s, int len);
  348. extern const char *mp_s_rmap;
  349. #ifdef __cplusplus
  350. }
  351. #endif
  352. #endif