libtommath.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef __LIBTOMMATH_H__
  2. #define __LIBTOMMATH_H__
  3. typedef signed short int16;
  4. #ifndef CHAR_BIT
  5. #define CHAR_BIT 8
  6. #endif
  7. #ifndef NULL
  8. #define NULL ((void *)0)
  9. #endif
  10. #define BN_MP_INVMOD_C
  11. #define BN_S_MP_EXPTMOD_C /* Note: #undef in tommath_superclass.h; this would
  12. * require BN_MP_EXPTMOD_FAST_C instead */
  13. #define BN_S_MP_MUL_DIGS_C
  14. #define BN_MP_INVMOD_SLOW_C
  15. #define BN_S_MP_SQR_C
  16. #define BN_S_MP_MUL_HIGH_DIGS_C /* Note: #undef in tommath_superclass.h; this
  17. * would require other than mp_reduce */
  18. #ifdef LTM_FAST
  19. /* Use faster div at the cost of about 1 kB */
  20. #define BN_MP_MUL_D_C
  21. /* Include faster exptmod (Montgomery) at the cost of about 2.5 kB in code */
  22. #define BN_MP_EXPTMOD_FAST_C
  23. #define BN_MP_MONTGOMERY_SETUP_C
  24. #define BN_FAST_MP_MONTGOMERY_REDUCE_C
  25. #define BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  26. #define BN_MP_MUL_2_C
  27. /* Include faster sqr at the cost of about 0.5 kB in code */
  28. #define BN_FAST_S_MP_SQR_C
  29. #else /* LTM_FAST */
  30. #define BN_MP_DIV_SMALL
  31. #define BN_MP_INIT_MULTI_C
  32. #define BN_MP_CLEAR_MULTI_C
  33. #define BN_MP_ABS_C
  34. #endif /* LTM_FAST */
  35. /* Current uses do not require support for negative exponent in exptmod, so we
  36. * can save about 1.5 kB in leaving out invmod. */
  37. #define LTM_NO_NEG_EXP
  38. /* from tommath.h */
  39. #ifndef MIN
  40. #define MIN(x,y) ((x)<(y)?(x):(y))
  41. #endif
  42. #ifndef MAX
  43. #define MAX(x,y) ((x)>(y)?(x):(y))
  44. #endif
  45. #define OPT_CAST(x)
  46. typedef unsigned int mp_digit;
  47. //typedef u64 mp_word;
  48. typedef unsigned long long mp_word;
  49. #define XMALLOC tls_mem_alloc
  50. #define XFREE tls_mem_free
  51. #define XREALLOC os_realloc
  52. #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
  53. #define MP_LT -1 /* less than */
  54. #define MP_EQ 0 /* equal to */
  55. #define MP_GT 1 /* greater than */
  56. #define MP_ZPOS 0 /* positive integer */
  57. #define MP_NEG 1 /* negative */
  58. #define MP_OKAY 0 /* ok result */
  59. #define MP_MEM -2 /* out of mem */
  60. #define MP_VAL -3 /* invalid input */
  61. #define MP_YES 1 /* yes response */
  62. #define MP_NO 0 /* no response */
  63. typedef int mp_err;
  64. /* define this to use lower memory usage routines (exptmods mostly) */
  65. #define MP_LOW_MEM
  66. /* default precision */
  67. #ifndef MP_PREC
  68. #ifndef MP_LOW_MEM
  69. #define MP_PREC 32 /* default digits of precision */
  70. #else
  71. #define MP_PREC 8 /* default digits of precision */
  72. #endif
  73. #endif
  74. /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
  75. #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
  76. /* the infamous mp_int structure */
  77. typedef struct {
  78. int16 used, alloc, sign;
  79. mp_digit *dp;
  80. } mp_int;
  81. /* ---> Basic Manipulations <--- */
  82. #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
  83. #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
  84. #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
  85. void mp_reverse (unsigned char *s, int len);
  86. #ifdef BN_MP_INIT_MULTI_C
  87. int mp_init_multi(mp_int *mp, ...);
  88. #endif
  89. #ifdef BN_MP_CLEAR_MULTI_C
  90. void mp_clear_multi(mp_int *mp, ...);
  91. #endif
  92. int mp_lshd(mp_int * a, int b);
  93. void mp_set(mp_int * a, mp_digit b);
  94. void mp_clamp(mp_int * a);
  95. void mp_exch(mp_int * a, mp_int * b);
  96. void mp_rshd(mp_int * a, int b);
  97. void mp_zero(mp_int * a);
  98. int mp_mod_2d(mp_int * a, int b, mp_int * c);
  99. int mp_div_2d(mp_int * a, int b, mp_int * c, mp_int * d);
  100. int mp_init_copy(mp_int * a, mp_int * b);
  101. int mp_mul_2d(mp_int * a, int b, mp_int * c);
  102. #ifndef LTM_NO_NEG_EXP
  103. int mp_div_2(mp_int * a, mp_int * b);
  104. int mp_invmod(mp_int * a, mp_int * b, mp_int * c);
  105. int mp_invmod_slow(mp_int * a, mp_int * b, mp_int * c);
  106. #endif /* LTM_NO_NEG_EXP */
  107. int mp_copy(mp_int * a, mp_int * b);
  108. int mp_count_bits(mp_int * a);
  109. int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  110. int mp_mod(mp_int * a, mp_int * b, mp_int * c);
  111. int mp_grow(mp_int * a, int size);
  112. int mp_cmp_mag(mp_int * a, mp_int * b);
  113. #ifdef BN_MP_ABS_C
  114. int mp_abs(mp_int * a, mp_int * b);
  115. #endif
  116. int mp_sqr(mp_int * a, mp_int * b);
  117. int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
  118. int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
  119. int mp_2expt(mp_int * a, int b);
  120. int mp_reduce_setup(mp_int * a, mp_int * b);
  121. int mp_reduce(mp_int * x, mp_int * m, mp_int * mu);
  122. int mp_init_size(mp_int * a, int size);
  123. #ifdef BN_MP_EXPTMOD_FAST_C
  124. int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
  125. #endif /* BN_MP_EXPTMOD_FAST_C */
  126. #ifdef BN_FAST_S_MP_SQR_C
  127. int fast_s_mp_sqr (mp_int * a, mp_int * b);
  128. #endif /* BN_FAST_S_MP_SQR_C */
  129. #ifdef BN_MP_MUL_D_C
  130. int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
  131. #endif /* BN_MP_MUL_D_C */
  132. #ifdef BN_MP_MUL_2_C
  133. /* b = a*2 */
  134. int mp_mul_2(mp_int * a, mp_int * b);
  135. #endif
  136. int mp_init_for_read_unsigned_bin(mp_int *a, mp_digit len);
  137. void mp_clear (mp_int * a);
  138. int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
  139. int mp_init (mp_int * a);
  140. int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
  141. int mp_to_unsigned_bin_nr (mp_int * a, unsigned char *b);
  142. int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
  143. int mp_unsigned_bin_size (mp_int * a);
  144. int mp_add (mp_int * a, mp_int * b, mp_int * c);
  145. int mp_cmp (mp_int * a, mp_int * b);
  146. int mp_sub (mp_int * a, mp_int * b, mp_int * c);
  147. int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  148. int mp_cmp_d(mp_int * a, mp_digit b);
  149. #ifdef BN_MP_MONTGOMERY_SETUP_C
  150. int mp_montgomery_setup (mp_int * n, mp_digit * rho);
  151. #endif
  152. #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  153. int mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
  154. #endif
  155. #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
  156. int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
  157. #endif
  158. #endif //__LIBTOMMATH_H__